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 static int mds_get_md_type(char *name)
458 if (!strcmp(name, XATTR_LUSTRE_MDS_LOV_EA))
460 if (!strcmp(name, XATTR_LUSTRE_MDS_MEA_EA))
462 if (!strcmp(name, XATTR_LUSTRE_MDS_SID_EA))
464 if (!strcmp(name, XATTR_LUSTRE_MDS_PID_EA))
466 if (!strcmp(name, XATTR_LUSTRE_MDS_KEY_EA))
472 /* In the raw-setattr case, we lock the child inode.
473 * In the write-back case or if being called from open, the client holds a lock
476 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
477 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
478 struct ptlrpc_request *req, struct lustre_handle *lh)
480 struct mds_obd *mds = mds_req2mds(req);
481 struct obd_device *obd = req->rq_export->exp_obd;
482 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
483 struct mds_body *body;
484 struct dentry *de = NULL;
485 struct inode *inode = NULL;
486 struct lustre_handle lockh[2] = {{0}, {0}};
489 struct mds_logcancel_data *mlcd = NULL;
490 int rc = 0, cleanup_phase = 0, err;
491 int repcnt = 1, repsize[2] = { sizeof(*body) };
492 int locked = 0, do_trunc = 0;
495 LASSERT(offset == 1);
497 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x",
498 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
499 rec->ur_iattr.ia_valid);
501 /* remote setfacl need special handling */
502 if ((rec->ur_iattr.ia_valid & ATTR_EA) &&
503 !strcmp(rec->ur_eadata, XATTR_NAME_LUSTRE_ACL)) {
504 return mds_reint_remote_setfacl(obd, med, rec, req);
507 if (rec->ur_iattr.ia_valid & ATTR_SIZE) {
508 repsize[repcnt++] = sizeof(struct lustre_capa);
509 do_trunc = 1; /* XXX: ATTR_SIZE will be cleared from ia_valid */
512 rc = lustre_pack_reply(req, repcnt, repsize, NULL);
516 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
517 MD_COUNTER_INCREMENT(obd, setattr);
519 if (med->med_remote) {
520 if (rec->ur_iattr.ia_valid & ATTR_GID) {
521 CWARN("Deny chgrp from remote client\n");
522 GOTO(cleanup, rc = -EPERM);
524 if (rec->ur_iattr.ia_valid & ATTR_UID) {
527 uid = mds_idmap_lookup_uid(med->med_idmap, 0,
528 rec->ur_iattr.ia_uid);
529 if (uid == MDS_IDMAP_NOTFOUND) {
530 CWARN("Deny chown to uid %u\n",
531 rec->ur_iattr.ia_uid);
532 GOTO(cleanup, rc = -EPERM);
534 rec->ur_iattr.ia_uid = uid;
538 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
539 de = mds_id2dentry(obd, rec->ur_id1, NULL);
541 GOTO(cleanup, rc = PTR_ERR(de));
543 __u64 lockpart = MDS_INODELOCK_UPDATE;
544 if (rec->ur_iattr.ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
545 lockpart |= MDS_INODELOCK_LOOKUP;
546 de = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
547 lockh, &parent_mode, NULL, 0, lockpart);
549 GOTO(cleanup, rc = PTR_ERR(de));
557 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
558 rec->ur_eadata != NULL)
561 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
563 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
565 GOTO(cleanup, rc = PTR_ERR(handle));
567 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
568 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
569 LTIME_S(rec->ur_iattr.ia_mtime),
570 LTIME_S(rec->ur_iattr.ia_ctime));
571 rc = mds_fix_attr(inode, rec);
575 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
576 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
577 (long)&rec->ur_iattr.ia_attr_flags);
579 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
582 if (rec->ur_iattr.ia_valid & ATTR_EA) {
583 int flags = (int) rec->ur_iattr.ia_attr_flags;
586 if (!med->med_remote && inode->i_op &&
587 inode->i_op->setxattr)
588 rc = inode->i_op->setxattr(
593 } else if (rec->ur_iattr.ia_valid & ATTR_EA_RM) {
595 if (inode->i_op && inode->i_op->removexattr)
596 rc = inode->i_op->removexattr(de,
598 } else if (rec->ur_iattr.ia_valid & ATTR_EA_CMOBD) {
602 LASSERT(rec->ur_eadata != NULL);
603 LASSERT(rec->ur_ea2data != NULL);
604 name = rec->ur_eadata;
606 /* XXX: tmp fix for setting LOV EA from CMOBD */
607 type = mds_get_md_type(name);
609 if (type == EA_LOV) {
610 CDEBUG(D_INFO, "set %s EA for cmobd\n", name);
612 rc = fsfilt_set_md(obd, inode, handle,
617 CERROR("fsfilt_set_md() failed, err %d\n",
622 } else if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
623 !((rec->ur_iattr.ia_valid & ATTR_KEY) ||
624 (rec->ur_iattr.ia_valid & ATTR_MAC))) {
626 struct lov_stripe_md *lsm = NULL;
627 struct lov_user_md *lum = NULL;
629 if (rec->ur_eadata != NULL) {
630 rc = ll_permission(inode, MAY_WRITE, NULL);
634 lum = rec->ur_eadata;
636 /* if lmm_stripe_size is -1 delete default
638 if (S_ISDIR(inode->i_mode) &&
639 lum->lmm_stripe_size == (typeof(lum->lmm_stripe_size))(-1)){
640 rc = fsfilt_set_md(obd, inode, handle, NULL, 0, EA_LOV);
644 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
646 &lsm, rec->ur_eadata);
650 obd_free_memmd(mds->mds_dt_exp, &lsm);
651 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
652 rec->ur_eadatalen, EA_LOV);
659 if ((rec->ur_iattr.ia_valid & ATTR_KEY) || (rec->ur_iattr.ia_valid & ATTR_MAC)) {
663 LASSERT(rec->ur_eadatalen || rec->ur_ea3datalen);
664 LASSERT(rec->ur_eadata || rec->ur_ea3data);
665 key = rec->ur_ea3data ? rec->ur_ea3data : rec->ur_eadata;
666 keylen = rec->ur_ea3datalen ? rec->ur_ea3datalen :
668 mds_set_gskey(obd, handle, inode, key, keylen,
669 rec->ur_iattr.ia_valid);
673 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
674 mds_pack_inode2body(obd, body, inode, 1);
676 /* Don't return OST-specific attributes if we didn't just set them */
677 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
678 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
679 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
680 body->valid |= OBD_MD_FLMTIME;
681 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
682 body->valid |= OBD_MD_FLATIME;
685 struct lustre_capa capa = {
686 .lc_uid = rec->ur_uc.luc_uid,
688 .lc_ino = inode->i_ino,
689 .lc_mdsid = mds->mds_num,
693 LASSERT(capa.lc_mdsid == mds->mds_num);
694 rc = mds_pack_capa(obd, med, NULL, &capa, req,
697 CERROR("mds_pack_capa: rc = %d\n", rc);
702 mds_body_do_reverse_map(med, body);
704 /* The logcookie should be no use anymore, why nobody remove
705 * following code block?
707 LASSERT(rec->ur_cookielen == 0);
708 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_dt_obd)) {
709 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
712 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
714 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
715 mlcd->mlcd_cookielen = rec->ur_cookielen;
716 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
717 mlcd->mlcd_cookielen;
718 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
719 mlcd->mlcd_cookielen);
720 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
721 mlcd->mlcd_eadatalen);
723 CERROR("unable to allocate log cancel data\n");
729 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
730 handle, mds_cancel_cookies_cb, mlcd);
731 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
732 switch (cleanup_phase) {
734 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
735 rec->ur_eadata != NULL)
740 if (lockh[1].cookie != 0)
741 ldlm_lock_decref(lockh + 1, parent_mode);
744 ldlm_lock_decref(lockh, LCK_PW);
746 ptlrpc_save_lock (req, lockh, LCK_PW);
761 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
762 struct ptlrpc_request *req)
764 struct mds_export_data *med = &req->rq_export->exp_mds_data;
765 struct dentry *parent, *child;
766 struct mds_body *body;
769 mds_req_from_mcd(req, med->med_mcd);
771 if (req->rq_status) {
776 parent = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
777 LASSERT(!IS_ERR(parent));
778 child = ll_lookup_one_len(rec->ur_name, parent,
779 rec->ur_namelen - 1);
780 LASSERT(!IS_ERR(child));
781 if ((child->d_flags & DCACHE_CROSS_REF)) {
782 LASSERTF(child->d_inode == NULL, "BUG 3869\n");
783 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
784 mds_pack_dentry2body(req2obd(req), body, child, 1);
785 } else if (child->d_inode == NULL) {
786 DEBUG_REQ(D_ERROR, req, "parent "DLID4" name %s mode %o",
787 OLID4(rec->ur_id1), rec->ur_name, rec->ur_mode);
788 LASSERTF(child->d_inode != NULL, "BUG 3869\n");
790 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
791 mds_pack_inode2body(req2obd(req), body, child->d_inode, 1);
798 static int mds_get_default_acl(struct inode *dir, void **pacl)
800 struct dentry de = { .d_inode = dir };
803 LASSERT(S_ISDIR(dir->i_mode));
805 if (!dir->i_op->getxattr)
808 size = dir->i_op->getxattr(&de, XATTR_NAME_ACL_DEFAULT, NULL, 0);
809 if (size == 0 || size == -ENODATA || size == -EOPNOTSUPP)
814 OBD_ALLOC(*pacl, size);
818 size2 = dir->i_op->getxattr(&de, XATTR_NAME_ACL_DEFAULT, *pacl, size);
820 /* since we already locked the dir, it should not change
821 * between the 2 getxattr calls
823 CERROR("2'nd getxattr got %d, expect %d\n", size2, size);
824 OBD_FREE(*pacl, size);
831 static int mds_reint_create(struct mds_update_record *rec, int offset,
832 struct ptlrpc_request *req,
833 struct lustre_handle *lh)
835 struct dentry *dparent = NULL;
836 struct mds_obd *mds = mds_req2mds(req);
837 struct obd_device *obd = req->rq_export->exp_obd;
838 struct mds_body *body = NULL;
839 struct dentry *dchild = NULL;
840 struct inode *dir = NULL;
842 struct lustre_handle lockh[2] = {{0}, {0}};
844 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
846 struct dentry_params dp;
847 struct mea *mea = NULL;
849 struct lustre_id sid;
853 LASSERT(offset == 1);
855 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
856 OBD_MDS_DEVICENAME));
858 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
859 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
860 rec->ur_name, rec->ur_mode);
862 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
864 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
865 GOTO(cleanup, rc = -ESTALE);
867 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
868 lockh, &parent_mode, rec->ur_name,
869 rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
870 if (IS_ERR(dparent)) {
871 rc = PTR_ERR(dparent);
872 CERROR("parent lookup error %d, id "DLID4"\n",
873 rc, OLID4(rec->ur_id1));
876 cleanup_phase = 1; /* locked parent dentry */
877 dir = dparent->d_inode;
880 ldlm_lock_dump_handle(D_OTHER, lockh);
882 /* get parent id: ldlm lock on the parent protects ea */
883 rc = mds_read_inode_sid(obd, dir, &sid);
885 CERROR("can't read parent id. ino(%lu) rc(%d)\n",
890 /* try to retrieve MEA data for this dir */
891 rc = mds_md_get_attr(obd, dparent->d_inode, &mea, &mea_size);
895 if (mea != NULL && mea->mea_count) {
897 * dir is already splitted, check is requested filename should
898 * live at this MDS or at another one.
900 int i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
901 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
902 CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
903 " should be %lu(%d)\n",
904 mea->mea_master, dparent->d_inode->i_ino,
905 dparent->d_inode->i_generation, rec->ur_name,
906 (unsigned long)id_group(&mea->mea_ids[i]), i);
907 GOTO(cleanup, rc = -ERESTART);
911 dchild = ll_lookup_one_len(rec->ur_name, dparent,
912 rec->ur_namelen - 1);
913 if (IS_ERR(dchild)) {
914 rc = PTR_ERR(dchild);
915 CERROR("Can't find "DLID4"/%s, error %d\n",
916 OLID4(rec->ur_id1), rec->ur_name, rc);
920 cleanup_phase = 2; /* child dentry */
922 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
924 if (type == S_IFREG || type == S_IFDIR) {
925 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
926 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
927 obd->obd_name, dparent->d_inode->i_ino,
928 dparent->d_inode->i_generation, rc, parent_mode);
930 /* dir got splitted */
931 GOTO(cleanup, rc = -ERESTART);
933 /* error happened during spitting. */
938 if (dir->i_mode & S_ISGID) {
939 if (S_ISDIR(rec->ur_mode))
940 rec->ur_mode |= S_ISGID;
943 /* for reint case stor ecookie should be zero */
944 if (rec->ur_flags & MDS_REINT_REQ) {
945 LASSERT(id_ino(rec->ur_id1) == 0);
946 LASSERT(id_ino(rec->ur_id2) == 0);
949 if (id_fid(rec->ur_id2))
950 fid = id_fid(rec->ur_id2);
952 fid = mds_alloc_fid(obd);
954 dchild->d_fsdata = (void *)&dp;
955 dp.p_inum = (unsigned long)id_ino(rec->ur_id2);
959 dp.p_group = mds->mds_num;
961 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
965 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
967 GOTO(cleanup, rc = PTR_ERR(handle));
968 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
970 /* XXX: here we should check what type of EA is in ur_eadata
971 * and do appropriate actions. --umka */
972 if (rec->ur_eadata && rec->ur_eadatalen &&
973 rc == 0 && dchild->d_inode != NULL) {
974 if (rec->ur_flags & MDS_REINT_REQ) {
975 /* for CMOBD to set lov md info when cmobd reint
977 CDEBUG(D_INFO, "set lsm %p, len %d to inode %lu \n",
978 rec->ur_eadata, rec->ur_eadatalen,
979 dchild->d_inode->i_ino);
980 rc = fsfilt_set_md(obd, dchild->d_inode, handle,
981 rec->ur_eadata, rec->ur_eadatalen,
984 CERROR("fsfilt_set_md() failed, err %d\n",
989 /* assumption: when ur_eadata is not NULL,
990 * ur_eadata is crypto key, should fix it later,
992 rc = mds_set_gskey(obd, handle, dchild->d_inode,
993 rec->ur_eadata, rec->ur_eadatalen,
994 ATTR_MAC | ATTR_KEY);
996 CWARN("mds_set_gskey() failed, err %d\n",
1007 * as Peter asked, mkdir() should distribute new directories
1008 * over the whole cluster in order to distribute namespace
1009 * processing load. first, we calculate which MDS to use to put
1010 * new directory's inode in.
1013 /* XXX: here we order mds_choose_mdsnum() to use local mdsnum
1014 * for reint requests. This should be gone when real flushing on
1015 * LMV is fixed. --umka */
1016 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1,
1017 rec->ur_flags, &req->rq_peer, dir,
1018 (rec->ur_flags & MDS_REINT_REQ) ? 1 : 0);
1020 if (i == mds->mds_num) {
1021 /* inode will be created locally */
1022 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
1024 GOTO(cleanup, rc = PTR_ERR(handle));
1026 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
1029 "Can't create dir \"%s\", rc = %d\n",
1030 dchild->d_name.name, rc);
1034 } else if (!DENTRY_VALID(dchild)) {
1035 /* inode will be created on another MDS */
1036 struct obdo *oa = NULL;
1040 /* first, create that inode */
1043 GOTO(cleanup, rc = -ENOMEM);
1048 if (rec->ur_eadata) {
1049 /* user asks for creating splitted dir */
1050 oa->o_easize = *((u16 *) rec->ur_eadata);
1053 obdo_from_inode(oa, dir, OBD_MD_FLATIME |
1054 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1056 /* adjust the uid/gid/mode bits */
1057 oa->o_mode = rec->ur_mode;
1058 oa->o_uid = current->fsuid;
1059 oa->o_gid = (dir->i_mode & S_ISGID) ?
1060 dir->i_gid : current->fsgid;
1062 /* letting remote MDS know that this is reint
1064 if (rec->ur_flags & MDS_REINT_REQ)
1065 oa->o_flags |= OBD_FL_REINT;
1067 /* transfer parent id to remote inode */
1068 memcpy(obdo_id(oa), &sid, sizeof(sid));
1069 oa->o_valid |= OBD_MD_FLTYPE | OBD_MD_FLUID |
1070 OBD_MD_FLGID | OBD_MD_FLIFID;
1072 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
1075 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1077 * here inode number and generation are
1078 * important, as this is replay request and we
1079 * need them to check if such an object is
1082 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
1083 obd->obd_name, rec->ur_namelen - 1,
1084 rec->ur_name, (unsigned)id_ino(rec->ur_id2),
1085 (unsigned)id_gen(rec->ur_id2));
1086 oa->o_id = id_ino(rec->ur_id2);
1087 oa->o_fid = id_fid(rec->ur_id2);
1088 oa->o_generation = id_gen(rec->ur_id2);
1089 oa->o_flags |= OBD_FL_RECREATE_OBJS;
1090 LASSERT(oa->o_fid != 0);
1093 /* obtain default ACL */
1094 acl_size = mds_get_default_acl(dir, &acl);
1097 GOTO(cleanup, rc = -ENOMEM);
1101 * before obd_create() is called, o_fid is not known if
1102 * this is not recovery of cause.
1104 rc = obd_create(mds->mds_md_exp, oa, acl, acl_size,
1108 OBD_FREE(acl, acl_size);
1111 CERROR("can't create remote inode: %d\n", rc);
1112 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
1113 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
1114 rec->ur_name, rec->ur_mode);
1119 LASSERT(oa->o_fid != 0);
1121 /* now, add new dir entry for it */
1122 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
1123 if (IS_ERR(handle)) {
1125 GOTO(cleanup, rc = PTR_ERR(handle));
1128 /* creating local dentry for remote inode. */
1129 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
1130 rec->ur_namelen - 1, oa->o_id,
1131 oa->o_generation, i, oa->o_fid);
1134 CERROR("Can't create local entry %*s for "
1135 "remote inode.\n", rec->ur_namelen - 1,
1141 body->valid |= OBD_MD_FLID | OBD_MD_MDS | OBD_MD_FID;
1143 obdo2id(&body->id1, oa);
1146 /* requested name exists in the directory */
1152 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
1154 GOTO(cleanup, rc = PTR_ERR(handle));
1155 if (rec->ur_tgt == NULL) /* no target supplied */
1156 rc = -EINVAL; /* -EPROTO? */
1158 rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
1165 int rdev = rec->ur_rdev;
1166 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
1168 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
1169 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
1173 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
1174 dchild->d_fsdata = NULL;
1175 GOTO(cleanup, rc = -EINVAL);
1178 /* In case we stored the desired inum in here, we want to clean up. */
1179 if (dchild->d_fsdata == (void *)(unsigned long)id_ino(rec->ur_id2))
1180 dchild->d_fsdata = NULL;
1183 CDEBUG(D_INODE, "error during create: %d\n", rc);
1185 } else if (dchild->d_inode) {
1186 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
1187 struct inode *inode = dchild->d_inode;
1191 iattr.ia_uid = rec->ur_fsuid;
1192 LTIME_S(iattr.ia_atime) = rec->ur_time;
1193 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1194 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1196 if (dir->i_mode & S_ISGID)
1197 iattr.ia_gid = dir->i_gid;
1199 iattr.ia_gid = rec->ur_fsgid;
1201 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
1202 ATTR_MTIME | ATTR_CTIME;
1204 if (id_ino(rec->ur_id2)) {
1205 LASSERT(id_ino(rec->ur_id2) == inode->i_ino);
1206 inode->i_generation = id_gen(rec->ur_id2);
1207 /* dirtied and committed by the upcoming setattr. */
1208 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
1209 inode->i_ino, inode->i_generation);
1211 mds_inode2id(obd, &body->id1, dchild->d_inode, fid);
1212 mds_update_inode_ids(obd, inode, handle, &body->id1, &sid);
1214 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
1216 CERROR("error on child setattr: rc = %d\n", rc);
1218 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1219 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1221 CERROR("error on parent setattr: rc = %d\n", rc);
1223 MD_COUNTER_INCREMENT(obd, create);
1225 /* take care of default stripe inheritance */
1226 if (type == S_IFDIR) {
1227 struct lov_mds_md lmm;
1228 int lmm_size = sizeof(lmm), nstripes = 0;
1230 rc = mds_get_md(obd, dir, &lmm, &lmm_size, 1, 0);
1232 down(&inode->i_sem);
1233 rc = fsfilt_set_md(obd, inode, handle,
1234 &lmm, lmm_size, EA_LOV);
1238 CERROR("error on copy stripe info: rc = %d\n",
1244 nstripes = *(u16 *)rec->ur_eadata;
1248 * we pass LCK_EX to split routine to signal,
1249 * that we have exclusive access to the
1250 * directory. Simple because nobody knows it
1251 * already exists -bzzz
1253 rc = mds_try_to_split_dir(obd, dchild,
1257 /* dir got splitted */
1259 } else if (rc < 0) {
1260 /* an error occured during
1268 mds_pack_inode2body(obd, body, inode, 1);
1269 mds_body_do_reverse_map(med, body);
1271 if (rec->ur_flags & MDS_REINT_REQ) {
1272 LASSERT(body != NULL);
1273 rc = mds_fidmap_add(obd, &body->id1);
1275 CERROR("can't create fid->ino mapping, "
1285 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
1287 if (rc && created) {
1288 /* Destroy the file we just created. This should not need extra
1289 * journal credits, as we have already modified all of the
1290 * blocks needed in order to create the file in the first
1294 err = vfs_rmdir(dir, dchild);
1296 CERROR("rmdir in error path: %d\n", err);
1299 err = vfs_unlink(dir, dchild);
1301 CERROR("unlink in error path: %d\n", err);
1304 } else if (created) {
1305 /* The inode we were allocated may have just been freed
1306 * by an unlink operation. We take this lock to
1307 * synchronize against the matching reply-ack-lock taken
1308 * in unlink, to avoid replay problems if this reply
1309 * makes it out to the client but the unlink's does not.
1310 * See bug 2029 for more detail.*/
1311 mds_lock_new_child(obd, dchild->d_inode, NULL);
1315 switch (cleanup_phase) {
1316 case 2: /* child dentry */
1318 mds_audit(req, dchild, rec->ur_name,
1319 rec->ur_namelen - 1, AUDIT_CREATE, rc);
1321 case 1: /* locked parent dentry */
1323 if (lockh[1].cookie != 0)
1324 ldlm_lock_decref(lockh + 1, parent_mode);
1327 ldlm_lock_decref(lockh, LCK_PW);
1329 ptlrpc_save_lock(req, lockh, LCK_PW);
1335 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1339 OBD_FREE(mea, mea_size);
1340 req->rq_status = rc;
1345 res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
1346 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1350 for (i = 0; i < RES_NAME_SIZE; i++) {
1352 * this is needed to make zeroed res_id entries to be put at the
1353 * end of list in *ordered_locks() .
1355 if (res1->name[i] == 0 && res2->name[i] != 0)
1357 if (res2->name[i] == 0 && res1->name[i] != 0)
1359 if (res1->name[i] > res2->name[i])
1361 if (res1->name[i] < res2->name[i])
1368 if (memcmp(p1, p2, sizeof(*p1)) < 0)
1374 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1375 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1376 * because they take the place of local semaphores.
1378 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
1379 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
1380 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
1381 struct lustre_handle *p1_lockh, int p1_lock_mode,
1382 ldlm_policy_data_t *p1_policy,
1383 struct ldlm_res_id *p2_res_id,
1384 struct lustre_handle *p2_lockh, int p2_lock_mode,
1385 ldlm_policy_data_t *p2_policy)
1387 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1388 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1389 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1390 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
1394 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1396 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1397 res_id[0]->name[0], res_id[1]->name[0]);
1399 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1400 handles[1] = p1_lockh;
1401 handles[0] = p2_lockh;
1402 res_id[1] = p1_res_id;
1403 res_id[0] = p2_res_id;
1404 lock_modes[1] = p1_lock_mode;
1405 lock_modes[0] = p2_lock_mode;
1406 policies[1] = p1_policy;
1407 policies[0] = p2_policy;
1410 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1411 res_id[0]->name[0], res_id[1]->name[0]);
1413 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1414 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1415 LDLM_IBITS, policies[0], lock_modes[0], &flags,
1416 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1417 NULL, 0, NULL, handles[0]);
1420 ldlm_lock_dump_handle(D_OTHER, handles[0]);
1422 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1423 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1424 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1425 ldlm_lock_addref(handles[1], lock_modes[1]);
1426 } else if (res_id[1]->name[0] != 0) {
1427 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1428 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1429 *res_id[1], LDLM_IBITS, policies[1],
1430 lock_modes[1], &flags, mds_blocking_ast,
1431 ldlm_completion_ast, NULL, NULL, NULL, 0,
1433 if (rc != ELDLM_OK) {
1434 ldlm_lock_decref(handles[0], lock_modes[0]);
1437 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1443 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1444 struct lustre_handle *p1_lockh, int p1_lock_mode,
1445 ldlm_policy_data_t *p1_policy,
1446 struct ldlm_res_id *p2_res_id,
1447 struct lustre_handle *p2_lockh, int p2_lock_mode,
1448 ldlm_policy_data_t *p2_policy,
1449 struct ldlm_res_id *c1_res_id,
1450 struct lustre_handle *c1_lockh, int c1_lock_mode,
1451 ldlm_policy_data_t *c1_policy,
1452 struct ldlm_res_id *c2_res_id,
1453 struct lustre_handle *c2_lockh, int c2_lock_mode,
1454 ldlm_policy_data_t *c2_policy)
1456 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1457 c1_res_id, c2_res_id };
1458 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1459 c1_lockh, c2_lockh };
1460 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1461 c1_lock_mode, c2_lock_mode };
1462 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1463 c1_policy, c2_policy};
1464 int rc, i, j, sorted, flags;
1467 CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1468 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1469 res_id[3]->name[0]);
1472 * simple insertion sort - we have at most 4 elements. Note, that zeroed
1473 * res_id should be at the end of list after sorting is finished.
1475 for (i = 1; i < 4; i++) {
1477 dlm_handles[4] = dlm_handles[i];
1478 res_id[4] = res_id[i];
1479 lock_modes[4] = lock_modes[i];
1480 policies[4] = policies[i];
1484 if (res_gt(res_id[j], res_id[4], policies[j],
1486 dlm_handles[j + 1] = dlm_handles[j];
1487 res_id[j + 1] = res_id[j];
1488 lock_modes[j + 1] = lock_modes[j];
1489 policies[j + 1] = policies[j];
1494 } while (j >= 0 && !sorted);
1496 dlm_handles[j + 1] = dlm_handles[4];
1497 res_id[j + 1] = res_id[4];
1498 lock_modes[j + 1] = lock_modes[4];
1499 policies[j + 1] = policies[4];
1502 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1503 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1504 res_id[3]->name[0]);
1506 /* XXX we could send ASTs on all these locks first before blocking? */
1507 for (i = 0; i < 4; i++) {
1508 flags = LDLM_FL_ATOMIC_CB;
1511 * nevertheless zeroed res_ids should be at the end of list, and
1512 * could use break here, I think, that it is more correctly for
1513 * clear understanding of code to have continue here, as it
1514 * clearly means, that zeroed res_id should be skipped and does
1515 * not mean, that if we meet zeroed res_id we should stop
1518 if (res_id[i]->name[0] == 0)
1522 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1523 (policies[i]->l_inodebits.bits &
1524 policies[i-1]->l_inodebits.bits) ) {
1525 memcpy(dlm_handles[i], dlm_handles[i-1],
1526 sizeof(*(dlm_handles[i])));
1527 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1529 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1530 *res_id[i], LDLM_IBITS,
1532 lock_modes[i], &flags,
1534 ldlm_completion_ast, NULL, NULL,
1535 NULL, 0, NULL, dlm_handles[i]);
1537 GOTO(out_err, rc = -EIO);
1538 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1545 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1550 /* In the unlikely case that the child changed while we were waiting
1551 * on the lock, we need to drop the lock on the old child and either:
1552 * - if the child has a lower resource name, then we have to also
1553 * drop the parent lock and regain the locks in the right order
1554 * - in the rename case, if the child has a lower resource name than one of
1555 * the other parent/child resources (maxres) we also need to reget the locks
1556 * - if the child has a higher resource name (this is the common case)
1557 * we can just get the lock on the new child (still in lock order)
1559 * Returns 0 if the child did not change or if it changed but could be locked.
1560 * Returns 1 if the child changed and we need to re-lock (no locks held).
1561 * Returns -ve error with a valid dchild (no locks held). */
1562 static int mds_verify_child(struct obd_device *obd,
1563 struct ldlm_res_id *parent_res_id,
1564 struct lustre_handle *parent_lockh,
1565 struct dentry *dparent, int parent_mode,
1566 struct ldlm_res_id *child_res_id,
1567 struct lustre_handle *child_lockh,
1568 struct dentry **dchildp, int child_mode,
1569 ldlm_policy_data_t *child_policy,
1570 const char *name, int namelen,
1571 struct ldlm_res_id *maxres,
1572 unsigned long child_ino, __u32 child_gen)
1574 struct lustre_id sid;
1575 struct dentry *vchild, *dchild = *dchildp;
1576 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1579 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1581 GOTO(cleanup, rc = PTR_ERR(vchild));
1583 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1584 if (child_gen == vchild->d_generation &&
1585 child_ino == vchild->d_inum) {
1594 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1595 (vchild->d_inode != NULL &&
1596 child_gen == vchild->d_inode->i_generation &&
1597 child_ino == vchild->d_inode->i_ino))) {
1605 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1606 vchild->d_inode, dchild ? dchild->d_inode : 0,
1607 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1608 child_res_id->name[0]);
1610 if (child_res_id->name[0] != 0)
1611 ldlm_lock_decref(child_lockh, child_mode);
1615 cleanup_phase = 1; /* parent lock only */
1616 *dchildp = dchild = vchild;
1618 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1619 int flags = LDLM_FL_ATOMIC_CB;
1621 if (dchild->d_inode) {
1622 down(&dchild->d_inode->i_sem);
1623 rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
1624 up(&dchild->d_inode->i_sem);
1626 CERROR("Can't read inode self id, inode %lu,"
1627 " rc %d\n", dchild->d_inode->i_ino, rc);
1630 child_res_id->name[0] = id_fid(&sid);
1631 child_res_id->name[1] = id_group(&sid);
1633 child_res_id->name[0] = dchild->d_fid;
1634 child_res_id->name[1] = dchild->d_mdsnum;
1637 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1638 res_gt(maxres, child_res_id, NULL, NULL)) {
1639 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1640 child_res_id->name[0], parent_res_id->name[0],
1642 GOTO(cleanup, rc = 1);
1645 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1646 *child_res_id, LDLM_IBITS, child_policy,
1647 child_mode, &flags, mds_blocking_ast,
1648 ldlm_completion_ast, NULL, NULL, NULL, 0,
1651 GOTO(cleanup, rc = -EIO);
1654 memset(child_res_id, 0, sizeof(*child_res_id));
1660 switch(cleanup_phase) {
1662 if (child_res_id->name[0] != 0)
1663 ldlm_lock_decref(child_lockh, child_mode);
1665 ldlm_lock_decref(parent_lockh, parent_mode);
1671 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1672 struct lustre_id *id,
1673 struct lustre_handle *parent_lockh,
1674 struct dentry **dparentp, int parent_mode,
1675 __u64 parent_lockpart, int *update_mode,
1676 char *name, int namelen,
1677 struct lustre_handle *child_lockh,
1678 struct dentry **dchildp, int child_mode,
1679 __u64 child_lockpart)
1681 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1682 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1683 struct ldlm_res_id parent_res_id = { .name = {0} };
1684 struct ldlm_res_id child_res_id = { .name = {0} };
1685 unsigned long child_ino = 0; __u32 child_gen = 0;
1686 int rc = 0, cleanup_phase = 0;
1687 struct lustre_id sid;
1688 struct inode *inode;
1691 /* Step 1: Lookup parent */
1692 *dparentp = mds_id2dentry(obd, id, NULL);
1693 if (IS_ERR(*dparentp)) {
1694 rc = PTR_ERR(*dparentp);
1699 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1700 (*dparentp)->d_inode->i_ino, name);
1702 parent_res_id.name[0] = id_fid(id);
1703 parent_res_id.name[1] = id_group(id);
1706 parent_lockh[1].cookie = 0;
1707 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1708 struct ldlm_res_id res_id = { .name = {0} };
1709 ldlm_policy_data_t policy;
1710 int flags = LDLM_FL_ATOMIC_CB;
1712 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1714 res_id.name[0] = id_fid(id);
1715 res_id.name[1] = id_group(id);
1716 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1718 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1719 res_id, LDLM_IBITS, &policy,
1720 *update_mode, &flags,
1722 ldlm_completion_ast,
1723 NULL, NULL, NULL, 0, NULL,
1729 parent_res_id.name[2] = full_name_hash((unsigned char *)name,
1732 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1733 (unsigned long)id_fid(id), (unsigned long)id_group(id),
1734 parent_res_id.name[2]);
1738 cleanup_phase = 1; /* parent dentry */
1740 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1741 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1742 if (IS_ERR(*dchildp)) {
1743 rc = PTR_ERR(*dchildp);
1744 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1748 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1750 * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1751 * lock. Drop possible UPDATE lock!
1753 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1754 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1756 child_res_id.name[0] = (*dchildp)->d_fid;
1757 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1758 child_ino = (*dchildp)->d_inum;
1759 child_gen = (*dchildp)->d_generation;
1763 inode = (*dchildp)->d_inode;
1765 inode = igrab(inode);
1769 down(&inode->i_sem);
1770 rc = mds_read_inode_sid(obd, inode, &sid);
1773 CERROR("Can't read inode self id, inode %lu, "
1774 "rc %d\n", inode->i_ino, rc);
1779 child_ino = inode->i_ino;
1780 child_gen = inode->i_generation;
1781 child_res_id.name[0] = id_fid(&sid);
1782 child_res_id.name[1] = id_group(&sid);
1786 cleanup_phase = 2; /* child dentry */
1788 /* Step 3: Lock parent and child in resource order. If child doesn't
1789 * exist, we still have to lock the parent and re-lookup. */
1790 rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1791 &parent_policy, &child_res_id, child_lockh,
1792 child_mode, &child_policy);
1796 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1797 cleanup_phase = 4; /* child lock */
1799 cleanup_phase = 3; /* parent lock */
1801 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1802 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1803 parent_mode, &child_res_id, child_lockh,
1804 dchildp, child_mode, &child_policy,
1805 name, namelen, &parent_res_id, child_ino,
1817 switch (cleanup_phase) {
1819 ldlm_lock_decref(child_lockh, child_mode);
1821 ldlm_lock_decref(parent_lockh, parent_mode);
1826 if (parent_lockh[1].cookie)
1827 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1835 void mds_reconstruct_generic(struct ptlrpc_request *req)
1837 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1838 mds_req_from_mcd(req, med->med_mcd);
1841 /* If we are unlinking an open file/dir (i.e. creating an orphan) then we
1842 * instead link the inode into the PENDING directory until it is finally
1843 * released. We can't simply call mds_reint_rename() or some part thereof,
1844 * because we don't have the inode to check for link count/open status until
1845 * after it is locked.
1847 * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1848 * before starting journal transaction.
1850 * returns 1 on success
1851 * returns 0 if we lost a race and didn't make a new link
1852 * returns negative on error
1854 static int mds_orphan_add_link(struct mds_update_record *rec,
1855 struct obd_device *obd, struct dentry *dentry)
1857 struct mds_obd *mds = &obd->u.mds;
1858 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1859 struct inode *inode = dentry->d_inode;
1860 struct dentry *pending_child;
1861 char idname[LL_ID_NAMELEN];
1862 int idlen = 0, rc, mode;
1865 LASSERT(inode != NULL);
1866 LASSERT(!mds_inode_is_orphan(inode));
1867 #ifndef HAVE_I_ALLOC_SEM
1868 LASSERT(down_trylock(&inode->i_sem) != 0);
1870 LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1872 idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1874 CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1875 mds_orphan_open_count(inode), inode->i_nlink,
1876 S_ISDIR(inode->i_mode) ? "dir" :
1877 S_ISREG(inode->i_mode) ? "file" : "other",
1878 rec->ur_name, idname);
1880 if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1883 pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1884 if (IS_ERR(pending_child))
1885 RETURN(PTR_ERR(pending_child));
1887 if (pending_child->d_inode != NULL) {
1888 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1889 LASSERT(pending_child->d_inode == inode);
1890 GOTO(out_dput, rc = 0);
1894 * link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG for
1895 * linking and return real mode back then -bzzz
1897 mode = inode->i_mode;
1898 inode->i_mode = S_IFREG;
1899 rc = vfs_link(dentry, pending_dir, pending_child);
1901 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1904 mds_inode_set_orphan(inode);
1906 /* return mode and correct i_nlink if inode is directory */
1907 inode->i_mode = mode;
1908 LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1909 S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1911 if (S_ISDIR(mode)) {
1913 i_nlink_inc(pending_dir);
1914 mark_inode_dirty(inode);
1915 mark_inode_dirty(pending_dir);
1918 GOTO(out_dput, rc = 1);
1920 l_dput(pending_child);
1924 int mds_create_local_dentry(struct mds_update_record *rec,
1925 struct obd_device *obd)
1927 struct mds_obd *mds = &obd->u.mds;
1928 struct inode *id_dir = mds->mds_id_dir->d_inode;
1929 int idlen = 0, rc, cleanup_phase = 0;
1930 struct dentry *new_child = NULL;
1931 char *idname = rec->ur_name;
1932 struct dentry *child = NULL;
1933 struct lustre_handle lockh[2] = {{0}, {0}};
1934 struct lustre_id sid;
1938 down(&id_dir->i_sem);
1939 idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1940 id_gen(rec->ur_id1));
1942 CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1943 idname, OLID4(rec->ur_id1));
1945 new_child = ll_lookup_one_len(idname, mds->mds_id_dir,
1948 if (IS_ERR(new_child)) {
1949 CERROR("can't lookup %s: %d\n", idname,
1950 (int) PTR_ERR(new_child));
1951 GOTO(cleanup, rc = PTR_ERR(new_child));
1955 down(&id_dir->i_sem);
1956 rc = mds_read_inode_sid(obd, id_dir, &sid);
1959 CERROR("Can't read inode self id, inode %lu, "
1960 "rc %d\n", id_dir->i_ino, rc);
1964 if (new_child->d_inode != NULL) {
1965 /* nice. we've already have local dentry! */
1966 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1967 (unsigned)new_child->d_inode->i_ino,
1968 (unsigned)new_child->d_inode->i_generation);
1970 id_ino(rec->ur_id1) = id_dir->i_ino;
1971 id_gen(rec->ur_id1) = id_dir->i_generation;
1972 rec->ur_namelen = idlen + 1;
1974 id_fid(rec->ur_id1) = id_fid(&sid);
1975 id_group(rec->ur_id1) = id_group(&sid);
1977 GOTO(cleanup, rc = 0);
1980 /* new, local dentry will be added soon. we need no aliases here */
1983 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1984 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1986 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1987 LCK_EX, lockh, NULL, NULL, 0,
1988 MDS_INODELOCK_UPDATE);
1991 if (IS_ERR(child)) {
1992 rc = PTR_ERR(child);
1993 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1994 CERROR("can't get victim: %d\n", rc);
1999 handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
2001 GOTO(cleanup, rc = PTR_ERR(handle));
2003 rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
2004 idlen, id_ino(rec->ur_id1),
2005 id_gen(rec->ur_id1), mds->mds_num,
2006 id_fid(rec->ur_id1));
2008 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
2009 (unsigned long)child->d_inode->i_ino,
2010 (unsigned long)child->d_inode->i_generation, rc);
2012 if (S_ISDIR(child->d_inode->i_mode)) {
2013 i_nlink_inc(id_dir);
2014 mark_inode_dirty(id_dir);
2016 mark_inode_dirty(child->d_inode);
2018 fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
2020 id_ino(rec->ur_id1) = id_dir->i_ino;
2021 id_gen(rec->ur_id1) = id_dir->i_generation;
2022 rec->ur_namelen = idlen + 1;
2024 id_fid(rec->ur_id1) = id_fid(&sid);
2025 id_group(rec->ur_id1) = id_group(&sid);
2029 switch(cleanup_phase) {
2031 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
2032 ldlm_lock_decref(lockh, LCK_EX);
2042 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
2043 struct ptlrpc_request *slave)
2045 void *cookie, *cookie2;
2046 struct mds_body *body2;
2047 struct mds_body *body;
2051 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
2052 LASSERT(body != NULL);
2054 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
2055 LASSERT(body2 != NULL);
2057 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
2060 memcpy(body2, body, sizeof(*body));
2061 body2->valid &= ~OBD_MD_FLCOOKIE;
2063 if (!(body->valid & OBD_MD_FLEASIZE) &&
2064 !(body->valid & OBD_MD_FLDIREA))
2067 if (body->eadatasize == 0) {
2068 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
2072 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
2074 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
2075 LASSERT(ea != NULL);
2077 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
2078 LASSERT(ea2 != NULL);
2080 memcpy(ea2, ea, body->eadatasize);
2082 if (body->valid & OBD_MD_FLCOOKIE) {
2083 LASSERT(master->rq_repmsg->buflens[2] >=
2084 slave->rq_repmsg->buflens[2]);
2085 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
2086 slave->rq_repmsg->buflens[2]);
2087 LASSERT(cookie != NULL);
2089 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
2090 master->rq_repmsg->buflens[2]);
2091 LASSERT(cookie2 != NULL);
2092 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
2093 body2->valid |= OBD_MD_FLCOOKIE;
2098 static int mds_reint_unlink_remote(struct mds_update_record *rec,
2099 int offset, struct ptlrpc_request *req,
2100 struct lustre_handle *parent_lockh,
2101 int update_mode, struct dentry *dparent,
2102 struct lustre_handle *child_lockh,
2103 struct dentry *dchild)
2105 struct obd_device *obd = req->rq_export->exp_obd;
2106 struct mds_obd *mds = mds_req2mds(req);
2107 struct ptlrpc_request *request = NULL;
2108 int rc = 0, cleanup_phase = 0;
2109 struct mdc_op_data *op_data;
2113 LASSERT(offset == 1 || offset == 3);
2115 /* time to drop i_nlink on remote MDS */
2116 OBD_ALLOC(op_data, sizeof(*op_data));
2117 if (op_data == NULL)
2120 memset(op_data, 0, sizeof(*op_data));
2121 mds_pack_dentry2id(obd, &op_data->id1, dchild, 1);
2122 op_data->create_mode = rec->ur_mode;
2124 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode "DLID4")",
2125 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
2127 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2128 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode "DLID4")",
2129 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
2132 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
2133 op_data->create_mode |= MDS_MODE_REPLAY;
2135 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2136 OBD_FREE(op_data, sizeof(*op_data));
2141 mds_copy_unlink_reply(req, request);
2142 ptlrpc_req_finished(request);
2146 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2149 GOTO(cleanup, rc = PTR_ERR(handle));
2150 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
2151 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
2156 req->rq_status = rc;
2159 if (parent_lockh[1].cookie != 0)
2160 ldlm_lock_decref(parent_lockh + 1, update_mode);
2162 ldlm_lock_decref(child_lockh, LCK_EX);
2164 ldlm_lock_decref(parent_lockh, LCK_PW);
2166 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2173 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
2174 struct ptlrpc_request *req, struct lustre_handle *lh)
2176 struct dentry *dparent = NULL, *dchild;
2177 struct mds_obd *mds = mds_req2mds(req);
2178 struct obd_device *obd = req->rq_export->exp_obd;
2179 struct mds_body *body = NULL;
2180 struct inode *child_inode = NULL;
2181 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2182 struct lustre_handle child_lockh = {0};
2183 struct lustre_handle child_reuse_lockh = {0};
2184 struct lustre_handle *slave_lockh = NULL;
2185 char idname[LL_ID_NAMELEN];
2186 struct llog_create_locks *lcl = NULL;
2187 void *handle = NULL;
2188 int rc = 0, cleanup_phase = 0;
2189 int unlink_by_id = 0;
2193 LASSERT(offset == 1 || offset == 3);
2195 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
2196 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2199 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2201 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2202 DEBUG_REQ(D_HA, req, "unlink replay");
2203 LASSERT(offset == 1); /* should not come from intent */
2204 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
2205 lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
2206 req->rq_repmsg->buflens[2]);
2209 MD_COUNTER_INCREMENT(obd, unlink);
2211 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
2212 GOTO(cleanup, rc = -ENOENT);
2214 if (rec->ur_namelen == 1) {
2215 /* this is request to drop i_nlink on local inode */
2217 rec->ur_name = idname;
2218 rc = mds_create_local_dentry(rec, obd);
2219 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
2220 DEBUG_REQ(D_HA, req,
2221 "drop nlink on inode "DLID4" (replay)",
2222 OLID4(rec->ur_id1));
2228 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
2229 /* master mds for directory asks slave removing inode is already
2231 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
2232 LCK_PW, parent_lockh,
2233 &update_mode, rec->ur_name,
2235 MDS_INODELOCK_UPDATE);
2236 if (IS_ERR(dparent))
2237 GOTO(cleanup, rc = PTR_ERR(dparent));
2238 dchild = ll_lookup_one_len(rec->ur_name, dparent,
2239 rec->ur_namelen - 1);
2241 GOTO(cleanup, rc = PTR_ERR(dchild));
2242 child_lockh.cookie = 0;
2243 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
2244 LASSERT(dchild->d_inode != NULL);
2245 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
2247 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
2248 parent_lockh, &dparent,
2249 LCK_PW, MDS_INODELOCK_UPDATE,
2250 &update_mode, rec->ur_name,
2251 rec->ur_namelen, &child_lockh,
2253 (MDS_INODELOCK_LOOKUP |
2254 MDS_INODELOCK_UPDATE));
2259 if (dchild->d_flags & DCACHE_CROSS_REF) {
2260 /* we should have parent lock only here */
2261 LASSERT(unlink_by_id == 0);
2262 LASSERT(dchild->d_mdsnum != mds->mds_num);
2263 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
2264 update_mode, dparent, &child_lockh, dchild);
2268 cleanup_phase = 1; /* dchild, dparent, locks */
2271 child_inode = dchild->d_inode;
2272 if (child_inode == NULL) {
2273 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
2274 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
2275 GOTO(cleanup, rc = -ENOENT);
2278 cleanup_phase = 2; /* dchild has a lock */
2280 /* We have to do these checks ourselves, in case we are making an
2281 * orphan. The client tells us whether rmdir() or unlink() was called,
2282 * so we need to return appropriate errors (bug 72).
2284 * We don't have to check permissions, because vfs_rename (called from
2285 * mds_open_unlink_rename) also calls may_delete. */
2286 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
2287 if (!S_ISDIR(child_inode->i_mode))
2288 GOTO(cleanup, rc = -ENOTDIR);
2290 if (S_ISDIR(child_inode->i_mode))
2291 GOTO(cleanup, rc = -EISDIR);
2294 /* handle splitted dir */
2295 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
2299 /* Step 4: Get a lock on the ino to sync with creation WRT inode
2300 * reuse (see bug 2029). */
2301 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
2304 cleanup_phase = 3; /* child inum lock */
2306 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2308 /* ldlm_reply in buf[0] if called via intent */
2314 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2315 LASSERT(body != NULL);
2317 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2318 DOWN_READ_I_ALLOC_SEM(child_inode);
2319 cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2321 /* If this is potentially the last reference to this inode, get the
2322 * OBD EA data first so the client can destroy OST objects. We
2323 * only do the object removal later if no open files/links remain. */
2324 if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2325 child_inode->i_nlink == 1) {
2326 if (mds_orphan_open_count(child_inode) > 0) {
2327 /* need to lock pending_dir before transaction */
2328 down(&mds->mds_pending_dir->d_inode->i_sem);
2329 cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2330 } else if (S_ISREG(child_inode->i_mode)) {
2331 mds_pack_inode2body(obd, body, child_inode, 0);
2332 mds_pack_md(obd, req->rq_repmsg, offset + 1,
2333 body, child_inode, MDS_PACK_MD_LOCK, 0);
2337 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2338 switch (child_inode->i_mode & S_IFMT) {
2340 /* Drop any lingering child directories before we start our
2341 * transaction, to avoid doing multiple inode dirty/delete
2342 * in our compound transaction (bug 1321). */
2343 shrink_dcache_parent(dchild);
2344 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2347 GOTO(cleanup, rc = PTR_ERR(handle));
2348 rc = vfs_rmdir(dparent->d_inode, dchild);
2351 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2352 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2354 handle = fsfilt_start_log(obd, dparent->d_inode,
2355 FSFILT_OP_UNLINK, NULL,
2356 le32_to_cpu(lmm->lmm_stripe_count));
2358 GOTO(cleanup, rc = PTR_ERR(handle));
2359 rc = vfs_unlink(dparent->d_inode, dchild);
2367 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2370 GOTO(cleanup, rc = PTR_ERR(handle));
2371 rc = vfs_unlink(dparent->d_inode, dchild);
2374 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2377 GOTO(cleanup, rc = -EINVAL);
2380 if (rc == 0 && child_inode->i_nlink == 0) {
2381 if (mds_orphan_open_count(child_inode) > 0)
2382 rc = mds_orphan_add_link(rec, obd, dchild);
2385 GOTO(cleanup, rc = 0);
2387 if (!S_ISREG(child_inode->i_mode))
2390 if (!(body->valid & OBD_MD_FLEASIZE)) {
2391 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2392 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2393 } else if (mds_log_op_unlink(obd, child_inode,
2394 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2395 req->rq_repmsg->buflens[offset + 1],
2396 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2397 req->rq_repmsg->buflens[offset + 2],
2399 body->valid |= OBD_MD_FLCOOKIE;
2402 rc = mds_destroy_object(obd, child_inode, 1);
2404 CERROR("can't remove OST object, err %d\n",
2408 if (child_inode->i_nlink == 0)
2409 mds_fidmap_del(obd, &body->id1);
2419 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2420 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2421 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2423 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2425 CERROR("error on parent setattr: rc = %d\n", err);
2427 rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2428 handle, req, rc, 0);
2430 (void)obd_set_info(mds->mds_dt_exp, strlen("unlinked"),
2431 "unlinked", 0, NULL);
2433 switch(cleanup_phase) {
2434 case 5: /* pending_dir semaphore */
2435 up(&mds->mds_pending_dir->d_inode->i_sem);
2436 case 4: /* child inode semaphore */
2437 UP_READ_I_ALLOC_SEM(child_inode);
2438 /* handle splitted dir */
2440 /* master directory can be non-empty or something else ... */
2441 mds_unlink_slave_objs(obd, dchild);
2444 ptlrpc_save_llog_lock(req, lcl);
2445 case 3: /* child ino-reuse lock */
2446 if (rc && body != NULL) {
2447 // Don't unlink the OST objects if the MDS unlink failed
2451 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2453 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2454 case 2: /* child lock */
2455 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2456 if (child_lockh.cookie)
2457 ldlm_lock_decref(&child_lockh, LCK_EX);
2458 case 1: /* child and parent dentry, parent lock */
2460 if (parent_lockh[1].cookie != 0)
2461 ldlm_lock_decref(parent_lockh + 1, update_mode);
2464 ldlm_lock_decref(parent_lockh, LCK_PW);
2466 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2467 if (dchild->d_inode && rc && (dchild->d_inode->i_nlink == 0 ||
2468 mds_inode_is_orphan(dchild->d_inode)))
2469 CDEBUG(D_ERROR, "unlink, but return %d\n", rc);
2471 /* catching failed permissions check for audit */
2473 mds_audit(req, dchild, NULL, 0, AUDIT_UNLINK, rc);
2481 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2484 req->rq_status = rc;
2489 * to service requests from remote MDS to increment i_nlink
2491 static int mds_reint_link_acquire(struct mds_update_record *rec,
2492 int offset, struct ptlrpc_request *req,
2493 struct lustre_handle *lh)
2495 struct obd_device *obd = req->rq_export->exp_obd;
2496 struct ldlm_res_id src_res_id = { .name = {0} };
2497 struct lustre_handle *handle = NULL, src_lockh = {0};
2498 struct mds_obd *mds = mds_req2mds(req);
2499 int rc = 0, cleanup_phase = 0;
2500 struct dentry *de_src = NULL;
2501 ldlm_policy_data_t policy;
2502 int flags = LDLM_FL_ATOMIC_CB;
2505 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2506 obd->obd_name, OLID4(rec->ur_id1));
2508 /* Step 1: Lookup the source inode and target directory by ID */
2509 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2511 GOTO(cleanup, rc = PTR_ERR(de_src));
2512 cleanup_phase = 1; /* source dentry */
2514 src_res_id.name[0] = id_fid(rec->ur_id1);
2515 src_res_id.name[1] = id_group(rec->ur_id1);
2516 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2518 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2519 src_res_id, LDLM_IBITS, &policy,
2520 LCK_EX, &flags, mds_blocking_ast,
2521 ldlm_completion_ast, NULL, NULL,
2522 NULL, 0, NULL, &src_lockh);
2524 GOTO(cleanup, rc = -ENOLCK);
2525 cleanup_phase = 2; /* lock */
2527 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2529 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2530 if (IS_ERR(handle)) {
2531 rc = PTR_ERR(handle);
2534 i_nlink_inc(de_src->d_inode);
2535 mark_inode_dirty(de_src->d_inode);
2539 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2540 handle, req, rc, 0);
2541 switch (cleanup_phase) {
2544 ldlm_lock_decref(&src_lockh, LCK_EX);
2546 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2552 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2555 req->rq_status = rc;
2560 * request to link to foreign inode:
2561 * - acquire i_nlinks on this inode
2564 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2565 int offset, struct ptlrpc_request *req,
2566 struct lustre_handle *lh)
2568 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2569 struct obd_device *obd = req->rq_export->exp_obd;
2570 struct dentry *de_tgt_dir = NULL;
2571 struct mds_obd *mds = mds_req2mds(req);
2572 int rc = 0, cleanup_phase = 0;
2573 struct mdc_op_data *op_data;
2574 struct ptlrpc_request *request = NULL;
2578 DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2579 ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2580 OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2581 OLID4(rec->ur_id1));
2583 de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2584 tgt_dir_lockh, &update_mode,
2585 rec->ur_name, rec->ur_namelen - 1,
2586 MDS_INODELOCK_UPDATE);
2587 if (IS_ERR(de_tgt_dir))
2588 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2591 OBD_ALLOC(op_data, sizeof(*op_data));
2592 if (op_data == NULL)
2593 GOTO(cleanup, rc = -ENOMEM);
2595 memset(op_data, 0, sizeof(*op_data));
2596 op_data->id1 = *(rec->ur_id1);
2597 rc = md_link(mds->mds_md_exp, op_data, &request);
2598 OBD_FREE(op_data, sizeof(*op_data));
2601 ptlrpc_req_finished(request);
2607 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2609 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2610 if (IS_ERR(handle)) {
2611 rc = PTR_ERR(handle);
2617 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2618 rec->ur_namelen - 1, id_ino(rec->ur_id1),
2619 id_gen(rec->ur_id1), id_group(rec->ur_id1),
2620 id_fid(rec->ur_id1));
2623 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2624 handle, req, rc, 0);
2626 switch (cleanup_phase) {
2629 OBD_ALLOC(op_data, sizeof(*op_data));
2630 if (op_data != NULL) {
2632 memset(op_data, 0, sizeof(*op_data));
2634 op_data->id1 = *(rec->ur_id1);
2635 op_data->create_mode = rec->ur_mode;
2637 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2638 OBD_FREE(op_data, sizeof(*op_data));
2640 ptlrpc_req_finished(request);
2642 CERROR("error %d while dropping i_nlink on "
2643 "remote inode\n", rc);
2646 CERROR("rc %d prevented dropping i_nlink on "
2647 "remote inode\n", -ENOMEM);
2653 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2655 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2658 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2660 ptlrpc_save_lock(req, tgt_dir_lockh + 1, update_mode);
2666 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2669 req->rq_status = rc;
2673 static int mds_reint_link(struct mds_update_record *rec, int offset,
2674 struct ptlrpc_request *req, struct lustre_handle *lh)
2676 struct obd_device *obd = req->rq_export->exp_obd;
2677 struct dentry *de_src = NULL;
2678 struct dentry *de_tgt_dir = NULL;
2679 struct dentry *dchild = NULL;
2680 struct mds_obd *mds = mds_req2mds(req);
2681 struct lustre_handle *handle = NULL;
2682 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2683 struct ldlm_res_id src_res_id = { .name = {0} };
2684 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2685 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2686 ldlm_policy_data_t tgt_dir_policy =
2687 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2688 int rc = 0, cleanup_phase = 0;
2690 int update_mode = 0;
2694 LASSERT(offset == 1);
2696 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2697 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2698 id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2701 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2702 MD_COUNTER_INCREMENT(obd, link);
2704 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2705 GOTO(cleanup, rc = -ENOENT);
2707 if (id_group(rec->ur_id1) != mds->mds_num) {
2708 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2712 if (rec->ur_namelen == 1) {
2713 rc = mds_reint_link_acquire(rec, offset, req, lh);
2717 /* Step 1: Lookup the source inode and target directory by ID */
2718 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2720 GOTO(cleanup, rc = PTR_ERR(de_src));
2722 cleanup_phase = 1; /* source dentry */
2724 de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2725 if (IS_ERR(de_tgt_dir)) {
2726 rc = PTR_ERR(de_tgt_dir);
2731 cleanup_phase = 2; /* target directory dentry */
2733 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2734 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2735 rec->ur_name, de_src->d_inode->i_ino);
2737 /* Step 2: Take the two locks */
2738 src_res_id.name[0] = id_fid(rec->ur_id1);
2739 src_res_id.name[1] = id_group(rec->ur_id1);
2740 tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2741 tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2744 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2745 int flags = LDLM_FL_ATOMIC_CB;
2746 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2748 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2749 tgt_dir_res_id, LDLM_IBITS,
2750 &src_policy, update_mode, &flags,
2752 ldlm_completion_ast, NULL, NULL,
2753 NULL, 0, NULL, tgt_dir_lockh + 1);
2755 GOTO(cleanup, rc = -ENOLCK);
2758 tgt_dir_res_id.name[2] = full_name_hash((unsigned char *)rec->ur_name,
2759 rec->ur_namelen - 1);
2760 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2761 (unsigned long)id_fid(rec->ur_id2),
2762 (unsigned long)id_group(rec->ur_id2),
2763 tgt_dir_res_id.name[2]);
2766 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2767 &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2768 LCK_EX, &tgt_dir_policy);
2772 cleanup_phase = 3; /* locks */
2774 /* Step 3: Lookup the child */
2775 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir,
2776 rec->ur_namelen - 1);
2777 if (IS_ERR(dchild)) {
2778 rc = PTR_ERR(dchild);
2779 if (rc != -EPERM && rc != -EACCES)
2780 CERROR("child lookup error %d\n", rc);
2784 cleanup_phase = 4; /* child dentry */
2786 if (dchild->d_inode) {
2787 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2788 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2793 /* Step 4: Do it. */
2794 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2796 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2797 if (IS_ERR(handle)) {
2798 rc = PTR_ERR(handle);
2802 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2803 if (rc && rc != -EPERM && rc != -EACCES)
2804 CERROR("vfs_link error %d\n", rc);
2806 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2807 handle, req, rc, 0);
2810 switch (cleanup_phase) {
2811 case 4: /* child dentry */
2815 ldlm_lock_decref(&src_lockh, LCK_EX);
2816 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2818 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2819 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2821 case 2: /* target dentry */
2823 if (tgt_dir_lockh[1].cookie && update_mode)
2824 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2828 case 1: /* source dentry */
2833 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2836 req->rq_status = rc;
2840 /* The idea here is that we need to get four locks in the end:
2841 * one on each parent directory, one on each child. We need to take
2842 * these locks in some kind of order (to avoid deadlocks), and the order
2843 * I selected is "increasing resource number" order. We need to look up
2844 * the children, however, before we know what the resource number(s) are.
2845 * Thus the following plan:
2847 * 1,2. Look up the parents
2848 * 3,4. Look up the children
2849 * 5. Take locks on the parents and children, in order
2850 * 6. Verify that the children haven't changed since they were looked up
2852 * If there was a race and the children changed since they were first looked
2853 * up, it is possible that mds_verify_child() will be able to just grab the
2854 * lock on the new child resource (if it has a higher resource than any other)
2855 * but we need to compare against not only its parent, but also against the
2856 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2858 * We need the fancy igrab() on the child inodes because we aren't holding a
2859 * lock on the parent after the lookup is done, so dentry->d_inode may change
2860 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2862 static int mds_get_parents_children_locked(struct obd_device *obd,
2863 struct mds_obd *mds,
2864 struct lustre_id *p1_id,
2865 struct dentry **de_srcdirp,
2866 struct lustre_id *p2_id,
2867 struct dentry **de_tgtdirp,
2869 const char *old_name, int old_len,
2870 struct dentry **de_oldp,
2871 const char *new_name, int new_len,
2872 struct dentry **de_newp,
2873 struct lustre_handle *dlm_handles,
2876 struct ldlm_res_id p1_res_id = { .name = {0} };
2877 struct ldlm_res_id p2_res_id = { .name = {0} };
2878 struct ldlm_res_id c1_res_id = { .name = {0} };
2879 struct ldlm_res_id c2_res_id = { .name = {0} };
2880 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2881 /* Only dentry should disappear, but the inode itself would be
2882 intact otherwise. */
2883 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2884 /* If something is going to be replaced, both dentry and inode locks are
2886 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_FULL}};
2887 struct ldlm_res_id *maxres_src, *maxres_tgt;
2888 struct inode *inode;
2889 int rc = 0, cleanup_phase = 0;
2890 __u32 child_gen1 = 0;
2891 __u32 child_gen2 = 0;
2892 unsigned long child_ino1 = 0;
2893 unsigned long child_ino2 = 0;
2896 /* Step 1: Lookup the source directory */
2897 *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2898 if (IS_ERR(*de_srcdirp))
2899 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2901 cleanup_phase = 1; /* source directory dentry */
2903 p1_res_id.name[0] = id_fid(p1_id);
2904 p1_res_id.name[1] = id_group(p1_id);
2906 /* Step 2: Lookup the target directory */
2907 if (id_equal_stc(p1_id, p2_id)) {
2908 *de_tgtdirp = dget(*de_srcdirp);
2910 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2911 if (IS_ERR(*de_tgtdirp)) {
2912 rc = PTR_ERR(*de_tgtdirp);
2918 cleanup_phase = 2; /* target directory dentry */
2920 p2_res_id.name[0] = id_fid(p2_id);
2921 p2_res_id.name[1] = id_group(p2_id);
2924 dlm_handles[5].cookie = 0;
2925 dlm_handles[6].cookie = 0;
2927 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2929 * get a temp lock on just fid, group to flush client cache and
2930 * to protect dirs from concurrent splitting.
2932 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2933 LCK_PW, &p_policy, &p2_res_id,
2934 &dlm_handles[6], LCK_PW, &p_policy);
2938 p1_res_id.name[2] = full_name_hash((unsigned char *)old_name,
2940 p2_res_id.name[2] = full_name_hash((unsigned char *)new_name,
2943 CDEBUG(D_INFO, "take locks on "
2944 LPX64":"LPX64":"LPX64", "LPX64":"LPX64":"LPX64"\n",
2945 p1_res_id.name[0], p1_res_id.name[1], p1_res_id.name[2],
2946 p2_res_id.name[0], p2_res_id.name[1], p2_res_id.name[2]);
2951 /* Step 3: Lookup the source child entry */
2952 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp,
2954 if (IS_ERR(*de_oldp)) {
2955 rc = PTR_ERR(*de_oldp);
2956 CERROR("old child lookup error (%.*s): %d\n",
2957 old_len - 1, old_name, rc);
2961 cleanup_phase = 4; /* original name dentry */
2963 inode = (*de_oldp)->d_inode;
2964 if (inode != NULL) {
2965 struct lustre_id sid;
2967 inode = igrab(inode);
2969 GOTO(cleanup, rc = -ENOENT);
2971 down(&inode->i_sem);
2972 rc = mds_read_inode_sid(obd, inode, &sid);
2975 CERROR("Can't read inode self id, inode %lu, "
2976 "rc %d\n", inode->i_ino, rc);
2981 child_ino1 = inode->i_ino;
2982 child_gen1 = inode->i_generation;
2983 c1_res_id.name[0] = id_fid(&sid);
2984 c1_res_id.name[1] = id_group(&sid);
2986 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2987 child_ino1 = (*de_oldp)->d_inum;
2988 child_gen1 = (*de_oldp)->d_generation;
2989 c1_res_id.name[0] = (*de_oldp)->d_fid;
2990 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2992 GOTO(cleanup, rc = -ENOENT);
2995 /* Step 4: Lookup the target child entry */
2996 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp,
2998 if (IS_ERR(*de_newp)) {
2999 rc = PTR_ERR(*de_newp);
3000 CERROR("new child lookup error (%.*s): %d\n",
3001 old_len - 1, old_name, rc);
3005 cleanup_phase = 5; /* target dentry */
3007 inode = (*de_newp)->d_inode;
3008 if (inode != NULL) {
3009 struct lustre_id sid;
3011 inode = igrab(inode);
3015 down(&inode->i_sem);
3016 rc = mds_read_inode_sid(obd, inode, &sid);
3019 CERROR("Can't read inode self id, inode %lu, "
3020 "rc %d\n", inode->i_ino, rc);
3024 child_ino2 = inode->i_ino;
3025 child_gen2 = inode->i_generation;
3026 c2_res_id.name[0] = id_fid(&sid);
3027 c2_res_id.name[1] = id_group(&sid);
3029 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
3030 child_ino2 = (*de_newp)->d_inum;
3031 child_gen2 = (*de_newp)->d_generation;
3032 c2_res_id.name[0] = (*de_newp)->d_fid;
3033 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
3037 /* Step 5: Take locks on the parents and child(ren) */
3038 maxres_src = &p1_res_id;
3039 maxres_tgt = &p2_res_id;
3040 cleanup_phase = 5; /* target dentry */
3042 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
3043 maxres_src = &c1_res_id;
3044 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
3045 maxres_tgt = &c2_res_id;
3047 rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
3049 &p2_res_id, &dlm_handles[1], parent_mode,
3051 &c1_res_id, &dlm_handles[2], child_mode,
3053 &c2_res_id, &dlm_handles[3], child_mode,
3058 cleanup_phase = 6; /* parent and child(ren) locks */
3060 /* Step 6a: Re-lookup source child to verify it hasn't changed */
3061 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
3062 parent_mode, &c1_res_id, &dlm_handles[2],
3063 de_oldp, child_mode, &c1_policy, old_name, old_len,
3064 maxres_tgt, child_ino1, child_gen1);
3066 if (c2_res_id.name[0] != 0)
3067 ldlm_lock_decref(&dlm_handles[3], child_mode);
3068 ldlm_lock_decref(&dlm_handles[1], parent_mode);
3075 if (!DENTRY_VALID(*de_oldp))
3076 GOTO(cleanup, rc = -ENOENT);
3078 /* Step 6b: Re-lookup target child to verify it hasn't changed */
3079 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
3080 parent_mode, &c2_res_id, &dlm_handles[3],
3081 de_newp, child_mode, &c2_policy, new_name,
3082 new_len, maxres_src, child_ino2, child_gen2);
3084 ldlm_lock_decref(&dlm_handles[2], child_mode);
3085 ldlm_lock_decref(&dlm_handles[0], parent_mode);
3095 switch (cleanup_phase) {
3096 case 6: /* child lock(s) */
3097 if (c2_res_id.name[0] != 0)
3098 ldlm_lock_decref(&dlm_handles[3], child_mode);
3099 if (c1_res_id.name[0] != 0)
3100 ldlm_lock_decref(&dlm_handles[2], child_mode);
3101 if (dlm_handles[1].cookie != 0)
3102 ldlm_lock_decref(&dlm_handles[1], parent_mode);
3103 if (dlm_handles[0].cookie != 0)
3104 ldlm_lock_decref(&dlm_handles[0], parent_mode);
3105 case 5: /* target dentry */
3107 case 4: /* source dentry */
3111 if (dlm_handles[5].cookie != 0)
3112 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3113 if (dlm_handles[6].cookie != 0)
3114 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3116 case 2: /* target directory dentry */
3117 l_dput(*de_tgtdirp);
3118 case 1: /* source directry dentry */
3119 l_dput(*de_srcdirp);
3127 * checks if dentry can be removed. This function also handles cross-ref
3130 static int mds_check_for_rename(struct obd_device *obd,
3131 struct dentry *dentry)
3133 struct mds_obd *mds = &obd->u.mds;
3134 struct lustre_handle *rlockh;
3135 struct ptlrpc_request *req;
3136 struct mdc_op_data *op_data;
3137 struct lookup_intent it;
3138 int handle_size, rc = 0;
3141 LASSERT(dentry != NULL);
3143 if (dentry->d_inode) {
3144 if (S_ISDIR(dentry->d_inode->i_mode) &&
3145 !mds_is_dir_empty(obd, dentry))
3148 LASSERT((dentry->d_flags & DCACHE_CROSS_REF));
3149 handle_size = sizeof(struct lustre_handle);
3151 OBD_ALLOC(rlockh, handle_size);
3155 memset(rlockh, 0, handle_size);
3156 OBD_ALLOC(op_data, sizeof(*op_data));
3157 if (op_data == NULL) {
3158 OBD_FREE(rlockh, handle_size);
3161 memset(op_data, 0, sizeof(*op_data));
3162 mds_pack_dentry2id(obd, &op_data->id1, dentry, 1);
3164 it.it_op = IT_UNLINK;
3165 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
3168 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
3169 op_data, rlockh, NULL, 0, ldlm_completion_ast,
3170 mds_blocking_ast, NULL);
3171 OBD_FREE(op_data, sizeof(*op_data));
3175 OBD_FREE(it.d.fs_data,
3176 sizeof(struct lustre_intent_data));
3179 if (rlockh->cookie != 0)
3180 ldlm_lock_decref(rlockh, LCK_EX);
3182 if (LUSTRE_IT(&it)->it_data) {
3183 req = (struct ptlrpc_request *)LUSTRE_IT(&it)->it_data;
3184 ptlrpc_req_finished(req);
3187 if (LUSTRE_IT(&it)->it_status)
3188 rc = LUSTRE_IT(&it)->it_status;
3189 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
3190 OBD_FREE(rlockh, handle_size);
3195 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
3196 struct ptlrpc_request *req, struct lustre_id *id,
3197 struct dentry *de_dir, struct dentry *de)
3199 struct obd_device *obd = req->rq_export->exp_obd;
3200 struct mds_obd *mds = mds_req2mds(req);
3201 void *handle = NULL;
3207 * name exists and points to local inode try to unlink this name
3208 * and create new one.
3210 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
3211 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
3212 (unsigned long)de->d_inode->i_generation);
3214 /* checking if we can remove local dentry. */
3215 rc = mds_check_for_rename(obd, de);
3219 handle = fsfilt_start(obd, de_dir->d_inode,
3220 FSFILT_OP_RENAME, NULL);
3222 GOTO(cleanup, rc = PTR_ERR(handle));
3223 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3226 } else if (de->d_flags & DCACHE_CROSS_REF) {
3227 CDEBUG(D_OTHER, "%s: %s points to remote inode %lu/%lu\n",
3228 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_mdsnum,
3229 (unsigned long)de->d_fid);
3231 /* checking if we can remove local dentry. */
3232 rc = mds_check_for_rename(obd, de);
3237 * to be fully POSIX compatible, we should add one more check:
3239 * if de_new is subdir of dir rec->ur_id1. If so - return
3242 * I do not know how to implement it right now, because
3243 * inodes/dentries for new and old names lie on different MDS,
3244 * so add this notice here just to make it visible for the rest
3245 * of developers and do not forget about. And when this check
3246 * will be added, del_cross_ref should gone, that is local
3247 * dentry is able to be removed if all checks passed. --umka
3250 handle = fsfilt_start(obd, de_dir->d_inode,
3251 FSFILT_OP_RENAME, NULL);
3253 GOTO(cleanup, rc = PTR_ERR(handle));
3254 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3258 /* name doesn't exist. the simplest case. */
3259 handle = fsfilt_start(obd, de_dir->d_inode,
3260 FSFILT_OP_LINK, NULL);
3262 GOTO(cleanup, rc = PTR_ERR(handle));
3265 rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
3266 rec->ur_tgtlen - 1, id_ino(id),
3267 id_gen(id), id_group(id), id_fid(id));
3269 CERROR("add_dir_entry() returned error %d\n", rc);
3275 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3276 handle, req, rc, 0);
3281 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
3282 struct ptlrpc_request *req, struct dentry *de_dir,
3285 struct obd_device *obd = req->rq_export->exp_obd;
3286 struct mds_obd *mds = mds_req2mds(req);
3287 void *handle = NULL;
3291 handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
3293 GOTO(cleanup, rc = PTR_ERR(handle));
3294 rc = fsfilt_del_dir_entry(obd, de);
3299 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3300 handle, req, rc, 0);
3304 static int mds_reint_rename_create_name(struct mds_update_record *rec,
3305 int offset, struct ptlrpc_request *req)
3307 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3308 struct obd_device *obd = req->rq_export->exp_obd;
3309 struct mds_obd *mds = mds_req2mds(req);
3310 struct lustre_handle child_lockh = {0};
3311 struct dentry *de_tgtdir = NULL;
3312 struct dentry *de_new = NULL;
3313 int cleanup_phase = 0;
3314 int update_mode, rc = 0;
3315 struct lustre_id ids[2]; /* sid, pid */
3316 struct obd_export *set_exp;
3320 * another MDS executing rename operation has asked us to create target
3321 * name. such a creation should destroy existing target name.
3323 CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
3324 obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
3326 /* first, lookup the target */
3327 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
3328 &de_tgtdir, LCK_PW, MDS_INODELOCK_UPDATE,
3329 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
3330 &child_lockh, &de_new, LCK_EX,
3331 MDS_INODELOCK_LOOKUP);
3335 /* get parent id: ldlm lock on the parent protects ea */
3336 rc = mds_read_inode_sid(obd, de_tgtdir->d_inode, &ids[1]);
3343 LASSERT(de_tgtdir->d_inode);
3346 rc = mds_add_local_dentry(rec, offset, req, rec->ur_id1,
3351 ids[0] = *(rec->ur_id1);
3352 if (id_group(ids) == mds->mds_num)
3353 set_exp = req->rq_export;
3355 set_exp = mds->mds_md_exp;
3356 rc = obd_set_info(set_exp, strlen("ids"), "ids",
3357 sizeof(struct lustre_id) * 2, ids);
3362 if (cleanup_phase == 1) {
3364 if (parent_lockh[1].cookie != 0)
3365 ldlm_lock_decref(parent_lockh + 1, update_mode);
3367 ldlm_lock_decref(parent_lockh, LCK_PW);
3368 if (child_lockh.cookie != 0)
3369 ldlm_lock_decref(&child_lockh, LCK_EX);
3374 req->rq_status = rc;
3378 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
3379 struct ptlrpc_request *req)
3381 struct obd_device *obd = req->rq_export->exp_obd;
3382 struct ptlrpc_request *req2 = NULL;
3383 struct dentry *de_srcdir = NULL;
3384 struct dentry *de_old = NULL;
3385 struct mds_obd *mds = mds_req2mds(req);
3386 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3387 struct lustre_handle child_lockh = {0};
3388 struct mdc_op_data *op_data;
3389 int update_mode, rc = 0;
3392 CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
3393 obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
3395 OBD_ALLOC(op_data, sizeof(*op_data));
3396 if (op_data == NULL)
3398 memset(op_data, 0, sizeof(*op_data));
3400 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
3401 &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
3402 &update_mode, rec->ur_name,
3403 rec->ur_namelen, &child_lockh, &de_old,
3404 LCK_EX, MDS_INODELOCK_LOOKUP);
3406 OBD_FREE(op_data, sizeof(*op_data));
3411 LASSERT(de_srcdir->d_inode);
3415 * we already know the target should be created on another MDS so, we
3416 * have to request that MDS to do it.
3419 /* prepare source id */
3420 if (de_old->d_flags & DCACHE_CROSS_REF) {
3421 LASSERT(de_old->d_inode == NULL);
3422 CDEBUG(D_OTHER, "request to move remote name\n");
3423 mds_pack_dentry2id(obd, &op_data->id1, de_old, 1);
3424 } else if (de_old->d_inode == NULL) {
3425 /* oh, source doesn't exist */
3426 OBD_FREE(op_data, sizeof(*op_data));
3427 GOTO(cleanup, rc = -ENOENT);
3429 struct lustre_id sid;
3430 struct inode *inode = de_old->d_inode;
3432 LASSERT(inode != NULL);
3433 CDEBUG(D_OTHER, "request to move local name\n");
3434 id_ino(&op_data->id1) = inode->i_ino;
3435 id_group(&op_data->id1) = mds->mds_num;
3436 id_gen(&op_data->id1) = inode->i_generation;
3438 down(&inode->i_sem);
3439 rc = mds_read_inode_sid(obd, inode, &sid);
3442 CERROR("Can't read inode self id, "
3443 "inode %lu, rc = %d\n",
3448 id_fid(&op_data->id1) = id_fid(&sid);
3451 op_data->id2 = *rec->ur_id2;
3452 rc = md_rename(mds->mds_md_exp, op_data, NULL, 0,
3453 rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3454 OBD_FREE(op_data, sizeof(*op_data));
3459 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3465 ptlrpc_req_finished(req2);
3468 if (parent_lockh[1].cookie != 0)
3469 ldlm_lock_decref(parent_lockh + 1, update_mode);
3471 ldlm_lock_decref(parent_lockh, LCK_PW);
3472 if (child_lockh.cookie != 0)
3473 ldlm_lock_decref(&child_lockh, LCK_EX);
3478 req->rq_status = rc;
3482 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3483 struct ptlrpc_request *req, struct lustre_handle *lockh)
3485 struct obd_device *obd = req->rq_export->exp_obd;
3486 struct dentry *de_srcdir = NULL;
3487 struct dentry *de_tgtdir = NULL;
3488 struct dentry *de_old = NULL;
3489 struct dentry *de_new = NULL;
3490 struct inode *old_inode = NULL, *new_inode = NULL;
3491 struct mds_obd *mds = mds_req2mds(req);
3492 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3493 struct mds_body *body = NULL;
3494 struct llog_create_locks *lcl = NULL;
3495 struct lov_mds_md *lmm = NULL;
3496 int rc = 0, cleanup_phase = 0;
3497 struct lustre_id ids[2]; /* sid, pid */
3498 void *handle = NULL;
3501 LASSERT(offset == 1);
3503 DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3504 OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3507 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3509 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3510 DEBUG_REQ(D_HA, req, "rename replay");
3511 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3512 lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3513 req->rq_repmsg->buflens[2]);
3516 MD_COUNTER_INCREMENT(obd, rename);
3518 if (rec->ur_namelen == 1) {
3519 rc = mds_reint_rename_create_name(rec, offset, req);
3523 /* check if new name should be located on remote target. */
3524 if (id_group(rec->ur_id2) != mds->mds_num) {
3525 rc = mds_reint_rename_to_remote(rec, offset, req);
3529 rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3530 rec->ur_id2, &de_tgtdir, LCK_PW,
3531 rec->ur_name, rec->ur_namelen,
3532 &de_old, rec->ur_tgt,
3533 rec->ur_tgtlen, &de_new,
3534 dlm_handles, LCK_EX);
3538 cleanup_phase = 1; /* parent(s), children, locks */
3539 old_inode = de_old->d_inode;
3540 new_inode = de_new->d_inode;
3542 /* sanity check for src inode */
3543 if (de_old->d_flags & DCACHE_CROSS_REF) {
3544 LASSERT(de_old->d_inode == NULL);
3547 * in the case of cross-ref dir, we can perform this check only
3548 * if child and parent lie on the same mds. This is because
3549 * otherwise they can have the same inode numbers.
3551 if (de_old->d_mdsnum == mds->mds_num) {
3552 if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3553 de_old->d_inum == de_tgtdir->d_inode->i_ino)
3554 GOTO(cleanup, rc = -EINVAL);
3557 LASSERT(de_old->d_inode != NULL);
3558 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3559 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3560 GOTO(cleanup, rc = -EINVAL);
3563 /* sanity check for dest inode */
3564 if (de_new->d_flags & DCACHE_CROSS_REF) {
3565 LASSERT(new_inode == NULL);
3567 /* the same check about target dentry. */
3568 if (de_new->d_mdsnum == mds->mds_num) {
3569 if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3570 de_new->d_inum == de_tgtdir->d_inode->i_ino)
3571 GOTO(cleanup, rc = -EINVAL);
3575 * regular files usualy do not have ->rename() implemented. But
3576 * we handle only this case when @de_new is cross-ref entry,
3577 * because in other cases it will be handled by vfs_rename().
3579 if (de_old->d_inode && (!de_old->d_inode->i_op ||
3580 !de_old->d_inode->i_op->rename))
3581 GOTO(cleanup, rc = -EPERM);
3584 (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3585 new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3586 GOTO(cleanup, rc = -EINVAL);
3591 * check if inodes point to each other. This should be checked before
3592 * is_subdir() check, as for the same entries it will think that they
3595 if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3596 !(de_new->d_flags & DCACHE_CROSS_REF) &&
3597 old_inode == new_inode)
3598 GOTO(cleanup, rc = 0);
3600 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3602 * check if we are moving old entry into its child. 2.6 does not check
3603 * for this in vfs_rename() anymore.
3605 if (is_subdir(de_new, de_old))
3606 GOTO(cleanup, rc = -EINVAL);
3610 * if we are about to remove the target at first, pass the EA of that
3611 * inode to client to perform and cleanup on OST.
3613 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3614 LASSERT(body != NULL);
3616 /* get new parent id: ldlm lock on the parent protects ea */
3617 rc = mds_read_inode_sid(obd, de_tgtdir->d_inode, &ids[1]);
3621 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3623 DOWN_READ_I_ALLOC_SEM(new_inode);
3625 cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3627 if (new_inode && ((S_ISDIR(new_inode->i_mode) &&
3628 new_inode->i_nlink == 2) ||
3629 new_inode->i_nlink == 1)) {
3630 if (mds_orphan_open_count(new_inode) > 0) {
3631 /* need to lock pending_dir before transaction */
3632 down(&mds->mds_pending_dir->d_inode->i_sem);
3633 cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3634 } else if (S_ISREG(new_inode->i_mode)) {
3635 mds_pack_inode2body(obd, body, new_inode, 0);
3636 mds_pack_md(obd, req->rq_repmsg, 1, body,
3637 new_inode, MDS_PACK_MD_LOCK, 0);
3641 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3642 de_srcdir->d_inode->i_sb);
3644 if (de_old->d_flags & DCACHE_CROSS_REF) {
3645 struct lustre_id old_id;
3646 struct obd_export *set_exp;
3649 mds_pack_dentry2id(obd, &old_id, de_old, 1);
3651 rc = mds_add_local_dentry(rec, offset, req, &old_id,
3656 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3662 if (id_group(ids) == mds->mds_num)
3663 set_exp = req->rq_export;
3665 set_exp = mds->mds_md_exp;
3666 rc = obd_set_info(set_exp, strlen("ids"), "ids",
3667 sizeof(struct lustre_id) * 2, ids);
3672 lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3673 handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3674 NULL, le32_to_cpu(lmm->lmm_stripe_count));
3677 GOTO(cleanup, rc = PTR_ERR(handle));
3680 de_old->d_fsdata = req;
3681 de_new->d_fsdata = req;
3682 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3685 if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3686 if (mds_orphan_open_count(new_inode) > 0)
3687 rc = mds_orphan_add_link(rec, obd, de_new);
3690 GOTO(cleanup, rc = 0);
3692 if (!S_ISREG(new_inode->i_mode))
3695 if (!(body->valid & OBD_MD_FLEASIZE)) {
3696 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3697 OBD_MD_FLATIME | OBD_MD_FLMTIME);
3698 } else if (mds_log_op_unlink(obd, new_inode,
3699 lustre_msg_buf(req->rq_repmsg,1,0),
3700 req->rq_repmsg->buflens[1],
3701 lustre_msg_buf(req->rq_repmsg,2,0),
3702 req->rq_repmsg->buflens[2],
3704 body->valid |= OBD_MD_FLCOOKIE;
3707 rc = mds_destroy_object(obd, old_inode, 1);
3709 CERROR("can't remove OST object, err %d\n",
3715 rc = mds_update_inode_ids(obd, de_old->d_inode,
3716 handle, NULL, &ids[1]);
3720 rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3721 handle, req, rc, 0);
3723 switch (cleanup_phase) {
3725 up(&mds->mds_pending_dir->d_inode->i_sem);
3728 UP_READ_I_ALLOC_SEM(new_inode);
3731 if (dlm_handles[5].cookie != 0)
3732 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3733 if (dlm_handles[6].cookie != 0)
3734 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3737 ptlrpc_save_llog_lock(req, lcl);
3740 if (dlm_handles[3].cookie != 0)
3741 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3742 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3743 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3744 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3746 if (dlm_handles[3].cookie != 0)
3747 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3748 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3749 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3750 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3759 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3762 req->rq_status = rc;
3766 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3767 struct ptlrpc_request *, struct lustre_handle *);
3769 static mds_reinter reinters[REINT_MAX + 1] = {
3770 [REINT_SETATTR] mds_reint_setattr,
3771 [REINT_CREATE] mds_reint_create,
3772 [REINT_LINK] mds_reint_link,
3773 [REINT_UNLINK] mds_reint_unlink,
3774 [REINT_RENAME] mds_reint_rename,
3775 [REINT_OPEN] mds_open
3778 int mds_reint_rec(struct mds_update_record *rec, int offset,
3779 struct ptlrpc_request *req, struct lustre_handle *lockh)
3781 struct obd_device *obd = req->rq_export->exp_obd;
3782 struct lvfs_run_ctxt saved;
3785 /* checked by unpacker */
3786 LASSERT(rec->ur_opcode <= REINT_MAX &&
3787 reinters[rec->ur_opcode] != NULL);
3789 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3790 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3791 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);