1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/mds/mds_reint.c
5 * Lustre Metadata Server (mds) reintegration routines
7 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8 * Author: Peter Braam <braam@clusterfs.com>
9 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * Author: Phil Schwan <phil@clusterfs.com>
12 * This file is part of Lustre, http://www.lustre.org.
14 * Lustre is free software; you can redistribute it and/or
15 * modify it under the terms of version 2 of the GNU General Public
16 * License as published by the Free Software Foundation.
18 * Lustre is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with Lustre; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 # define EXPORT_SYMTAB
31 #define DEBUG_SUBSYSTEM S_MDS
34 #include <linux/jbd.h>
35 #include <linux/namei.h>
36 #include <linux/ext3_fs.h>
37 #include <linux/obd_support.h>
38 #include <linux/obd_class.h>
39 #include <linux/obd.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_idl.h>
42 #include <linux/lustre_mds.h>
43 #include <linux/lustre_dlm.h>
44 #include <linux/lustre_log.h>
45 #include <linux/lustre_fsfilt.h>
46 #include <linux/lustre_acl.h>
47 #include <linux/lustre_lite.h>
48 #include <linux/lustre_smfs.h>
49 #include "mds_internal.h"
51 struct mds_logcancel_data {
52 struct lov_mds_md *mlcd_lmm;
56 struct llog_cookie mlcd_cookies[0];
59 static void mds_cancel_cookies_cb(struct obd_device *obd,
60 __u64 transno, void *cb_data,
63 struct mds_logcancel_data *mlcd = cb_data;
64 struct lov_stripe_md *lsm = NULL;
65 struct llog_ctxt *ctxt;
68 obd_transno_commit_cb(obd, transno, error);
70 CDEBUG(D_HA, "cancelling %d cookies\n",
71 (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
73 rc = obd_unpackmd(obd->u.mds.mds_dt_exp, &lsm, mlcd->mlcd_lmm,
74 mlcd->mlcd_eadatalen);
76 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
77 (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
80 ///* XXX 0 normally, SENDNOW for debug */);
81 ctxt = llog_get_context(&obd->obd_llogs,
82 mlcd->mlcd_cookies[0].lgc_subsys + 1);
83 rc = llog_cancel(ctxt, mlcd->mlcd_cookielen /
84 sizeof(*mlcd->mlcd_cookies),
85 mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW, lsm);
87 CERROR("error cancelling %d log cookies: rc %d\n",
88 (int)(mlcd->mlcd_cookielen /
89 sizeof(*mlcd->mlcd_cookies)), rc);
90 obd_free_memmd(obd->u.mds.mds_dt_exp, &lsm);
93 OBD_FREE(mlcd, mlcd->mlcd_size);
96 /* Assumes caller has already pushed us into the kernel context. */
97 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
98 struct ptlrpc_request *req, int rc, __u32 op_data)
100 struct mds_export_data *med = &req->rq_export->exp_mds_data;
101 struct obd_device *obd = req->rq_export->exp_obd;
102 struct mds_client_data *mcd = med->med_mcd;
103 int err, log_pri = D_HA;
108 /* if the export has already been failed, we have no last_rcvd slot */
109 if (req->rq_export->exp_failed) {
110 CERROR("committing transaction for disconnected client\n");
112 GOTO(out_commit, rc);
119 if (handle == NULL) {
120 /* if we're starting our own xaction, use our own inode */
121 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
122 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
123 if (IS_ERR(handle)) {
124 CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
125 RETURN(PTR_ERR(handle));
131 transno = req->rq_reqmsg->transno;
133 LASSERTF(transno == 0, "BUG 3934, t"LPU64" rc %d\n", transno, rc);
134 } else if (transno == 0) {
135 spin_lock(&mds->mds_transno_lock);
136 transno = ++mds->mds_last_transno;
137 spin_unlock(&mds->mds_transno_lock);
139 spin_lock(&mds->mds_transno_lock);
140 if (transno > mds->mds_last_transno)
141 mds->mds_last_transno = transno;
142 spin_unlock(&mds->mds_transno_lock);
144 req->rq_repmsg->transno = req->rq_transno = transno;
145 if (req->rq_reqmsg->opc == MDS_CLOSE) {
146 mcd->mcd_last_close_transno = cpu_to_le64(transno);
147 mcd->mcd_last_close_xid = cpu_to_le64(req->rq_xid);
148 mcd->mcd_last_close_result = cpu_to_le32(rc);
149 mcd->mcd_last_close_data = cpu_to_le32(op_data);
151 mcd->mcd_last_transno = cpu_to_le64(transno);
152 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
153 mcd->mcd_last_result = cpu_to_le32(rc);
154 mcd->mcd_last_data = cpu_to_le32(op_data);
157 fsfilt_add_journal_cb(obd, mds->mds_sb, transno, handle,
158 mds_commit_last_transno_cb, NULL);
160 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd,
161 sizeof(*mcd), &off, 0);
169 DEBUG_REQ(log_pri, req,
170 "wrote trans #"LPU64" client %s at idx %u: err = %d",
171 transno, mcd->mcd_uuid, med->med_idx, err);
173 err = mds_update_last_fid(obd, handle, 0);
180 err = mds_dt_write_objids(obd);
186 CDEBUG(log_pri, "wrote objids: err = %d\n", err);
190 err = fsfilt_commit(obd, mds->mds_sb, inode, handle,
191 req->rq_export->exp_sync);
193 CERROR("error committing transaction: %d\n", err);
201 /* this gives the same functionality as the code between
202 * sys_chmod and inode_setattr
203 * chown_common and inode_setattr
204 * utimes and inode_setattr
207 /* Just for the case if we have some clients that know about ATTR_RAW */
208 #define ATTR_RAW 8192
210 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
212 time_t now = LTIME_S(CURRENT_TIME);
213 struct iattr *attr = &rec->ur_iattr;
214 unsigned int ia_valid = attr->ia_valid;
218 /* only fix up attrs if the client VFS didn't already */
220 if (!(ia_valid & ATTR_RAW))
223 if (!(ia_valid & ATTR_CTIME_SET))
224 LTIME_S(attr->ia_ctime) = now;
225 if (!(ia_valid & ATTR_ATIME_SET))
226 LTIME_S(attr->ia_atime) = now;
227 if (!(ia_valid & ATTR_MTIME_SET))
228 LTIME_S(attr->ia_mtime) = now;
230 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
234 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
235 if (rec->ur_fsuid != inode->i_uid &&
236 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
240 if (ia_valid & ATTR_SIZE) {
241 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
245 if (ia_valid & ATTR_UID) {
248 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
250 if (attr->ia_uid == (uid_t) -1)
251 attr->ia_uid = inode->i_uid;
252 if (attr->ia_gid == (gid_t) -1)
253 attr->ia_gid = inode->i_gid;
254 attr->ia_mode = inode->i_mode;
256 * If the user or group of a non-directory has been
257 * changed by a non-root user, remove the setuid bit.
258 * 19981026 David C Niemi <niemi@tux.org>
260 * Changed this to apply to all users, including root,
261 * to avoid some races. This is the behavior we had in
262 * 2.0. The check for non-root was definitely wrong
263 * for 2.2 anyway, as it should have been using
264 * CAP_FSETID rather than fsuid -- 19990830 SD.
266 if ((inode->i_mode & S_ISUID) == S_ISUID &&
267 !S_ISDIR(inode->i_mode)) {
268 attr->ia_mode &= ~S_ISUID;
269 attr->ia_valid |= ATTR_MODE;
272 * Likewise, if the user or group of a non-directory
273 * has been changed by a non-root user, remove the
274 * setgid bit UNLESS there is no group execute bit
275 * (this would be a file marked for mandatory
276 * locking). 19981026 David C Niemi <niemi@tux.org>
278 * Removed the fsuid check (see the comment above) --
281 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
282 (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
283 attr->ia_mode &= ~S_ISGID;
284 attr->ia_valid |= ATTR_MODE;
286 } else if (ia_valid & ATTR_MODE) {
287 int mode = attr->ia_mode;
289 if (attr->ia_mode == (mode_t) -1)
290 attr->ia_mode = inode->i_mode;
292 (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
297 void mds_steal_ack_locks(struct ptlrpc_request *req)
299 struct obd_export *exp = req->rq_export;
300 char str[PTL_NALFMT_SIZE];
301 struct list_head *tmp;
302 struct ptlrpc_reply_state *oldrep;
303 struct ptlrpc_service *svc;
304 struct llog_create_locks *lcl;
308 /* CAVEAT EMPTOR: spinlock order */
309 spin_lock_irqsave (&exp->exp_lock, flags);
310 list_for_each (tmp, &exp->exp_outstanding_replies) {
311 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
313 if (oldrep->rs_xid != req->rq_xid)
316 if (oldrep->rs_msg->opc != req->rq_reqmsg->opc)
317 CERROR ("Resent req xid "LPX64" has mismatched opc: "
318 "new %d old %d\n", req->rq_xid,
319 req->rq_reqmsg->opc, oldrep->rs_msg->opc);
321 svc = oldrep->rs_srv_ni->sni_service;
322 spin_lock (&svc->srv_lock);
324 list_del_init (&oldrep->rs_exp_list);
326 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
327 " o%d NID %s\n", oldrep->rs_nlocks, oldrep,
328 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg->opc,
329 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
331 for (i = 0; i < oldrep->rs_nlocks; i++)
332 ptlrpc_save_lock(req,
333 &oldrep->rs_locks[i],
334 oldrep->rs_modes[i]);
335 oldrep->rs_nlocks = 0;
337 lcl = oldrep->rs_llog_locks;
338 oldrep->rs_llog_locks = NULL;
340 ptlrpc_save_llog_lock(req, lcl);
342 DEBUG_REQ(D_HA, req, "stole locks for");
343 ptlrpc_schedule_difficult_reply (oldrep);
345 spin_unlock (&svc->srv_lock);
346 spin_unlock_irqrestore (&exp->exp_lock, flags);
349 spin_unlock_irqrestore (&exp->exp_lock, flags);
352 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
354 if (req->rq_reqmsg->opc == MDS_CLOSE) {
355 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
356 mcd->mcd_last_close_transno, mcd->mcd_last_close_result);
357 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_close_transno;
358 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_close_result;
360 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
361 mcd->mcd_last_transno, mcd->mcd_last_result);
362 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
363 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
366 mds_steal_ack_locks(req);
369 static void reconstruct_reint_setattr(struct mds_update_record *rec,
370 int offset, struct ptlrpc_request *req)
372 struct mds_export_data *med = &req->rq_export->exp_mds_data;
373 struct mds_body *body;
376 mds_req_from_mcd(req, med->med_mcd);
378 de = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
380 LASSERT(PTR_ERR(de) == req->rq_status);
384 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
385 mds_pack_inode2body(req2obd(req), body, de->d_inode, 1);
387 /* Don't return OST-specific attributes if we didn't just set them */
388 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
389 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
390 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
391 body->valid |= OBD_MD_FLMTIME;
392 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
393 body->valid |= OBD_MD_FLATIME;
397 /*This is a tmp fix for cmobd setattr reint*/
399 #define XATTR_LUSTRE_MDS_LOV_EA "lov"
400 #define XATTR_LUSTRE_MDS_MEA_EA "mea"
401 #define XATTR_LUSTRE_MDS_MID_EA "mid"
402 #define XATTR_LUSTRE_MDS_SID_EA "sid"
404 static int mds_get_md_type(char *name)
406 if (!strcmp(name, XATTR_LUSTRE_MDS_LOV_EA))
408 if (!strcmp(name, XATTR_LUSTRE_MDS_MEA_EA))
410 if (!strcmp(name, XATTR_LUSTRE_MDS_MID_EA))
412 if (!strcmp(name, XATTR_LUSTRE_MDS_SID_EA))
416 /* In the raw-setattr case, we lock the child inode.
417 * In the write-back case or if being called from open, the client holds a lock
420 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
421 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
422 struct ptlrpc_request *req, struct lustre_handle *lh)
424 struct mds_obd *mds = mds_req2mds(req);
425 struct obd_device *obd = req->rq_export->exp_obd;
426 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
427 struct mds_body *body;
428 struct dentry *de = NULL;
429 struct inode *inode = NULL;
430 struct lustre_handle lockh[2] = {{0}, {0}};
433 struct mds_logcancel_data *mlcd = NULL;
434 int rc = 0, cleanup_phase = 0, err;
438 LASSERT(offset == 1);
440 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x",
441 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
442 rec->ur_iattr.ia_valid);
444 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
445 MD_COUNTER_INCREMENT(obd, setattr);
447 if (med->med_remote) {
448 if (rec->ur_iattr.ia_valid & ATTR_GID) {
449 CWARN("Deny chgrp from remote client\n");
450 GOTO(cleanup, rc = -EPERM);
452 if (rec->ur_iattr.ia_valid & ATTR_UID) {
455 uid = mds_idmap_lookup_uid(med->med_idmap, 0,
456 rec->ur_iattr.ia_uid);
457 if (uid == MDS_IDMAP_NOTFOUND) {
458 CWARN("Deny chown to uid %u\n",
459 rec->ur_iattr.ia_uid);
460 GOTO(cleanup, rc = -EPERM);
462 rec->ur_iattr.ia_uid = uid;
466 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
467 de = mds_id2dentry(obd, rec->ur_id1, NULL);
469 GOTO(cleanup, rc = PTR_ERR(de));
471 __u64 lockpart = MDS_INODELOCK_UPDATE;
472 if (rec->ur_iattr.ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
473 lockpart |= MDS_INODELOCK_LOOKUP;
474 de = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
475 lockh, &parent_mode, NULL, 0, lockpart);
477 GOTO(cleanup, rc = PTR_ERR(de));
485 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
486 rec->ur_eadata != NULL)
489 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
491 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
493 GOTO(cleanup, rc = PTR_ERR(handle));
495 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
496 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
497 LTIME_S(rec->ur_iattr.ia_mtime),
498 LTIME_S(rec->ur_iattr.ia_ctime));
499 rc = mds_fix_attr(inode, rec);
503 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
504 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
505 (long)&rec->ur_iattr.ia_attr_flags);
507 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
510 if (rec->ur_iattr.ia_valid & ATTR_EA) {
511 int flags = (int)rec->ur_iattr.ia_attr_flags;
514 if (inode->i_op && inode->i_op->setxattr)
515 rc = inode->i_op->setxattr(de, rec->ur_eadata,
516 rec->ur_ea2data, rec->ur_ea2datalen,
518 } else if (rec->ur_iattr.ia_valid & ATTR_EA_RM) {
520 if (inode->i_op && inode->i_op->removexattr)
521 rc = inode->i_op->removexattr(de, rec->ur_eadata);
522 } else if (rec->ur_iattr.ia_valid & ATTR_EA_CMOBD) {
525 /*tmp fix for cmobd set md reint*/
526 LASSERT(rec->ur_eadata != NULL);
527 LASSERT(rec->ur_ea2data != NULL);
528 name = rec->ur_eadata;
529 CDEBUG(D_INFO, "set %s EA for cmobd \n", name);
530 type = mds_get_md_type(name);
532 rc = fsfilt_set_md(obd, inode, handle,
534 rec->ur_ea2datalen, type);
537 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) {
538 struct lov_stripe_md *lsm = NULL;
539 struct lov_user_md *lum = NULL;
541 if (rec->ur_eadata != NULL) {
542 rc = ll_permission(inode, MAY_WRITE, NULL);
546 lum = rec->ur_eadata;
548 /* if lmm_stripe_size is -1 delete default
550 if (S_ISDIR(inode->i_mode) &&
551 lum->lmm_stripe_size == (typeof(lum->lmm_stripe_size))(-1)){
552 rc = fsfilt_set_md(obd, inode, handle, NULL, 0, EA_LOV);
556 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
558 &lsm, rec->ur_eadata);
562 obd_free_memmd(mds->mds_dt_exp, &lsm);
563 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
564 rec->ur_eadatalen, EA_LOV);
572 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
573 mds_pack_inode2body(obd, body, inode, 1);
575 /* Don't return OST-specific attributes if we didn't just set them */
576 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
577 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
578 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
579 body->valid |= OBD_MD_FLMTIME;
580 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
581 body->valid |= OBD_MD_FLATIME;
583 mds_body_do_reverse_map(med, body);
585 /* The logcookie should be no use anymore, why nobody remove
586 * following code block?
588 LASSERT(rec->ur_cookielen == 0);
589 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_dt_obd)) {
590 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
593 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
595 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
596 mlcd->mlcd_cookielen = rec->ur_cookielen;
597 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
598 mlcd->mlcd_cookielen;
599 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
600 mlcd->mlcd_cookielen);
601 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
602 mlcd->mlcd_eadatalen);
604 CERROR("unable to allocate log cancel data\n");
610 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
611 handle, mds_cancel_cookies_cb, mlcd);
612 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
613 switch (cleanup_phase) {
615 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
616 rec->ur_eadata != NULL)
621 if (lockh[1].cookie != 0)
622 ldlm_lock_decref(lockh + 1, parent_mode);
625 ldlm_lock_decref(lockh, LCK_PW);
627 ptlrpc_save_lock (req, lockh, LCK_PW);
642 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
643 struct ptlrpc_request *req)
645 struct mds_export_data *med = &req->rq_export->exp_mds_data;
646 struct dentry *parent, *child;
647 struct mds_body *body;
650 mds_req_from_mcd(req, med->med_mcd);
652 if (req->rq_status) {
657 parent = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
658 LASSERT(!IS_ERR(parent));
659 child = ll_lookup_one_len(rec->ur_name, parent,
660 rec->ur_namelen - 1);
661 LASSERT(!IS_ERR(child));
662 if ((child->d_flags & DCACHE_CROSS_REF)) {
663 LASSERTF(child->d_inode == NULL, "BUG 3869\n");
664 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
665 mds_pack_dentry2body(req2obd(req), body, child, 1);
666 } else if (child->d_inode == NULL) {
667 DEBUG_REQ(D_ERROR, req, "parent "DLID4" name %s mode %o",
668 OLID4(rec->ur_id1), rec->ur_name, rec->ur_mode);
669 LASSERTF(child->d_inode != NULL, "BUG 3869\n");
671 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
672 mds_pack_inode2body(req2obd(req), body, child->d_inode, 1);
679 static int mds_get_default_acl(struct inode *dir, void **pacl)
681 struct dentry de = { .d_inode = dir };
684 LASSERT(S_ISDIR(dir->i_mode));
686 if (!dir->i_op->getxattr)
689 size = dir->i_op->getxattr(&de, XATTR_NAME_ACL_DEFAULT, NULL, 0);
690 if (size == 0 || size == -ENODATA || size == -EOPNOTSUPP)
695 OBD_ALLOC(*pacl, size);
699 size2 = dir->i_op->getxattr(&de, XATTR_NAME_ACL_DEFAULT, *pacl, size);
701 /* since we already locked the dir, it should not change
702 * between the 2 getxattr calls
704 CERROR("2'nd getxattr got %d, expect %d\n", size2, size);
705 OBD_FREE(*pacl, size);
712 static int mds_reint_create(struct mds_update_record *rec, int offset,
713 struct ptlrpc_request *req,
714 struct lustre_handle *lh)
716 struct dentry *dparent = NULL;
717 struct mds_obd *mds = mds_req2mds(req);
718 struct obd_device *obd = req->rq_export->exp_obd;
719 struct dentry *dchild = NULL;
720 struct inode *dir = NULL;
722 struct lustre_handle lockh[2] = {{0}, {0}};
724 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
726 struct dentry_params dp;
727 struct mea *mea = NULL;
732 LASSERT(offset == 1);
734 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
735 OBD_MDS_DEVICENAME));
737 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
738 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
739 rec->ur_name, rec->ur_mode);
741 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
743 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
744 GOTO(cleanup, rc = -ESTALE);
746 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
747 lockh, &parent_mode, rec->ur_name,
748 rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
749 if (IS_ERR(dparent)) {
750 rc = PTR_ERR(dparent);
751 CERROR("parent lookup error %d\n", rc);
754 cleanup_phase = 1; /* locked parent dentry */
755 dir = dparent->d_inode;
758 ldlm_lock_dump_handle(D_OTHER, lockh);
760 /* try to retrieve MEA data for this dir */
761 rc = mds_md_get_attr(obd, dparent->d_inode, &mea, &mea_size);
767 * dir is already splitted, check is requested filename should
768 * live at this MDS or at another one.
770 int i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
771 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
772 CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
773 " should be %lu(%d)\n",
774 mea->mea_master, dparent->d_inode->i_ino,
775 dparent->d_inode->i_generation, rec->ur_name,
776 (unsigned long)id_group(&mea->mea_ids[i]), i);
777 GOTO(cleanup, rc = -ERESTART);
781 dchild = ll_lookup_one_len(rec->ur_name, dparent,
782 rec->ur_namelen - 1);
783 if (IS_ERR(dchild)) {
784 rc = PTR_ERR(dchild);
785 CERROR("Can't find "DLID4"/%s, error %d\n",
786 OLID4(rec->ur_id1), rec->ur_name, rc);
790 cleanup_phase = 2; /* child dentry */
792 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
794 if (type == S_IFREG || type == S_IFDIR) {
795 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
796 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
797 obd->obd_name, dparent->d_inode->i_ino,
798 dparent->d_inode->i_generation, rc, parent_mode);
800 /* dir got splitted */
801 GOTO(cleanup, rc = -ERESTART);
803 /* error happened during spitting. */
808 if (dir->i_mode & S_ISGID) {
809 if (S_ISDIR(rec->ur_mode))
810 rec->ur_mode |= S_ISGID;
814 * here inode number should be used only in the case of replaying. It is
815 * needed to check if object already created in the case of creating
818 if (id_ino(rec->ur_id2))
819 fid = id_fid(rec->ur_id2);
821 fid = mds_alloc_fid(obd);
822 dchild->d_fsdata = (void *)&dp;
823 dp.p_inum = (unsigned long)id_ino(rec->ur_id2);
826 dp.p_group = mds->mds_num;
830 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
832 GOTO(cleanup, rc = PTR_ERR(handle));
833 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
841 * as Peter asked, mkdir() should distribute new directories
842 * over the whole cluster in order to distribute namespace
843 * processing load. first, we calculate which MDS to use to put
844 * new directory's inode in.
846 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1,
847 rec->ur_flags, &req->rq_peer, dir);
848 if (i == mds->mds_num) {
849 /* inode will be created locally */
850 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
852 GOTO(cleanup, rc = PTR_ERR(handle));
854 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
857 "Can't create dir \"%s\", rc = %d\n",
858 dchild->d_name.name, rc);
862 down(&dchild->d_inode->i_sem);
864 rc = mds_update_inode_sid(obd, dchild->d_inode,
865 handle, rec->ur_id2);
867 CERROR("mds_update_inode_sid() failed, inode %lu, "
868 "rc %d\n", dchild->d_inode->i_ino, rc);
872 * make sure, that fid is up-to-date.
874 mds_set_last_fid(obd, id_fid(rec->ur_id2));
876 rc = mds_set_inode_sid(obd, dchild->d_inode,
879 CERROR("mds_set_inode_sid() failed, inode %lu, "
880 "rc %d\n", dchild->d_inode->i_ino, rc);
883 up(&dchild->d_inode->i_sem);
889 nstripes = *(u16 *)rec->ur_eadata;
891 if (rc == 0 && nstripes) {
893 * we pass LCK_EX to split routine to signal,
894 * that we have exclusive access to the
895 * directory. Simple because nobody knows it
896 * already exists -bzzz
898 rc = mds_try_to_split_dir(obd, dchild,
902 /* dir got splitted */
905 /* an error occured during
910 } else if (!DENTRY_VALID(dchild)) {
911 /* inode will be created on another MDS */
912 struct obdo *oa = NULL;
913 struct mds_body *body;
917 /* first, create that inode */
920 GOTO(cleanup, rc = -ENOMEM);
925 if (rec->ur_eadata) {
926 /* user asks for creating splitted dir */
927 oa->o_easize = *((u16 *) rec->ur_eadata);
930 obdo_from_inode(oa, dir, OBD_MD_FLATIME |
931 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
933 /* adjust the uid/gid/mode bits */
934 oa->o_mode = rec->ur_mode;
935 oa->o_uid = current->fsuid;
936 oa->o_gid = (dir->i_mode & S_ISGID) ?
937 dir->i_gid : current->fsgid;
938 oa->o_valid |= OBD_MD_FLTYPE|OBD_MD_FLUID|OBD_MD_FLGID;
940 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
943 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
945 * here inode number and generation are
946 * important, as this is replay request and we
947 * need them to check if such an object is
950 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
951 obd->obd_name, rec->ur_namelen - 1,
952 rec->ur_name, (unsigned)id_ino(rec->ur_id2),
953 (unsigned)id_gen(rec->ur_id2));
954 oa->o_id = id_ino(rec->ur_id2);
955 oa->o_fid = id_fid(rec->ur_id2);
956 oa->o_generation = id_gen(rec->ur_id2);
957 oa->o_flags |= OBD_FL_RECREATE_OBJS;
958 LASSERT(oa->o_fid != 0);
961 /* obtain default ACL */
962 acl_size = mds_get_default_acl(dir, &acl);
965 GOTO(cleanup, rc = -ENOMEM);
969 * before obd_create() is called, o_fid is not known if
970 * this is not recovery of cause.
972 rc = obd_create(mds->mds_md_exp, oa, acl, acl_size,
976 OBD_FREE(acl, acl_size);
979 CERROR("can't create remote inode: %d\n", rc);
980 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
981 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
982 rec->ur_name, rec->ur_mode);
987 LASSERT(oa->o_fid != 0);
989 /* now, add new dir entry for it */
990 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
991 if (IS_ERR(handle)) {
993 GOTO(cleanup, rc = PTR_ERR(handle));
996 /* creating local dentry for remote inode. */
997 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
998 rec->ur_namelen - 1, oa->o_id,
999 oa->o_generation, i, oa->o_fid);
1002 CERROR("Can't create local entry %*s for "
1003 "remote inode.\n", rec->ur_namelen - 1,
1009 body = lustre_msg_buf(req->rq_repmsg,
1011 body->valid |= OBD_MD_FLID | OBD_MD_MDS | OBD_MD_FID;
1013 obdo2id(&body->id1, oa);
1016 /* requested name exists in the directory */
1023 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
1025 GOTO(cleanup, rc = PTR_ERR(handle));
1026 if (rec->ur_tgt == NULL) /* no target supplied */
1027 rc = -EINVAL; /* -EPROTO? */
1029 rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
1037 int rdev = rec->ur_rdev;
1038 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
1040 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
1041 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
1046 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
1047 dchild->d_fsdata = NULL;
1048 GOTO(cleanup, rc = -EINVAL);
1051 /* In case we stored the desired inum in here, we want to clean up. */
1052 if (dchild->d_fsdata == (void *)(unsigned long)id_ino(rec->ur_id2))
1053 dchild->d_fsdata = NULL;
1056 CDEBUG(D_INODE, "error during create: %d\n", rc);
1058 } else if (dchild->d_inode) {
1059 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
1061 struct mds_body *body;
1062 struct inode *inode = dchild->d_inode;
1065 iattr.ia_uid = rec->ur_fsuid;
1066 LTIME_S(iattr.ia_atime) = rec->ur_time;
1067 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1068 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1070 if (dir->i_mode & S_ISGID)
1071 iattr.ia_gid = dir->i_gid;
1073 iattr.ia_gid = rec->ur_fsgid;
1075 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
1076 ATTR_MTIME | ATTR_CTIME;
1078 if (id_ino(rec->ur_id2)) {
1079 LASSERT(id_ino(rec->ur_id2) == inode->i_ino);
1080 inode->i_generation = id_gen(rec->ur_id2);
1082 if (type != S_IFDIR) {
1083 down(&inode->i_sem);
1084 rc = mds_update_inode_sid(obd, inode,
1085 handle, rec->ur_id2);
1088 CERROR("Can't update inode self id, "
1093 * make sure, that fid is up-to-date.
1095 mds_set_last_fid(obd, id_fid(rec->ur_id2));
1098 /* dirtied and committed by the upcoming setattr. */
1099 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
1100 inode->i_ino, inode->i_generation);
1102 struct lustre_handle child_ino_lockh;
1104 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
1105 inode->i_ino, inode->i_generation);
1107 if (type != S_IFDIR) {
1109 * allocate new id for @inode if it is not dir,
1110 * because for dir it was already done.
1112 down(&inode->i_sem);
1113 rc = mds_set_inode_sid(obd, inode,
1117 CERROR("mds_set_inode_sid() failed, "
1118 "inode %lu, rc %d\n", inode->i_ino,
1125 * the inode we were allocated may have just
1126 * been freed by an unlink operation. We take
1127 * this lock to synchronize against the matching
1128 * reply-ack-lock taken in unlink, to avoid
1129 * replay problems if this reply makes it out to
1130 * the client but the unlink's does not. See
1131 * bug 2029 for more detail.
1133 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
1134 if (rc != ELDLM_OK) {
1135 CERROR("error locking for unlink/create sync: "
1138 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
1143 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
1145 CERROR("error on child setattr: rc = %d\n", rc);
1147 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1148 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1150 CERROR("error on parent setattr: rc = %d\n", rc);
1152 MD_COUNTER_INCREMENT(obd, create);
1154 /* take care of default stripe inheritance */
1155 if (type == S_IFDIR) {
1156 struct lov_mds_md lmm;
1157 int lmm_size = sizeof(lmm);
1159 rc = mds_get_md(obd, dir, &lmm, &lmm_size, 1, 0);
1161 down(&inode->i_sem);
1162 rc = fsfilt_set_md(obd, inode, handle,
1163 &lmm, lmm_size, EA_LOV);
1167 CERROR("error on copy stripe info: rc = %d\n",
1173 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1174 mds_pack_inode2body(obd, body, inode, 1);
1175 mds_body_do_reverse_map(med, body);
1180 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
1182 if (rc && created) {
1183 /* Destroy the file we just created. This should not need extra
1184 * journal credits, as we have already modified all of the
1185 * blocks needed in order to create the file in the first
1189 err = vfs_rmdir(dir, dchild);
1191 CERROR("rmdir in error path: %d\n", err);
1194 err = vfs_unlink(dir, dchild);
1196 CERROR("unlink in error path: %d\n", err);
1202 switch (cleanup_phase) {
1203 case 2: /* child dentry */
1205 case 1: /* locked parent dentry */
1207 if (lockh[1].cookie != 0)
1208 ldlm_lock_decref(lockh + 1, parent_mode);
1211 ldlm_lock_decref(lockh, LCK_PW);
1213 ptlrpc_save_lock(req, lockh, LCK_PW);
1219 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1223 OBD_FREE(mea, mea_size);
1224 req->rq_status = rc;
1229 res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
1230 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1234 for (i = 0; i < RES_NAME_SIZE; i++) {
1236 * this is needed to make zeroed res_id entries to be put at the
1237 * end of list in *ordered_locks() .
1239 if (res1->name[i] == 0 && res2->name[i] != 0)
1241 if (res2->name[i] == 0 && res1->name[i] != 0)
1243 if (res1->name[i] > res2->name[i])
1245 if (res1->name[i] < res2->name[i])
1252 if (memcmp(p1, p2, sizeof(*p1)) < 0)
1258 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1259 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1260 * because they take the place of local semaphores.
1262 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
1263 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
1264 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
1265 struct lustre_handle *p1_lockh, int p1_lock_mode,
1266 ldlm_policy_data_t *p1_policy,
1267 struct ldlm_res_id *p2_res_id,
1268 struct lustre_handle *p2_lockh, int p2_lock_mode,
1269 ldlm_policy_data_t *p2_policy)
1271 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1272 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1273 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1274 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
1278 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1280 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1281 res_id[0]->name[0], res_id[1]->name[0]);
1283 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1284 handles[1] = p1_lockh;
1285 handles[0] = p2_lockh;
1286 res_id[1] = p1_res_id;
1287 res_id[0] = p2_res_id;
1288 lock_modes[1] = p1_lock_mode;
1289 lock_modes[0] = p2_lock_mode;
1290 policies[1] = p1_policy;
1291 policies[0] = p2_policy;
1294 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1295 res_id[0]->name[0], res_id[1]->name[0]);
1297 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1298 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1299 LDLM_IBITS, policies[0], lock_modes[0], &flags,
1300 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1301 NULL, 0, NULL, handles[0]);
1304 ldlm_lock_dump_handle(D_OTHER, handles[0]);
1306 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1307 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1308 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1309 ldlm_lock_addref(handles[1], lock_modes[1]);
1310 } else if (res_id[1]->name[0] != 0) {
1311 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1312 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1313 *res_id[1], LDLM_IBITS, policies[1],
1314 lock_modes[1], &flags, mds_blocking_ast,
1315 ldlm_completion_ast, NULL, NULL, NULL, 0,
1317 if (rc != ELDLM_OK) {
1318 ldlm_lock_decref(handles[0], lock_modes[0]);
1321 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1327 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1328 struct lustre_handle *p1_lockh, int p1_lock_mode,
1329 ldlm_policy_data_t *p1_policy,
1330 struct ldlm_res_id *p2_res_id,
1331 struct lustre_handle *p2_lockh, int p2_lock_mode,
1332 ldlm_policy_data_t *p2_policy,
1333 struct ldlm_res_id *c1_res_id,
1334 struct lustre_handle *c1_lockh, int c1_lock_mode,
1335 ldlm_policy_data_t *c1_policy,
1336 struct ldlm_res_id *c2_res_id,
1337 struct lustre_handle *c2_lockh, int c2_lock_mode,
1338 ldlm_policy_data_t *c2_policy)
1340 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1341 c1_res_id, c2_res_id };
1342 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1343 c1_lockh, c2_lockh };
1344 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1345 c1_lock_mode, c2_lock_mode };
1346 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1347 c1_policy, c2_policy};
1348 int rc, i, j, sorted, flags;
1351 CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1352 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1353 res_id[3]->name[0]);
1356 * simple insertion sort - we have at most 4 elements. Note, that zeroed
1357 * res_id should be at the end of list after sorting is finished.
1359 for (i = 1; i < 4; i++) {
1361 dlm_handles[4] = dlm_handles[i];
1362 res_id[4] = res_id[i];
1363 lock_modes[4] = lock_modes[i];
1364 policies[4] = policies[i];
1368 if (res_gt(res_id[j], res_id[4], policies[j],
1370 dlm_handles[j + 1] = dlm_handles[j];
1371 res_id[j + 1] = res_id[j];
1372 lock_modes[j + 1] = lock_modes[j];
1373 policies[j + 1] = policies[j];
1378 } while (j >= 0 && !sorted);
1380 dlm_handles[j + 1] = dlm_handles[4];
1381 res_id[j + 1] = res_id[4];
1382 lock_modes[j + 1] = lock_modes[4];
1383 policies[j + 1] = policies[4];
1386 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1387 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1388 res_id[3]->name[0]);
1390 /* XXX we could send ASTs on all these locks first before blocking? */
1391 for (i = 0; i < 4; i++) {
1395 * nevertheless zeroed res_ids should be at the end of list, and
1396 * could use break here, I think, that it is more correctly for
1397 * clear understanding of code to have continue here, as it
1398 * clearly means, that zeroed res_id should be skipped and does
1399 * not mean, that if we meet zeroed res_id we should stop
1402 if (res_id[i]->name[0] == 0)
1406 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1407 (policies[i]->l_inodebits.bits &
1408 policies[i-1]->l_inodebits.bits) ) {
1409 memcpy(dlm_handles[i], dlm_handles[i-1],
1410 sizeof(*(dlm_handles[i])));
1411 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1413 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1414 *res_id[i], LDLM_IBITS,
1416 lock_modes[i], &flags,
1418 ldlm_completion_ast, NULL, NULL,
1419 NULL, 0, NULL, dlm_handles[i]);
1421 GOTO(out_err, rc = -EIO);
1422 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1429 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1434 /* In the unlikely case that the child changed while we were waiting
1435 * on the lock, we need to drop the lock on the old child and either:
1436 * - if the child has a lower resource name, then we have to also
1437 * drop the parent lock and regain the locks in the right order
1438 * - in the rename case, if the child has a lower resource name than one of
1439 * the other parent/child resources (maxres) we also need to reget the locks
1440 * - if the child has a higher resource name (this is the common case)
1441 * we can just get the lock on the new child (still in lock order)
1443 * Returns 0 if the child did not change or if it changed but could be locked.
1444 * Returns 1 if the child changed and we need to re-lock (no locks held).
1445 * Returns -ve error with a valid dchild (no locks held). */
1446 static int mds_verify_child(struct obd_device *obd,
1447 struct ldlm_res_id *parent_res_id,
1448 struct lustre_handle *parent_lockh,
1449 struct dentry *dparent, int parent_mode,
1450 struct ldlm_res_id *child_res_id,
1451 struct lustre_handle *child_lockh,
1452 struct dentry **dchildp, int child_mode,
1453 ldlm_policy_data_t *child_policy,
1454 const char *name, int namelen,
1455 struct ldlm_res_id *maxres,
1456 unsigned long child_ino, __u32 child_gen)
1458 struct lustre_id sid;
1459 struct dentry *vchild, *dchild = *dchildp;
1460 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1463 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1465 GOTO(cleanup, rc = PTR_ERR(vchild));
1467 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1468 if (child_gen == vchild->d_generation &&
1469 child_ino == vchild->d_inum) {
1478 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1479 (vchild->d_inode != NULL &&
1480 child_gen == vchild->d_inode->i_generation &&
1481 child_ino == vchild->d_inode->i_ino))) {
1489 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1490 vchild->d_inode, dchild ? dchild->d_inode : 0,
1491 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1492 child_res_id->name[0]);
1494 if (child_res_id->name[0] != 0)
1495 ldlm_lock_decref(child_lockh, child_mode);
1499 cleanup_phase = 1; /* parent lock only */
1500 *dchildp = dchild = vchild;
1502 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1503 int flags = LDLM_FL_ATOMIC_CB;
1505 if (dchild->d_inode) {
1506 down(&dchild->d_inode->i_sem);
1507 rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
1508 up(&dchild->d_inode->i_sem);
1510 CERROR("Can't read inode self id, inode %lu,"
1511 " rc %d\n", dchild->d_inode->i_ino, rc);
1514 child_res_id->name[0] = id_fid(&sid);
1515 child_res_id->name[1] = id_group(&sid);
1517 child_res_id->name[0] = dchild->d_fid;
1518 child_res_id->name[1] = dchild->d_mdsnum;
1521 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1522 res_gt(maxres, child_res_id, NULL, NULL)) {
1523 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1524 child_res_id->name[0], parent_res_id->name[0],
1526 GOTO(cleanup, rc = 1);
1529 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1530 *child_res_id, LDLM_IBITS, child_policy,
1531 child_mode, &flags, mds_blocking_ast,
1532 ldlm_completion_ast, NULL, NULL, NULL, 0,
1535 GOTO(cleanup, rc = -EIO);
1538 memset(child_res_id, 0, sizeof(*child_res_id));
1544 switch(cleanup_phase) {
1546 if (child_res_id->name[0] != 0)
1547 ldlm_lock_decref(child_lockh, child_mode);
1549 ldlm_lock_decref(parent_lockh, parent_mode);
1555 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1556 struct lustre_id *id,
1557 struct lustre_handle *parent_lockh,
1558 struct dentry **dparentp, int parent_mode,
1559 __u64 parent_lockpart, int *update_mode,
1560 char *name, int namelen,
1561 struct lustre_handle *child_lockh,
1562 struct dentry **dchildp, int child_mode,
1563 __u64 child_lockpart)
1565 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1566 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1567 struct ldlm_res_id parent_res_id = { .name = {0} };
1568 struct ldlm_res_id child_res_id = { .name = {0} };
1569 unsigned long child_ino = 0; __u32 child_gen = 0;
1570 int rc = 0, cleanup_phase = 0;
1571 struct lustre_id sid;
1572 struct inode *inode;
1575 /* Step 1: Lookup parent */
1576 *dparentp = mds_id2dentry(obd, id, NULL);
1577 if (IS_ERR(*dparentp)) {
1578 rc = PTR_ERR(*dparentp);
1583 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1584 (*dparentp)->d_inode->i_ino, name);
1586 parent_res_id.name[0] = id_fid(id);
1587 parent_res_id.name[1] = id_group(id);
1590 parent_lockh[1].cookie = 0;
1591 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1592 struct ldlm_res_id res_id = { .name = {0} };
1593 ldlm_policy_data_t policy;
1594 int flags = LDLM_FL_ATOMIC_CB;
1596 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1598 res_id.name[0] = id_fid(id);
1599 res_id.name[1] = id_group(id);
1600 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1602 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1603 res_id, LDLM_IBITS, &policy,
1604 *update_mode, &flags,
1606 ldlm_completion_ast,
1607 NULL, NULL, NULL, 0, NULL,
1613 parent_res_id.name[2] = full_name_hash((unsigned char *)name,
1616 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1617 (unsigned long)id_fid(id), (unsigned long)id_group(id),
1618 parent_res_id.name[2]);
1622 cleanup_phase = 1; /* parent dentry */
1624 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1625 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1626 if (IS_ERR(*dchildp)) {
1627 rc = PTR_ERR(*dchildp);
1628 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1632 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1634 * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1635 * lock. Drop possible UPDATE lock!
1637 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1638 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1640 child_res_id.name[0] = (*dchildp)->d_fid;
1641 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1642 child_ino = (*dchildp)->d_inum;
1643 child_gen = (*dchildp)->d_generation;
1647 inode = (*dchildp)->d_inode;
1649 inode = igrab(inode);
1653 down(&inode->i_sem);
1654 rc = mds_read_inode_sid(obd, inode, &sid);
1657 CERROR("Can't read inode self id, inode %lu, "
1658 "rc %d\n", inode->i_ino, rc);
1663 child_ino = inode->i_ino;
1664 child_gen = inode->i_generation;
1665 child_res_id.name[0] = id_fid(&sid);
1666 child_res_id.name[1] = id_group(&sid);
1670 cleanup_phase = 2; /* child dentry */
1672 /* Step 3: Lock parent and child in resource order. If child doesn't
1673 * exist, we still have to lock the parent and re-lookup. */
1674 rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1675 &parent_policy, &child_res_id, child_lockh,
1676 child_mode, &child_policy);
1680 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1681 cleanup_phase = 4; /* child lock */
1683 cleanup_phase = 3; /* parent lock */
1685 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1686 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1687 parent_mode, &child_res_id, child_lockh,
1688 dchildp, child_mode, &child_policy,
1689 name, namelen, &parent_res_id, child_ino,
1701 switch (cleanup_phase) {
1703 ldlm_lock_decref(child_lockh, child_mode);
1705 ldlm_lock_decref(parent_lockh, parent_mode);
1710 if (parent_lockh[1].cookie)
1711 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1719 void mds_reconstruct_generic(struct ptlrpc_request *req)
1721 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1722 mds_req_from_mcd(req, med->med_mcd);
1725 /* If we are unlinking an open file/dir (i.e. creating an orphan) then we
1726 * instead link the inode into the PENDING directory until it is finally
1727 * released. We can't simply call mds_reint_rename() or some part thereof,
1728 * because we don't have the inode to check for link count/open status until
1729 * after it is locked.
1731 * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1732 * before starting journal transaction.
1734 * returns 1 on success
1735 * returns 0 if we lost a race and didn't make a new link
1736 * returns negative on error
1738 static int mds_orphan_add_link(struct mds_update_record *rec,
1739 struct obd_device *obd, struct dentry *dentry)
1741 struct mds_obd *mds = &obd->u.mds;
1742 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1743 struct inode *inode = dentry->d_inode;
1744 struct dentry *pending_child;
1745 char idname[LL_ID_NAMELEN];
1746 int idlen = 0, rc, mode;
1749 LASSERT(inode != NULL);
1750 LASSERT(!mds_inode_is_orphan(inode));
1751 #ifndef HAVE_I_ALLOC_SEM
1752 LASSERT(down_trylock(&inode->i_sem) != 0);
1754 LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1756 idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1758 CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1759 mds_orphan_open_count(inode), inode->i_nlink,
1760 S_ISDIR(inode->i_mode) ? "dir" :
1761 S_ISREG(inode->i_mode) ? "file" : "other",
1762 rec->ur_name, idname);
1764 if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1767 pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1768 if (IS_ERR(pending_child))
1769 RETURN(PTR_ERR(pending_child));
1771 if (pending_child->d_inode != NULL) {
1772 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1773 LASSERT(pending_child->d_inode == inode);
1774 GOTO(out_dput, rc = 0);
1778 * link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG for
1779 * linking and return real mode back then -bzzz
1781 mode = inode->i_mode;
1782 inode->i_mode = S_IFREG;
1783 rc = vfs_link(dentry, pending_dir, pending_child);
1785 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1788 mds_inode_set_orphan(inode);
1790 /* return mode and correct i_nlink if inode is directory */
1791 inode->i_mode = mode;
1792 LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1793 S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1795 if (S_ISDIR(mode)) {
1797 i_nlink_inc(pending_dir);
1798 mark_inode_dirty(inode);
1799 mark_inode_dirty(pending_dir);
1802 GOTO(out_dput, rc = 1);
1804 l_dput(pending_child);
1808 int mds_create_local_dentry(struct mds_update_record *rec,
1809 struct obd_device *obd)
1811 struct mds_obd *mds = &obd->u.mds;
1812 struct inode *id_dir = mds->mds_id_dir->d_inode;
1813 int idlen = 0, rc, cleanup_phase = 0;
1814 struct dentry *new_child = NULL;
1815 char *idname = rec->ur_name;
1816 struct dentry *child = NULL;
1817 struct lustre_handle lockh[2] = {{0}, {0}};
1818 struct lustre_id sid;
1822 down(&id_dir->i_sem);
1823 idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1824 id_gen(rec->ur_id1));
1826 CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1827 idname, OLID4(rec->ur_id1));
1829 new_child = ll_lookup_one_len(idname, mds->mds_id_dir,
1832 if (IS_ERR(new_child)) {
1833 CERROR("can't lookup %s: %d\n", idname,
1834 (int) PTR_ERR(new_child));
1835 GOTO(cleanup, rc = PTR_ERR(new_child));
1839 down(&id_dir->i_sem);
1840 rc = mds_read_inode_sid(obd, id_dir, &sid);
1843 CERROR("Can't read inode self id, inode %lu, "
1844 "rc %d\n", id_dir->i_ino, rc);
1848 if (new_child->d_inode != NULL) {
1849 /* nice. we've already have local dentry! */
1850 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1851 (unsigned)new_child->d_inode->i_ino,
1852 (unsigned)new_child->d_inode->i_generation);
1854 id_ino(rec->ur_id1) = id_dir->i_ino;
1855 id_gen(rec->ur_id1) = id_dir->i_generation;
1856 rec->ur_namelen = idlen + 1;
1858 id_fid(rec->ur_id1) = id_fid(&sid);
1859 id_group(rec->ur_id1) = id_group(&sid);
1861 GOTO(cleanup, rc = 0);
1864 /* new, local dentry will be added soon. we need no aliases here */
1867 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1868 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1870 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1871 LCK_EX, lockh, NULL, NULL, 0,
1872 MDS_INODELOCK_UPDATE);
1875 if (IS_ERR(child)) {
1876 rc = PTR_ERR(child);
1877 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1878 CERROR("can't get victim: %d\n", rc);
1883 handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
1885 GOTO(cleanup, rc = PTR_ERR(handle));
1887 rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
1888 idlen, id_ino(rec->ur_id1),
1889 id_gen(rec->ur_id1), mds->mds_num,
1890 id_fid(rec->ur_id1));
1892 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1893 (unsigned long)child->d_inode->i_ino,
1894 (unsigned long)child->d_inode->i_generation, rc);
1896 if (S_ISDIR(child->d_inode->i_mode)) {
1897 i_nlink_inc(id_dir);
1898 mark_inode_dirty(id_dir);
1900 mark_inode_dirty(child->d_inode);
1902 fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
1904 id_ino(rec->ur_id1) = id_dir->i_ino;
1905 id_gen(rec->ur_id1) = id_dir->i_generation;
1906 rec->ur_namelen = idlen + 1;
1908 id_fid(rec->ur_id1) = id_fid(&sid);
1909 id_group(rec->ur_id1) = id_group(&sid);
1913 switch(cleanup_phase) {
1915 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1916 ldlm_lock_decref(lockh, LCK_EX);
1926 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1927 struct ptlrpc_request *slave)
1929 void *cookie, *cookie2;
1930 struct mds_body *body2;
1931 struct mds_body *body;
1935 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1936 LASSERT(body != NULL);
1938 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1939 LASSERT(body2 != NULL);
1941 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
1944 memcpy(body2, body, sizeof(*body));
1945 body2->valid &= ~OBD_MD_FLCOOKIE;
1947 if (!(body->valid & OBD_MD_FLEASIZE) &&
1948 !(body->valid & OBD_MD_FLDIREA))
1951 if (body->eadatasize == 0) {
1952 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1956 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1958 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1959 LASSERT(ea != NULL);
1961 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1962 LASSERT(ea2 != NULL);
1964 memcpy(ea2, ea, body->eadatasize);
1966 if (body->valid & OBD_MD_FLCOOKIE) {
1967 LASSERT(master->rq_repmsg->buflens[2] >=
1968 slave->rq_repmsg->buflens[2]);
1969 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1970 slave->rq_repmsg->buflens[2]);
1971 LASSERT(cookie != NULL);
1973 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1974 master->rq_repmsg->buflens[2]);
1975 LASSERT(cookie2 != NULL);
1976 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1977 body2->valid |= OBD_MD_FLCOOKIE;
1982 static int mds_reint_unlink_remote(struct mds_update_record *rec,
1983 int offset, struct ptlrpc_request *req,
1984 struct lustre_handle *parent_lockh,
1985 int update_mode, struct dentry *dparent,
1986 struct lustre_handle *child_lockh,
1987 struct dentry *dchild)
1989 struct obd_device *obd = req->rq_export->exp_obd;
1990 struct mds_obd *mds = mds_req2mds(req);
1991 struct ptlrpc_request *request = NULL;
1992 int rc = 0, cleanup_phase = 0;
1993 struct mdc_op_data *op_data;
1997 LASSERT(offset == 1 || offset == 3);
1999 /* time to drop i_nlink on remote MDS */
2000 OBD_ALLOC(op_data, sizeof(*op_data));
2001 if (op_data == NULL)
2004 memset(op_data, 0, sizeof(*op_data));
2005 mds_pack_dentry2id(obd, &op_data->id1, dchild, 1);
2006 op_data->create_mode = rec->ur_mode;
2008 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode "DLID4")",
2009 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
2011 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2012 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode "DLID4")",
2013 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
2016 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
2017 op_data->create_mode |= MDS_MODE_REPLAY;
2019 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2020 OBD_FREE(op_data, sizeof(*op_data));
2025 mds_copy_unlink_reply(req, request);
2026 ptlrpc_req_finished(request);
2030 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2033 GOTO(cleanup, rc = PTR_ERR(handle));
2034 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
2035 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
2040 req->rq_status = rc;
2043 if (parent_lockh[1].cookie != 0)
2044 ldlm_lock_decref(parent_lockh + 1, update_mode);
2046 ldlm_lock_decref(child_lockh, LCK_EX);
2048 ldlm_lock_decref(parent_lockh, LCK_PW);
2050 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2057 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
2058 struct ptlrpc_request *req,
2059 struct lustre_handle *lh)
2061 struct dentry *dparent = NULL, *dchild;
2062 struct mds_obd *mds = mds_req2mds(req);
2063 struct obd_device *obd = req->rq_export->exp_obd;
2064 struct mds_body *body = NULL;
2065 struct inode *child_inode = NULL;
2066 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2067 struct lustre_handle child_lockh = {0};
2068 struct lustre_handle child_reuse_lockh = {0};
2069 struct lustre_handle *slave_lockh = NULL;
2070 char idname[LL_ID_NAMELEN];
2071 struct llog_create_locks *lcl = NULL;
2072 void *handle = NULL;
2073 int rc = 0, cleanup_phase = 0;
2074 int unlink_by_id = 0;
2078 LASSERT(offset == 1 || offset == 3);
2080 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
2081 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2084 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2086 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2087 DEBUG_REQ(D_HA, req, "unlink replay");
2088 LASSERT(offset == 1); /* should not come from intent */
2089 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
2090 lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
2091 req->rq_repmsg->buflens[2]);
2094 MD_COUNTER_INCREMENT(obd, unlink);
2096 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
2097 GOTO(cleanup, rc = -ENOENT);
2099 if (rec->ur_namelen == 1) {
2100 /* this is request to drop i_nlink on local inode */
2102 rec->ur_name = idname;
2103 rc = mds_create_local_dentry(rec, obd);
2104 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
2105 DEBUG_REQ(D_HA, req,
2106 "drop nlink on inode "DLID4" (replay)",
2107 OLID4(rec->ur_id1));
2113 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
2114 /* master mds for directory asks slave removing inode is already
2116 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
2117 LCK_PW, parent_lockh,
2118 &update_mode, rec->ur_name,
2120 MDS_INODELOCK_UPDATE);
2121 if (IS_ERR(dparent))
2122 GOTO(cleanup, rc = PTR_ERR(dparent));
2123 dchild = ll_lookup_one_len(rec->ur_name, dparent,
2124 rec->ur_namelen - 1);
2126 GOTO(cleanup, rc = PTR_ERR(dchild));
2127 child_lockh.cookie = 0;
2128 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
2129 LASSERT(dchild->d_inode != NULL);
2130 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
2132 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
2133 parent_lockh, &dparent,
2134 LCK_PW, MDS_INODELOCK_UPDATE,
2135 &update_mode, rec->ur_name,
2136 rec->ur_namelen, &child_lockh,
2138 MDS_INODELOCK_LOOKUP |
2139 MDS_INODELOCK_UPDATE);
2144 if (dchild->d_flags & DCACHE_CROSS_REF) {
2145 /* we should have parent lock only here */
2146 LASSERT(unlink_by_id == 0);
2147 LASSERT(dchild->d_mdsnum != mds->mds_num);
2148 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
2149 update_mode, dparent, &child_lockh, dchild);
2153 cleanup_phase = 1; /* dchild, dparent, locks */
2156 child_inode = dchild->d_inode;
2157 if (child_inode == NULL) {
2158 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
2159 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
2160 GOTO(cleanup, rc = -ENOENT);
2163 cleanup_phase = 2; /* dchild has a lock */
2165 /* We have to do these checks ourselves, in case we are making an
2166 * orphan. The client tells us whether rmdir() or unlink() was called,
2167 * so we need to return appropriate errors (bug 72).
2169 * We don't have to check permissions, because vfs_rename (called from
2170 * mds_open_unlink_rename) also calls may_delete. */
2171 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
2172 if (!S_ISDIR(child_inode->i_mode))
2173 GOTO(cleanup, rc = -ENOTDIR);
2175 if (S_ISDIR(child_inode->i_mode))
2176 GOTO(cleanup, rc = -EISDIR);
2179 /* handle splitted dir */
2180 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
2184 /* Step 4: Get a lock on the ino to sync with creation WRT inode
2185 * reuse (see bug 2029). */
2186 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
2189 cleanup_phase = 3; /* child inum lock */
2191 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2193 /* ldlm_reply in buf[0] if called via intent */
2199 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2200 LASSERT(body != NULL);
2202 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2203 DOWN_READ_I_ALLOC_SEM(child_inode);
2204 cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2206 /* If this is potentially the last reference to this inode, get the
2207 * OBD EA data first so the client can destroy OST objects. We
2208 * only do the object removal later if no open files/links remain. */
2209 if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2210 child_inode->i_nlink == 1) {
2211 if (mds_orphan_open_count(child_inode) > 0) {
2212 /* need to lock pending_dir before transaction */
2213 down(&mds->mds_pending_dir->d_inode->i_sem);
2214 cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2215 } else if (S_ISREG(child_inode->i_mode)) {
2216 mds_pack_inode2body(obd, body, child_inode, 0);
2217 mds_pack_md(obd, req->rq_repmsg, offset + 1,
2218 body, child_inode, MDS_PACK_MD_LOCK, 0);
2222 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2223 switch (child_inode->i_mode & S_IFMT) {
2225 /* Drop any lingering child directories before we start our
2226 * transaction, to avoid doing multiple inode dirty/delete
2227 * in our compound transaction (bug 1321). */
2228 shrink_dcache_parent(dchild);
2229 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2232 GOTO(cleanup, rc = PTR_ERR(handle));
2233 rc = vfs_rmdir(dparent->d_inode, dchild);
2236 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2237 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2239 handle = fsfilt_start_log(obd, dparent->d_inode,
2240 FSFILT_OP_UNLINK, NULL,
2241 le32_to_cpu(lmm->lmm_stripe_count));
2243 GOTO(cleanup, rc = PTR_ERR(handle));
2244 rc = vfs_unlink(dparent->d_inode, dchild);
2252 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2255 GOTO(cleanup, rc = PTR_ERR(handle));
2256 rc = vfs_unlink(dparent->d_inode, dchild);
2259 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2262 GOTO(cleanup, rc = -EINVAL);
2265 if (rc == 0 && child_inode->i_nlink == 0) {
2266 if (mds_orphan_open_count(child_inode) > 0)
2267 rc = mds_orphan_add_link(rec, obd, dchild);
2270 GOTO(cleanup, rc = 0);
2272 if (!S_ISREG(child_inode->i_mode))
2275 if (!(body->valid & OBD_MD_FLEASIZE)) {
2276 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2277 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2278 } else if (mds_log_op_unlink(obd, child_inode,
2279 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2280 req->rq_repmsg->buflens[offset + 1],
2281 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2282 req->rq_repmsg->buflens[offset + 2],
2284 body->valid |= OBD_MD_FLCOOKIE;
2287 rc = mds_destroy_object(obd, child_inode, 1);
2289 CERROR("can't remove OST object, err %d\n",
2301 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2302 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2303 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2305 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2307 CERROR("error on parent setattr: rc = %d\n", err);
2309 rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2310 handle, req, rc, 0);
2312 (void)obd_set_info(mds->mds_dt_exp, strlen("unlinked"),
2313 "unlinked", 0, NULL);
2314 switch(cleanup_phase) {
2315 case 5: /* pending_dir semaphore */
2316 up(&mds->mds_pending_dir->d_inode->i_sem);
2317 case 4: /* child inode semaphore */
2318 UP_READ_I_ALLOC_SEM(child_inode);
2319 /* handle splitted dir */
2321 /* master directory can be non-empty or something else ... */
2322 mds_unlink_slave_objs(obd, dchild);
2325 ptlrpc_save_llog_lock(req, lcl);
2326 case 3: /* child ino-reuse lock */
2327 if (rc && body != NULL) {
2328 // Don't unlink the OST objects if the MDS unlink failed
2332 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2334 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2335 case 2: /* child lock */
2336 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2337 if (child_lockh.cookie)
2338 ldlm_lock_decref(&child_lockh, LCK_EX);
2339 case 1: /* child and parent dentry, parent lock */
2341 if (parent_lockh[1].cookie != 0)
2342 ldlm_lock_decref(parent_lockh + 1, update_mode);
2345 ldlm_lock_decref(parent_lockh, LCK_PW);
2347 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2354 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2357 req->rq_status = rc;
2362 * to service requests from remote MDS to increment i_nlink
2364 static int mds_reint_link_acquire(struct mds_update_record *rec,
2365 int offset, struct ptlrpc_request *req,
2366 struct lustre_handle *lh)
2368 struct obd_device *obd = req->rq_export->exp_obd;
2369 struct ldlm_res_id src_res_id = { .name = {0} };
2370 struct lustre_handle *handle = NULL, src_lockh = {0};
2371 struct mds_obd *mds = mds_req2mds(req);
2372 int rc = 0, cleanup_phase = 0;
2373 struct dentry *de_src = NULL;
2374 ldlm_policy_data_t policy;
2375 int flags = LDLM_FL_ATOMIC_CB;
2378 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2379 obd->obd_name, OLID4(rec->ur_id1));
2381 /* Step 1: Lookup the source inode and target directory by ID */
2382 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2384 GOTO(cleanup, rc = PTR_ERR(de_src));
2385 cleanup_phase = 1; /* source dentry */
2387 src_res_id.name[0] = id_fid(rec->ur_id1);
2388 src_res_id.name[1] = id_group(rec->ur_id1);
2389 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2391 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2392 src_res_id, LDLM_IBITS, &policy,
2393 LCK_EX, &flags, mds_blocking_ast,
2394 ldlm_completion_ast, NULL, NULL,
2395 NULL, 0, NULL, &src_lockh);
2397 GOTO(cleanup, rc = -ENOLCK);
2398 cleanup_phase = 2; /* lock */
2400 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2402 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2403 if (IS_ERR(handle)) {
2404 rc = PTR_ERR(handle);
2407 i_nlink_inc(de_src->d_inode);
2408 mark_inode_dirty(de_src->d_inode);
2412 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2413 handle, req, rc, 0);
2414 switch (cleanup_phase) {
2417 ldlm_lock_decref(&src_lockh, LCK_EX);
2419 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2425 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2428 req->rq_status = rc;
2433 * request to link to foreign inode:
2434 * - acquire i_nlinks on this inode
2437 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2438 int offset, struct ptlrpc_request *req,
2439 struct lustre_handle *lh)
2441 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2442 struct obd_device *obd = req->rq_export->exp_obd;
2443 struct dentry *de_tgt_dir = NULL;
2444 struct mds_obd *mds = mds_req2mds(req);
2445 int rc = 0, cleanup_phase = 0;
2446 struct mdc_op_data *op_data;
2447 struct ptlrpc_request *request = NULL;
2451 DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2452 ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2453 OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2454 OLID4(rec->ur_id1));
2456 de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2457 tgt_dir_lockh, &update_mode,
2458 rec->ur_name, rec->ur_namelen - 1,
2459 MDS_INODELOCK_UPDATE);
2460 if (IS_ERR(de_tgt_dir))
2461 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2464 OBD_ALLOC(op_data, sizeof(*op_data));
2465 if (op_data == NULL)
2466 GOTO(cleanup, rc = -ENOMEM);
2468 memset(op_data, 0, sizeof(*op_data));
2469 op_data->id1 = *(rec->ur_id1);
2470 rc = md_link(mds->mds_md_exp, op_data, &request);
2471 OBD_FREE(op_data, sizeof(*op_data));
2474 ptlrpc_req_finished(request);
2480 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2482 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2483 if (IS_ERR(handle)) {
2484 rc = PTR_ERR(handle);
2490 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2491 rec->ur_namelen - 1, id_ino(rec->ur_id1),
2492 id_gen(rec->ur_id1), id_group(rec->ur_id1),
2493 id_fid(rec->ur_id1));
2496 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2497 handle, req, rc, 0);
2499 switch (cleanup_phase) {
2502 OBD_ALLOC(op_data, sizeof(*op_data));
2503 if (op_data != NULL) {
2505 memset(op_data, 0, sizeof(*op_data));
2507 op_data->id1 = *(rec->ur_id1);
2508 op_data->create_mode = rec->ur_mode;
2510 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2511 OBD_FREE(op_data, sizeof(*op_data));
2513 ptlrpc_req_finished(request);
2515 CERROR("error %d while dropping i_nlink on "
2516 "remote inode\n", rc);
2519 CERROR("rc %d prevented dropping i_nlink on "
2520 "remote inode\n", -ENOMEM);
2526 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2528 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2531 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2533 ptlrpc_save_lock(req, tgt_dir_lockh + 1, update_mode);
2539 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2542 req->rq_status = rc;
2546 static int mds_reint_link(struct mds_update_record *rec, int offset,
2547 struct ptlrpc_request *req,
2548 struct lustre_handle *lh)
2550 struct obd_device *obd = req->rq_export->exp_obd;
2551 struct dentry *de_src = NULL;
2552 struct dentry *de_tgt_dir = NULL;
2553 struct dentry *dchild = NULL;
2554 struct mds_obd *mds = mds_req2mds(req);
2555 struct lustre_handle *handle = NULL;
2556 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2557 struct ldlm_res_id src_res_id = { .name = {0} };
2558 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2559 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2560 ldlm_policy_data_t tgt_dir_policy =
2561 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2562 int rc = 0, cleanup_phase = 0;
2564 int update_mode = 0;
2568 LASSERT(offset == 1);
2570 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2571 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2572 id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2575 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2576 MD_COUNTER_INCREMENT(obd, link);
2578 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2579 GOTO(cleanup, rc = -ENOENT);
2581 if (id_group(rec->ur_id1) != mds->mds_num) {
2582 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2586 if (rec->ur_namelen == 1) {
2587 rc = mds_reint_link_acquire(rec, offset, req, lh);
2591 /* Step 1: Lookup the source inode and target directory by ID */
2592 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2594 GOTO(cleanup, rc = PTR_ERR(de_src));
2596 cleanup_phase = 1; /* source dentry */
2598 de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2599 if (IS_ERR(de_tgt_dir)) {
2600 rc = PTR_ERR(de_tgt_dir);
2605 cleanup_phase = 2; /* target directory dentry */
2607 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2608 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2609 rec->ur_name, de_src->d_inode->i_ino);
2611 /* Step 2: Take the two locks */
2612 src_res_id.name[0] = id_fid(rec->ur_id1);
2613 src_res_id.name[1] = id_group(rec->ur_id1);
2614 tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2615 tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2618 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2619 int flags = LDLM_FL_ATOMIC_CB;
2620 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2622 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2623 tgt_dir_res_id, LDLM_IBITS,
2624 &src_policy, update_mode, &flags,
2626 ldlm_completion_ast, NULL, NULL,
2627 NULL, 0, NULL, tgt_dir_lockh + 1);
2629 GOTO(cleanup, rc = -ENOLCK);
2632 tgt_dir_res_id.name[2] = full_name_hash((unsigned char *)rec->ur_name,
2633 rec->ur_namelen - 1);
2634 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2635 (unsigned long)id_fid(rec->ur_id2),
2636 (unsigned long)id_group(rec->ur_id2),
2637 tgt_dir_res_id.name[2]);
2640 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2641 &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2642 LCK_EX, &tgt_dir_policy);
2646 cleanup_phase = 3; /* locks */
2648 /* Step 3: Lookup the child */
2649 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir,
2650 rec->ur_namelen - 1);
2651 if (IS_ERR(dchild)) {
2652 rc = PTR_ERR(dchild);
2653 if (rc != -EPERM && rc != -EACCES)
2654 CERROR("child lookup error %d\n", rc);
2658 cleanup_phase = 4; /* child dentry */
2660 if (dchild->d_inode) {
2661 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2662 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2667 /* Step 4: Do it. */
2668 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2670 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2671 if (IS_ERR(handle)) {
2672 rc = PTR_ERR(handle);
2676 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2677 if (rc && rc != -EPERM && rc != -EACCES)
2678 CERROR("vfs_link error %d\n", rc);
2680 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2681 handle, req, rc, 0);
2684 switch (cleanup_phase) {
2685 case 4: /* child dentry */
2689 ldlm_lock_decref(&src_lockh, LCK_EX);
2690 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2692 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2693 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2695 case 2: /* target dentry */
2697 if (tgt_dir_lockh[1].cookie && update_mode)
2698 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2702 case 1: /* source dentry */
2707 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2710 req->rq_status = rc;
2714 /* The idea here is that we need to get four locks in the end:
2715 * one on each parent directory, one on each child. We need to take
2716 * these locks in some kind of order (to avoid deadlocks), and the order
2717 * I selected is "increasing resource number" order. We need to look up
2718 * the children, however, before we know what the resource number(s) are.
2719 * Thus the following plan:
2721 * 1,2. Look up the parents
2722 * 3,4. Look up the children
2723 * 5. Take locks on the parents and children, in order
2724 * 6. Verify that the children haven't changed since they were looked up
2726 * If there was a race and the children changed since they were first looked
2727 * up, it is possible that mds_verify_child() will be able to just grab the
2728 * lock on the new child resource (if it has a higher resource than any other)
2729 * but we need to compare against not only its parent, but also against the
2730 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2732 * We need the fancy igrab() on the child inodes because we aren't holding a
2733 * lock on the parent after the lookup is done, so dentry->d_inode may change
2734 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2736 static int mds_get_parents_children_locked(struct obd_device *obd,
2737 struct mds_obd *mds,
2738 struct lustre_id *p1_id,
2739 struct dentry **de_srcdirp,
2740 struct lustre_id *p2_id,
2741 struct dentry **de_tgtdirp,
2743 const char *old_name, int old_len,
2744 struct dentry **de_oldp,
2745 const char *new_name, int new_len,
2746 struct dentry **de_newp,
2747 struct lustre_handle *dlm_handles,
2750 struct ldlm_res_id p1_res_id = { .name = {0} };
2751 struct ldlm_res_id p2_res_id = { .name = {0} };
2752 struct ldlm_res_id c1_res_id = { .name = {0} };
2753 struct ldlm_res_id c2_res_id = { .name = {0} };
2754 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2755 /* Only dentry should disappear, but the inode itself would be
2756 intact otherwise. */
2757 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2758 /* If something is going to be replaced, both dentry and inode locks are
2760 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_FULL}};
2761 struct ldlm_res_id *maxres_src, *maxres_tgt;
2762 struct inode *inode;
2763 int rc = 0, cleanup_phase = 0;
2764 __u32 child_gen1 = 0;
2765 __u32 child_gen2 = 0;
2766 unsigned long child_ino1 = 0;
2767 unsigned long child_ino2 = 0;
2770 /* Step 1: Lookup the source directory */
2771 *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2772 if (IS_ERR(*de_srcdirp))
2773 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2775 cleanup_phase = 1; /* source directory dentry */
2777 p1_res_id.name[0] = id_fid(p1_id);
2778 p1_res_id.name[1] = id_group(p1_id);
2780 /* Step 2: Lookup the target directory */
2781 if (id_equal_stc(p1_id, p2_id)) {
2782 *de_tgtdirp = dget(*de_srcdirp);
2784 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2785 if (IS_ERR(*de_tgtdirp)) {
2786 rc = PTR_ERR(*de_tgtdirp);
2792 cleanup_phase = 2; /* target directory dentry */
2794 p2_res_id.name[0] = id_fid(p2_id);
2795 p2_res_id.name[1] = id_group(p2_id);
2798 dlm_handles[5].cookie = 0;
2799 dlm_handles[6].cookie = 0;
2801 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2803 * get a temp lock on just fid, group to flush client cache and
2804 * to protect dirs from concurrent splitting.
2806 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2807 LCK_PW, &p_policy, &p2_res_id,
2808 &dlm_handles[6], LCK_PW, &p_policy);
2812 p1_res_id.name[2] = full_name_hash((unsigned char *)old_name,
2814 p2_res_id.name[2] = full_name_hash((unsigned char *)new_name,
2817 CDEBUG(D_INFO, "take locks on "
2818 LPX64":"LPX64":"LPX64", "LPX64":"LPX64":"LPX64"\n",
2819 p1_res_id.name[0], p1_res_id.name[1], p1_res_id.name[2],
2820 p2_res_id.name[0], p2_res_id.name[1], p2_res_id.name[2]);
2825 /* Step 3: Lookup the source child entry */
2826 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp,
2828 if (IS_ERR(*de_oldp)) {
2829 rc = PTR_ERR(*de_oldp);
2830 CERROR("old child lookup error (%.*s): %d\n",
2831 old_len - 1, old_name, rc);
2835 cleanup_phase = 4; /* original name dentry */
2837 inode = (*de_oldp)->d_inode;
2838 if (inode != NULL) {
2839 struct lustre_id sid;
2841 inode = igrab(inode);
2843 GOTO(cleanup, rc = -ENOENT);
2845 down(&inode->i_sem);
2846 rc = mds_read_inode_sid(obd, inode, &sid);
2849 CERROR("Can't read inode self id, inode %lu, "
2850 "rc %d\n", inode->i_ino, rc);
2855 child_ino1 = inode->i_ino;
2856 child_gen1 = inode->i_generation;
2857 c1_res_id.name[0] = id_fid(&sid);
2858 c1_res_id.name[1] = id_group(&sid);
2860 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2861 child_ino1 = (*de_oldp)->d_inum;
2862 child_gen1 = (*de_oldp)->d_generation;
2863 c1_res_id.name[0] = (*de_oldp)->d_fid;
2864 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2866 GOTO(cleanup, rc = -ENOENT);
2869 /* Step 4: Lookup the target child entry */
2870 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp,
2872 if (IS_ERR(*de_newp)) {
2873 rc = PTR_ERR(*de_newp);
2874 CERROR("new child lookup error (%.*s): %d\n",
2875 old_len - 1, old_name, rc);
2879 cleanup_phase = 5; /* target dentry */
2881 inode = (*de_newp)->d_inode;
2882 if (inode != NULL) {
2883 struct lustre_id sid;
2885 inode = igrab(inode);
2889 down(&inode->i_sem);
2890 rc = mds_read_inode_sid(obd, inode, &sid);
2893 CERROR("Can't read inode self id, inode %lu, "
2894 "rc %d\n", inode->i_ino, rc);
2898 child_ino2 = inode->i_ino;
2899 child_gen2 = inode->i_generation;
2900 c2_res_id.name[0] = id_fid(&sid);
2901 c2_res_id.name[1] = id_group(&sid);
2903 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2904 child_ino2 = (*de_newp)->d_inum;
2905 child_gen2 = (*de_newp)->d_generation;
2906 c2_res_id.name[0] = (*de_newp)->d_fid;
2907 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
2911 /* Step 5: Take locks on the parents and child(ren) */
2912 maxres_src = &p1_res_id;
2913 maxres_tgt = &p2_res_id;
2914 cleanup_phase = 5; /* target dentry */
2916 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
2917 maxres_src = &c1_res_id;
2918 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
2919 maxres_tgt = &c2_res_id;
2921 rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
2923 &p2_res_id, &dlm_handles[1], parent_mode,
2925 &c1_res_id, &dlm_handles[2], child_mode,
2927 &c2_res_id, &dlm_handles[3], child_mode,
2932 cleanup_phase = 6; /* parent and child(ren) locks */
2934 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2935 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2936 parent_mode, &c1_res_id, &dlm_handles[2],
2937 de_oldp, child_mode, &c1_policy, old_name, old_len,
2938 maxres_tgt, child_ino1, child_gen1);
2940 if (c2_res_id.name[0] != 0)
2941 ldlm_lock_decref(&dlm_handles[3], child_mode);
2942 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2949 if (!DENTRY_VALID(*de_oldp))
2950 GOTO(cleanup, rc = -ENOENT);
2952 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2953 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2954 parent_mode, &c2_res_id, &dlm_handles[3],
2955 de_newp, child_mode, &c2_policy, new_name,
2956 new_len, maxres_src, child_ino2, child_gen2);
2958 ldlm_lock_decref(&dlm_handles[2], child_mode);
2959 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2969 switch (cleanup_phase) {
2970 case 6: /* child lock(s) */
2971 if (c2_res_id.name[0] != 0)
2972 ldlm_lock_decref(&dlm_handles[3], child_mode);
2973 if (c1_res_id.name[0] != 0)
2974 ldlm_lock_decref(&dlm_handles[2], child_mode);
2975 if (dlm_handles[1].cookie != 0)
2976 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2977 if (dlm_handles[0].cookie != 0)
2978 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2979 case 5: /* target dentry */
2981 case 4: /* source dentry */
2985 if (dlm_handles[5].cookie != 0)
2986 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2987 if (dlm_handles[6].cookie != 0)
2988 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2990 case 2: /* target directory dentry */
2991 l_dput(*de_tgtdirp);
2992 case 1: /* source directry dentry */
2993 l_dput(*de_srcdirp);
3001 * checks if dentry can be removed. This function also handles cross-ref
3004 static int mds_check_for_rename(struct obd_device *obd,
3005 struct dentry *dentry)
3007 struct mds_obd *mds = &obd->u.mds;
3008 struct lustre_handle *rlockh;
3009 struct ptlrpc_request *req;
3010 struct mdc_op_data *op_data;
3011 struct lookup_intent it;
3012 int handle_size, rc = 0;
3015 LASSERT(dentry != NULL);
3017 if (dentry->d_inode) {
3018 if (S_ISDIR(dentry->d_inode->i_mode) &&
3019 !mds_is_dir_empty(obd, dentry))
3022 LASSERT((dentry->d_flags & DCACHE_CROSS_REF));
3023 handle_size = sizeof(struct lustre_handle);
3025 OBD_ALLOC(rlockh, handle_size);
3029 memset(rlockh, 0, handle_size);
3030 OBD_ALLOC(op_data, sizeof(*op_data));
3031 if (op_data == NULL) {
3032 OBD_FREE(rlockh, handle_size);
3035 memset(op_data, 0, sizeof(*op_data));
3036 mds_pack_dentry2id(obd, &op_data->id1, dentry, 1);
3038 it.it_op = IT_UNLINK;
3039 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
3042 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
3043 op_data, rlockh, NULL, 0, ldlm_completion_ast,
3044 mds_blocking_ast, NULL);
3045 OBD_FREE(op_data, sizeof(*op_data));
3049 OBD_FREE(it.d.fs_data,
3050 sizeof(struct lustre_intent_data));
3053 if (rlockh->cookie != 0)
3054 ldlm_lock_decref(rlockh, LCK_EX);
3056 if (LUSTRE_IT(&it)->it_data) {
3057 req = (struct ptlrpc_request *)LUSTRE_IT(&it)->it_data;
3058 ptlrpc_req_finished(req);
3061 if (LUSTRE_IT(&it)->it_status)
3062 rc = LUSTRE_IT(&it)->it_status;
3063 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
3064 OBD_FREE(rlockh, handle_size);
3069 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
3070 struct ptlrpc_request *req, struct lustre_id *id,
3071 struct dentry *de_dir, struct dentry *de)
3073 struct obd_device *obd = req->rq_export->exp_obd;
3074 struct mds_obd *mds = mds_req2mds(req);
3075 void *handle = NULL;
3081 * name exists and points to local inode try to unlink this name
3082 * and create new one.
3084 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
3085 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
3086 (unsigned long)de->d_inode->i_generation);
3088 /* checking if we can remove local dentry. */
3089 rc = mds_check_for_rename(obd, de);
3093 handle = fsfilt_start(obd, de_dir->d_inode,
3094 FSFILT_OP_RENAME, NULL);
3096 GOTO(cleanup, rc = PTR_ERR(handle));
3097 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3100 } else if (de->d_flags & DCACHE_CROSS_REF) {
3101 CDEBUG(D_OTHER, "%s: %s points to remote inode %lu/%lu\n",
3102 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_mdsnum,
3103 (unsigned long)de->d_fid);
3105 /* checking if we can remove local dentry. */
3106 rc = mds_check_for_rename(obd, de);
3111 * to be fully POSIX compatible, we should add one more check:
3113 * if de_new is subdir of dir rec->ur_id1. If so - return
3116 * I do not know how to implement it right now, because
3117 * inodes/dentries for new and old names lie on different MDS,
3118 * so add this notice here just to make it visible for the rest
3119 * of developers and do not forget about. And when this check
3120 * will be added, del_cross_ref should gone, that is local
3121 * dentry is able to be removed if all checks passed. --umka
3124 handle = fsfilt_start(obd, de_dir->d_inode,
3125 FSFILT_OP_RENAME, NULL);
3127 GOTO(cleanup, rc = PTR_ERR(handle));
3128 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3132 /* name doesn't exist. the simplest case. */
3133 handle = fsfilt_start(obd, de_dir->d_inode,
3134 FSFILT_OP_LINK, NULL);
3136 GOTO(cleanup, rc = PTR_ERR(handle));
3139 rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
3140 rec->ur_tgtlen - 1, id_ino(id),
3141 id_gen(id), id_group(id), id_fid(id));
3143 CERROR("add_dir_entry() returned error %d\n", rc);
3149 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3150 handle, req, rc, 0);
3155 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
3156 struct ptlrpc_request *req, struct dentry *de_dir,
3159 struct obd_device *obd = req->rq_export->exp_obd;
3160 struct mds_obd *mds = mds_req2mds(req);
3161 void *handle = NULL;
3165 handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
3167 GOTO(cleanup, rc = PTR_ERR(handle));
3168 rc = fsfilt_del_dir_entry(obd, de);
3173 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3174 handle, req, rc, 0);
3178 static int mds_reint_rename_create_name(struct mds_update_record *rec,
3179 int offset, struct ptlrpc_request *req)
3181 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3182 struct obd_device *obd = req->rq_export->exp_obd;
3183 struct mds_obd *mds = mds_req2mds(req);
3184 struct lustre_handle child_lockh = {0};
3185 struct dentry *de_tgtdir = NULL;
3186 struct dentry *de_new = NULL;
3187 int cleanup_phase = 0;
3188 int update_mode, rc = 0;
3192 * another MDS executing rename operation has asked us to create target
3193 * name. such a creation should destroy existing target name.
3195 CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
3196 obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
3198 /* first, lookup the target */
3199 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
3200 &de_tgtdir, LCK_PW, MDS_INODELOCK_UPDATE,
3201 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
3202 &child_lockh, &de_new, LCK_EX,
3203 MDS_INODELOCK_LOOKUP);
3210 LASSERT(de_tgtdir->d_inode);
3213 rc = mds_add_local_dentry(rec, offset, req, rec->ur_id1,
3219 if (cleanup_phase == 1) {
3221 if (parent_lockh[1].cookie != 0)
3222 ldlm_lock_decref(parent_lockh + 1, update_mode);
3224 ldlm_lock_decref(parent_lockh, LCK_PW);
3225 if (child_lockh.cookie != 0)
3226 ldlm_lock_decref(&child_lockh, LCK_EX);
3231 req->rq_status = rc;
3235 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
3236 struct ptlrpc_request *req)
3238 struct obd_device *obd = req->rq_export->exp_obd;
3239 struct ptlrpc_request *req2 = NULL;
3240 struct dentry *de_srcdir = NULL;
3241 struct dentry *de_old = NULL;
3242 struct mds_obd *mds = mds_req2mds(req);
3243 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3244 struct lustre_handle child_lockh = {0};
3245 struct mdc_op_data *op_data;
3246 int update_mode, rc = 0;
3249 CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
3250 obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
3252 OBD_ALLOC(op_data, sizeof(*op_data));
3253 if (op_data == NULL)
3255 memset(op_data, 0, sizeof(*op_data));
3257 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
3258 &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
3259 &update_mode, rec->ur_name,
3260 rec->ur_namelen, &child_lockh, &de_old,
3261 LCK_EX, MDS_INODELOCK_LOOKUP);
3263 OBD_FREE(op_data, sizeof(*op_data));
3268 LASSERT(de_srcdir->d_inode);
3272 * we already know the target should be created on another MDS so, we
3273 * have to request that MDS to do it.
3276 /* prepare source id */
3277 if (de_old->d_flags & DCACHE_CROSS_REF) {
3278 LASSERT(de_old->d_inode == NULL);
3279 CDEBUG(D_OTHER, "request to move remote name\n");
3280 mds_pack_dentry2id(obd, &op_data->id1, de_old, 1);
3281 } else if (de_old->d_inode == NULL) {
3282 /* oh, source doesn't exist */
3283 OBD_FREE(op_data, sizeof(*op_data));
3284 GOTO(cleanup, rc = -ENOENT);
3286 struct lustre_id sid;
3287 struct inode *inode = de_old->d_inode;
3289 LASSERT(inode != NULL);
3290 CDEBUG(D_OTHER, "request to move local name\n");
3291 id_ino(&op_data->id1) = inode->i_ino;
3292 id_group(&op_data->id1) = mds->mds_num;
3293 id_gen(&op_data->id1) = inode->i_generation;
3295 down(&inode->i_sem);
3296 rc = mds_read_inode_sid(obd, inode, &sid);
3299 CERROR("Can't read inode self id, "
3300 "inode %lu, rc = %d\n",
3305 id_fid(&op_data->id1) = id_fid(&sid);
3308 op_data->id2 = *rec->ur_id2;
3309 rc = md_rename(mds->mds_md_exp, op_data, NULL, 0,
3310 rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3311 OBD_FREE(op_data, sizeof(*op_data));
3316 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3322 ptlrpc_req_finished(req2);
3325 if (parent_lockh[1].cookie != 0)
3326 ldlm_lock_decref(parent_lockh + 1, update_mode);
3328 ldlm_lock_decref(parent_lockh, LCK_PW);
3329 if (child_lockh.cookie != 0)
3330 ldlm_lock_decref(&child_lockh, LCK_EX);
3335 req->rq_status = rc;
3339 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3340 struct ptlrpc_request *req, struct lustre_handle *lockh)
3342 struct obd_device *obd = req->rq_export->exp_obd;
3343 struct dentry *de_srcdir = NULL;
3344 struct dentry *de_tgtdir = NULL;
3345 struct dentry *de_old = NULL;
3346 struct dentry *de_new = NULL;
3347 struct inode *old_inode = NULL, *new_inode = NULL;
3348 struct mds_obd *mds = mds_req2mds(req);
3349 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3350 struct mds_body *body = NULL;
3351 struct llog_create_locks *lcl = NULL;
3352 struct lov_mds_md *lmm = NULL;
3353 int rc = 0, cleanup_phase = 0;
3354 void *handle = NULL;
3357 LASSERT(offset == 1);
3359 DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3360 OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3363 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3365 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3366 DEBUG_REQ(D_HA, req, "rename replay");
3367 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3368 lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3369 req->rq_repmsg->buflens[2]);
3372 MD_COUNTER_INCREMENT(obd, rename);
3374 if (rec->ur_namelen == 1) {
3375 rc = mds_reint_rename_create_name(rec, offset, req);
3379 /* check if new name should be located on remote target. */
3380 if (id_group(rec->ur_id2) != mds->mds_num) {
3381 rc = mds_reint_rename_to_remote(rec, offset, req);
3385 rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3386 rec->ur_id2, &de_tgtdir, LCK_PW,
3387 rec->ur_name, rec->ur_namelen,
3388 &de_old, rec->ur_tgt,
3389 rec->ur_tgtlen, &de_new,
3390 dlm_handles, LCK_EX);
3394 cleanup_phase = 1; /* parent(s), children, locks */
3395 old_inode = de_old->d_inode;
3396 new_inode = de_new->d_inode;
3398 /* sanity check for src inode */
3399 if (de_old->d_flags & DCACHE_CROSS_REF) {
3400 LASSERT(de_old->d_inode == NULL);
3403 * in the case of cross-ref dir, we can perform this check only
3404 * if child and parent lie on the same mds. This is because
3405 * otherwise they can have the same inode numbers.
3407 if (de_old->d_mdsnum == mds->mds_num) {
3408 if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3409 de_old->d_inum == de_tgtdir->d_inode->i_ino)
3410 GOTO(cleanup, rc = -EINVAL);
3413 LASSERT(de_old->d_inode != NULL);
3414 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3415 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3416 GOTO(cleanup, rc = -EINVAL);
3419 /* sanity check for dest inode */
3420 if (de_new->d_flags & DCACHE_CROSS_REF) {
3421 LASSERT(new_inode == NULL);
3423 /* the same check about target dentry. */
3424 if (de_new->d_mdsnum == mds->mds_num) {
3425 if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3426 de_new->d_inum == de_tgtdir->d_inode->i_ino)
3427 GOTO(cleanup, rc = -EINVAL);
3431 * regular files usualy do not have ->rename() implemented. But
3432 * we handle only this case when @de_new is cross-ref entry,
3433 * because in other cases it will be handled by vfs_rename().
3435 if (de_old->d_inode && (!de_old->d_inode->i_op ||
3436 !de_old->d_inode->i_op->rename))
3437 GOTO(cleanup, rc = -EPERM);
3440 (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3441 new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3442 GOTO(cleanup, rc = -EINVAL);
3447 * check if inodes point to each other. This should be checked before
3448 * is_subdir() check, as for the same entries it will think that they
3451 if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3452 !(de_new->d_flags & DCACHE_CROSS_REF) &&
3453 old_inode == new_inode)
3454 GOTO(cleanup, rc = 0);
3456 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3458 * check if we are moving old entry into its child. 2.6 does not check
3459 * for this in vfs_rename() anymore.
3461 if (is_subdir(de_new, de_old))
3462 GOTO(cleanup, rc = -EINVAL);
3466 * if we are about to remove the target at first, pass the EA of that
3467 * inode to client to perform and cleanup on OST.
3469 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3470 LASSERT(body != NULL);
3472 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3474 DOWN_READ_I_ALLOC_SEM(new_inode);
3476 cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3478 if (new_inode && ((S_ISDIR(new_inode->i_mode) &&
3479 new_inode->i_nlink == 2) ||
3480 new_inode->i_nlink == 1)) {
3481 if (mds_orphan_open_count(new_inode) > 0) {
3482 /* need to lock pending_dir before transaction */
3483 down(&mds->mds_pending_dir->d_inode->i_sem);
3484 cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3485 } else if (S_ISREG(new_inode->i_mode)) {
3486 mds_pack_inode2body(obd, body, new_inode, 0);
3487 mds_pack_md(obd, req->rq_repmsg, 1, body,
3488 new_inode, MDS_PACK_MD_LOCK, 0);
3492 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3493 de_srcdir->d_inode->i_sb);
3495 if (de_old->d_flags & DCACHE_CROSS_REF) {
3496 struct lustre_id old_id;
3498 mds_pack_dentry2id(obd, &old_id, de_old, 1);
3500 rc = mds_add_local_dentry(rec, offset, req, &old_id,
3505 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3510 lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3511 handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3512 NULL, le32_to_cpu(lmm->lmm_stripe_count));
3515 GOTO(cleanup, rc = PTR_ERR(handle));
3518 de_old->d_fsdata = req;
3519 de_new->d_fsdata = req;
3520 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3523 if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3524 if (mds_orphan_open_count(new_inode) > 0)
3525 rc = mds_orphan_add_link(rec, obd, de_new);
3528 GOTO(cleanup, rc = 0);
3530 if (!S_ISREG(new_inode->i_mode))
3533 if (!(body->valid & OBD_MD_FLEASIZE)) {
3534 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3535 OBD_MD_FLATIME | OBD_MD_FLMTIME);
3536 } else if (mds_log_op_unlink(obd, new_inode,
3537 lustre_msg_buf(req->rq_repmsg,1,0),
3538 req->rq_repmsg->buflens[1],
3539 lustre_msg_buf(req->rq_repmsg,2,0),
3540 req->rq_repmsg->buflens[2],
3542 body->valid |= OBD_MD_FLCOOKIE;
3545 rc = mds_destroy_object(obd, old_inode, 1);
3547 CERROR("can't remove OST object, err %d\n",
3554 rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3555 handle, req, rc, 0);
3557 switch (cleanup_phase) {
3559 up(&mds->mds_pending_dir->d_inode->i_sem);
3562 UP_READ_I_ALLOC_SEM(new_inode);
3565 if (dlm_handles[5].cookie != 0)
3566 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3567 if (dlm_handles[6].cookie != 0)
3568 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3571 ptlrpc_save_llog_lock(req, lcl);
3574 if (dlm_handles[3].cookie != 0)
3575 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3576 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3577 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3578 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3580 if (dlm_handles[3].cookie != 0)
3581 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3582 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3583 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3584 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3593 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3596 req->rq_status = rc;
3600 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3601 struct ptlrpc_request *, struct lustre_handle *);
3603 static mds_reinter reinters[REINT_MAX + 1] = {
3604 [REINT_SETATTR] mds_reint_setattr,
3605 [REINT_CREATE] mds_reint_create,
3606 [REINT_LINK] mds_reint_link,
3607 [REINT_UNLINK] mds_reint_unlink,
3608 [REINT_RENAME] mds_reint_rename,
3609 [REINT_OPEN] mds_open
3612 int mds_reint_rec(struct mds_update_record *rec, int offset,
3613 struct ptlrpc_request *req, struct lustre_handle *lockh)
3615 struct obd_device *obd = req->rq_export->exp_obd;
3616 struct lvfs_run_ctxt saved;
3619 /* checked by unpacker */
3620 LASSERT(rec->ur_opcode <= REINT_MAX &&
3621 reinters[rec->ur_opcode] != NULL);
3623 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3624 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3625 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);