1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/mds/mds_reint.c
5 * Lustre Metadata Server (mds) reintegration routines
7 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8 * Author: Peter Braam <braam@clusterfs.com>
9 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * Author: Phil Schwan <phil@clusterfs.com>
12 * This file is part of Lustre, http://www.lustre.org.
14 * Lustre is free software; you can redistribute it and/or
15 * modify it under the terms of version 2 of the GNU General Public
16 * License as published by the Free Software Foundation.
18 * Lustre is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with Lustre; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 # define EXPORT_SYMTAB
31 #define DEBUG_SUBSYSTEM S_MDS
34 #include <linux/jbd.h>
35 #include <linux/namei.h>
36 #include <linux/ext3_fs.h>
37 #include <linux/obd_support.h>
38 #include <linux/obd_class.h>
39 #include <linux/obd.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_idl.h>
42 #include <linux/lustre_mds.h>
43 #include <linux/lustre_dlm.h>
44 #include <linux/lustre_log.h>
45 #include <linux/lustre_fsfilt.h>
46 #include <linux/lustre_acl.h>
47 #include <linux/lustre_lite.h>
48 #include <linux/lustre_smfs.h>
49 #include "mds_internal.h"
51 struct mds_logcancel_data {
52 struct lov_mds_md *mlcd_lmm;
56 struct llog_cookie mlcd_cookies[0];
59 static void mds_cancel_cookies_cb(struct obd_device *obd,
60 __u64 transno, void *cb_data,
63 struct mds_logcancel_data *mlcd = cb_data;
64 struct lov_stripe_md *lsm = NULL;
65 struct llog_ctxt *ctxt;
68 obd_transno_commit_cb(obd, transno, error);
70 CDEBUG(D_HA, "cancelling %d cookies\n",
71 (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
73 rc = obd_unpackmd(obd->u.mds.mds_dt_exp, &lsm, mlcd->mlcd_lmm,
74 mlcd->mlcd_eadatalen);
76 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
77 (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
80 ///* XXX 0 normally, SENDNOW for debug */);
81 ctxt = llog_get_context(&obd->obd_llogs,
82 mlcd->mlcd_cookies[0].lgc_subsys + 1);
83 rc = llog_cancel(ctxt, mlcd->mlcd_cookielen /
84 sizeof(*mlcd->mlcd_cookies),
85 mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW, lsm);
87 CERROR("error cancelling %d log cookies: rc %d\n",
88 (int)(mlcd->mlcd_cookielen /
89 sizeof(*mlcd->mlcd_cookies)), rc);
90 obd_free_memmd(obd->u.mds.mds_dt_exp, &lsm);
93 OBD_FREE(mlcd, mlcd->mlcd_size);
96 /* Assumes caller has already pushed us into the kernel context. */
97 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
98 struct ptlrpc_request *req, int rc, __u32 op_data)
100 struct mds_export_data *med = &req->rq_export->exp_mds_data;
101 struct obd_device *obd = req->rq_export->exp_obd;
102 struct mds_client_data *mcd = med->med_mcd;
103 int err, log_pri = D_HA;
108 /* if the export has already been failed, we have no last_rcvd slot */
109 if (req->rq_export->exp_failed) {
110 CERROR("committing transaction for disconnected client\n");
112 GOTO(out_commit, rc);
119 if (handle == NULL) {
120 /* if we're starting our own xaction, use our own inode */
121 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
122 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
123 if (IS_ERR(handle)) {
124 CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
125 RETURN(PTR_ERR(handle));
131 transno = req->rq_reqmsg->transno;
133 LASSERTF(transno == 0, "BUG 3934, t"LPU64" rc %d\n", transno, rc);
134 } else if (transno == 0) {
135 spin_lock(&mds->mds_transno_lock);
136 transno = ++mds->mds_last_transno;
137 spin_unlock(&mds->mds_transno_lock);
139 spin_lock(&mds->mds_transno_lock);
140 if (transno > mds->mds_last_transno)
141 mds->mds_last_transno = transno;
142 spin_unlock(&mds->mds_transno_lock);
144 req->rq_repmsg->transno = req->rq_transno = transno;
145 if (req->rq_reqmsg->opc == MDS_CLOSE) {
146 mcd->mcd_last_close_transno = cpu_to_le64(transno);
147 mcd->mcd_last_close_xid = cpu_to_le64(req->rq_xid);
148 mcd->mcd_last_close_result = cpu_to_le32(rc);
149 mcd->mcd_last_close_data = cpu_to_le32(op_data);
151 mcd->mcd_last_transno = cpu_to_le64(transno);
152 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
153 mcd->mcd_last_result = cpu_to_le32(rc);
154 mcd->mcd_last_data = cpu_to_le32(op_data);
157 fsfilt_add_journal_cb(obd, mds->mds_sb, transno, handle,
158 mds_commit_last_transno_cb, NULL);
160 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd,
161 sizeof(*mcd), &off, 0);
169 DEBUG_REQ(log_pri, req,
170 "wrote trans #"LPU64" client %s at idx %u: err = %d",
171 transno, mcd->mcd_uuid, med->med_idx, err);
173 err = mds_update_last_fid(obd, handle, 0);
180 err = mds_dt_write_objids(obd);
186 CDEBUG(log_pri, "wrote objids: err = %d\n", err);
190 err = fsfilt_commit(obd, mds->mds_sb, inode, handle,
191 req->rq_export->exp_sync);
193 CERROR("error committing transaction: %d\n", err);
201 /* this gives the same functionality as the code between
202 * sys_chmod and inode_setattr
203 * chown_common and inode_setattr
204 * utimes and inode_setattr
207 /* Just for the case if we have some clients that know about ATTR_RAW */
208 #define ATTR_RAW 8192
210 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
212 time_t now = LTIME_S(CURRENT_TIME);
213 struct iattr *attr = &rec->ur_iattr;
214 unsigned int ia_valid = attr->ia_valid;
218 /* only fix up attrs if the client VFS didn't already */
220 if (!(ia_valid & ATTR_RAW))
223 if (!(ia_valid & ATTR_CTIME_SET))
224 LTIME_S(attr->ia_ctime) = now;
225 if (!(ia_valid & ATTR_ATIME_SET))
226 LTIME_S(attr->ia_atime) = now;
227 if (!(ia_valid & ATTR_MTIME_SET))
228 LTIME_S(attr->ia_mtime) = now;
230 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
234 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
235 if (rec->ur_fsuid != inode->i_uid &&
236 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
240 if (ia_valid & ATTR_SIZE) {
241 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
245 if (ia_valid & ATTR_UID) {
248 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
250 if (attr->ia_uid == (uid_t) -1)
251 attr->ia_uid = inode->i_uid;
252 if (attr->ia_gid == (gid_t) -1)
253 attr->ia_gid = inode->i_gid;
254 attr->ia_mode = inode->i_mode;
256 * If the user or group of a non-directory has been
257 * changed by a non-root user, remove the setuid bit.
258 * 19981026 David C Niemi <niemi@tux.org>
260 * Changed this to apply to all users, including root,
261 * to avoid some races. This is the behavior we had in
262 * 2.0. The check for non-root was definitely wrong
263 * for 2.2 anyway, as it should have been using
264 * CAP_FSETID rather than fsuid -- 19990830 SD.
266 if ((inode->i_mode & S_ISUID) == S_ISUID &&
267 !S_ISDIR(inode->i_mode)) {
268 attr->ia_mode &= ~S_ISUID;
269 attr->ia_valid |= ATTR_MODE;
272 * Likewise, if the user or group of a non-directory
273 * has been changed by a non-root user, remove the
274 * setgid bit UNLESS there is no group execute bit
275 * (this would be a file marked for mandatory
276 * locking). 19981026 David C Niemi <niemi@tux.org>
278 * Removed the fsuid check (see the comment above) --
281 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
282 (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
283 attr->ia_mode &= ~S_ISGID;
284 attr->ia_valid |= ATTR_MODE;
286 } else if (ia_valid & ATTR_MODE) {
287 int mode = attr->ia_mode;
289 if (attr->ia_mode == (mode_t) -1)
290 attr->ia_mode = inode->i_mode;
292 (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
297 void mds_steal_ack_locks(struct ptlrpc_request *req)
299 struct obd_export *exp = req->rq_export;
300 char str[PTL_NALFMT_SIZE];
301 struct list_head *tmp;
302 struct ptlrpc_reply_state *oldrep;
303 struct ptlrpc_service *svc;
304 struct llog_create_locks *lcl;
308 /* CAVEAT EMPTOR: spinlock order */
309 spin_lock_irqsave (&exp->exp_lock, flags);
310 list_for_each (tmp, &exp->exp_outstanding_replies) {
311 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
313 if (oldrep->rs_xid != req->rq_xid)
316 if (oldrep->rs_msg->opc != req->rq_reqmsg->opc)
317 CERROR ("Resent req xid "LPX64" has mismatched opc: "
318 "new %d old %d\n", req->rq_xid,
319 req->rq_reqmsg->opc, oldrep->rs_msg->opc);
321 svc = oldrep->rs_srv_ni->sni_service;
322 spin_lock (&svc->srv_lock);
324 list_del_init (&oldrep->rs_exp_list);
326 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
327 " o%d NID %s\n", oldrep->rs_nlocks, oldrep,
328 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg->opc,
329 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
331 for (i = 0; i < oldrep->rs_nlocks; i++)
332 ptlrpc_save_lock(req,
333 &oldrep->rs_locks[i],
334 oldrep->rs_modes[i]);
335 oldrep->rs_nlocks = 0;
337 lcl = oldrep->rs_llog_locks;
338 oldrep->rs_llog_locks = NULL;
340 ptlrpc_save_llog_lock(req, lcl);
342 DEBUG_REQ(D_HA, req, "stole locks for");
343 ptlrpc_schedule_difficult_reply (oldrep);
345 spin_unlock (&svc->srv_lock);
346 spin_unlock_irqrestore (&exp->exp_lock, flags);
349 spin_unlock_irqrestore (&exp->exp_lock, flags);
352 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
354 if (req->rq_reqmsg->opc == MDS_CLOSE) {
355 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
356 mcd->mcd_last_close_transno, mcd->mcd_last_close_result);
357 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_close_transno;
358 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_close_result;
360 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
361 mcd->mcd_last_transno, mcd->mcd_last_result);
362 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
363 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
366 mds_steal_ack_locks(req);
369 static void reconstruct_reint_setattr(struct mds_update_record *rec,
370 int offset, struct ptlrpc_request *req)
372 struct mds_export_data *med = &req->rq_export->exp_mds_data;
373 struct mds_body *body;
376 mds_req_from_mcd(req, med->med_mcd);
378 de = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
380 LASSERT(PTR_ERR(de) == req->rq_status);
384 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
385 mds_pack_inode2body(req2obd(req), body, de->d_inode, 1);
387 /* Don't return OST-specific attributes if we didn't just set them */
388 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
389 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
390 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
391 body->valid |= OBD_MD_FLMTIME;
392 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
393 body->valid |= OBD_MD_FLATIME;
398 static int mds_reint_remote_setfacl(struct obd_device *obd,
399 struct mds_export_data *med,
400 struct mds_update_record *rec,
401 struct ptlrpc_request *req)
403 struct rmtacl_upcall_desc desc;
406 struct mds_body *body;
408 int repsize[2] = { sizeof(*body), LUSTRE_ACL_SIZE_MAX };
411 rc = lustre_pack_reply(req, 2, repsize, NULL);
415 de = mds_id2dentry(obd, rec->ur_id1, NULL);
417 GOTO(out, rc = PTR_ERR(de));
422 /* setxattr from remote client:
424 memset(&desc, 0, sizeof(desc));
425 desc.cmd = (char *) rec->ur_ea2data;
426 desc.cmdlen = rec->ur_ea2datalen;
427 desc.res = lustre_msg_buf(req->rq_repmsg, 1, LUSTRE_ACL_SIZE_MAX);
428 desc.reslen = LUSTRE_ACL_SIZE_MAX;
430 mds_do_remote_acl_upcall(&desc);
431 if (desc.upcall_status)
432 GOTO(out_put, rc = desc.upcall_status);
435 desc.status = -desc.status;
437 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
440 /* client (lmv) will do limited checking upon replied mds_body,
441 * we pack it as normal, but "steal" field "flags" field to store
442 * the acl execution status.
444 mds_pack_inode2body(obd, body, inode, 1);
445 body->flags = desc.status;
446 mds_body_do_reverse_map(med, body);
456 /*This is a tmp fix for cmobd setattr reint*/
458 #define XATTR_LUSTRE_MDS_LOV_EA "lov"
459 #define XATTR_LUSTRE_MDS_MEA_EA "mea"
460 #define XATTR_LUSTRE_MDS_MID_EA "mid"
461 #define XATTR_LUSTRE_MDS_SID_EA "sid"
463 static int mds_get_md_type(char *name)
465 if (!strcmp(name, XATTR_LUSTRE_MDS_LOV_EA))
467 if (!strcmp(name, XATTR_LUSTRE_MDS_MEA_EA))
469 if (!strcmp(name, XATTR_LUSTRE_MDS_MID_EA))
471 if (!strcmp(name, XATTR_LUSTRE_MDS_SID_EA))
476 /* In the raw-setattr case, we lock the child inode.
477 * In the write-back case or if being called from open, the client holds a lock
480 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
481 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
482 struct ptlrpc_request *req, struct lustre_handle *lh)
484 struct mds_obd *mds = mds_req2mds(req);
485 struct obd_device *obd = req->rq_export->exp_obd;
486 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
487 struct mds_body *body;
488 struct dentry *de = NULL;
489 struct inode *inode = NULL;
490 struct lustre_handle lockh[2] = {{0}, {0}};
493 struct mds_logcancel_data *mlcd = NULL;
494 int rc = 0, cleanup_phase = 0, err;
495 int repsize = sizeof(*body), locked = 0;
498 LASSERT(offset == 1);
500 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x",
501 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
502 rec->ur_iattr.ia_valid);
504 /* remote setfacl need special handling */
505 if ((rec->ur_iattr.ia_valid & ATTR_EA) &&
506 !strcmp(rec->ur_eadata, XATTR_NAME_LUSTRE_ACL)) {
507 return mds_reint_remote_setfacl(obd, med, rec, req);
510 rc = lustre_pack_reply(req, 1, &repsize, NULL);
514 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
515 MD_COUNTER_INCREMENT(obd, setattr);
517 if (med->med_remote) {
518 if (rec->ur_iattr.ia_valid & ATTR_GID) {
519 CWARN("Deny chgrp from remote client\n");
520 GOTO(cleanup, rc = -EPERM);
522 if (rec->ur_iattr.ia_valid & ATTR_UID) {
525 uid = mds_idmap_lookup_uid(med->med_idmap, 0,
526 rec->ur_iattr.ia_uid);
527 if (uid == MDS_IDMAP_NOTFOUND) {
528 CWARN("Deny chown to uid %u\n",
529 rec->ur_iattr.ia_uid);
530 GOTO(cleanup, rc = -EPERM);
532 rec->ur_iattr.ia_uid = uid;
536 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
537 de = mds_id2dentry(obd, rec->ur_id1, NULL);
539 GOTO(cleanup, rc = PTR_ERR(de));
541 __u64 lockpart = MDS_INODELOCK_UPDATE;
542 if (rec->ur_iattr.ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
543 lockpart |= MDS_INODELOCK_LOOKUP;
544 de = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
545 lockh, &parent_mode, NULL, 0, lockpart);
547 GOTO(cleanup, rc = PTR_ERR(de));
555 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
556 rec->ur_eadata != NULL)
559 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
561 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
563 GOTO(cleanup, rc = PTR_ERR(handle));
565 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
566 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
567 LTIME_S(rec->ur_iattr.ia_mtime),
568 LTIME_S(rec->ur_iattr.ia_ctime));
569 rc = mds_fix_attr(inode, rec);
573 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
574 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
575 (long)&rec->ur_iattr.ia_attr_flags);
577 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
580 if (rec->ur_iattr.ia_valid & ATTR_EA) {
581 int flags = (int) rec->ur_iattr.ia_attr_flags;
584 if (!med->med_remote && inode->i_op &&
585 inode->i_op->setxattr)
586 rc = inode->i_op->setxattr(
591 } else if (rec->ur_iattr.ia_valid & ATTR_EA_RM) {
593 if (inode->i_op && inode->i_op->removexattr)
594 rc = inode->i_op->removexattr(de,
596 } else if (rec->ur_iattr.ia_valid & ATTR_EA_CMOBD) {
599 /*tmp fix for cmobd set md reint*/
600 LASSERT(rec->ur_eadata != NULL);
601 LASSERT(rec->ur_ea2data != NULL);
602 name = rec->ur_eadata;
603 CDEBUG(D_INFO, "set %s EA for cmobd \n", name);
604 type = mds_get_md_type(name);
606 rc = fsfilt_set_md(obd, inode, handle,
608 rec->ur_ea2datalen, type);
611 } else if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
612 !((rec->ur_iattr.ia_valid & ATTR_KEY) ||
613 (rec->ur_iattr.ia_valid & ATTR_MAC))) {
614 struct lov_stripe_md *lsm = NULL;
615 struct lov_user_md *lum = NULL;
617 if (rec->ur_eadata != NULL) {
618 rc = ll_permission(inode, MAY_WRITE, NULL);
622 lum = rec->ur_eadata;
624 /* if lmm_stripe_size is -1 delete default
626 if (S_ISDIR(inode->i_mode) &&
627 lum->lmm_stripe_size == (typeof(lum->lmm_stripe_size))(-1)){
628 rc = fsfilt_set_md(obd, inode, handle, NULL, 0, EA_LOV);
632 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
634 &lsm, rec->ur_eadata);
638 obd_free_memmd(mds->mds_dt_exp, &lsm);
639 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
640 rec->ur_eadatalen, EA_LOV);
646 if ((rec->ur_iattr.ia_valid & ATTR_KEY) ||
647 (rec->ur_iattr.ia_valid & ATTR_MAC)) {
650 LASSERT(rec->ur_eadatalen || rec->ur_ea3datalen);
651 LASSERT(rec->ur_eadata || rec->ur_ea3data);
652 key = rec->ur_eadata ? rec->ur_eadata : rec->ur_ea3data;
653 keylen = rec->ur_eadatalen ? rec->ur_eadatalen :
655 mds_set_gskey(obd, handle, inode, key, keylen,
656 rec->ur_iattr.ia_valid);
660 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
661 mds_pack_inode2body(obd, body, inode, 1);
663 /* Don't return OST-specific attributes if we didn't just set them */
664 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
665 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
666 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
667 body->valid |= OBD_MD_FLMTIME;
668 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
669 body->valid |= OBD_MD_FLATIME;
671 mds_body_do_reverse_map(med, body);
673 /* The logcookie should be no use anymore, why nobody remove
674 * following code block?
676 LASSERT(rec->ur_cookielen == 0);
677 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_dt_obd)) {
678 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
681 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
683 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
684 mlcd->mlcd_cookielen = rec->ur_cookielen;
685 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
686 mlcd->mlcd_cookielen;
687 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
688 mlcd->mlcd_cookielen);
689 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
690 mlcd->mlcd_eadatalen);
692 CERROR("unable to allocate log cancel data\n");
698 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
699 handle, mds_cancel_cookies_cb, mlcd);
700 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
701 switch (cleanup_phase) {
703 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
704 rec->ur_eadata != NULL)
709 if (lockh[1].cookie != 0)
710 ldlm_lock_decref(lockh + 1, parent_mode);
713 ldlm_lock_decref(lockh, LCK_PW);
715 ptlrpc_save_lock (req, lockh, LCK_PW);
730 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
731 struct ptlrpc_request *req)
733 struct mds_export_data *med = &req->rq_export->exp_mds_data;
734 struct dentry *parent, *child;
735 struct mds_body *body;
738 mds_req_from_mcd(req, med->med_mcd);
740 if (req->rq_status) {
745 parent = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
746 LASSERT(!IS_ERR(parent));
747 child = ll_lookup_one_len(rec->ur_name, parent,
748 rec->ur_namelen - 1);
749 LASSERT(!IS_ERR(child));
750 if ((child->d_flags & DCACHE_CROSS_REF)) {
751 LASSERTF(child->d_inode == NULL, "BUG 3869\n");
752 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
753 mds_pack_dentry2body(req2obd(req), body, child, 1);
754 } else if (child->d_inode == NULL) {
755 DEBUG_REQ(D_ERROR, req, "parent "DLID4" name %s mode %o",
756 OLID4(rec->ur_id1), rec->ur_name, rec->ur_mode);
757 LASSERTF(child->d_inode != NULL, "BUG 3869\n");
759 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
760 mds_pack_inode2body(req2obd(req), body, child->d_inode, 1);
767 static int mds_get_default_acl(struct inode *dir, void **pacl)
769 struct dentry de = { .d_inode = dir };
772 LASSERT(S_ISDIR(dir->i_mode));
774 if (!dir->i_op->getxattr)
777 size = dir->i_op->getxattr(&de, XATTR_NAME_ACL_DEFAULT, NULL, 0);
778 if (size == 0 || size == -ENODATA || size == -EOPNOTSUPP)
783 OBD_ALLOC(*pacl, size);
787 size2 = dir->i_op->getxattr(&de, XATTR_NAME_ACL_DEFAULT, *pacl, size);
789 /* since we already locked the dir, it should not change
790 * between the 2 getxattr calls
792 CERROR("2'nd getxattr got %d, expect %d\n", size2, size);
793 OBD_FREE(*pacl, size);
800 static int mds_reint_create(struct mds_update_record *rec, int offset,
801 struct ptlrpc_request *req,
802 struct lustre_handle *lh)
804 struct dentry *dparent = NULL;
805 struct mds_obd *mds = mds_req2mds(req);
806 struct obd_device *obd = req->rq_export->exp_obd;
807 struct dentry *dchild = NULL;
808 struct inode *dir = NULL;
810 struct lustre_handle lockh[2] = {{0}, {0}};
812 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
814 struct dentry_params dp;
815 struct mea *mea = NULL;
820 LASSERT(offset == 1);
822 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
823 OBD_MDS_DEVICENAME));
825 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
826 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
827 rec->ur_name, rec->ur_mode);
829 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
831 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
832 GOTO(cleanup, rc = -ESTALE);
834 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
835 lockh, &parent_mode, rec->ur_name,
836 rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
837 if (IS_ERR(dparent)) {
838 rc = PTR_ERR(dparent);
839 CERROR("parent lookup error %d\n", rc);
842 cleanup_phase = 1; /* locked parent dentry */
843 dir = dparent->d_inode;
846 ldlm_lock_dump_handle(D_OTHER, lockh);
848 /* try to retrieve MEA data for this dir */
849 rc = mds_md_get_attr(obd, dparent->d_inode, &mea, &mea_size);
855 * dir is already splitted, check is requested filename should
856 * live at this MDS or at another one.
858 int i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
859 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
860 CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
861 " should be %lu(%d)\n",
862 mea->mea_master, dparent->d_inode->i_ino,
863 dparent->d_inode->i_generation, rec->ur_name,
864 (unsigned long)id_group(&mea->mea_ids[i]), i);
865 GOTO(cleanup, rc = -ERESTART);
869 dchild = ll_lookup_one_len(rec->ur_name, dparent,
870 rec->ur_namelen - 1);
871 if (IS_ERR(dchild)) {
872 rc = PTR_ERR(dchild);
873 CERROR("Can't find "DLID4"/%s, error %d\n",
874 OLID4(rec->ur_id1), rec->ur_name, rc);
878 cleanup_phase = 2; /* child dentry */
880 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
882 if (type == S_IFREG || type == S_IFDIR) {
883 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
884 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
885 obd->obd_name, dparent->d_inode->i_ino,
886 dparent->d_inode->i_generation, rc, parent_mode);
888 /* dir got splitted */
889 GOTO(cleanup, rc = -ERESTART);
891 /* error happened during spitting. */
896 if (dir->i_mode & S_ISGID) {
897 if (S_ISDIR(rec->ur_mode))
898 rec->ur_mode |= S_ISGID;
902 * here inode number should be used only in the case of replaying. It is
903 * needed to check if object already created in the case of creating
906 if (id_ino(rec->ur_id2))
907 fid = id_fid(rec->ur_id2);
909 fid = mds_alloc_fid(obd);
910 dchild->d_fsdata = (void *)&dp;
911 dp.p_inum = (unsigned long)id_ino(rec->ur_id2);
914 dp.p_group = mds->mds_num;
918 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
920 GOTO(cleanup, rc = PTR_ERR(handle));
921 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
922 if (rec->ur_eadata && rec->ur_eadatalen &&
923 (rc == 0) && (dchild->d_inode != NULL)) {
924 /*Assumption: When ur_eadata is not NULL,
925 *ur_eadata is crypto key, should fix it later, Wangdi*/
926 mds_set_gskey(obd, handle, dchild->d_inode,
927 rec->ur_eadata, rec->ur_eadatalen,
928 ATTR_MAC | ATTR_KEY);
937 * as Peter asked, mkdir() should distribute new directories
938 * over the whole cluster in order to distribute namespace
939 * processing load. first, we calculate which MDS to use to put
940 * new directory's inode in.
942 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1,
943 rec->ur_flags, &req->rq_peer, dir);
944 if (i == mds->mds_num) {
945 /* inode will be created locally */
946 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
948 GOTO(cleanup, rc = PTR_ERR(handle));
950 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
953 "Can't create dir \"%s\", rc = %d\n",
954 dchild->d_name.name, rc);
958 down(&dchild->d_inode->i_sem);
960 rc = mds_update_inode_sid(obd, dchild->d_inode,
961 handle, rec->ur_id2);
963 CERROR("mds_update_inode_sid() failed, inode %lu, "
964 "rc %d\n", dchild->d_inode->i_ino, rc);
968 * make sure, that fid is up-to-date.
970 mds_set_last_fid(obd, id_fid(rec->ur_id2));
972 rc = mds_set_inode_sid(obd, dchild->d_inode,
975 CERROR("mds_set_inode_sid() failed, inode %lu, "
976 "rc %d\n", dchild->d_inode->i_ino, rc);
979 up(&dchild->d_inode->i_sem);
985 nstripes = *(u16 *)rec->ur_eadata;
987 if (rc == 0 && nstripes) {
989 * we pass LCK_EX to split routine to signal,
990 * that we have exclusive access to the
991 * directory. Simple because nobody knows it
992 * already exists -bzzz
994 rc = mds_try_to_split_dir(obd, dchild,
998 /* dir got splitted */
1000 } else if (rc < 0) {
1001 /* an error occured during
1006 } else if (!DENTRY_VALID(dchild)) {
1007 /* inode will be created on another MDS */
1008 struct obdo *oa = NULL;
1009 struct mds_body *body;
1013 /* first, create that inode */
1016 GOTO(cleanup, rc = -ENOMEM);
1021 if (rec->ur_eadata) {
1022 /* user asks for creating splitted dir */
1023 oa->o_easize = *((u16 *) rec->ur_eadata);
1026 obdo_from_inode(oa, dir, OBD_MD_FLATIME |
1027 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1029 /* adjust the uid/gid/mode bits */
1030 oa->o_mode = rec->ur_mode;
1031 oa->o_uid = current->fsuid;
1032 oa->o_gid = (dir->i_mode & S_ISGID) ?
1033 dir->i_gid : current->fsgid;
1034 oa->o_valid |= OBD_MD_FLTYPE|OBD_MD_FLUID|OBD_MD_FLGID;
1036 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
1039 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1041 * here inode number and generation are
1042 * important, as this is replay request and we
1043 * need them to check if such an object is
1046 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
1047 obd->obd_name, rec->ur_namelen - 1,
1048 rec->ur_name, (unsigned)id_ino(rec->ur_id2),
1049 (unsigned)id_gen(rec->ur_id2));
1050 oa->o_id = id_ino(rec->ur_id2);
1051 oa->o_fid = id_fid(rec->ur_id2);
1052 oa->o_generation = id_gen(rec->ur_id2);
1053 oa->o_flags |= OBD_FL_RECREATE_OBJS;
1054 LASSERT(oa->o_fid != 0);
1057 /* obtain default ACL */
1058 acl_size = mds_get_default_acl(dir, &acl);
1061 GOTO(cleanup, rc = -ENOMEM);
1065 * before obd_create() is called, o_fid is not known if
1066 * this is not recovery of cause.
1068 rc = obd_create(mds->mds_md_exp, oa, acl, acl_size,
1072 OBD_FREE(acl, acl_size);
1075 CERROR("can't create remote inode: %d\n", rc);
1076 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
1077 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
1078 rec->ur_name, rec->ur_mode);
1083 LASSERT(oa->o_fid != 0);
1085 /* now, add new dir entry for it */
1086 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
1087 if (IS_ERR(handle)) {
1089 GOTO(cleanup, rc = PTR_ERR(handle));
1092 /* creating local dentry for remote inode. */
1093 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
1094 rec->ur_namelen - 1, oa->o_id,
1095 oa->o_generation, i, oa->o_fid);
1098 CERROR("Can't create local entry %*s for "
1099 "remote inode.\n", rec->ur_namelen - 1,
1105 body = lustre_msg_buf(req->rq_repmsg,
1107 body->valid |= OBD_MD_FLID | OBD_MD_MDS | OBD_MD_FID;
1109 obdo2id(&body->id1, oa);
1112 /* requested name exists in the directory */
1119 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
1121 GOTO(cleanup, rc = PTR_ERR(handle));
1122 if (rec->ur_tgt == NULL) /* no target supplied */
1123 rc = -EINVAL; /* -EPROTO? */
1125 rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
1133 int rdev = rec->ur_rdev;
1134 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
1136 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
1137 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
1142 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
1143 dchild->d_fsdata = NULL;
1144 GOTO(cleanup, rc = -EINVAL);
1147 /* In case we stored the desired inum in here, we want to clean up. */
1148 if (dchild->d_fsdata == (void *)(unsigned long)id_ino(rec->ur_id2))
1149 dchild->d_fsdata = NULL;
1152 CDEBUG(D_INODE, "error during create: %d\n", rc);
1154 } else if (dchild->d_inode) {
1155 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
1157 struct mds_body *body;
1158 struct inode *inode = dchild->d_inode;
1161 iattr.ia_uid = rec->ur_fsuid;
1162 LTIME_S(iattr.ia_atime) = rec->ur_time;
1163 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1164 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1166 if (dir->i_mode & S_ISGID)
1167 iattr.ia_gid = dir->i_gid;
1169 iattr.ia_gid = rec->ur_fsgid;
1171 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
1172 ATTR_MTIME | ATTR_CTIME;
1174 if (id_ino(rec->ur_id2)) {
1175 LASSERT(id_ino(rec->ur_id2) == inode->i_ino);
1176 inode->i_generation = id_gen(rec->ur_id2);
1178 if (type != S_IFDIR) {
1179 down(&inode->i_sem);
1180 rc = mds_update_inode_sid(obd, inode,
1181 handle, rec->ur_id2);
1184 CERROR("Can't update inode self id, "
1189 * make sure, that fid is up-to-date.
1191 mds_set_last_fid(obd, id_fid(rec->ur_id2));
1194 /* dirtied and committed by the upcoming setattr. */
1195 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
1196 inode->i_ino, inode->i_generation);
1198 struct lustre_handle child_ino_lockh;
1200 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
1201 inode->i_ino, inode->i_generation);
1203 if (type != S_IFDIR) {
1205 * allocate new id for @inode if it is not dir,
1206 * because for dir it was already done.
1208 down(&inode->i_sem);
1209 rc = mds_set_inode_sid(obd, inode,
1213 CERROR("mds_set_inode_sid() failed, "
1214 "inode %lu, rc %d\n", inode->i_ino,
1221 * the inode we were allocated may have just
1222 * been freed by an unlink operation. We take
1223 * this lock to synchronize against the matching
1224 * reply-ack-lock taken in unlink, to avoid
1225 * replay problems if this reply makes it out to
1226 * the client but the unlink's does not. See
1227 * bug 2029 for more detail.
1229 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
1230 if (rc != ELDLM_OK) {
1231 CERROR("error locking for unlink/create sync: "
1234 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
1239 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
1241 CERROR("error on child setattr: rc = %d\n", rc);
1243 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1244 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1246 CERROR("error on parent setattr: rc = %d\n", rc);
1248 MD_COUNTER_INCREMENT(obd, create);
1250 /* take care of default stripe inheritance */
1251 if (type == S_IFDIR) {
1252 struct lov_mds_md lmm;
1253 int lmm_size = sizeof(lmm);
1255 rc = mds_get_md(obd, dir, &lmm, &lmm_size, 1, 0);
1257 down(&inode->i_sem);
1258 rc = fsfilt_set_md(obd, inode, handle,
1259 &lmm, lmm_size, EA_LOV);
1263 CERROR("error on copy stripe info: rc = %d\n",
1269 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1270 mds_pack_inode2body(obd, body, inode, 1);
1271 mds_body_do_reverse_map(med, body);
1276 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
1278 if (rc && created) {
1279 /* Destroy the file we just created. This should not need extra
1280 * journal credits, as we have already modified all of the
1281 * blocks needed in order to create the file in the first
1285 err = vfs_rmdir(dir, dchild);
1287 CERROR("rmdir in error path: %d\n", err);
1290 err = vfs_unlink(dir, dchild);
1292 CERROR("unlink in error path: %d\n", err);
1298 switch (cleanup_phase) {
1299 case 2: /* child dentry */
1301 case 1: /* locked parent dentry */
1303 if (lockh[1].cookie != 0)
1304 ldlm_lock_decref(lockh + 1, parent_mode);
1307 ldlm_lock_decref(lockh, LCK_PW);
1309 ptlrpc_save_lock(req, lockh, LCK_PW);
1315 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1319 OBD_FREE(mea, mea_size);
1320 req->rq_status = rc;
1325 res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
1326 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1330 for (i = 0; i < RES_NAME_SIZE; i++) {
1332 * this is needed to make zeroed res_id entries to be put at the
1333 * end of list in *ordered_locks() .
1335 if (res1->name[i] == 0 && res2->name[i] != 0)
1337 if (res2->name[i] == 0 && res1->name[i] != 0)
1339 if (res1->name[i] > res2->name[i])
1341 if (res1->name[i] < res2->name[i])
1348 if (memcmp(p1, p2, sizeof(*p1)) < 0)
1354 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1355 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1356 * because they take the place of local semaphores.
1358 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
1359 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
1360 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
1361 struct lustre_handle *p1_lockh, int p1_lock_mode,
1362 ldlm_policy_data_t *p1_policy,
1363 struct ldlm_res_id *p2_res_id,
1364 struct lustre_handle *p2_lockh, int p2_lock_mode,
1365 ldlm_policy_data_t *p2_policy)
1367 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1368 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1369 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1370 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
1374 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1376 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1377 res_id[0]->name[0], res_id[1]->name[0]);
1379 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1380 handles[1] = p1_lockh;
1381 handles[0] = p2_lockh;
1382 res_id[1] = p1_res_id;
1383 res_id[0] = p2_res_id;
1384 lock_modes[1] = p1_lock_mode;
1385 lock_modes[0] = p2_lock_mode;
1386 policies[1] = p1_policy;
1387 policies[0] = p2_policy;
1390 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1391 res_id[0]->name[0], res_id[1]->name[0]);
1393 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1394 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1395 LDLM_IBITS, policies[0], lock_modes[0], &flags,
1396 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1397 NULL, 0, NULL, handles[0]);
1400 ldlm_lock_dump_handle(D_OTHER, handles[0]);
1402 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1403 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1404 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1405 ldlm_lock_addref(handles[1], lock_modes[1]);
1406 } else if (res_id[1]->name[0] != 0) {
1407 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1408 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1409 *res_id[1], LDLM_IBITS, policies[1],
1410 lock_modes[1], &flags, mds_blocking_ast,
1411 ldlm_completion_ast, NULL, NULL, NULL, 0,
1413 if (rc != ELDLM_OK) {
1414 ldlm_lock_decref(handles[0], lock_modes[0]);
1417 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1423 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1424 struct lustre_handle *p1_lockh, int p1_lock_mode,
1425 ldlm_policy_data_t *p1_policy,
1426 struct ldlm_res_id *p2_res_id,
1427 struct lustre_handle *p2_lockh, int p2_lock_mode,
1428 ldlm_policy_data_t *p2_policy,
1429 struct ldlm_res_id *c1_res_id,
1430 struct lustre_handle *c1_lockh, int c1_lock_mode,
1431 ldlm_policy_data_t *c1_policy,
1432 struct ldlm_res_id *c2_res_id,
1433 struct lustre_handle *c2_lockh, int c2_lock_mode,
1434 ldlm_policy_data_t *c2_policy)
1436 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1437 c1_res_id, c2_res_id };
1438 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1439 c1_lockh, c2_lockh };
1440 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1441 c1_lock_mode, c2_lock_mode };
1442 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1443 c1_policy, c2_policy};
1444 int rc, i, j, sorted, flags;
1447 CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1448 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1449 res_id[3]->name[0]);
1452 * simple insertion sort - we have at most 4 elements. Note, that zeroed
1453 * res_id should be at the end of list after sorting is finished.
1455 for (i = 1; i < 4; i++) {
1457 dlm_handles[4] = dlm_handles[i];
1458 res_id[4] = res_id[i];
1459 lock_modes[4] = lock_modes[i];
1460 policies[4] = policies[i];
1464 if (res_gt(res_id[j], res_id[4], policies[j],
1466 dlm_handles[j + 1] = dlm_handles[j];
1467 res_id[j + 1] = res_id[j];
1468 lock_modes[j + 1] = lock_modes[j];
1469 policies[j + 1] = policies[j];
1474 } while (j >= 0 && !sorted);
1476 dlm_handles[j + 1] = dlm_handles[4];
1477 res_id[j + 1] = res_id[4];
1478 lock_modes[j + 1] = lock_modes[4];
1479 policies[j + 1] = policies[4];
1482 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1483 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1484 res_id[3]->name[0]);
1486 /* XXX we could send ASTs on all these locks first before blocking? */
1487 for (i = 0; i < 4; i++) {
1491 * nevertheless zeroed res_ids should be at the end of list, and
1492 * could use break here, I think, that it is more correctly for
1493 * clear understanding of code to have continue here, as it
1494 * clearly means, that zeroed res_id should be skipped and does
1495 * not mean, that if we meet zeroed res_id we should stop
1498 if (res_id[i]->name[0] == 0)
1502 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1503 (policies[i]->l_inodebits.bits &
1504 policies[i-1]->l_inodebits.bits) ) {
1505 memcpy(dlm_handles[i], dlm_handles[i-1],
1506 sizeof(*(dlm_handles[i])));
1507 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1509 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1510 *res_id[i], LDLM_IBITS,
1512 lock_modes[i], &flags,
1514 ldlm_completion_ast, NULL, NULL,
1515 NULL, 0, NULL, dlm_handles[i]);
1517 GOTO(out_err, rc = -EIO);
1518 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1525 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1530 /* In the unlikely case that the child changed while we were waiting
1531 * on the lock, we need to drop the lock on the old child and either:
1532 * - if the child has a lower resource name, then we have to also
1533 * drop the parent lock and regain the locks in the right order
1534 * - in the rename case, if the child has a lower resource name than one of
1535 * the other parent/child resources (maxres) we also need to reget the locks
1536 * - if the child has a higher resource name (this is the common case)
1537 * we can just get the lock on the new child (still in lock order)
1539 * Returns 0 if the child did not change or if it changed but could be locked.
1540 * Returns 1 if the child changed and we need to re-lock (no locks held).
1541 * Returns -ve error with a valid dchild (no locks held). */
1542 static int mds_verify_child(struct obd_device *obd,
1543 struct ldlm_res_id *parent_res_id,
1544 struct lustre_handle *parent_lockh,
1545 struct dentry *dparent, int parent_mode,
1546 struct ldlm_res_id *child_res_id,
1547 struct lustre_handle *child_lockh,
1548 struct dentry **dchildp, int child_mode,
1549 ldlm_policy_data_t *child_policy,
1550 const char *name, int namelen,
1551 struct ldlm_res_id *maxres,
1552 unsigned long child_ino, __u32 child_gen)
1554 struct lustre_id sid;
1555 struct dentry *vchild, *dchild = *dchildp;
1556 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1559 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1561 GOTO(cleanup, rc = PTR_ERR(vchild));
1563 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1564 if (child_gen == vchild->d_generation &&
1565 child_ino == vchild->d_inum) {
1574 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1575 (vchild->d_inode != NULL &&
1576 child_gen == vchild->d_inode->i_generation &&
1577 child_ino == vchild->d_inode->i_ino))) {
1585 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1586 vchild->d_inode, dchild ? dchild->d_inode : 0,
1587 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1588 child_res_id->name[0]);
1590 if (child_res_id->name[0] != 0)
1591 ldlm_lock_decref(child_lockh, child_mode);
1595 cleanup_phase = 1; /* parent lock only */
1596 *dchildp = dchild = vchild;
1598 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1599 int flags = LDLM_FL_ATOMIC_CB;
1601 if (dchild->d_inode) {
1602 down(&dchild->d_inode->i_sem);
1603 rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
1604 up(&dchild->d_inode->i_sem);
1606 CERROR("Can't read inode self id, inode %lu,"
1607 " rc %d\n", dchild->d_inode->i_ino, rc);
1610 child_res_id->name[0] = id_fid(&sid);
1611 child_res_id->name[1] = id_group(&sid);
1613 child_res_id->name[0] = dchild->d_fid;
1614 child_res_id->name[1] = dchild->d_mdsnum;
1617 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1618 res_gt(maxres, child_res_id, NULL, NULL)) {
1619 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1620 child_res_id->name[0], parent_res_id->name[0],
1622 GOTO(cleanup, rc = 1);
1625 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1626 *child_res_id, LDLM_IBITS, child_policy,
1627 child_mode, &flags, mds_blocking_ast,
1628 ldlm_completion_ast, NULL, NULL, NULL, 0,
1631 GOTO(cleanup, rc = -EIO);
1634 memset(child_res_id, 0, sizeof(*child_res_id));
1640 switch(cleanup_phase) {
1642 if (child_res_id->name[0] != 0)
1643 ldlm_lock_decref(child_lockh, child_mode);
1645 ldlm_lock_decref(parent_lockh, parent_mode);
1651 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1652 struct lustre_id *id,
1653 struct lustre_handle *parent_lockh,
1654 struct dentry **dparentp, int parent_mode,
1655 __u64 parent_lockpart, int *update_mode,
1656 char *name, int namelen,
1657 struct lustre_handle *child_lockh,
1658 struct dentry **dchildp, int child_mode,
1659 __u64 child_lockpart)
1661 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1662 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1663 struct ldlm_res_id parent_res_id = { .name = {0} };
1664 struct ldlm_res_id child_res_id = { .name = {0} };
1665 unsigned long child_ino = 0; __u32 child_gen = 0;
1666 int rc = 0, cleanup_phase = 0;
1667 struct lustre_id sid;
1668 struct inode *inode;
1671 /* Step 1: Lookup parent */
1672 *dparentp = mds_id2dentry(obd, id, NULL);
1673 if (IS_ERR(*dparentp)) {
1674 rc = PTR_ERR(*dparentp);
1679 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1680 (*dparentp)->d_inode->i_ino, name);
1682 parent_res_id.name[0] = id_fid(id);
1683 parent_res_id.name[1] = id_group(id);
1686 parent_lockh[1].cookie = 0;
1687 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1688 struct ldlm_res_id res_id = { .name = {0} };
1689 ldlm_policy_data_t policy;
1690 int flags = LDLM_FL_ATOMIC_CB;
1692 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1694 res_id.name[0] = id_fid(id);
1695 res_id.name[1] = id_group(id);
1696 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1698 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1699 res_id, LDLM_IBITS, &policy,
1700 *update_mode, &flags,
1702 ldlm_completion_ast,
1703 NULL, NULL, NULL, 0, NULL,
1709 parent_res_id.name[2] = full_name_hash((unsigned char *)name,
1712 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1713 (unsigned long)id_fid(id), (unsigned long)id_group(id),
1714 parent_res_id.name[2]);
1718 cleanup_phase = 1; /* parent dentry */
1720 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1721 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1722 if (IS_ERR(*dchildp)) {
1723 rc = PTR_ERR(*dchildp);
1724 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1728 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1730 * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1731 * lock. Drop possible UPDATE lock!
1733 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1734 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1736 child_res_id.name[0] = (*dchildp)->d_fid;
1737 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1738 child_ino = (*dchildp)->d_inum;
1739 child_gen = (*dchildp)->d_generation;
1743 inode = (*dchildp)->d_inode;
1745 inode = igrab(inode);
1749 down(&inode->i_sem);
1750 rc = mds_read_inode_sid(obd, inode, &sid);
1753 CERROR("Can't read inode self id, inode %lu, "
1754 "rc %d\n", inode->i_ino, rc);
1759 child_ino = inode->i_ino;
1760 child_gen = inode->i_generation;
1761 child_res_id.name[0] = id_fid(&sid);
1762 child_res_id.name[1] = id_group(&sid);
1766 cleanup_phase = 2; /* child dentry */
1768 /* Step 3: Lock parent and child in resource order. If child doesn't
1769 * exist, we still have to lock the parent and re-lookup. */
1770 rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1771 &parent_policy, &child_res_id, child_lockh,
1772 child_mode, &child_policy);
1776 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1777 cleanup_phase = 4; /* child lock */
1779 cleanup_phase = 3; /* parent lock */
1781 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1782 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1783 parent_mode, &child_res_id, child_lockh,
1784 dchildp, child_mode, &child_policy,
1785 name, namelen, &parent_res_id, child_ino,
1797 switch (cleanup_phase) {
1799 ldlm_lock_decref(child_lockh, child_mode);
1801 ldlm_lock_decref(parent_lockh, parent_mode);
1806 if (parent_lockh[1].cookie)
1807 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1815 void mds_reconstruct_generic(struct ptlrpc_request *req)
1817 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1818 mds_req_from_mcd(req, med->med_mcd);
1821 /* If we are unlinking an open file/dir (i.e. creating an orphan) then we
1822 * instead link the inode into the PENDING directory until it is finally
1823 * released. We can't simply call mds_reint_rename() or some part thereof,
1824 * because we don't have the inode to check for link count/open status until
1825 * after it is locked.
1827 * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1828 * before starting journal transaction.
1830 * returns 1 on success
1831 * returns 0 if we lost a race and didn't make a new link
1832 * returns negative on error
1834 static int mds_orphan_add_link(struct mds_update_record *rec,
1835 struct obd_device *obd, struct dentry *dentry)
1837 struct mds_obd *mds = &obd->u.mds;
1838 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1839 struct inode *inode = dentry->d_inode;
1840 struct dentry *pending_child;
1841 char idname[LL_ID_NAMELEN];
1842 int idlen = 0, rc, mode;
1845 LASSERT(inode != NULL);
1846 LASSERT(!mds_inode_is_orphan(inode));
1847 #ifndef HAVE_I_ALLOC_SEM
1848 LASSERT(down_trylock(&inode->i_sem) != 0);
1850 LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1852 idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1854 CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1855 mds_orphan_open_count(inode), inode->i_nlink,
1856 S_ISDIR(inode->i_mode) ? "dir" :
1857 S_ISREG(inode->i_mode) ? "file" : "other",
1858 rec->ur_name, idname);
1860 if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1863 pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1864 if (IS_ERR(pending_child))
1865 RETURN(PTR_ERR(pending_child));
1867 if (pending_child->d_inode != NULL) {
1868 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1869 LASSERT(pending_child->d_inode == inode);
1870 GOTO(out_dput, rc = 0);
1874 * link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG for
1875 * linking and return real mode back then -bzzz
1877 mode = inode->i_mode;
1878 inode->i_mode = S_IFREG;
1879 rc = vfs_link(dentry, pending_dir, pending_child);
1881 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1884 mds_inode_set_orphan(inode);
1886 /* return mode and correct i_nlink if inode is directory */
1887 inode->i_mode = mode;
1888 LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1889 S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1891 if (S_ISDIR(mode)) {
1893 i_nlink_inc(pending_dir);
1894 mark_inode_dirty(inode);
1895 mark_inode_dirty(pending_dir);
1898 GOTO(out_dput, rc = 1);
1900 l_dput(pending_child);
1904 int mds_create_local_dentry(struct mds_update_record *rec,
1905 struct obd_device *obd)
1907 struct mds_obd *mds = &obd->u.mds;
1908 struct inode *id_dir = mds->mds_id_dir->d_inode;
1909 int idlen = 0, rc, cleanup_phase = 0;
1910 struct dentry *new_child = NULL;
1911 char *idname = rec->ur_name;
1912 struct dentry *child = NULL;
1913 struct lustre_handle lockh[2] = {{0}, {0}};
1914 struct lustre_id sid;
1918 down(&id_dir->i_sem);
1919 idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1920 id_gen(rec->ur_id1));
1922 CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1923 idname, OLID4(rec->ur_id1));
1925 new_child = ll_lookup_one_len(idname, mds->mds_id_dir,
1928 if (IS_ERR(new_child)) {
1929 CERROR("can't lookup %s: %d\n", idname,
1930 (int) PTR_ERR(new_child));
1931 GOTO(cleanup, rc = PTR_ERR(new_child));
1935 down(&id_dir->i_sem);
1936 rc = mds_read_inode_sid(obd, id_dir, &sid);
1939 CERROR("Can't read inode self id, inode %lu, "
1940 "rc %d\n", id_dir->i_ino, rc);
1944 if (new_child->d_inode != NULL) {
1945 /* nice. we've already have local dentry! */
1946 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1947 (unsigned)new_child->d_inode->i_ino,
1948 (unsigned)new_child->d_inode->i_generation);
1950 id_ino(rec->ur_id1) = id_dir->i_ino;
1951 id_gen(rec->ur_id1) = id_dir->i_generation;
1952 rec->ur_namelen = idlen + 1;
1954 id_fid(rec->ur_id1) = id_fid(&sid);
1955 id_group(rec->ur_id1) = id_group(&sid);
1957 GOTO(cleanup, rc = 0);
1960 /* new, local dentry will be added soon. we need no aliases here */
1963 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1964 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1966 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1967 LCK_EX, lockh, NULL, NULL, 0,
1968 MDS_INODELOCK_UPDATE);
1971 if (IS_ERR(child)) {
1972 rc = PTR_ERR(child);
1973 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1974 CERROR("can't get victim: %d\n", rc);
1979 handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
1981 GOTO(cleanup, rc = PTR_ERR(handle));
1983 rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
1984 idlen, id_ino(rec->ur_id1),
1985 id_gen(rec->ur_id1), mds->mds_num,
1986 id_fid(rec->ur_id1));
1988 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1989 (unsigned long)child->d_inode->i_ino,
1990 (unsigned long)child->d_inode->i_generation, rc);
1992 if (S_ISDIR(child->d_inode->i_mode)) {
1993 i_nlink_inc(id_dir);
1994 mark_inode_dirty(id_dir);
1996 mark_inode_dirty(child->d_inode);
1998 fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
2000 id_ino(rec->ur_id1) = id_dir->i_ino;
2001 id_gen(rec->ur_id1) = id_dir->i_generation;
2002 rec->ur_namelen = idlen + 1;
2004 id_fid(rec->ur_id1) = id_fid(&sid);
2005 id_group(rec->ur_id1) = id_group(&sid);
2009 switch(cleanup_phase) {
2011 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
2012 ldlm_lock_decref(lockh, LCK_EX);
2022 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
2023 struct ptlrpc_request *slave)
2025 void *cookie, *cookie2;
2026 struct mds_body *body2;
2027 struct mds_body *body;
2031 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
2032 LASSERT(body != NULL);
2034 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
2035 LASSERT(body2 != NULL);
2037 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
2040 memcpy(body2, body, sizeof(*body));
2041 body2->valid &= ~OBD_MD_FLCOOKIE;
2043 if (!(body->valid & OBD_MD_FLEASIZE) &&
2044 !(body->valid & OBD_MD_FLDIREA))
2047 if (body->eadatasize == 0) {
2048 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
2052 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
2054 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
2055 LASSERT(ea != NULL);
2057 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
2058 LASSERT(ea2 != NULL);
2060 memcpy(ea2, ea, body->eadatasize);
2062 if (body->valid & OBD_MD_FLCOOKIE) {
2063 LASSERT(master->rq_repmsg->buflens[2] >=
2064 slave->rq_repmsg->buflens[2]);
2065 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
2066 slave->rq_repmsg->buflens[2]);
2067 LASSERT(cookie != NULL);
2069 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
2070 master->rq_repmsg->buflens[2]);
2071 LASSERT(cookie2 != NULL);
2072 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
2073 body2->valid |= OBD_MD_FLCOOKIE;
2078 static int mds_reint_unlink_remote(struct mds_update_record *rec,
2079 int offset, struct ptlrpc_request *req,
2080 struct lustre_handle *parent_lockh,
2081 int update_mode, struct dentry *dparent,
2082 struct lustre_handle *child_lockh,
2083 struct dentry *dchild)
2085 struct obd_device *obd = req->rq_export->exp_obd;
2086 struct mds_obd *mds = mds_req2mds(req);
2087 struct ptlrpc_request *request = NULL;
2088 int rc = 0, cleanup_phase = 0;
2089 struct mdc_op_data *op_data;
2093 LASSERT(offset == 1 || offset == 3);
2095 /* time to drop i_nlink on remote MDS */
2096 OBD_ALLOC(op_data, sizeof(*op_data));
2097 if (op_data == NULL)
2100 memset(op_data, 0, sizeof(*op_data));
2101 mds_pack_dentry2id(obd, &op_data->id1, dchild, 1);
2102 op_data->create_mode = rec->ur_mode;
2104 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode "DLID4")",
2105 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
2107 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2108 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode "DLID4")",
2109 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
2112 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
2113 op_data->create_mode |= MDS_MODE_REPLAY;
2115 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2116 OBD_FREE(op_data, sizeof(*op_data));
2121 mds_copy_unlink_reply(req, request);
2122 ptlrpc_req_finished(request);
2126 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2129 GOTO(cleanup, rc = PTR_ERR(handle));
2130 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
2131 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
2136 req->rq_status = rc;
2139 if (parent_lockh[1].cookie != 0)
2140 ldlm_lock_decref(parent_lockh + 1, update_mode);
2142 ldlm_lock_decref(child_lockh, LCK_EX);
2144 ldlm_lock_decref(parent_lockh, LCK_PW);
2146 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2153 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
2154 struct ptlrpc_request *req,
2155 struct lustre_handle *lh)
2157 struct dentry *dparent = NULL, *dchild;
2158 struct mds_obd *mds = mds_req2mds(req);
2159 struct obd_device *obd = req->rq_export->exp_obd;
2160 struct mds_body *body = NULL;
2161 struct inode *child_inode = NULL;
2162 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2163 struct lustre_handle child_lockh = {0};
2164 struct lustre_handle child_reuse_lockh = {0};
2165 struct lustre_handle *slave_lockh = NULL;
2166 char idname[LL_ID_NAMELEN];
2167 struct llog_create_locks *lcl = NULL;
2168 void *handle = NULL;
2169 int rc = 0, cleanup_phase = 0;
2170 int unlink_by_id = 0;
2174 LASSERT(offset == 1 || offset == 3);
2176 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
2177 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2180 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2182 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2183 DEBUG_REQ(D_HA, req, "unlink replay");
2184 LASSERT(offset == 1); /* should not come from intent */
2185 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
2186 lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
2187 req->rq_repmsg->buflens[2]);
2190 MD_COUNTER_INCREMENT(obd, unlink);
2192 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
2193 GOTO(cleanup, rc = -ENOENT);
2195 if (rec->ur_namelen == 1) {
2196 /* this is request to drop i_nlink on local inode */
2198 rec->ur_name = idname;
2199 rc = mds_create_local_dentry(rec, obd);
2200 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
2201 DEBUG_REQ(D_HA, req,
2202 "drop nlink on inode "DLID4" (replay)",
2203 OLID4(rec->ur_id1));
2209 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
2210 /* master mds for directory asks slave removing inode is already
2212 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
2213 LCK_PW, parent_lockh,
2214 &update_mode, rec->ur_name,
2216 MDS_INODELOCK_UPDATE);
2217 if (IS_ERR(dparent))
2218 GOTO(cleanup, rc = PTR_ERR(dparent));
2219 dchild = ll_lookup_one_len(rec->ur_name, dparent,
2220 rec->ur_namelen - 1);
2222 GOTO(cleanup, rc = PTR_ERR(dchild));
2223 child_lockh.cookie = 0;
2224 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
2225 LASSERT(dchild->d_inode != NULL);
2226 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
2228 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
2229 parent_lockh, &dparent,
2230 LCK_PW, MDS_INODELOCK_UPDATE,
2231 &update_mode, rec->ur_name,
2232 rec->ur_namelen, &child_lockh,
2234 MDS_INODELOCK_LOOKUP |
2235 MDS_INODELOCK_UPDATE);
2240 if (dchild->d_flags & DCACHE_CROSS_REF) {
2241 /* we should have parent lock only here */
2242 LASSERT(unlink_by_id == 0);
2243 LASSERT(dchild->d_mdsnum != mds->mds_num);
2244 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
2245 update_mode, dparent, &child_lockh, dchild);
2249 cleanup_phase = 1; /* dchild, dparent, locks */
2252 child_inode = dchild->d_inode;
2253 if (child_inode == NULL) {
2254 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
2255 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
2256 GOTO(cleanup, rc = -ENOENT);
2259 cleanup_phase = 2; /* dchild has a lock */
2261 /* We have to do these checks ourselves, in case we are making an
2262 * orphan. The client tells us whether rmdir() or unlink() was called,
2263 * so we need to return appropriate errors (bug 72).
2265 * We don't have to check permissions, because vfs_rename (called from
2266 * mds_open_unlink_rename) also calls may_delete. */
2267 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
2268 if (!S_ISDIR(child_inode->i_mode))
2269 GOTO(cleanup, rc = -ENOTDIR);
2271 if (S_ISDIR(child_inode->i_mode))
2272 GOTO(cleanup, rc = -EISDIR);
2275 /* handle splitted dir */
2276 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
2280 /* Step 4: Get a lock on the ino to sync with creation WRT inode
2281 * reuse (see bug 2029). */
2282 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
2285 cleanup_phase = 3; /* child inum lock */
2287 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2289 /* ldlm_reply in buf[0] if called via intent */
2295 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2296 LASSERT(body != NULL);
2298 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2299 DOWN_READ_I_ALLOC_SEM(child_inode);
2300 cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2302 /* If this is potentially the last reference to this inode, get the
2303 * OBD EA data first so the client can destroy OST objects. We
2304 * only do the object removal later if no open files/links remain. */
2305 if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2306 child_inode->i_nlink == 1) {
2307 if (mds_orphan_open_count(child_inode) > 0) {
2308 /* need to lock pending_dir before transaction */
2309 down(&mds->mds_pending_dir->d_inode->i_sem);
2310 cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2311 } else if (S_ISREG(child_inode->i_mode)) {
2312 mds_pack_inode2body(obd, body, child_inode, 0);
2313 mds_pack_md(obd, req->rq_repmsg, offset + 1,
2314 body, child_inode, MDS_PACK_MD_LOCK, 0);
2318 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2319 switch (child_inode->i_mode & S_IFMT) {
2321 /* Drop any lingering child directories before we start our
2322 * transaction, to avoid doing multiple inode dirty/delete
2323 * in our compound transaction (bug 1321). */
2324 shrink_dcache_parent(dchild);
2325 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2328 GOTO(cleanup, rc = PTR_ERR(handle));
2329 rc = vfs_rmdir(dparent->d_inode, dchild);
2332 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2333 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2335 handle = fsfilt_start_log(obd, dparent->d_inode,
2336 FSFILT_OP_UNLINK, NULL,
2337 le32_to_cpu(lmm->lmm_stripe_count));
2339 GOTO(cleanup, rc = PTR_ERR(handle));
2340 rc = vfs_unlink(dparent->d_inode, dchild);
2348 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2351 GOTO(cleanup, rc = PTR_ERR(handle));
2352 rc = vfs_unlink(dparent->d_inode, dchild);
2355 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2358 GOTO(cleanup, rc = -EINVAL);
2361 if (rc == 0 && child_inode->i_nlink == 0) {
2362 if (mds_orphan_open_count(child_inode) > 0)
2363 rc = mds_orphan_add_link(rec, obd, dchild);
2366 GOTO(cleanup, rc = 0);
2368 if (!S_ISREG(child_inode->i_mode))
2371 if (!(body->valid & OBD_MD_FLEASIZE)) {
2372 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2373 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2374 } else if (mds_log_op_unlink(obd, child_inode,
2375 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2376 req->rq_repmsg->buflens[offset + 1],
2377 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2378 req->rq_repmsg->buflens[offset + 2],
2380 body->valid |= OBD_MD_FLCOOKIE;
2383 rc = mds_destroy_object(obd, child_inode, 1);
2385 CERROR("can't remove OST object, err %d\n",
2397 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2398 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2399 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2401 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2403 CERROR("error on parent setattr: rc = %d\n", err);
2405 rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2406 handle, req, rc, 0);
2408 (void)obd_set_info(mds->mds_dt_exp, strlen("unlinked"),
2409 "unlinked", 0, NULL);
2410 switch(cleanup_phase) {
2411 case 5: /* pending_dir semaphore */
2412 up(&mds->mds_pending_dir->d_inode->i_sem);
2413 case 4: /* child inode semaphore */
2414 UP_READ_I_ALLOC_SEM(child_inode);
2415 /* handle splitted dir */
2417 /* master directory can be non-empty or something else ... */
2418 mds_unlink_slave_objs(obd, dchild);
2421 ptlrpc_save_llog_lock(req, lcl);
2422 case 3: /* child ino-reuse lock */
2423 if (rc && body != NULL) {
2424 // Don't unlink the OST objects if the MDS unlink failed
2428 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2430 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2431 case 2: /* child lock */
2432 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2433 if (child_lockh.cookie)
2434 ldlm_lock_decref(&child_lockh, LCK_EX);
2435 case 1: /* child and parent dentry, parent lock */
2437 if (parent_lockh[1].cookie != 0)
2438 ldlm_lock_decref(parent_lockh + 1, update_mode);
2441 ldlm_lock_decref(parent_lockh, LCK_PW);
2443 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2450 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2453 req->rq_status = rc;
2458 * to service requests from remote MDS to increment i_nlink
2460 static int mds_reint_link_acquire(struct mds_update_record *rec,
2461 int offset, struct ptlrpc_request *req,
2462 struct lustre_handle *lh)
2464 struct obd_device *obd = req->rq_export->exp_obd;
2465 struct ldlm_res_id src_res_id = { .name = {0} };
2466 struct lustre_handle *handle = NULL, src_lockh = {0};
2467 struct mds_obd *mds = mds_req2mds(req);
2468 int rc = 0, cleanup_phase = 0;
2469 struct dentry *de_src = NULL;
2470 ldlm_policy_data_t policy;
2471 int flags = LDLM_FL_ATOMIC_CB;
2474 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2475 obd->obd_name, OLID4(rec->ur_id1));
2477 /* Step 1: Lookup the source inode and target directory by ID */
2478 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2480 GOTO(cleanup, rc = PTR_ERR(de_src));
2481 cleanup_phase = 1; /* source dentry */
2483 src_res_id.name[0] = id_fid(rec->ur_id1);
2484 src_res_id.name[1] = id_group(rec->ur_id1);
2485 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2487 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2488 src_res_id, LDLM_IBITS, &policy,
2489 LCK_EX, &flags, mds_blocking_ast,
2490 ldlm_completion_ast, NULL, NULL,
2491 NULL, 0, NULL, &src_lockh);
2493 GOTO(cleanup, rc = -ENOLCK);
2494 cleanup_phase = 2; /* lock */
2496 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2498 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2499 if (IS_ERR(handle)) {
2500 rc = PTR_ERR(handle);
2503 i_nlink_inc(de_src->d_inode);
2504 mark_inode_dirty(de_src->d_inode);
2508 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2509 handle, req, rc, 0);
2510 switch (cleanup_phase) {
2513 ldlm_lock_decref(&src_lockh, LCK_EX);
2515 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2521 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2524 req->rq_status = rc;
2529 * request to link to foreign inode:
2530 * - acquire i_nlinks on this inode
2533 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2534 int offset, struct ptlrpc_request *req,
2535 struct lustre_handle *lh)
2537 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2538 struct obd_device *obd = req->rq_export->exp_obd;
2539 struct dentry *de_tgt_dir = NULL;
2540 struct mds_obd *mds = mds_req2mds(req);
2541 int rc = 0, cleanup_phase = 0;
2542 struct mdc_op_data *op_data;
2543 struct ptlrpc_request *request = NULL;
2547 DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2548 ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2549 OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2550 OLID4(rec->ur_id1));
2552 de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2553 tgt_dir_lockh, &update_mode,
2554 rec->ur_name, rec->ur_namelen - 1,
2555 MDS_INODELOCK_UPDATE);
2556 if (IS_ERR(de_tgt_dir))
2557 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2560 OBD_ALLOC(op_data, sizeof(*op_data));
2561 if (op_data == NULL)
2562 GOTO(cleanup, rc = -ENOMEM);
2564 memset(op_data, 0, sizeof(*op_data));
2565 op_data->id1 = *(rec->ur_id1);
2566 rc = md_link(mds->mds_md_exp, op_data, &request);
2567 OBD_FREE(op_data, sizeof(*op_data));
2570 ptlrpc_req_finished(request);
2576 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2578 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2579 if (IS_ERR(handle)) {
2580 rc = PTR_ERR(handle);
2586 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2587 rec->ur_namelen - 1, id_ino(rec->ur_id1),
2588 id_gen(rec->ur_id1), id_group(rec->ur_id1),
2589 id_fid(rec->ur_id1));
2592 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2593 handle, req, rc, 0);
2595 switch (cleanup_phase) {
2598 OBD_ALLOC(op_data, sizeof(*op_data));
2599 if (op_data != NULL) {
2601 memset(op_data, 0, sizeof(*op_data));
2603 op_data->id1 = *(rec->ur_id1);
2604 op_data->create_mode = rec->ur_mode;
2606 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2607 OBD_FREE(op_data, sizeof(*op_data));
2609 ptlrpc_req_finished(request);
2611 CERROR("error %d while dropping i_nlink on "
2612 "remote inode\n", rc);
2615 CERROR("rc %d prevented dropping i_nlink on "
2616 "remote inode\n", -ENOMEM);
2622 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2624 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2627 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2629 ptlrpc_save_lock(req, tgt_dir_lockh + 1, update_mode);
2635 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2638 req->rq_status = rc;
2642 static int mds_reint_link(struct mds_update_record *rec, int offset,
2643 struct ptlrpc_request *req,
2644 struct lustre_handle *lh)
2646 struct obd_device *obd = req->rq_export->exp_obd;
2647 struct dentry *de_src = NULL;
2648 struct dentry *de_tgt_dir = NULL;
2649 struct dentry *dchild = NULL;
2650 struct mds_obd *mds = mds_req2mds(req);
2651 struct lustre_handle *handle = NULL;
2652 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2653 struct ldlm_res_id src_res_id = { .name = {0} };
2654 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2655 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2656 ldlm_policy_data_t tgt_dir_policy =
2657 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2658 int rc = 0, cleanup_phase = 0;
2660 int update_mode = 0;
2664 LASSERT(offset == 1);
2666 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2667 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2668 id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2671 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2672 MD_COUNTER_INCREMENT(obd, link);
2674 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2675 GOTO(cleanup, rc = -ENOENT);
2677 if (id_group(rec->ur_id1) != mds->mds_num) {
2678 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2682 if (rec->ur_namelen == 1) {
2683 rc = mds_reint_link_acquire(rec, offset, req, lh);
2687 /* Step 1: Lookup the source inode and target directory by ID */
2688 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2690 GOTO(cleanup, rc = PTR_ERR(de_src));
2692 cleanup_phase = 1; /* source dentry */
2694 de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2695 if (IS_ERR(de_tgt_dir)) {
2696 rc = PTR_ERR(de_tgt_dir);
2701 cleanup_phase = 2; /* target directory dentry */
2703 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2704 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2705 rec->ur_name, de_src->d_inode->i_ino);
2707 /* Step 2: Take the two locks */
2708 src_res_id.name[0] = id_fid(rec->ur_id1);
2709 src_res_id.name[1] = id_group(rec->ur_id1);
2710 tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2711 tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2714 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2715 int flags = LDLM_FL_ATOMIC_CB;
2716 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2718 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2719 tgt_dir_res_id, LDLM_IBITS,
2720 &src_policy, update_mode, &flags,
2722 ldlm_completion_ast, NULL, NULL,
2723 NULL, 0, NULL, tgt_dir_lockh + 1);
2725 GOTO(cleanup, rc = -ENOLCK);
2728 tgt_dir_res_id.name[2] = full_name_hash((unsigned char *)rec->ur_name,
2729 rec->ur_namelen - 1);
2730 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2731 (unsigned long)id_fid(rec->ur_id2),
2732 (unsigned long)id_group(rec->ur_id2),
2733 tgt_dir_res_id.name[2]);
2736 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2737 &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2738 LCK_EX, &tgt_dir_policy);
2742 cleanup_phase = 3; /* locks */
2744 /* Step 3: Lookup the child */
2745 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir,
2746 rec->ur_namelen - 1);
2747 if (IS_ERR(dchild)) {
2748 rc = PTR_ERR(dchild);
2749 if (rc != -EPERM && rc != -EACCES)
2750 CERROR("child lookup error %d\n", rc);
2754 cleanup_phase = 4; /* child dentry */
2756 if (dchild->d_inode) {
2757 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2758 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2763 /* Step 4: Do it. */
2764 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2766 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2767 if (IS_ERR(handle)) {
2768 rc = PTR_ERR(handle);
2772 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2773 if (rc && rc != -EPERM && rc != -EACCES)
2774 CERROR("vfs_link error %d\n", rc);
2776 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2777 handle, req, rc, 0);
2780 switch (cleanup_phase) {
2781 case 4: /* child dentry */
2785 ldlm_lock_decref(&src_lockh, LCK_EX);
2786 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2788 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2789 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2791 case 2: /* target dentry */
2793 if (tgt_dir_lockh[1].cookie && update_mode)
2794 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2798 case 1: /* source dentry */
2803 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2806 req->rq_status = rc;
2810 /* The idea here is that we need to get four locks in the end:
2811 * one on each parent directory, one on each child. We need to take
2812 * these locks in some kind of order (to avoid deadlocks), and the order
2813 * I selected is "increasing resource number" order. We need to look up
2814 * the children, however, before we know what the resource number(s) are.
2815 * Thus the following plan:
2817 * 1,2. Look up the parents
2818 * 3,4. Look up the children
2819 * 5. Take locks on the parents and children, in order
2820 * 6. Verify that the children haven't changed since they were looked up
2822 * If there was a race and the children changed since they were first looked
2823 * up, it is possible that mds_verify_child() will be able to just grab the
2824 * lock on the new child resource (if it has a higher resource than any other)
2825 * but we need to compare against not only its parent, but also against the
2826 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2828 * We need the fancy igrab() on the child inodes because we aren't holding a
2829 * lock on the parent after the lookup is done, so dentry->d_inode may change
2830 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2832 static int mds_get_parents_children_locked(struct obd_device *obd,
2833 struct mds_obd *mds,
2834 struct lustre_id *p1_id,
2835 struct dentry **de_srcdirp,
2836 struct lustre_id *p2_id,
2837 struct dentry **de_tgtdirp,
2839 const char *old_name, int old_len,
2840 struct dentry **de_oldp,
2841 const char *new_name, int new_len,
2842 struct dentry **de_newp,
2843 struct lustre_handle *dlm_handles,
2846 struct ldlm_res_id p1_res_id = { .name = {0} };
2847 struct ldlm_res_id p2_res_id = { .name = {0} };
2848 struct ldlm_res_id c1_res_id = { .name = {0} };
2849 struct ldlm_res_id c2_res_id = { .name = {0} };
2850 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2851 /* Only dentry should disappear, but the inode itself would be
2852 intact otherwise. */
2853 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2854 /* If something is going to be replaced, both dentry and inode locks are
2856 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_FULL}};
2857 struct ldlm_res_id *maxres_src, *maxres_tgt;
2858 struct inode *inode;
2859 int rc = 0, cleanup_phase = 0;
2860 __u32 child_gen1 = 0;
2861 __u32 child_gen2 = 0;
2862 unsigned long child_ino1 = 0;
2863 unsigned long child_ino2 = 0;
2866 /* Step 1: Lookup the source directory */
2867 *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2868 if (IS_ERR(*de_srcdirp))
2869 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2871 cleanup_phase = 1; /* source directory dentry */
2873 p1_res_id.name[0] = id_fid(p1_id);
2874 p1_res_id.name[1] = id_group(p1_id);
2876 /* Step 2: Lookup the target directory */
2877 if (id_equal_stc(p1_id, p2_id)) {
2878 *de_tgtdirp = dget(*de_srcdirp);
2880 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2881 if (IS_ERR(*de_tgtdirp)) {
2882 rc = PTR_ERR(*de_tgtdirp);
2888 cleanup_phase = 2; /* target directory dentry */
2890 p2_res_id.name[0] = id_fid(p2_id);
2891 p2_res_id.name[1] = id_group(p2_id);
2894 dlm_handles[5].cookie = 0;
2895 dlm_handles[6].cookie = 0;
2897 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2899 * get a temp lock on just fid, group to flush client cache and
2900 * to protect dirs from concurrent splitting.
2902 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2903 LCK_PW, &p_policy, &p2_res_id,
2904 &dlm_handles[6], LCK_PW, &p_policy);
2908 p1_res_id.name[2] = full_name_hash((unsigned char *)old_name,
2910 p2_res_id.name[2] = full_name_hash((unsigned char *)new_name,
2913 CDEBUG(D_INFO, "take locks on "
2914 LPX64":"LPX64":"LPX64", "LPX64":"LPX64":"LPX64"\n",
2915 p1_res_id.name[0], p1_res_id.name[1], p1_res_id.name[2],
2916 p2_res_id.name[0], p2_res_id.name[1], p2_res_id.name[2]);
2921 /* Step 3: Lookup the source child entry */
2922 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp,
2924 if (IS_ERR(*de_oldp)) {
2925 rc = PTR_ERR(*de_oldp);
2926 CERROR("old child lookup error (%.*s): %d\n",
2927 old_len - 1, old_name, rc);
2931 cleanup_phase = 4; /* original name dentry */
2933 inode = (*de_oldp)->d_inode;
2934 if (inode != NULL) {
2935 struct lustre_id sid;
2937 inode = igrab(inode);
2939 GOTO(cleanup, rc = -ENOENT);
2941 down(&inode->i_sem);
2942 rc = mds_read_inode_sid(obd, inode, &sid);
2945 CERROR("Can't read inode self id, inode %lu, "
2946 "rc %d\n", inode->i_ino, rc);
2951 child_ino1 = inode->i_ino;
2952 child_gen1 = inode->i_generation;
2953 c1_res_id.name[0] = id_fid(&sid);
2954 c1_res_id.name[1] = id_group(&sid);
2956 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2957 child_ino1 = (*de_oldp)->d_inum;
2958 child_gen1 = (*de_oldp)->d_generation;
2959 c1_res_id.name[0] = (*de_oldp)->d_fid;
2960 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2962 GOTO(cleanup, rc = -ENOENT);
2965 /* Step 4: Lookup the target child entry */
2966 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp,
2968 if (IS_ERR(*de_newp)) {
2969 rc = PTR_ERR(*de_newp);
2970 CERROR("new child lookup error (%.*s): %d\n",
2971 old_len - 1, old_name, rc);
2975 cleanup_phase = 5; /* target dentry */
2977 inode = (*de_newp)->d_inode;
2978 if (inode != NULL) {
2979 struct lustre_id sid;
2981 inode = igrab(inode);
2985 down(&inode->i_sem);
2986 rc = mds_read_inode_sid(obd, inode, &sid);
2989 CERROR("Can't read inode self id, inode %lu, "
2990 "rc %d\n", inode->i_ino, rc);
2994 child_ino2 = inode->i_ino;
2995 child_gen2 = inode->i_generation;
2996 c2_res_id.name[0] = id_fid(&sid);
2997 c2_res_id.name[1] = id_group(&sid);
2999 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
3000 child_ino2 = (*de_newp)->d_inum;
3001 child_gen2 = (*de_newp)->d_generation;
3002 c2_res_id.name[0] = (*de_newp)->d_fid;
3003 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
3007 /* Step 5: Take locks on the parents and child(ren) */
3008 maxres_src = &p1_res_id;
3009 maxres_tgt = &p2_res_id;
3010 cleanup_phase = 5; /* target dentry */
3012 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
3013 maxres_src = &c1_res_id;
3014 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
3015 maxres_tgt = &c2_res_id;
3017 rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
3019 &p2_res_id, &dlm_handles[1], parent_mode,
3021 &c1_res_id, &dlm_handles[2], child_mode,
3023 &c2_res_id, &dlm_handles[3], child_mode,
3028 cleanup_phase = 6; /* parent and child(ren) locks */
3030 /* Step 6a: Re-lookup source child to verify it hasn't changed */
3031 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
3032 parent_mode, &c1_res_id, &dlm_handles[2],
3033 de_oldp, child_mode, &c1_policy, old_name, old_len,
3034 maxres_tgt, child_ino1, child_gen1);
3036 if (c2_res_id.name[0] != 0)
3037 ldlm_lock_decref(&dlm_handles[3], child_mode);
3038 ldlm_lock_decref(&dlm_handles[1], parent_mode);
3045 if (!DENTRY_VALID(*de_oldp))
3046 GOTO(cleanup, rc = -ENOENT);
3048 /* Step 6b: Re-lookup target child to verify it hasn't changed */
3049 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
3050 parent_mode, &c2_res_id, &dlm_handles[3],
3051 de_newp, child_mode, &c2_policy, new_name,
3052 new_len, maxres_src, child_ino2, child_gen2);
3054 ldlm_lock_decref(&dlm_handles[2], child_mode);
3055 ldlm_lock_decref(&dlm_handles[0], parent_mode);
3065 switch (cleanup_phase) {
3066 case 6: /* child lock(s) */
3067 if (c2_res_id.name[0] != 0)
3068 ldlm_lock_decref(&dlm_handles[3], child_mode);
3069 if (c1_res_id.name[0] != 0)
3070 ldlm_lock_decref(&dlm_handles[2], child_mode);
3071 if (dlm_handles[1].cookie != 0)
3072 ldlm_lock_decref(&dlm_handles[1], parent_mode);
3073 if (dlm_handles[0].cookie != 0)
3074 ldlm_lock_decref(&dlm_handles[0], parent_mode);
3075 case 5: /* target dentry */
3077 case 4: /* source dentry */
3081 if (dlm_handles[5].cookie != 0)
3082 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3083 if (dlm_handles[6].cookie != 0)
3084 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3086 case 2: /* target directory dentry */
3087 l_dput(*de_tgtdirp);
3088 case 1: /* source directry dentry */
3089 l_dput(*de_srcdirp);
3097 * checks if dentry can be removed. This function also handles cross-ref
3100 static int mds_check_for_rename(struct obd_device *obd,
3101 struct dentry *dentry)
3103 struct mds_obd *mds = &obd->u.mds;
3104 struct lustre_handle *rlockh;
3105 struct ptlrpc_request *req;
3106 struct mdc_op_data *op_data;
3107 struct lookup_intent it;
3108 int handle_size, rc = 0;
3111 LASSERT(dentry != NULL);
3113 if (dentry->d_inode) {
3114 if (S_ISDIR(dentry->d_inode->i_mode) &&
3115 !mds_is_dir_empty(obd, dentry))
3118 LASSERT((dentry->d_flags & DCACHE_CROSS_REF));
3119 handle_size = sizeof(struct lustre_handle);
3121 OBD_ALLOC(rlockh, handle_size);
3125 memset(rlockh, 0, handle_size);
3126 OBD_ALLOC(op_data, sizeof(*op_data));
3127 if (op_data == NULL) {
3128 OBD_FREE(rlockh, handle_size);
3131 memset(op_data, 0, sizeof(*op_data));
3132 mds_pack_dentry2id(obd, &op_data->id1, dentry, 1);
3134 it.it_op = IT_UNLINK;
3135 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
3138 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
3139 op_data, rlockh, NULL, 0, ldlm_completion_ast,
3140 mds_blocking_ast, NULL);
3141 OBD_FREE(op_data, sizeof(*op_data));
3145 OBD_FREE(it.d.fs_data,
3146 sizeof(struct lustre_intent_data));
3149 if (rlockh->cookie != 0)
3150 ldlm_lock_decref(rlockh, LCK_EX);
3152 if (LUSTRE_IT(&it)->it_data) {
3153 req = (struct ptlrpc_request *)LUSTRE_IT(&it)->it_data;
3154 ptlrpc_req_finished(req);
3157 if (LUSTRE_IT(&it)->it_status)
3158 rc = LUSTRE_IT(&it)->it_status;
3159 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
3160 OBD_FREE(rlockh, handle_size);
3165 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
3166 struct ptlrpc_request *req, struct lustre_id *id,
3167 struct dentry *de_dir, struct dentry *de)
3169 struct obd_device *obd = req->rq_export->exp_obd;
3170 struct mds_obd *mds = mds_req2mds(req);
3171 void *handle = NULL;
3177 * name exists and points to local inode try to unlink this name
3178 * and create new one.
3180 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
3181 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
3182 (unsigned long)de->d_inode->i_generation);
3184 /* checking if we can remove local dentry. */
3185 rc = mds_check_for_rename(obd, de);
3189 handle = fsfilt_start(obd, de_dir->d_inode,
3190 FSFILT_OP_RENAME, NULL);
3192 GOTO(cleanup, rc = PTR_ERR(handle));
3193 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3196 } else if (de->d_flags & DCACHE_CROSS_REF) {
3197 CDEBUG(D_OTHER, "%s: %s points to remote inode %lu/%lu\n",
3198 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_mdsnum,
3199 (unsigned long)de->d_fid);
3201 /* checking if we can remove local dentry. */
3202 rc = mds_check_for_rename(obd, de);
3207 * to be fully POSIX compatible, we should add one more check:
3209 * if de_new is subdir of dir rec->ur_id1. If so - return
3212 * I do not know how to implement it right now, because
3213 * inodes/dentries for new and old names lie on different MDS,
3214 * so add this notice here just to make it visible for the rest
3215 * of developers and do not forget about. And when this check
3216 * will be added, del_cross_ref should gone, that is local
3217 * dentry is able to be removed if all checks passed. --umka
3220 handle = fsfilt_start(obd, de_dir->d_inode,
3221 FSFILT_OP_RENAME, NULL);
3223 GOTO(cleanup, rc = PTR_ERR(handle));
3224 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3228 /* name doesn't exist. the simplest case. */
3229 handle = fsfilt_start(obd, de_dir->d_inode,
3230 FSFILT_OP_LINK, NULL);
3232 GOTO(cleanup, rc = PTR_ERR(handle));
3235 rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
3236 rec->ur_tgtlen - 1, id_ino(id),
3237 id_gen(id), id_group(id), id_fid(id));
3239 CERROR("add_dir_entry() returned error %d\n", rc);
3245 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3246 handle, req, rc, 0);
3251 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
3252 struct ptlrpc_request *req, struct dentry *de_dir,
3255 struct obd_device *obd = req->rq_export->exp_obd;
3256 struct mds_obd *mds = mds_req2mds(req);
3257 void *handle = NULL;
3261 handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
3263 GOTO(cleanup, rc = PTR_ERR(handle));
3264 rc = fsfilt_del_dir_entry(obd, de);
3269 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3270 handle, req, rc, 0);
3274 static int mds_reint_rename_create_name(struct mds_update_record *rec,
3275 int offset, struct ptlrpc_request *req)
3277 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3278 struct obd_device *obd = req->rq_export->exp_obd;
3279 struct mds_obd *mds = mds_req2mds(req);
3280 struct lustre_handle child_lockh = {0};
3281 struct dentry *de_tgtdir = NULL;
3282 struct dentry *de_new = NULL;
3283 int cleanup_phase = 0;
3284 int update_mode, rc = 0;
3288 * another MDS executing rename operation has asked us to create target
3289 * name. such a creation should destroy existing target name.
3291 CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
3292 obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
3294 /* first, lookup the target */
3295 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
3296 &de_tgtdir, LCK_PW, MDS_INODELOCK_UPDATE,
3297 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
3298 &child_lockh, &de_new, LCK_EX,
3299 MDS_INODELOCK_LOOKUP);
3306 LASSERT(de_tgtdir->d_inode);
3309 rc = mds_add_local_dentry(rec, offset, req, rec->ur_id1,
3315 if (cleanup_phase == 1) {
3317 if (parent_lockh[1].cookie != 0)
3318 ldlm_lock_decref(parent_lockh + 1, update_mode);
3320 ldlm_lock_decref(parent_lockh, LCK_PW);
3321 if (child_lockh.cookie != 0)
3322 ldlm_lock_decref(&child_lockh, LCK_EX);
3327 req->rq_status = rc;
3331 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
3332 struct ptlrpc_request *req)
3334 struct obd_device *obd = req->rq_export->exp_obd;
3335 struct ptlrpc_request *req2 = NULL;
3336 struct dentry *de_srcdir = NULL;
3337 struct dentry *de_old = NULL;
3338 struct mds_obd *mds = mds_req2mds(req);
3339 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3340 struct lustre_handle child_lockh = {0};
3341 struct mdc_op_data *op_data;
3342 int update_mode, rc = 0;
3345 CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
3346 obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
3348 OBD_ALLOC(op_data, sizeof(*op_data));
3349 if (op_data == NULL)
3351 memset(op_data, 0, sizeof(*op_data));
3353 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
3354 &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
3355 &update_mode, rec->ur_name,
3356 rec->ur_namelen, &child_lockh, &de_old,
3357 LCK_EX, MDS_INODELOCK_LOOKUP);
3359 OBD_FREE(op_data, sizeof(*op_data));
3364 LASSERT(de_srcdir->d_inode);
3368 * we already know the target should be created on another MDS so, we
3369 * have to request that MDS to do it.
3372 /* prepare source id */
3373 if (de_old->d_flags & DCACHE_CROSS_REF) {
3374 LASSERT(de_old->d_inode == NULL);
3375 CDEBUG(D_OTHER, "request to move remote name\n");
3376 mds_pack_dentry2id(obd, &op_data->id1, de_old, 1);
3377 } else if (de_old->d_inode == NULL) {
3378 /* oh, source doesn't exist */
3379 OBD_FREE(op_data, sizeof(*op_data));
3380 GOTO(cleanup, rc = -ENOENT);
3382 struct lustre_id sid;
3383 struct inode *inode = de_old->d_inode;
3385 LASSERT(inode != NULL);
3386 CDEBUG(D_OTHER, "request to move local name\n");
3387 id_ino(&op_data->id1) = inode->i_ino;
3388 id_group(&op_data->id1) = mds->mds_num;
3389 id_gen(&op_data->id1) = inode->i_generation;
3391 down(&inode->i_sem);
3392 rc = mds_read_inode_sid(obd, inode, &sid);
3395 CERROR("Can't read inode self id, "
3396 "inode %lu, rc = %d\n",
3401 id_fid(&op_data->id1) = id_fid(&sid);
3404 op_data->id2 = *rec->ur_id2;
3405 rc = md_rename(mds->mds_md_exp, op_data, NULL, 0,
3406 rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3407 OBD_FREE(op_data, sizeof(*op_data));
3412 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3418 ptlrpc_req_finished(req2);
3421 if (parent_lockh[1].cookie != 0)
3422 ldlm_lock_decref(parent_lockh + 1, update_mode);
3424 ldlm_lock_decref(parent_lockh, LCK_PW);
3425 if (child_lockh.cookie != 0)
3426 ldlm_lock_decref(&child_lockh, LCK_EX);
3431 req->rq_status = rc;
3435 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3436 struct ptlrpc_request *req, struct lustre_handle *lockh)
3438 struct obd_device *obd = req->rq_export->exp_obd;
3439 struct dentry *de_srcdir = NULL;
3440 struct dentry *de_tgtdir = NULL;
3441 struct dentry *de_old = NULL;
3442 struct dentry *de_new = NULL;
3443 struct inode *old_inode = NULL, *new_inode = NULL;
3444 struct mds_obd *mds = mds_req2mds(req);
3445 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3446 struct mds_body *body = NULL;
3447 struct llog_create_locks *lcl = NULL;
3448 struct lov_mds_md *lmm = NULL;
3449 int rc = 0, cleanup_phase = 0;
3450 void *handle = NULL;
3453 LASSERT(offset == 1);
3455 DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3456 OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3459 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3461 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3462 DEBUG_REQ(D_HA, req, "rename replay");
3463 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3464 lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3465 req->rq_repmsg->buflens[2]);
3468 MD_COUNTER_INCREMENT(obd, rename);
3470 if (rec->ur_namelen == 1) {
3471 rc = mds_reint_rename_create_name(rec, offset, req);
3475 /* check if new name should be located on remote target. */
3476 if (id_group(rec->ur_id2) != mds->mds_num) {
3477 rc = mds_reint_rename_to_remote(rec, offset, req);
3481 rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3482 rec->ur_id2, &de_tgtdir, LCK_PW,
3483 rec->ur_name, rec->ur_namelen,
3484 &de_old, rec->ur_tgt,
3485 rec->ur_tgtlen, &de_new,
3486 dlm_handles, LCK_EX);
3490 cleanup_phase = 1; /* parent(s), children, locks */
3491 old_inode = de_old->d_inode;
3492 new_inode = de_new->d_inode;
3494 /* sanity check for src inode */
3495 if (de_old->d_flags & DCACHE_CROSS_REF) {
3496 LASSERT(de_old->d_inode == NULL);
3499 * in the case of cross-ref dir, we can perform this check only
3500 * if child and parent lie on the same mds. This is because
3501 * otherwise they can have the same inode numbers.
3503 if (de_old->d_mdsnum == mds->mds_num) {
3504 if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3505 de_old->d_inum == de_tgtdir->d_inode->i_ino)
3506 GOTO(cleanup, rc = -EINVAL);
3509 LASSERT(de_old->d_inode != NULL);
3510 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3511 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3512 GOTO(cleanup, rc = -EINVAL);
3515 /* sanity check for dest inode */
3516 if (de_new->d_flags & DCACHE_CROSS_REF) {
3517 LASSERT(new_inode == NULL);
3519 /* the same check about target dentry. */
3520 if (de_new->d_mdsnum == mds->mds_num) {
3521 if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3522 de_new->d_inum == de_tgtdir->d_inode->i_ino)
3523 GOTO(cleanup, rc = -EINVAL);
3527 * regular files usualy do not have ->rename() implemented. But
3528 * we handle only this case when @de_new is cross-ref entry,
3529 * because in other cases it will be handled by vfs_rename().
3531 if (de_old->d_inode && (!de_old->d_inode->i_op ||
3532 !de_old->d_inode->i_op->rename))
3533 GOTO(cleanup, rc = -EPERM);
3536 (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3537 new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3538 GOTO(cleanup, rc = -EINVAL);
3543 * check if inodes point to each other. This should be checked before
3544 * is_subdir() check, as for the same entries it will think that they
3547 if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3548 !(de_new->d_flags & DCACHE_CROSS_REF) &&
3549 old_inode == new_inode)
3550 GOTO(cleanup, rc = 0);
3552 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3554 * check if we are moving old entry into its child. 2.6 does not check
3555 * for this in vfs_rename() anymore.
3557 if (is_subdir(de_new, de_old))
3558 GOTO(cleanup, rc = -EINVAL);
3562 * if we are about to remove the target at first, pass the EA of that
3563 * inode to client to perform and cleanup on OST.
3565 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3566 LASSERT(body != NULL);
3568 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3570 DOWN_READ_I_ALLOC_SEM(new_inode);
3572 cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3574 if (new_inode && ((S_ISDIR(new_inode->i_mode) &&
3575 new_inode->i_nlink == 2) ||
3576 new_inode->i_nlink == 1)) {
3577 if (mds_orphan_open_count(new_inode) > 0) {
3578 /* need to lock pending_dir before transaction */
3579 down(&mds->mds_pending_dir->d_inode->i_sem);
3580 cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3581 } else if (S_ISREG(new_inode->i_mode)) {
3582 mds_pack_inode2body(obd, body, new_inode, 0);
3583 mds_pack_md(obd, req->rq_repmsg, 1, body,
3584 new_inode, MDS_PACK_MD_LOCK, 0);
3588 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3589 de_srcdir->d_inode->i_sb);
3591 if (de_old->d_flags & DCACHE_CROSS_REF) {
3592 struct lustre_id old_id;
3594 mds_pack_dentry2id(obd, &old_id, de_old, 1);
3596 rc = mds_add_local_dentry(rec, offset, req, &old_id,
3601 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3606 lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3607 handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3608 NULL, le32_to_cpu(lmm->lmm_stripe_count));
3611 GOTO(cleanup, rc = PTR_ERR(handle));
3614 de_old->d_fsdata = req;
3615 de_new->d_fsdata = req;
3616 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3619 if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3620 if (mds_orphan_open_count(new_inode) > 0)
3621 rc = mds_orphan_add_link(rec, obd, de_new);
3624 GOTO(cleanup, rc = 0);
3626 if (!S_ISREG(new_inode->i_mode))
3629 if (!(body->valid & OBD_MD_FLEASIZE)) {
3630 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3631 OBD_MD_FLATIME | OBD_MD_FLMTIME);
3632 } else if (mds_log_op_unlink(obd, new_inode,
3633 lustre_msg_buf(req->rq_repmsg,1,0),
3634 req->rq_repmsg->buflens[1],
3635 lustre_msg_buf(req->rq_repmsg,2,0),
3636 req->rq_repmsg->buflens[2],
3638 body->valid |= OBD_MD_FLCOOKIE;
3641 rc = mds_destroy_object(obd, old_inode, 1);
3643 CERROR("can't remove OST object, err %d\n",
3650 rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3651 handle, req, rc, 0);
3653 switch (cleanup_phase) {
3655 up(&mds->mds_pending_dir->d_inode->i_sem);
3658 UP_READ_I_ALLOC_SEM(new_inode);
3661 if (dlm_handles[5].cookie != 0)
3662 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3663 if (dlm_handles[6].cookie != 0)
3664 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3667 ptlrpc_save_llog_lock(req, lcl);
3670 if (dlm_handles[3].cookie != 0)
3671 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3672 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3673 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3674 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3676 if (dlm_handles[3].cookie != 0)
3677 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3678 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3679 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3680 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3689 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3692 req->rq_status = rc;
3696 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3697 struct ptlrpc_request *, struct lustre_handle *);
3699 static mds_reinter reinters[REINT_MAX + 1] = {
3700 [REINT_SETATTR] mds_reint_setattr,
3701 [REINT_CREATE] mds_reint_create,
3702 [REINT_LINK] mds_reint_link,
3703 [REINT_UNLINK] mds_reint_unlink,
3704 [REINT_RENAME] mds_reint_rename,
3705 [REINT_OPEN] mds_open
3708 int mds_reint_rec(struct mds_update_record *rec, int offset,
3709 struct ptlrpc_request *req, struct lustre_handle *lockh)
3711 struct obd_device *obd = req->rq_export->exp_obd;
3712 struct lvfs_run_ctxt saved;
3715 /* checked by unpacker */
3716 LASSERT(rec->ur_opcode <= REINT_MAX &&
3717 reinters[rec->ur_opcode] != NULL);
3719 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3720 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3721 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);