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/ext3_fs.h>
36 #include <linux/obd_support.h>
37 #include <linux/obd_class.h>
38 #include <linux/obd.h>
39 #include <linux/lustre_lib.h>
40 #include <linux/lustre_idl.h>
41 #include <linux/lustre_mds.h>
42 #include <linux/lustre_dlm.h>
43 #include <linux/lustre_log.h>
44 #include <linux/lustre_fsfilt.h>
45 #include "mds_internal.h"
47 struct mds_logcancel_data {
48 struct lov_mds_md *mlcd_lmm;
52 struct llog_cookie mlcd_cookies[0];
55 static void mds_cancel_cookies_cb(struct obd_device *obd,
56 __u64 transno, void *cb_data,
59 struct mds_logcancel_data *mlcd = cb_data;
60 struct lov_stripe_md *lsm = NULL;
61 struct llog_ctxt *ctxt;
64 obd_transno_commit_cb(obd, transno, error);
66 CDEBUG(D_HA, "cancelling %d cookies\n",
67 (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
69 rc = obd_unpackmd(obd->u.mds.mds_dt_exp, &lsm, mlcd->mlcd_lmm,
70 mlcd->mlcd_eadatalen);
72 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
73 (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
76 ///* XXX 0 normally, SENDNOW for debug */);
77 ctxt = llog_get_context(&obd->obd_llogs,
78 mlcd->mlcd_cookies[0].lgc_subsys + 1);
79 rc = llog_cancel(ctxt, mlcd->mlcd_cookielen /
80 sizeof(*mlcd->mlcd_cookies),
81 mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW, lsm);
83 CERROR("error cancelling %d log cookies: rc %d\n",
84 (int)(mlcd->mlcd_cookielen /
85 sizeof(*mlcd->mlcd_cookies)), rc);
88 OBD_FREE(mlcd, mlcd->mlcd_size);
91 /* Assumes caller has already pushed us into the kernel context. */
92 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
93 struct ptlrpc_request *req, int rc, __u32 op_data)
95 struct mds_export_data *med = &req->rq_export->exp_mds_data;
96 struct obd_device *obd = req->rq_export->exp_obd;
97 struct mds_client_data *mcd = med->med_mcd;
98 int err, log_pri = D_HA;
103 /* if the export has already been failed, we have no last_rcvd slot */
104 if (req->rq_export->exp_failed) {
105 CERROR("committing transaction for disconnected client\n");
107 GOTO(out_commit, rc);
114 if (handle == NULL) {
115 /* if we're starting our own xaction, use our own inode */
116 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
117 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
118 if (IS_ERR(handle)) {
119 CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
120 RETURN(PTR_ERR(handle));
126 transno = req->rq_reqmsg->transno;
128 LASSERTF(transno == 0, "BUG 3934, t"LPU64" rc %d\n", transno, rc);
129 } else if (transno == 0) {
130 spin_lock(&mds->mds_transno_lock);
131 transno = ++mds->mds_last_transno;
132 spin_unlock(&mds->mds_transno_lock);
134 spin_lock(&mds->mds_transno_lock);
135 if (transno > mds->mds_last_transno)
136 mds->mds_last_transno = transno;
137 spin_unlock(&mds->mds_transno_lock);
139 req->rq_repmsg->transno = req->rq_transno = transno;
140 mcd->mcd_last_transno = cpu_to_le64(transno);
141 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
142 mcd->mcd_last_result = cpu_to_le32(rc);
143 mcd->mcd_last_data = cpu_to_le32(op_data);
145 fsfilt_add_journal_cb(obd, mds->mds_sb, transno, handle,
146 mds_commit_last_transno_cb, NULL);
148 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd,
149 sizeof(*mcd), &off, 0);
157 DEBUG_REQ(log_pri, req,
158 "wrote trans #"LPU64" client %s at idx %u: err = %d",
159 transno, mcd->mcd_uuid, med->med_idx, err);
161 err = mds_update_last_fid(obd, handle, 0);
168 err = mds_dt_write_objids(obd);
174 CDEBUG(log_pri, "wrote objids: err = %d\n", err);
178 err = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
180 CERROR("error committing transaction: %d\n", err);
188 /* this gives the same functionality as the code between
189 * sys_chmod and inode_setattr
190 * chown_common and inode_setattr
191 * utimes and inode_setattr
193 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
195 time_t now = LTIME_S(CURRENT_TIME);
196 struct iattr *attr = &rec->ur_iattr;
197 unsigned int ia_valid = attr->ia_valid;
201 /* only fix up attrs if the client VFS didn't already */
202 if (!(ia_valid & ATTR_RAW))
205 if (!(ia_valid & ATTR_CTIME_SET))
206 LTIME_S(attr->ia_ctime) = now;
207 if (!(ia_valid & ATTR_ATIME_SET))
208 LTIME_S(attr->ia_atime) = now;
209 if (!(ia_valid & ATTR_MTIME_SET))
210 LTIME_S(attr->ia_mtime) = now;
212 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
216 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
217 if (rec->ur_fsuid != inode->i_uid &&
218 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
222 if (ia_valid & ATTR_SIZE) {
223 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
227 if (ia_valid & ATTR_UID) {
230 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
232 if (attr->ia_uid == (uid_t) -1)
233 attr->ia_uid = inode->i_uid;
234 if (attr->ia_gid == (gid_t) -1)
235 attr->ia_gid = inode->i_gid;
236 attr->ia_mode = inode->i_mode;
238 * If the user or group of a non-directory has been
239 * changed by a non-root user, remove the setuid bit.
240 * 19981026 David C Niemi <niemi@tux.org>
242 * Changed this to apply to all users, including root,
243 * to avoid some races. This is the behavior we had in
244 * 2.0. The check for non-root was definitely wrong
245 * for 2.2 anyway, as it should have been using
246 * CAP_FSETID rather than fsuid -- 19990830 SD.
248 if ((inode->i_mode & S_ISUID) == S_ISUID &&
249 !S_ISDIR(inode->i_mode)) {
250 attr->ia_mode &= ~S_ISUID;
251 attr->ia_valid |= ATTR_MODE;
254 * Likewise, if the user or group of a non-directory
255 * has been changed by a non-root user, remove the
256 * setgid bit UNLESS there is no group execute bit
257 * (this would be a file marked for mandatory
258 * locking). 19981026 David C Niemi <niemi@tux.org>
260 * Removed the fsuid check (see the comment above) --
263 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
264 (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
265 attr->ia_mode &= ~S_ISGID;
266 attr->ia_valid |= ATTR_MODE;
268 } else if (ia_valid & ATTR_MODE) {
269 int mode = attr->ia_mode;
271 if (attr->ia_mode == (mode_t) -1)
272 attr->ia_mode = inode->i_mode;
274 (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
279 void mds_steal_ack_locks(struct ptlrpc_request *req)
281 struct obd_export *exp = req->rq_export;
282 char str[PTL_NALFMT_SIZE];
283 struct list_head *tmp;
284 struct ptlrpc_reply_state *oldrep;
285 struct ptlrpc_service *svc;
286 struct llog_create_locks *lcl;
290 /* CAVEAT EMPTOR: spinlock order */
291 spin_lock_irqsave (&exp->exp_lock, flags);
292 list_for_each (tmp, &exp->exp_outstanding_replies) {
293 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
295 if (oldrep->rs_xid != req->rq_xid)
298 if (oldrep->rs_msg.opc != req->rq_reqmsg->opc)
299 CERROR ("Resent req xid "LPX64" has mismatched opc: "
300 "new %d old %d\n", req->rq_xid,
301 req->rq_reqmsg->opc, oldrep->rs_msg.opc);
303 svc = oldrep->rs_srv_ni->sni_service;
304 spin_lock (&svc->srv_lock);
306 list_del_init (&oldrep->rs_exp_list);
308 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
309 " o%d NID %s\n", oldrep->rs_nlocks, oldrep,
310 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg.opc,
311 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
313 for (i = 0; i < oldrep->rs_nlocks; i++)
314 ptlrpc_save_lock(req,
315 &oldrep->rs_locks[i],
316 oldrep->rs_modes[i]);
317 oldrep->rs_nlocks = 0;
319 lcl = oldrep->rs_llog_locks;
320 oldrep->rs_llog_locks = NULL;
322 ptlrpc_save_llog_lock(req, lcl);
324 DEBUG_REQ(D_HA, req, "stole locks for");
325 ptlrpc_schedule_difficult_reply (oldrep);
327 spin_unlock (&svc->srv_lock);
328 spin_unlock_irqrestore (&exp->exp_lock, flags);
331 spin_unlock_irqrestore (&exp->exp_lock, flags);
334 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
336 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
337 mcd->mcd_last_transno, mcd->mcd_last_result);
338 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
339 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
341 mds_steal_ack_locks(req);
344 static void reconstruct_reint_setattr(struct mds_update_record *rec,
345 int offset, struct ptlrpc_request *req)
347 struct mds_export_data *med = &req->rq_export->exp_mds_data;
348 struct mds_body *body;
351 mds_req_from_mcd(req, med->med_mcd);
353 de = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
355 LASSERT(PTR_ERR(de) == req->rq_status);
359 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
360 mds_pack_inode2body(req2obd(req), body, de->d_inode, 0);
362 /* Don't return OST-specific attributes if we didn't just set them */
363 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
364 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
365 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
366 body->valid |= OBD_MD_FLMTIME;
367 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
368 body->valid |= OBD_MD_FLATIME;
373 /* In the raw-setattr case, we lock the child inode.
374 * In the write-back case or if being called from open, the client holds a lock
377 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
378 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
379 struct ptlrpc_request *req, struct lustre_handle *lh)
381 struct mds_obd *mds = mds_req2mds(req);
382 struct obd_device *obd = req->rq_export->exp_obd;
383 struct mds_body *body;
385 struct inode *inode = NULL;
386 struct lustre_handle lockh[2] = {{0}, {0}};
389 struct mds_logcancel_data *mlcd = NULL;
390 int rc = 0, cleanup_phase = 0, err;
394 LASSERT(offset == 1);
396 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x",
397 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
398 rec->ur_iattr.ia_valid);
400 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
401 MD_COUNTER_INCREMENT(obd, setattr);
403 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
404 de = mds_id2dentry(obd, rec->ur_id1, NULL);
406 GOTO(cleanup, rc = PTR_ERR(de));
408 __u64 lockpart = MDS_INODELOCK_UPDATE;
409 if (rec->ur_iattr.ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
410 lockpart |= MDS_INODELOCK_LOOKUP;
411 de = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
412 lockh, &parent_mode, NULL, 0, lockpart);
414 GOTO(cleanup, rc = PTR_ERR(de));
422 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
423 rec->ur_eadata != NULL)
426 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
428 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
430 GOTO(cleanup, rc = PTR_ERR(handle));
432 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
433 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
434 LTIME_S(rec->ur_iattr.ia_mtime),
435 LTIME_S(rec->ur_iattr.ia_ctime));
436 rc = mds_fix_attr(inode, rec);
440 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
441 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
442 (long)&rec->ur_iattr.ia_attr_flags);
444 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
446 if (rc == 0 && (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
447 rec->ur_eadata != NULL) {
448 struct lov_stripe_md *lsm = NULL;
450 rc = ll_permission(inode, MAY_WRITE, NULL);
454 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, mds->mds_dt_exp,
455 0, &lsm, rec->ur_eadata);
459 obd_free_memmd(mds->mds_dt_exp, &lsm);
461 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
467 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
468 mds_pack_inode2body(obd, body, inode, 0);
469 body->id1 = *rec->ur_id1;
471 /* Don't return OST-specific attributes if we didn't just set them */
472 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
473 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
474 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
475 body->valid |= OBD_MD_FLMTIME;
476 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
477 body->valid |= OBD_MD_FLATIME;
479 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_dt_obd)) {
480 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
483 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
485 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
486 mlcd->mlcd_cookielen = rec->ur_cookielen;
487 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
488 mlcd->mlcd_cookielen;
489 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
490 mlcd->mlcd_cookielen);
491 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
492 mlcd->mlcd_eadatalen);
494 CERROR("unable to allocate log cancel data\n");
500 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
501 handle, mds_cancel_cookies_cb, mlcd);
502 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
503 switch (cleanup_phase) {
505 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
506 rec->ur_eadata != NULL)
511 if (lockh[1].cookie != 0)
512 ldlm_lock_decref(lockh + 1, parent_mode);
515 ldlm_lock_decref(lockh, LCK_PW);
517 ptlrpc_save_lock (req, lockh, LCK_PW);
532 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
533 struct ptlrpc_request *req)
535 struct mds_export_data *med = &req->rq_export->exp_mds_data;
536 struct dentry *parent, *child;
537 struct mds_body *body;
540 mds_req_from_mcd(req, med->med_mcd);
542 if (req->rq_status) {
547 parent = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
548 LASSERT(!IS_ERR(parent));
549 child = ll_lookup_one_len(rec->ur_name, parent,
550 rec->ur_namelen - 1);
551 LASSERT(!IS_ERR(child));
552 if ((child->d_flags & DCACHE_CROSS_REF)) {
553 LASSERTF(child->d_inode == NULL, "BUG 3869\n");
554 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
555 mds_pack_dentry2body(req2obd(req), body, child, 1);
556 } else if (child->d_inode == NULL) {
557 DEBUG_REQ(D_ERROR, req, "parent "DLID4" name %s mode %o",
558 OLID4(rec->ur_id1), rec->ur_name, rec->ur_mode);
559 LASSERTF(child->d_inode != NULL, "BUG 3869\n");
561 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
562 mds_pack_inode2body(req2obd(req), body, child->d_inode, 1);
569 static int mds_reint_create(struct mds_update_record *rec, int offset,
570 struct ptlrpc_request *req,
571 struct lustre_handle *lh)
573 struct dentry *dparent = NULL;
574 struct mds_obd *mds = mds_req2mds(req);
575 struct obd_device *obd = req->rq_export->exp_obd;
576 struct dentry *dchild = NULL;
577 struct inode *dir = NULL;
579 struct lustre_handle lockh[2] = {{0}, {0}};
581 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
583 struct dentry_params dp;
584 struct mea *mea = NULL;
588 LASSERT(offset == 1);
590 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
593 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
594 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
595 rec->ur_name, rec->ur_mode);
597 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
599 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
600 GOTO(cleanup, rc = -ESTALE);
602 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
603 lockh, &parent_mode, rec->ur_name,
604 rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
605 if (IS_ERR(dparent)) {
606 rc = PTR_ERR(dparent);
607 CERROR("parent lookup error %d\n", rc);
610 cleanup_phase = 1; /* locked parent dentry */
611 dir = dparent->d_inode;
614 ldlm_lock_dump_handle(D_OTHER, lockh);
616 /* try to retrieve MEA data for this dir */
617 rc = mds_md_get_attr(obd, dparent->d_inode, &mea, &mea_size);
623 * dir is already splitted, check is requested filename should
624 * live at this MDS or at another one.
626 int i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
627 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
628 CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
629 " should be %lu(%d)\n",
630 mea->mea_master, dparent->d_inode->i_ino,
631 dparent->d_inode->i_generation, rec->ur_name,
632 (unsigned long)id_group(&mea->mea_ids[i]), i);
633 GOTO(cleanup, rc = -ERESTART);
637 dchild = ll_lookup_one_len(rec->ur_name, dparent,
638 rec->ur_namelen - 1);
639 if (IS_ERR(dchild)) {
640 rc = PTR_ERR(dchild);
641 CERROR("Can't find "DLID4"/%s, error %d\n",
642 OLID4(rec->ur_id1), rec->ur_name, rc);
646 cleanup_phase = 2; /* child dentry */
648 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
650 if (type == S_IFREG || type == S_IFDIR) {
651 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
652 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
653 obd->obd_name, dparent->d_inode->i_ino,
654 dparent->d_inode->i_generation, rc, parent_mode);
656 /* dir got splitted */
657 GOTO(cleanup, rc = -ERESTART);
659 /* error happened during spitting. */
664 if (dir->i_mode & S_ISGID) {
665 if (S_ISDIR(rec->ur_mode))
666 rec->ur_mode |= S_ISGID;
670 * here inode number should be used only in the case of replaying. It is
671 * needed to check if object already created in the case of creating
674 dchild->d_fsdata = (void *)&dp;
675 dp.p_inum = (unsigned long)id_ino(rec->ur_id2);
680 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
682 GOTO(cleanup, rc = PTR_ERR(handle));
683 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
691 * as Peter asked, mkdir() should distribute new directories
692 * over the whole cluster in order to distribute namespace
693 * processing load. first, we calculate which MDS to use to put
694 * new directory's inode in.
696 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1,
698 if (i == mds->mds_num) {
699 /* inode will be created locally */
700 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
702 GOTO(cleanup, rc = PTR_ERR(handle));
704 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
706 CERROR("Can't create dir %s, rc = %d\n",
707 dchild->d_name.name, rc);
711 down(&dchild->d_inode->i_sem);
713 rc = mds_update_inode_sid(obd, dchild->d_inode,
714 handle, rec->ur_id2);
716 CERROR("mds_update_inode_sid() failed, inode %lu, "
717 "rc %d\n", dchild->d_inode->i_ino, rc);
721 * make sure, that fid is up-to-date.
723 mds_set_last_fid(obd, id_fid(rec->ur_id2));
725 rc = mds_alloc_inode_sid(obd, dchild->d_inode,
728 CERROR("mds_alloc_inode_sid() failed, inode %lu, "
729 "rc %d\n", dchild->d_inode->i_ino, rc);
732 up(&dchild->d_inode->i_sem);
738 nstripes = *(u16 *)rec->ur_eadata;
740 if (rc == 0 && nstripes) {
742 * we pass LCK_EX to split routine to signal,
743 * that we have exclusive access to the
744 * directory. Simple because nobody knows it
745 * already exists -bzzz
747 rc = mds_try_to_split_dir(obd, dchild,
751 /* dir got splitted */
754 /* an error occured during
759 } else if (!DENTRY_VALID(dchild)) {
760 /* inode will be created on another MDS */
761 struct obdo *oa = NULL;
762 struct mds_body *body;
764 /* first, create that inode */
767 GOTO(cleanup, rc = -ENOMEM);
772 if (rec->ur_eadata) {
773 /* user asks for creating splitted dir */
774 oa->o_easize = *((u16 *) rec->ur_eadata);
777 obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
778 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
779 OBD_MD_FLUID | OBD_MD_FLGID);
781 oa->o_mode = dir->i_mode;
783 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
786 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
788 * here inode number and generation are
789 * important, as this is replay request and we
790 * need them to check if such an object is
793 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
794 obd->obd_name, rec->ur_namelen - 1,
795 rec->ur_name, (unsigned)id_ino(rec->ur_id2),
796 (unsigned)id_gen(rec->ur_id2));
797 oa->o_id = id_ino(rec->ur_id2);
798 oa->o_fid = id_fid(rec->ur_id2);
799 oa->o_generation = id_gen(rec->ur_id2);
800 oa->o_flags |= OBD_FL_RECREATE_OBJS;
803 * fid should be defined here. It should be
806 LASSERT(oa->o_fid != 0);
810 * before obd_create() is called, o_fid is not known if
811 * this is not recovery of cause.
813 rc = obd_create(mds->mds_md_exp, oa, NULL, NULL);
815 CERROR("can't create remote inode: %d\n", rc);
816 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
817 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
818 rec->ur_name, rec->ur_mode);
823 LASSERT(oa->o_fid != 0);
825 /* now, add new dir entry for it */
826 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
827 if (IS_ERR(handle)) {
829 GOTO(cleanup, rc = PTR_ERR(handle));
832 /* creating local dentry for remote inode. */
833 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
834 rec->ur_namelen - 1, oa->o_id,
835 oa->o_generation, i, oa->o_fid);
838 CERROR("Can't create local entry %*s for "
839 "remote inode.\n", rec->ur_namelen - 1,
845 body = lustre_msg_buf(req->rq_repmsg,
847 body->valid |= OBD_MD_FLID | OBD_MD_MDS;
849 obdo2id(&body->id1, oa);
852 /* requested name exists in the directory */
859 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
861 GOTO(cleanup, rc = PTR_ERR(handle));
862 if (rec->ur_tgt == NULL) /* no target supplied */
863 rc = -EINVAL; /* -EPROTO? */
865 rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
873 int rdev = rec->ur_rdev;
874 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
876 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
877 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
882 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
883 dchild->d_fsdata = NULL;
884 GOTO(cleanup, rc = -EINVAL);
887 /* In case we stored the desired inum in here, we want to clean up. */
888 if (dchild->d_fsdata == (void *)(unsigned long)id_ino(rec->ur_id2))
889 dchild->d_fsdata = NULL;
892 CDEBUG(D_INODE, "error during create: %d\n", rc);
894 } else if (dchild->d_inode) {
896 struct mds_body *body;
897 struct inode *inode = dchild->d_inode;
900 iattr.ia_uid = rec->ur_fsuid;
901 LTIME_S(iattr.ia_atime) = rec->ur_time;
902 LTIME_S(iattr.ia_ctime) = rec->ur_time;
903 LTIME_S(iattr.ia_mtime) = rec->ur_time;
905 if (dir->i_mode & S_ISGID)
906 iattr.ia_gid = dir->i_gid;
908 iattr.ia_gid = rec->ur_fsgid;
910 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
911 ATTR_MTIME | ATTR_CTIME;
913 if (id_ino(rec->ur_id2)) {
914 LASSERT(id_ino(rec->ur_id2) == inode->i_ino);
915 inode->i_generation = id_gen(rec->ur_id2);
917 if (type != S_IFDIR) {
919 * updating inode self id, as inode already
920 * exists and we should make sure, its sid will
921 * be the same as we reveived.
924 rc = mds_update_inode_sid(obd, inode,
925 handle, rec->ur_id2);
928 CERROR("Can't update inode self id, "
933 * make sure, that fid is up-to-date.
935 mds_set_last_fid(obd, id_fid(rec->ur_id2));
938 /* dirtied and committed by the upcoming setattr. */
939 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
940 inode->i_ino, inode->i_generation);
942 struct lustre_handle child_ino_lockh;
944 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
945 inode->i_ino, inode->i_generation);
947 if (type != S_IFDIR) {
949 * allocate new id for @inode if it is not dir,
950 * because for dir it was already done.
953 rc = mds_alloc_inode_sid(obd, inode,
957 CERROR("mds_alloc_inode_sid() failed, "
958 "inode %lu, rc %d\n", inode->i_ino,
965 * the inode we were allocated may have just
966 * been freed by an unlink operation. We take
967 * this lock to synchronize against the matching
968 * reply-ack-lock taken in unlink, to avoid
969 * replay problems if this reply makes it out to
970 * the client but the unlink's does not. See
971 * bug 2029 for more detail.
973 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
974 if (rc != ELDLM_OK) {
975 CERROR("error locking for unlink/create sync: "
978 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
983 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
985 CERROR("error on child setattr: rc = %d\n", rc);
987 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
988 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
990 CERROR("error on parent setattr: rc = %d\n", rc);
992 MD_COUNTER_INCREMENT(obd, create);
994 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
995 mds_pack_inode2body(obd, body, inode, 1);
1000 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
1002 if (rc && created) {
1003 /* Destroy the file we just created. This should not need extra
1004 * journal credits, as we have already modified all of the
1005 * blocks needed in order to create the file in the first
1009 err = vfs_rmdir(dir, dchild);
1011 CERROR("rmdir in error path: %d\n", err);
1014 err = vfs_unlink(dir, dchild);
1016 CERROR("unlink in error path: %d\n", err);
1022 switch (cleanup_phase) {
1023 case 2: /* child dentry */
1025 case 1: /* locked parent dentry */
1027 if (lockh[1].cookie != 0)
1028 ldlm_lock_decref(lockh + 1, parent_mode);
1031 ldlm_lock_decref(lockh, LCK_PW);
1033 ptlrpc_save_lock(req, lockh, LCK_PW);
1039 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1043 OBD_FREE(mea, mea_size);
1044 req->rq_status = rc;
1049 res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
1050 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1054 for (i = 0; i < RES_NAME_SIZE; i++) {
1056 * this is needed to make zeroed res_id entries to be put at the
1057 * end of list in *ordered_locks() .
1059 if (res1->name[i] == 0 && res2->name[i] != 0)
1061 if (res2->name[i] == 0 && res1->name[i] != 0)
1063 if (res1->name[i] > res2->name[i])
1065 if (res1->name[i] < res2->name[i])
1072 if (memcmp(p1, p2, sizeof(*p1)) < 0)
1078 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1079 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1080 * because they take the place of local semaphores.
1082 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
1083 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
1084 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
1085 struct lustre_handle *p1_lockh, int p1_lock_mode,
1086 ldlm_policy_data_t *p1_policy,
1087 struct ldlm_res_id *p2_res_id,
1088 struct lustre_handle *p2_lockh, int p2_lock_mode,
1089 ldlm_policy_data_t *p2_policy)
1091 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1092 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1093 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1094 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
1098 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1100 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1101 res_id[0]->name[0], res_id[1]->name[0]);
1103 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1104 handles[1] = p1_lockh;
1105 handles[0] = p2_lockh;
1106 res_id[1] = p1_res_id;
1107 res_id[0] = p2_res_id;
1108 lock_modes[1] = p1_lock_mode;
1109 lock_modes[0] = p2_lock_mode;
1110 policies[1] = p1_policy;
1111 policies[0] = p2_policy;
1114 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1115 res_id[0]->name[0], res_id[1]->name[0]);
1117 flags = LDLM_FL_LOCAL_ONLY;
1118 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1119 LDLM_IBITS, policies[0], lock_modes[0], &flags,
1120 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1121 NULL, 0, NULL, handles[0]);
1124 ldlm_lock_dump_handle(D_OTHER, handles[0]);
1126 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1127 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1128 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1129 ldlm_lock_addref(handles[1], lock_modes[1]);
1130 } else if (res_id[1]->name[0] != 0) {
1131 flags = LDLM_FL_LOCAL_ONLY;
1132 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1133 *res_id[1], LDLM_IBITS, policies[1],
1134 lock_modes[1], &flags, mds_blocking_ast,
1135 ldlm_completion_ast, NULL, NULL, NULL, 0,
1137 if (rc != ELDLM_OK) {
1138 ldlm_lock_decref(handles[0], lock_modes[0]);
1141 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1147 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1148 struct lustre_handle *p1_lockh, int p1_lock_mode,
1149 ldlm_policy_data_t *p1_policy,
1150 struct ldlm_res_id *p2_res_id,
1151 struct lustre_handle *p2_lockh, int p2_lock_mode,
1152 ldlm_policy_data_t *p2_policy,
1153 struct ldlm_res_id *c1_res_id,
1154 struct lustre_handle *c1_lockh, int c1_lock_mode,
1155 ldlm_policy_data_t *c1_policy,
1156 struct ldlm_res_id *c2_res_id,
1157 struct lustre_handle *c2_lockh, int c2_lock_mode,
1158 ldlm_policy_data_t *c2_policy)
1160 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1161 c1_res_id, c2_res_id };
1162 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1163 c1_lockh, c2_lockh };
1164 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1165 c1_lock_mode, c2_lock_mode };
1166 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1167 c1_policy, c2_policy};
1168 int rc, i, j, sorted, flags;
1171 CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1172 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1173 res_id[3]->name[0]);
1176 * simple insertion sort - we have at most 4 elements. Note, that zeroed
1177 * res_id should be at the end of list after sorting is finished.
1179 for (i = 1; i < 4; i++) {
1181 dlm_handles[4] = dlm_handles[i];
1182 res_id[4] = res_id[i];
1183 lock_modes[4] = lock_modes[i];
1184 policies[4] = policies[i];
1188 if (res_gt(res_id[j], res_id[4], policies[j],
1190 dlm_handles[j + 1] = dlm_handles[j];
1191 res_id[j + 1] = res_id[j];
1192 lock_modes[j + 1] = lock_modes[j];
1193 policies[j + 1] = policies[j];
1198 } while (j >= 0 && !sorted);
1200 dlm_handles[j + 1] = dlm_handles[4];
1201 res_id[j + 1] = res_id[4];
1202 lock_modes[j + 1] = lock_modes[4];
1203 policies[j + 1] = policies[4];
1206 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1207 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1208 res_id[3]->name[0]);
1210 /* XXX we could send ASTs on all these locks first before blocking? */
1211 for (i = 0; i < 4; i++) {
1215 * nevertheless zeroed res_ids should be at the end of list, and
1216 * could use break here, I think, that it is more correctly for
1217 * clear understanding of code to have continue here, as it
1218 * clearly means, that zeroed res_id should be skipped and does
1219 * not mean, that if we meet zeroed res_id we should stop
1222 if (res_id[i]->name[0] == 0)
1226 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1227 (policies[i]->l_inodebits.bits &
1228 policies[i-1]->l_inodebits.bits) ) {
1229 memcpy(dlm_handles[i], dlm_handles[i-1],
1230 sizeof(*(dlm_handles[i])));
1231 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1233 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1234 *res_id[i], LDLM_IBITS,
1236 lock_modes[i], &flags,
1238 ldlm_completion_ast, NULL, NULL,
1239 NULL, 0, NULL, dlm_handles[i]);
1241 GOTO(out_err, rc = -EIO);
1242 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1249 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1254 /* In the unlikely case that the child changed while we were waiting
1255 * on the lock, we need to drop the lock on the old child and either:
1256 * - if the child has a lower resource name, then we have to also
1257 * drop the parent lock and regain the locks in the right order
1258 * - in the rename case, if the child has a lower resource name than one of
1259 * the other parent/child resources (maxres) we also need to reget the locks
1260 * - if the child has a higher resource name (this is the common case)
1261 * we can just get the lock on the new child (still in lock order)
1263 * Returns 0 if the child did not change or if it changed but could be locked.
1264 * Returns 1 if the child changed and we need to re-lock (no locks held).
1265 * Returns -ve error with a valid dchild (no locks held). */
1266 static int mds_verify_child(struct obd_device *obd,
1267 struct ldlm_res_id *parent_res_id,
1268 struct lustre_handle *parent_lockh,
1269 struct dentry *dparent, int parent_mode,
1270 struct ldlm_res_id *child_res_id,
1271 struct lustre_handle *child_lockh,
1272 struct dentry **dchildp, int child_mode,
1273 ldlm_policy_data_t *child_policy,
1274 const char *name, int namelen,
1275 struct ldlm_res_id *maxres,
1276 unsigned long child_ino,
1279 struct lustre_id sid;
1280 struct dentry *vchild, *dchild = *dchildp;
1281 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1284 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1286 GOTO(cleanup, rc = PTR_ERR(vchild));
1288 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1289 if (child_gen == vchild->d_generation &&
1290 child_ino == vchild->d_inum) {
1299 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1300 (vchild->d_inode != NULL &&
1301 child_gen == vchild->d_inode->i_generation &&
1302 child_ino == vchild->d_inode->i_ino))) {
1310 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1311 vchild->d_inode, dchild ? dchild->d_inode : 0,
1312 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1313 child_res_id->name[0]);
1315 if (child_res_id->name[0] != 0)
1316 ldlm_lock_decref(child_lockh, child_mode);
1320 cleanup_phase = 1; /* parent lock only */
1321 *dchildp = dchild = vchild;
1323 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1326 if (dchild->d_inode) {
1327 down(&dchild->d_inode->i_sem);
1328 rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
1329 up(&dchild->d_inode->i_sem);
1331 CERROR("Can't read inode self id, inode %lu,"
1332 " rc %d\n", dchild->d_inode->i_ino, rc);
1335 child_res_id->name[0] = id_fid(&sid);
1336 child_res_id->name[1] = id_group(&sid);
1338 child_res_id->name[0] = dchild->d_fid;
1339 child_res_id->name[1] = dchild->d_mdsnum;
1342 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1343 res_gt(maxres, child_res_id, NULL, NULL)) {
1344 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1345 child_res_id->name[0], parent_res_id->name[0],
1347 GOTO(cleanup, rc = 1);
1350 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1351 *child_res_id, LDLM_IBITS, child_policy,
1352 child_mode, &flags, mds_blocking_ast,
1353 ldlm_completion_ast, NULL, NULL, NULL, 0,
1356 GOTO(cleanup, rc = -EIO);
1359 memset(child_res_id, 0, sizeof(*child_res_id));
1365 switch(cleanup_phase) {
1367 if (child_res_id->name[0] != 0)
1368 ldlm_lock_decref(child_lockh, child_mode);
1370 ldlm_lock_decref(parent_lockh, parent_mode);
1376 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1377 struct lustre_id *id,
1378 struct lustre_handle *parent_lockh,
1379 struct dentry **dparentp, int parent_mode,
1380 __u64 parent_lockpart, int *update_mode,
1381 char *name, int namelen,
1382 struct lustre_handle *child_lockh,
1383 struct dentry **dchildp, int child_mode,
1384 __u64 child_lockpart)
1386 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1387 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1388 struct ldlm_res_id parent_res_id = { .name = {0} };
1389 struct ldlm_res_id child_res_id = { .name = {0} };
1390 int rc = 0, cleanup_phase = 0;
1391 unsigned long child_ino;
1392 struct lustre_id sid;
1393 __u32 child_gen = 0;
1394 struct inode *inode;
1397 /* Step 1: Lookup parent */
1398 *dparentp = mds_id2dentry(obd, id, NULL);
1399 if (IS_ERR(*dparentp)) {
1400 rc = PTR_ERR(*dparentp);
1405 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1406 (*dparentp)->d_inode->i_ino, name);
1408 parent_res_id.name[0] = id_fid(id);
1409 parent_res_id.name[1] = id_group(id);
1412 parent_lockh[1].cookie = 0;
1413 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1414 struct ldlm_res_id res_id = { .name = {0} };
1415 ldlm_policy_data_t policy;
1418 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1420 res_id.name[0] = id_fid(id);
1421 res_id.name[1] = id_group(id);
1422 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1424 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1425 res_id, LDLM_IBITS, &policy,
1426 *update_mode, &flags,
1428 ldlm_completion_ast,
1429 NULL, NULL, NULL, 0, NULL,
1435 parent_res_id.name[2] = full_name_hash(name, namelen - 1);
1437 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1438 (unsigned long)id_fid(id), (unsigned long)id_group(id),
1439 parent_res_id.name[2]);
1443 cleanup_phase = 1; /* parent dentry */
1445 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1446 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1447 if (IS_ERR(*dchildp)) {
1448 rc = PTR_ERR(*dchildp);
1449 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1453 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1455 * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1456 * lock. Drop possible UPDATE lock!
1458 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1459 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1461 child_res_id.name[0] = (*dchildp)->d_fid;
1462 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1463 child_gen = (*dchildp)->d_generation;
1464 child_ino = (*dchildp)->d_inum;
1468 inode = (*dchildp)->d_inode;
1470 inode = igrab(inode);
1474 down(&inode->i_sem);
1475 rc = mds_read_inode_sid(obd, inode, &sid);
1478 CERROR("Can't read inode self id, inode %lu, "
1479 "rc %d\n", inode->i_ino, rc);
1484 child_res_id.name[0] = id_fid(&sid);
1485 child_res_id.name[1] = id_group(&sid);
1486 child_gen = inode->i_generation;
1487 child_ino = inode->i_ino;
1491 cleanup_phase = 2; /* child dentry */
1493 /* Step 3: Lock parent and child in resource order. If child doesn't
1494 * exist, we still have to lock the parent and re-lookup. */
1495 rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1496 &parent_policy, &child_res_id, child_lockh,
1497 child_mode, &child_policy);
1501 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1502 cleanup_phase = 4; /* child lock */
1504 cleanup_phase = 3; /* parent lock */
1506 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1507 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1508 parent_mode, &child_res_id, child_lockh,
1509 dchildp, child_mode, &child_policy,
1510 name, namelen, &parent_res_id,
1511 child_ino, child_gen);
1522 switch (cleanup_phase) {
1524 ldlm_lock_decref(child_lockh, child_mode);
1526 ldlm_lock_decref(parent_lockh, parent_mode);
1531 if (parent_lockh[1].cookie)
1532 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1540 void mds_reconstruct_generic(struct ptlrpc_request *req)
1542 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1543 mds_req_from_mcd(req, med->med_mcd);
1546 /* If we are unlinking an open file/dir (i.e. creating an orphan) then
1547 * we instead link the inode into the PENDING directory until it is
1548 * finally released. We can't simply call mds_reint_rename() or some
1549 * part thereof, because we don't have the inode to check for link
1550 * count/open status until after it is locked.
1552 * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1553 * before starting journal transaction.
1555 * returns 1 on success
1556 * returns 0 if we lost a race and didn't make a new link
1557 * returns negative on error
1559 static int mds_orphan_add_link(struct mds_update_record *rec,
1560 struct obd_device *obd, struct dentry *dentry)
1562 struct mds_obd *mds = &obd->u.mds;
1563 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1564 struct inode *inode = dentry->d_inode;
1565 struct dentry *pending_child;
1566 char idname[LL_ID_NAMELEN];
1567 int idlen = 0, rc, mode;
1570 LASSERT(inode != NULL);
1571 LASSERT(!mds_inode_is_orphan(inode));
1572 #ifndef HAVE_I_ALLOC_SEM
1573 LASSERT(down_trylock(&inode->i_sem) != 0);
1575 LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1577 idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1579 CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1580 mds_orphan_open_count(inode), inode->i_nlink,
1581 S_ISDIR(inode->i_mode) ? "dir" :
1582 S_ISREG(inode->i_mode) ? "file" : "other",
1583 rec->ur_name, idname);
1585 if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1588 pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1589 if (IS_ERR(pending_child))
1590 RETURN(PTR_ERR(pending_child));
1592 if (pending_child->d_inode != NULL) {
1593 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1594 LASSERT(pending_child->d_inode == inode);
1595 GOTO(out_dput, rc = 0);
1598 /* link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG
1599 * for linking and return real mode back then -bzzz */
1600 mode = inode->i_mode;
1601 inode->i_mode = S_IFREG;
1602 rc = vfs_link(dentry, pending_dir, pending_child);
1604 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1607 mds_inode_set_orphan(inode);
1609 /* return mode and correct i_nlink if inode is directory */
1610 inode->i_mode = mode;
1611 LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1612 S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1614 if (S_ISDIR(mode)) {
1616 pending_dir->i_nlink++;
1617 mark_inode_dirty(inode);
1618 mark_inode_dirty(pending_dir);
1623 l_dput(pending_child);
1627 int mds_create_local_dentry(struct mds_update_record *rec,
1628 struct obd_device *obd)
1630 struct mds_obd *mds = &obd->u.mds;
1631 struct inode *id_dir = mds->mds_id_dir->d_inode;
1632 int idlen = 0, rc, cleanup_phase = 0;
1633 struct dentry *new_child = NULL;
1634 char *idname = rec->ur_name;
1635 struct dentry *child = NULL;
1636 struct lustre_handle lockh[2] = {{0}, {0}};
1637 struct lustre_id sid;
1641 down(&id_dir->i_sem);
1642 idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1643 id_gen(rec->ur_id1));
1645 CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1646 idname, OLID4(rec->ur_id1));
1648 new_child = ll_lookup_one_len(idname, mds->mds_id_dir,
1651 if (IS_ERR(new_child)) {
1652 CERROR("can't lookup %s: %d\n", idname,
1653 (int) PTR_ERR(new_child));
1654 GOTO(cleanup, rc = PTR_ERR(new_child));
1658 down(&id_dir->i_sem);
1659 rc = mds_read_inode_sid(obd, id_dir, &sid);
1662 CERROR("Can't read inode self id, inode %lu, "
1663 "rc %d\n", id_dir->i_ino, rc);
1667 if (new_child->d_inode != NULL) {
1668 /* nice. we've already have local dentry! */
1669 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1670 (unsigned)new_child->d_inode->i_ino,
1671 (unsigned)new_child->d_inode->i_generation);
1673 id_ino(rec->ur_id1) = id_dir->i_ino;
1674 id_gen(rec->ur_id1) = id_dir->i_generation;
1675 rec->ur_namelen = idlen + 1;
1677 id_fid(rec->ur_id1) = id_fid(&sid);
1678 id_group(rec->ur_id1) = id_group(&sid);
1680 GOTO(cleanup, rc = 0);
1683 /* new, local dentry will be added soon. we need no aliases here */
1686 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1687 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1689 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1690 LCK_EX, lockh, NULL, NULL, 0,
1691 MDS_INODELOCK_UPDATE);
1694 if (IS_ERR(child)) {
1695 rc = PTR_ERR(child);
1696 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1697 CERROR("can't get victim: %d\n", rc);
1702 handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
1704 GOTO(cleanup, rc = PTR_ERR(handle));
1706 rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
1707 idlen, id_ino(rec->ur_id1),
1708 id_gen(rec->ur_id1), mds->mds_num,
1709 id_fid(rec->ur_id1));
1711 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1712 (unsigned long)child->d_inode->i_ino,
1713 (unsigned long)child->d_inode->i_generation, rc);
1715 if (S_ISDIR(child->d_inode->i_mode)) {
1717 mark_inode_dirty(id_dir);
1719 mark_inode_dirty(child->d_inode);
1721 fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
1723 id_ino(rec->ur_id1) = id_dir->i_ino;
1724 id_gen(rec->ur_id1) = id_dir->i_generation;
1725 rec->ur_namelen = idlen + 1;
1727 id_fid(rec->ur_id1) = id_fid(&sid);
1728 id_group(rec->ur_id1) = id_group(&sid);
1732 switch(cleanup_phase) {
1734 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1735 ldlm_lock_decref(lockh, LCK_EX);
1745 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1746 struct ptlrpc_request *slave)
1748 void *cookie, *cookie2;
1749 struct mds_body *body2;
1750 struct mds_body *body;
1754 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1755 LASSERT(body != NULL);
1757 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1758 LASSERT(body2 != NULL);
1760 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
1763 memcpy(body2, body, sizeof(*body));
1764 body2->valid &= ~OBD_MD_FLCOOKIE;
1766 if (!(body->valid & OBD_MD_FLEASIZE) &&
1767 !(body->valid & OBD_MD_FLDIREA))
1770 if (body->eadatasize == 0) {
1771 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1775 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1777 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1778 LASSERT(ea != NULL);
1780 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1781 LASSERT(ea2 != NULL);
1783 memcpy(ea2, ea, body->eadatasize);
1785 if (body->valid & OBD_MD_FLCOOKIE) {
1786 LASSERT(master->rq_repmsg->buflens[2] >=
1787 slave->rq_repmsg->buflens[2]);
1788 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1789 slave->rq_repmsg->buflens[2]);
1790 LASSERT(cookie != NULL);
1792 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1793 master->rq_repmsg->buflens[2]);
1794 LASSERT(cookie2 != NULL);
1795 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1796 body2->valid |= OBD_MD_FLCOOKIE;
1801 static int mds_reint_unlink_remote(struct mds_update_record *rec,
1802 int offset, struct ptlrpc_request *req,
1803 struct lustre_handle *parent_lockh,
1804 int update_mode, struct dentry *dparent,
1805 struct lustre_handle *child_lockh,
1806 struct dentry *dchild)
1808 struct obd_device *obd = req->rq_export->exp_obd;
1809 struct mds_obd *mds = mds_req2mds(req);
1810 struct ptlrpc_request *request = NULL;
1811 int rc = 0, cleanup_phase = 0;
1812 struct mdc_op_data *op_data;
1816 LASSERT(offset == 1 || offset == 3);
1818 /* time to drop i_nlink on remote MDS */
1819 OBD_ALLOC(op_data, sizeof(*op_data));
1820 if (op_data == NULL)
1823 memset(op_data, 0, sizeof(*op_data));
1824 mds_pack_dentry2id(obd, &op_data->id1, dchild, 1);
1825 op_data->create_mode = rec->ur_mode;
1827 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode "DLID4")",
1828 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1830 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1831 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode "DLID4")",
1832 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1835 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1836 op_data->create_mode |= MDS_MODE_REPLAY;
1838 rc = md_unlink(mds->mds_md_exp, op_data, &request);
1839 OBD_FREE(op_data, sizeof(*op_data));
1844 mds_copy_unlink_reply(req, request);
1845 ptlrpc_req_finished(request);
1849 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1852 GOTO(cleanup, rc = PTR_ERR(handle));
1853 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
1854 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1859 req->rq_status = rc;
1862 if (parent_lockh[1].cookie != 0)
1863 ldlm_lock_decref(parent_lockh + 1, update_mode);
1865 ldlm_lock_decref(child_lockh, LCK_EX);
1867 ldlm_lock_decref(parent_lockh, LCK_PW);
1869 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1876 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1877 struct ptlrpc_request *req,
1878 struct lustre_handle *lh)
1880 struct dentry *dparent = NULL, *dchild;
1881 struct mds_obd *mds = mds_req2mds(req);
1882 struct obd_device *obd = req->rq_export->exp_obd;
1883 struct mds_body *body = NULL;
1884 struct inode *child_inode = NULL;
1885 struct lustre_handle parent_lockh[2] = {{0}, {0}};
1886 struct lustre_handle child_lockh = {0};
1887 struct lustre_handle child_reuse_lockh = {0};
1888 struct lustre_handle *slave_lockh = NULL;
1889 char idname[LL_ID_NAMELEN];
1890 struct llog_create_locks *lcl = NULL;
1891 void *handle = NULL;
1892 int rc = 0, cleanup_phase = 0;
1893 int unlink_by_id = 0;
1897 LASSERT(offset == 1 || offset == 3);
1899 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1900 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
1903 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1905 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1906 DEBUG_REQ(D_HA, req, "unlink replay");
1907 LASSERT(offset == 1); /* should not come from intent */
1908 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
1909 lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
1910 req->rq_repmsg->buflens[2]);
1913 MD_COUNTER_INCREMENT(obd, unlink);
1915 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1916 GOTO(cleanup, rc = -ENOENT);
1918 if (rec->ur_namelen == 1) {
1919 /* this is request to drop i_nlink on local inode */
1921 rec->ur_name = idname;
1922 rc = mds_create_local_dentry(rec, obd);
1923 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
1924 DEBUG_REQ(D_HA, req,
1925 "drop nlink on inode "DLID4" (replay)",
1926 OLID4(rec->ur_id1));
1932 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1933 /* master mds for directory asks slave removing inode is already
1935 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1936 LCK_PW, parent_lockh,
1937 &update_mode, rec->ur_name,
1939 MDS_INODELOCK_UPDATE);
1940 if (IS_ERR(dparent))
1941 GOTO(cleanup, rc = PTR_ERR(dparent));
1942 dchild = ll_lookup_one_len(rec->ur_name, dparent,
1943 rec->ur_namelen - 1);
1945 GOTO(cleanup, rc = PTR_ERR(dchild));
1946 child_lockh.cookie = 0;
1947 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
1948 LASSERT(dchild->d_inode != NULL);
1949 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
1951 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
1952 parent_lockh, &dparent,
1953 LCK_PW, MDS_INODELOCK_UPDATE,
1954 &update_mode, rec->ur_name,
1955 rec->ur_namelen, &child_lockh,
1957 MDS_INODELOCK_LOOKUP |
1958 MDS_INODELOCK_UPDATE);
1963 if (dchild->d_flags & DCACHE_CROSS_REF) {
1964 /* we should have parent lock only here */
1965 LASSERT(unlink_by_id == 0);
1966 LASSERT(dchild->d_mdsnum != mds->mds_num);
1967 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
1968 update_mode, dparent, &child_lockh, dchild);
1972 cleanup_phase = 1; /* dchild, dparent, locks */
1975 child_inode = dchild->d_inode;
1976 if (child_inode == NULL) {
1977 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1978 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
1979 GOTO(cleanup, rc = -ENOENT);
1982 cleanup_phase = 2; /* dchild has a lock */
1984 /* We have to do these checks ourselves, in case we are making an
1985 * orphan. The client tells us whether rmdir() or unlink() was called,
1986 * so we need to return appropriate errors (bug 72).
1988 * We don't have to check permissions, because vfs_rename (called from
1989 * mds_open_unlink_rename) also calls may_delete. */
1990 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1991 if (!S_ISDIR(child_inode->i_mode))
1992 GOTO(cleanup, rc = -ENOTDIR);
1994 if (S_ISDIR(child_inode->i_mode))
1995 GOTO(cleanup, rc = -EISDIR);
1998 /* handle splitted dir */
1999 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
2003 /* Step 4: Get a lock on the ino to sync with creation WRT inode
2004 * reuse (see bug 2029). */
2005 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
2008 cleanup_phase = 3; /* child inum lock */
2010 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2012 /* ldlm_reply in buf[0] if called via intent */
2018 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2019 LASSERT(body != NULL);
2021 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2022 DOWN_READ_I_ALLOC_SEM(child_inode);
2023 cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2025 /* If this is potentially the last reference to this inode, get the
2026 * OBD EA data first so the client can destroy OST objects. We
2027 * only do the object removal later if no open files/links remain. */
2028 if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2029 child_inode->i_nlink == 1) {
2030 if (mds_orphan_open_count(child_inode) > 0) {
2031 /* need to lock pending_dir before transaction */
2032 down(&mds->mds_pending_dir->d_inode->i_sem);
2033 cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2034 } else if (S_ISREG(child_inode->i_mode)) {
2035 mds_pack_inode2body(obd, body, child_inode, 0);
2036 mds_pack_md(obd, req->rq_repmsg, offset + 1,
2037 body, child_inode, MDS_PACK_MD_LOCK);
2041 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2042 switch (child_inode->i_mode & S_IFMT) {
2044 /* Drop any lingering child directories before we start our
2045 * transaction, to avoid doing multiple inode dirty/delete
2046 * in our compound transaction (bug 1321). */
2047 shrink_dcache_parent(dchild);
2048 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2051 GOTO(cleanup, rc = PTR_ERR(handle));
2052 rc = vfs_rmdir(dparent->d_inode, dchild);
2055 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2056 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2058 handle = fsfilt_start_log(obd, dparent->d_inode,
2059 FSFILT_OP_UNLINK, NULL,
2060 le32_to_cpu(lmm->lmm_stripe_count));
2062 GOTO(cleanup, rc = PTR_ERR(handle));
2063 rc = vfs_unlink(dparent->d_inode, dchild);
2071 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2074 GOTO(cleanup, rc = PTR_ERR(handle));
2075 rc = vfs_unlink(dparent->d_inode, dchild);
2078 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2081 GOTO(cleanup, rc = -EINVAL);
2084 if (rc == 0 && child_inode->i_nlink == 0) {
2085 if (mds_orphan_open_count(child_inode) > 0)
2086 rc = mds_orphan_add_link(rec, obd, dchild);
2089 GOTO(cleanup, rc = 0);
2091 if (!S_ISREG(child_inode->i_mode))
2094 if (!(body->valid & OBD_MD_FLEASIZE)) {
2095 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2096 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2097 } else if (mds_log_op_unlink(obd, child_inode,
2098 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2099 req->rq_repmsg->buflens[offset + 1],
2100 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2101 req->rq_repmsg->buflens[offset+2],
2103 body->valid |= OBD_MD_FLCOOKIE;
2114 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2115 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2116 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2118 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2120 CERROR("error on parent setattr: rc = %d\n", err);
2122 rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2123 handle, req, rc, 0);
2125 (void)obd_set_info(mds->mds_dt_exp, strlen("unlinked"),
2126 "unlinked", 0, NULL);
2127 switch(cleanup_phase) {
2128 case 5: /* pending_dir semaphore */
2129 up(&mds->mds_pending_dir->d_inode->i_sem);
2130 case 4: /* child inode semaphore */
2131 UP_READ_I_ALLOC_SEM(child_inode);
2132 /* handle splitted dir */
2134 /* master directory can be non-empty or something else ... */
2135 mds_unlink_slave_objs(obd, dchild);
2138 ptlrpc_save_llog_lock(req, lcl);
2139 case 3: /* child ino-reuse lock */
2140 if (rc && body != NULL) {
2141 // Don't unlink the OST objects if the MDS unlink failed
2145 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2147 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2148 case 2: /* child lock */
2149 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2150 if (child_lockh.cookie)
2151 ldlm_lock_decref(&child_lockh, LCK_EX);
2152 case 1: /* child and parent dentry, parent lock */
2154 if (parent_lockh[1].cookie != 0)
2155 ldlm_lock_decref(parent_lockh + 1, update_mode);
2158 ldlm_lock_decref(parent_lockh, LCK_PW);
2160 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2167 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2170 req->rq_status = rc;
2175 * to service requests from remote MDS to increment i_nlink
2177 static int mds_reint_link_acquire(struct mds_update_record *rec,
2178 int offset, struct ptlrpc_request *req,
2179 struct lustre_handle *lh)
2181 struct obd_device *obd = req->rq_export->exp_obd;
2182 struct ldlm_res_id src_res_id = { .name = {0} };
2183 struct lustre_handle *handle = NULL, src_lockh = {0};
2184 struct mds_obd *mds = mds_req2mds(req);
2185 int rc = 0, cleanup_phase = 0;
2186 struct dentry *de_src = NULL;
2187 ldlm_policy_data_t policy;
2191 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2192 obd->obd_name, OLID4(rec->ur_id1));
2194 /* Step 1: Lookup the source inode and target directory by ID */
2195 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2197 GOTO(cleanup, rc = PTR_ERR(de_src));
2198 cleanup_phase = 1; /* source dentry */
2200 src_res_id.name[0] = id_fid(rec->ur_id1);
2201 src_res_id.name[1] = id_group(rec->ur_id1);
2202 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2204 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2205 src_res_id, LDLM_IBITS, &policy,
2206 LCK_EX, &flags, mds_blocking_ast,
2207 ldlm_completion_ast, NULL, NULL,
2208 NULL, 0, NULL, &src_lockh);
2210 GOTO(cleanup, rc = -ENOLCK);
2211 cleanup_phase = 2; /* lock */
2213 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2215 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2216 if (IS_ERR(handle)) {
2217 rc = PTR_ERR(handle);
2220 de_src->d_inode->i_nlink++;
2221 mark_inode_dirty(de_src->d_inode);
2224 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2225 handle, req, rc, 0);
2227 switch (cleanup_phase) {
2230 ldlm_lock_decref(&src_lockh, LCK_EX);
2232 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2238 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2241 req->rq_status = rc;
2246 * request to link to foreign inode:
2247 * - acquire i_nlinks on this inode
2250 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2251 int offset, struct ptlrpc_request *req,
2252 struct lustre_handle *lh)
2254 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2255 struct obd_device *obd = req->rq_export->exp_obd;
2256 struct dentry *de_tgt_dir = NULL;
2257 struct mds_obd *mds = mds_req2mds(req);
2258 int rc = 0, cleanup_phase = 0;
2259 struct mdc_op_data *op_data;
2260 struct ptlrpc_request *request = NULL;
2264 DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2265 ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2266 OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2267 OLID4(rec->ur_id1));
2269 de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2270 tgt_dir_lockh, &update_mode,
2271 rec->ur_name, rec->ur_namelen - 1,
2272 MDS_INODELOCK_UPDATE);
2273 if (IS_ERR(de_tgt_dir))
2274 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2277 OBD_ALLOC(op_data, sizeof(*op_data));
2278 if (op_data == NULL)
2279 GOTO(cleanup, rc = -ENOMEM);
2281 op_data->id1 = *(rec->ur_id1);
2282 op_data->namelen = 0;
2283 op_data->name = NULL;
2284 rc = md_link(mds->mds_md_exp, op_data, &request);
2285 OBD_FREE(op_data, sizeof(*op_data));
2291 ptlrpc_req_finished(request);
2293 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2295 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2296 if (IS_ERR(handle)) {
2297 rc = PTR_ERR(handle);
2301 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2302 rec->ur_namelen - 1, id_ino(rec->ur_id1),
2303 id_gen(rec->ur_id1), id_group(rec->ur_id1),
2304 id_fid(rec->ur_id1));
2308 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2309 handle, req, rc, 0);
2312 switch (cleanup_phase) {
2315 /* FIXME: drop i_nlink on remote inode here */
2316 CERROR("MUST drop drop i_nlink here\n");
2321 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2323 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2326 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2328 ptlrpc_save_lock(req, tgt_dir_lockh + 1, update_mode);
2334 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2337 req->rq_status = rc;
2341 static int mds_reint_link(struct mds_update_record *rec, int offset,
2342 struct ptlrpc_request *req,
2343 struct lustre_handle *lh)
2345 struct obd_device *obd = req->rq_export->exp_obd;
2346 struct dentry *de_src = NULL;
2347 struct dentry *de_tgt_dir = NULL;
2348 struct dentry *dchild = NULL;
2349 struct mds_obd *mds = mds_req2mds(req);
2350 struct lustre_handle *handle = NULL;
2351 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2352 struct ldlm_res_id src_res_id = { .name = {0} };
2353 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2354 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2355 ldlm_policy_data_t tgt_dir_policy =
2356 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2357 int rc = 0, cleanup_phase = 0;
2359 int update_mode = 0;
2363 LASSERT(offset == 1);
2365 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2366 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2367 id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2370 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2371 MD_COUNTER_INCREMENT(obd, link);
2373 // memset(tgt_dir_lockh, 0, 2*sizeof(tgt_dir_lockh[0]));
2374 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2375 GOTO(cleanup, rc = -ENOENT);
2377 if (id_group(rec->ur_id1) != mds->mds_num) {
2378 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2382 if (rec->ur_namelen == 1) {
2383 rc = mds_reint_link_acquire(rec, offset, req, lh);
2387 /* Step 1: Lookup the source inode and target directory by ID */
2388 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2390 GOTO(cleanup, rc = PTR_ERR(de_src));
2392 cleanup_phase = 1; /* source dentry */
2394 de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2395 if (IS_ERR(de_tgt_dir)) {
2396 rc = PTR_ERR(de_tgt_dir);
2401 cleanup_phase = 2; /* target directory dentry */
2403 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2404 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2405 rec->ur_name, de_src->d_inode->i_ino);
2407 /* Step 2: Take the two locks */
2408 src_res_id.name[0] = id_fid(rec->ur_id1);
2409 src_res_id.name[1] = id_group(rec->ur_id1);
2410 tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2411 tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2414 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2416 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2418 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2419 tgt_dir_res_id, LDLM_IBITS,
2420 &src_policy, update_mode, &flags,
2422 ldlm_completion_ast, NULL, NULL,
2423 NULL, 0, NULL, tgt_dir_lockh + 1);
2425 GOTO(cleanup, rc = -ENOLCK);
2428 tgt_dir_res_id.name[2] = full_name_hash(rec->ur_name,
2429 rec->ur_namelen - 1);
2430 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2431 (unsigned long)id_fid(rec->ur_id2),
2432 (unsigned long)id_group(rec->ur_id2),
2433 tgt_dir_res_id.name[2]);
2436 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2437 &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2438 LCK_EX, &tgt_dir_policy);
2442 cleanup_phase = 3; /* locks */
2444 /* Step 3: Lookup the child */
2445 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir,
2446 rec->ur_namelen - 1);
2447 if (IS_ERR(dchild)) {
2448 rc = PTR_ERR(dchild);
2449 if (rc != -EPERM && rc != -EACCES)
2450 CERROR("child lookup error %d\n", rc);
2454 cleanup_phase = 4; /* child dentry */
2456 if (dchild->d_inode) {
2457 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2458 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2463 /* Step 4: Do it. */
2464 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2466 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2467 if (IS_ERR(handle)) {
2468 rc = PTR_ERR(handle);
2472 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2473 if (rc && rc != -EPERM && rc != -EACCES)
2474 CERROR("vfs_link error %d\n", rc);
2476 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2477 handle, req, rc, 0);
2480 switch (cleanup_phase) {
2481 case 4: /* child dentry */
2485 ldlm_lock_decref(&src_lockh, LCK_EX);
2486 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2488 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2489 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2491 case 2: /* target dentry */
2493 if (tgt_dir_lockh[1].cookie && update_mode)
2494 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2498 case 1: /* source dentry */
2503 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2506 req->rq_status = rc;
2510 /* The idea here is that we need to get four locks in the end:
2511 * one on each parent directory, one on each child. We need to take
2512 * these locks in some kind of order (to avoid deadlocks), and the order
2513 * I selected is "increasing resource number" order. We need to look up
2514 * the children, however, before we know what the resource number(s) are.
2515 * Thus the following plan:
2517 * 1,2. Look up the parents
2518 * 3,4. Look up the children
2519 * 5. Take locks on the parents and children, in order
2520 * 6. Verify that the children haven't changed since they were looked up
2522 * If there was a race and the children changed since they were first looked
2523 * up, it is possible that mds_verify_child() will be able to just grab the
2524 * lock on the new child resource (if it has a higher resource than any other)
2525 * but we need to compare against not only its parent, but also against the
2526 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2528 * We need the fancy igrab() on the child inodes because we aren't holding a
2529 * lock on the parent after the lookup is done, so dentry->d_inode may change
2530 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2532 static int mds_get_parents_children_locked(struct obd_device *obd,
2533 struct mds_obd *mds,
2534 struct lustre_id *p1_id,
2535 struct dentry **de_srcdirp,
2536 struct lustre_id *p2_id,
2537 struct dentry **de_tgtdirp,
2539 const char *old_name, int old_len,
2540 struct dentry **de_oldp,
2541 const char *new_name, int new_len,
2542 struct dentry **de_newp,
2543 struct lustre_handle *dlm_handles,
2546 struct ldlm_res_id p1_res_id = { .name = {0} };
2547 struct ldlm_res_id p2_res_id = { .name = {0} };
2548 struct ldlm_res_id c1_res_id = { .name = {0} };
2549 struct ldlm_res_id c2_res_id = { .name = {0} };
2550 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2551 /* Only dentry should change, but the inode itself would be
2553 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2554 /* If something is going to be replaced, both dentry and inode locks are
2556 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP|
2557 MDS_INODELOCK_UPDATE}};
2558 struct ldlm_res_id *maxres_src, *maxres_tgt;
2559 struct inode *inode;
2560 __u32 child1_gen = 0;
2561 __u32 child2_gen = 0;
2562 unsigned long child1_ino;
2563 unsigned long child2_ino;
2564 int rc = 0, cleanup_phase = 0;
2567 /* Step 1: Lookup the source directory */
2568 *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2569 if (IS_ERR(*de_srcdirp))
2570 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2572 cleanup_phase = 1; /* source directory dentry */
2574 p1_res_id.name[0] = id_fid(p1_id);
2575 p1_res_id.name[1] = id_group(p1_id);
2577 /* Step 2: Lookup the target directory */
2578 if (id_equal_stc(p1_id, p2_id)) {
2579 *de_tgtdirp = dget(*de_srcdirp);
2581 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2582 if (IS_ERR(*de_tgtdirp)) {
2583 rc = PTR_ERR(*de_tgtdirp);
2589 cleanup_phase = 2; /* target directory dentry */
2591 p2_res_id.name[0] = id_fid(p2_id);
2592 p2_res_id.name[1] = id_group(p2_id);
2595 dlm_handles[5].cookie = 0;
2596 dlm_handles[6].cookie = 0;
2598 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2600 * get a temp lock on just fid, group to flush client cache and
2601 * to protect dirs from concurrent splitting.
2603 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2604 LCK_PW, &p_policy, &p2_res_id,
2605 &dlm_handles[6], LCK_PW, &p_policy);
2609 p1_res_id.name[2] = full_name_hash(old_name, old_len - 1);
2610 p2_res_id.name[2] = full_name_hash(new_name, new_len - 1);
2612 CDEBUG(D_INFO, "take locks on "
2613 LPX64":"LPX64":"LPX64", "LPX64":"LPX64":"LPX64"\n",
2614 p1_res_id.name[0], p1_res_id.name[1], p1_res_id.name[2],
2615 p2_res_id.name[0], p2_res_id.name[1], p2_res_id.name[2]);
2620 /* Step 3: Lookup the source child entry */
2621 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp,
2623 if (IS_ERR(*de_oldp)) {
2624 rc = PTR_ERR(*de_oldp);
2625 CERROR("old child lookup error (%*s): %d\n",
2626 old_len - 1, old_name, rc);
2630 cleanup_phase = 4; /* original name dentry */
2632 inode = (*de_oldp)->d_inode;
2633 if (inode != NULL) {
2634 struct lustre_id sid;
2636 inode = igrab(inode);
2638 GOTO(cleanup, rc = -ENOENT);
2640 down(&inode->i_sem);
2641 rc = mds_read_inode_sid(obd, inode, &sid);
2644 CERROR("Can't read inode self id, inode %lu, "
2645 "rc %d\n", inode->i_ino, rc);
2650 c1_res_id.name[0] = id_fid(&sid);
2651 c1_res_id.name[1] = id_group(&sid);
2652 child1_gen = inode->i_generation;
2653 child1_ino = inode->i_ino;
2655 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2656 c1_res_id.name[0] = (*de_oldp)->d_fid;
2657 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2658 child1_gen = (*de_oldp)->d_generation;
2659 child1_ino = (*de_oldp)->d_inum;
2661 GOTO(cleanup, rc = -ENOENT);
2664 /* Step 4: Lookup the target child entry */
2665 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp,
2667 if (IS_ERR(*de_newp)) {
2668 rc = PTR_ERR(*de_newp);
2669 CERROR("new child lookup error (%*s): %d\n",
2670 old_len - 1, old_name, rc);
2674 cleanup_phase = 5; /* target dentry */
2676 inode = (*de_newp)->d_inode;
2677 if (inode != NULL) {
2678 struct lustre_id sid;
2680 inode = igrab(inode);
2684 down(&inode->i_sem);
2685 rc = mds_read_inode_sid(obd, inode, &sid);
2688 CERROR("Can't read inode self id, inode %lu, "
2689 "rc %d\n", inode->i_ino, rc);
2693 c2_res_id.name[0] = id_fid(&sid);
2694 c2_res_id.name[1] = id_group(&sid);
2695 child2_gen = inode->i_generation;
2696 child2_ino = inode->i_ino;
2698 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2699 c2_res_id.name[0] = (*de_newp)->d_fid;
2700 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
2701 child2_gen = (*de_newp)->d_generation;
2702 child2_ino = (*de_newp)->d_inum;
2706 /* Step 5: Take locks on the parents and child(ren) */
2707 maxres_src = &p1_res_id;
2708 maxres_tgt = &p2_res_id;
2709 cleanup_phase = 5; /* target dentry */
2711 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
2712 maxres_src = &c1_res_id;
2713 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
2714 maxres_tgt = &c2_res_id;
2716 rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
2718 &p2_res_id, &dlm_handles[1], parent_mode,
2720 &c1_res_id, &dlm_handles[2], child_mode,
2722 &c2_res_id, &dlm_handles[3], child_mode,
2727 cleanup_phase = 6; /* parent and child(ren) locks */
2729 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2730 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2731 parent_mode, &c1_res_id, &dlm_handles[2],
2732 de_oldp, child_mode, &c1_policy, old_name,old_len,
2733 maxres_tgt, child1_ino, child1_gen);
2735 if (c2_res_id.name[0] != 0)
2736 ldlm_lock_decref(&dlm_handles[3], child_mode);
2737 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2744 if (!DENTRY_VALID(*de_oldp))
2745 GOTO(cleanup, rc = -ENOENT);
2747 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2748 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2749 parent_mode, &c2_res_id, &dlm_handles[3],
2750 de_newp, child_mode, &c2_policy, new_name,
2751 new_len, maxres_src, child2_ino, child2_gen);
2753 ldlm_lock_decref(&dlm_handles[2], child_mode);
2754 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2764 switch (cleanup_phase) {
2765 case 6: /* child lock(s) */
2766 if (c2_res_id.name[0] != 0)
2767 ldlm_lock_decref(&dlm_handles[3], child_mode);
2768 if (c1_res_id.name[0] != 0)
2769 ldlm_lock_decref(&dlm_handles[2], child_mode);
2770 if (dlm_handles[1].cookie != 0)
2771 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2772 if (dlm_handles[0].cookie != 0)
2773 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2774 case 5: /* target dentry */
2776 case 4: /* source dentry */
2780 if (dlm_handles[5].cookie != 0)
2781 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2782 if (dlm_handles[6].cookie != 0)
2783 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2785 case 2: /* target directory dentry */
2786 l_dput(*de_tgtdirp);
2787 case 1: /* source directry dentry */
2788 l_dput(*de_srcdirp);
2796 * checks if dentry can be removed. This function also handles cross-ref
2799 static int mds_check_for_rename(struct obd_device *obd,
2800 struct dentry *dentry)
2802 struct mds_obd *mds = &obd->u.mds;
2803 struct lustre_handle *rlockh;
2804 struct ptlrpc_request *req;
2805 struct mdc_op_data *op_data;
2806 struct lookup_intent it;
2807 int handle_size, rc = 0;
2810 LASSERT(dentry != NULL);
2812 if (dentry->d_inode) {
2813 if (S_ISDIR(dentry->d_inode->i_mode) &&
2814 !mds_is_dir_empty(obd, dentry))
2817 LASSERT((dentry->d_flags & DCACHE_CROSS_REF));
2818 handle_size = sizeof(struct lustre_handle);
2820 OBD_ALLOC(rlockh, handle_size);
2824 memset(rlockh, 0, handle_size);
2825 OBD_ALLOC(op_data, sizeof(*op_data));
2826 if (op_data == NULL) {
2827 OBD_FREE(rlockh, handle_size);
2830 memset(op_data, 0, sizeof(*op_data));
2831 mds_pack_dentry2id(obd, &op_data->id1, dentry, 1);
2833 it.it_op = IT_UNLINK;
2834 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
2835 op_data, rlockh, NULL, 0, ldlm_completion_ast,
2836 mds_blocking_ast, NULL);
2837 OBD_FREE(op_data, sizeof(*op_data));
2842 if (rlockh->cookie != 0)
2843 ldlm_lock_decref(rlockh, LCK_EX);
2845 if (it.d.lustre.it_data) {
2846 req = (struct ptlrpc_request *)it.d.lustre.it_data;
2847 ptlrpc_req_finished(req);
2850 if (it.d.lustre.it_status)
2851 rc = it.d.lustre.it_status;
2852 OBD_FREE(rlockh, handle_size);
2857 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
2858 struct ptlrpc_request *req, struct lustre_id *id,
2859 struct dentry *de_dir, struct dentry *de,
2862 struct obd_device *obd = req->rq_export->exp_obd;
2863 struct mds_obd *mds = mds_req2mds(req);
2864 void *handle = NULL;
2870 * name exists and points to local inode try to unlink this name
2871 * and create new one.
2873 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
2874 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
2875 (unsigned long)de->d_inode->i_generation);
2877 /* checking if we can remove local dentry. */
2878 rc = mds_check_for_rename(obd, de);
2882 handle = fsfilt_start(obd, de_dir->d_inode,
2883 FSFILT_OP_RENAME, NULL);
2885 GOTO(cleanup, rc = PTR_ERR(handle));
2886 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
2889 } else if (de->d_flags & DCACHE_CROSS_REF) {
2890 CDEBUG(D_OTHER, "%s: %s points to remote inode %lu/%lu\n",
2891 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_mdsnum,
2892 (unsigned long)de->d_fid);
2894 /* checking if we can remove local dentry. */
2895 rc = mds_check_for_rename(obd, de);
2900 * to be fully POSIX compatible, we should add one more check:
2902 * if de_new is subdir of dir rec->ur_id1. If so - return
2905 * I do not know how to implement it right now, because
2906 * inodes/dentries for new and old names lie on different MDS,
2907 * so add this notice here just to make it visible for the rest
2908 * of developers and do not forget about. And when this check
2909 * will be added, del_cross_ref should gone, that is local
2910 * dentry is able to be removed if all checks passed.
2912 * Currently -EEXISTS is returned by fsfilt_add_dir_entry() what
2913 * is not fully correct. --umka
2916 if (del_cross_ref) {
2917 handle = fsfilt_start(obd, de_dir->d_inode,
2918 FSFILT_OP_RENAME, NULL);
2920 GOTO(cleanup, rc = PTR_ERR(handle));
2921 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
2927 /* name doesn't exist. the simplest case. */
2928 handle = fsfilt_start(obd, de_dir->d_inode,
2929 FSFILT_OP_LINK, NULL);
2931 GOTO(cleanup, rc = PTR_ERR(handle));
2934 rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
2935 rec->ur_tgtlen - 1, id_ino(id),
2936 id_gen(id), id_group(id), id_fid(id));
2938 CERROR("add_dir_entry() returned error %d\n", rc);
2944 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
2945 handle, req, rc, 0);
2950 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
2951 struct ptlrpc_request *req, struct dentry *de_dir,
2954 struct obd_device *obd = req->rq_export->exp_obd;
2955 struct mds_obd *mds = mds_req2mds(req);
2956 void *handle = NULL;
2960 handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
2962 GOTO(cleanup, rc = PTR_ERR(handle));
2963 rc = fsfilt_del_dir_entry(obd, de);
2968 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
2969 handle, req, rc, 0);
2973 static int mds_reint_rename_create_name(struct mds_update_record *rec,
2974 int offset, struct ptlrpc_request *req)
2976 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2977 struct obd_device *obd = req->rq_export->exp_obd;
2978 struct mds_obd *mds = mds_req2mds(req);
2979 struct lustre_handle child_lockh = {0};
2980 struct dentry *de_tgtdir = NULL;
2981 struct dentry *de_new = NULL;
2982 int cleanup_phase = 0;
2983 int update_mode, rc = 0;
2987 * another MDS executing rename operation has asked us to create target
2988 * name. such a creation should destroy existing target name.
2990 CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
2991 obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
2993 /* first, lookup the target */
2994 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
2995 &de_tgtdir, LCK_PW, MDS_INODELOCK_UPDATE,
2996 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
2997 &child_lockh, &de_new, LCK_EX,
2998 MDS_INODELOCK_LOOKUP);
3005 LASSERT(de_tgtdir->d_inode);
3008 rc = mds_add_local_dentry(rec, offset, req, rec->ur_id1,
3009 de_tgtdir, de_new, 0);
3014 if (cleanup_phase == 1) {
3016 if (parent_lockh[1].cookie != 0)
3017 ldlm_lock_decref(parent_lockh + 1, update_mode);
3019 ldlm_lock_decref(parent_lockh, LCK_PW);
3020 if (child_lockh.cookie != 0)
3021 ldlm_lock_decref(&child_lockh, LCK_EX);
3026 req->rq_status = rc;
3030 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
3031 struct ptlrpc_request *req)
3033 struct obd_device *obd = req->rq_export->exp_obd;
3034 struct ptlrpc_request *req2 = NULL;
3035 struct dentry *de_srcdir = NULL;
3036 struct dentry *de_old = NULL;
3037 struct mds_obd *mds = mds_req2mds(req);
3038 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3039 struct lustre_handle child_lockh = {0};
3040 struct mdc_op_data *op_data;
3041 int update_mode, rc = 0;
3044 CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
3045 obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
3047 OBD_ALLOC(op_data, sizeof(*op_data));
3048 if (op_data == NULL)
3050 memset(op_data, 0, sizeof(*op_data));
3052 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
3053 &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
3054 &update_mode, rec->ur_name,
3055 rec->ur_namelen, &child_lockh, &de_old,
3056 LCK_EX, MDS_INODELOCK_LOOKUP);
3059 LASSERT(de_srcdir->d_inode);
3063 * we already know the target should be created on another MDS so, we
3064 * have to request that MDS to do it.
3067 /* prepare source id */
3068 if (de_old->d_flags & DCACHE_CROSS_REF) {
3069 LASSERT(de_old->d_inode == NULL);
3070 CDEBUG(D_OTHER, "request to move remote name\n");
3071 mds_pack_dentry2id(obd, &op_data->id1, de_old, 1);
3072 } else if (de_old->d_inode == NULL) {
3073 /* oh, source doesn't exist */
3074 OBD_FREE(op_data, sizeof(*op_data));
3075 GOTO(cleanup, rc = -ENOENT);
3077 struct lustre_id sid;
3078 struct inode *inode = de_old->d_inode;
3080 LASSERT(inode != NULL);
3081 CDEBUG(D_OTHER, "request to move local name\n");
3082 id_ino(&op_data->id1) = inode->i_ino;
3083 id_group(&op_data->id1) = mds->mds_num;
3084 id_gen(&op_data->id1) = inode->i_generation;
3086 down(&inode->i_sem);
3087 rc = mds_read_inode_sid(obd, inode, &sid);
3090 CERROR("Can't read inode self id, "
3091 "inode %lu, rc = %d\n",
3096 id_fid(&op_data->id1) = id_fid(&sid);
3099 op_data->id2 = *rec->ur_id2;
3100 rc = md_rename(mds->mds_md_exp, op_data, NULL, 0,
3101 rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3102 OBD_FREE(op_data, sizeof(*op_data));
3107 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3113 ptlrpc_req_finished(req2);
3116 if (parent_lockh[1].cookie != 0)
3117 ldlm_lock_decref(parent_lockh + 1, update_mode);
3119 ldlm_lock_decref(parent_lockh, LCK_PW);
3120 if (child_lockh.cookie != 0)
3121 ldlm_lock_decref(&child_lockh, LCK_EX);
3126 req->rq_status = rc;
3130 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3131 struct ptlrpc_request *req, struct lustre_handle *lockh)
3133 struct obd_device *obd = req->rq_export->exp_obd;
3134 struct dentry *de_srcdir = NULL;
3135 struct dentry *de_tgtdir = NULL;
3136 struct dentry *de_old = NULL;
3137 struct dentry *de_new = NULL;
3138 struct inode *old_inode = NULL, *new_inode = NULL;
3139 struct mds_obd *mds = mds_req2mds(req);
3140 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3141 struct mds_body *body = NULL;
3142 struct llog_create_locks *lcl = NULL;
3143 struct lov_mds_md *lmm = NULL;
3144 int rc = 0, cleanup_phase = 0;
3145 void *handle = NULL;
3148 LASSERT(offset == 1);
3150 DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3151 OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3154 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3156 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3157 DEBUG_REQ(D_HA, req, "rename replay\n");
3158 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3159 lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3160 req->rq_repmsg->buflens[2]);
3163 MD_COUNTER_INCREMENT(obd, rename);
3165 if (rec->ur_namelen == 1) {
3166 rc = mds_reint_rename_create_name(rec, offset, req);
3170 /* check if new name should be located on remote target. */
3171 if (id_group(rec->ur_id2) != mds->mds_num) {
3172 rc = mds_reint_rename_to_remote(rec, offset, req);
3176 rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3177 rec->ur_id2, &de_tgtdir, LCK_PW,
3178 rec->ur_name, rec->ur_namelen,
3179 &de_old, rec->ur_tgt,
3180 rec->ur_tgtlen, &de_new,
3181 dlm_handles, LCK_EX);
3185 cleanup_phase = 1; /* parent(s), children, locks */
3186 old_inode = de_old->d_inode;
3187 new_inode = de_new->d_inode;
3189 /* sanity check for src inode */
3190 if (de_old->d_flags & DCACHE_CROSS_REF) {
3191 LASSERT(de_old->d_inode == NULL);
3194 * in the case of cross-ref dir, we can perform this check only
3195 * if child and parent lie on the same mds. This is because
3196 * otherwise they can have the same inode numbers.
3198 if (de_old->d_mdsnum == mds->mds_num) {
3199 if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3200 de_old->d_inum == de_tgtdir->d_inode->i_ino)
3201 GOTO(cleanup, rc = -EINVAL);
3204 LASSERT(de_old->d_inode != NULL);
3205 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3206 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3207 GOTO(cleanup, rc = -EINVAL);
3210 /* sanity check for dest inode */
3211 if (de_new->d_flags & DCACHE_CROSS_REF) {
3212 LASSERT(new_inode == NULL);
3214 /* the same check about target dentry. */
3215 if (de_new->d_mdsnum == mds->mds_num) {
3216 if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3217 de_new->d_inum == de_tgtdir->d_inode->i_ino)
3218 GOTO(cleanup, rc = -EINVAL);
3222 * regular files usualy do not have ->rename() implemented. But
3223 * we handle only this case when @de_new is cross-ref entry,
3224 * because in other cases it will be handled by vfs_rename().
3226 if (de_old->d_inode && (!de_old->d_inode->i_op ||
3227 !de_old->d_inode->i_op->rename))
3228 GOTO(cleanup, rc = -EPERM);
3231 (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3232 new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3233 GOTO(cleanup, rc = -EINVAL);
3237 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3239 * check if we are moving old entry into its child. 2.6 does not check
3240 * for this in vfs_rename() anymore.
3242 if (is_subdir(de_new, de_old))
3243 GOTO(cleanup, rc = -EINVAL);
3246 /* check if inodes point to each other. */
3247 if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3248 !(de_new->d_flags & DCACHE_CROSS_REF) &&
3249 old_inode == new_inode)
3250 GOTO(cleanup, rc = 0);
3253 * if we are about to remove the target at first, pass the EA of that
3254 * inode to client to perform and cleanup on OST.
3256 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3257 LASSERT(body != NULL);
3259 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3261 DOWN_READ_I_ALLOC_SEM(new_inode);
3263 cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3265 if (new_inode && ((S_ISDIR(new_inode->i_mode) &&
3266 new_inode->i_nlink == 2) ||
3267 new_inode->i_nlink == 1)) {
3268 if (mds_orphan_open_count(new_inode) > 0) {
3269 /* need to lock pending_dir before transaction */
3270 down(&mds->mds_pending_dir->d_inode->i_sem);
3271 cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3272 } else if (S_ISREG(new_inode->i_mode)) {
3273 mds_pack_inode2body(obd, body, new_inode, 0);
3274 mds_pack_md(obd, req->rq_repmsg, 1, body,
3275 new_inode, MDS_PACK_MD_LOCK);
3279 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3280 de_srcdir->d_inode->i_sb);
3282 if (de_old->d_flags & DCACHE_CROSS_REF) {
3283 struct lustre_id old_id;
3285 mds_pack_dentry2id(obd, &old_id, de_old, 1);
3287 rc = mds_add_local_dentry(rec, offset, req, &old_id,
3288 de_tgtdir, de_new, 1);
3292 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3297 lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3298 handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3299 NULL, le32_to_cpu(lmm->lmm_stripe_count));
3302 GOTO(cleanup, rc = PTR_ERR(handle));
3305 de_old->d_fsdata = req;
3306 de_new->d_fsdata = req;
3307 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3310 if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3311 if (mds_orphan_open_count(new_inode) > 0)
3312 rc = mds_orphan_add_link(rec, obd, de_new);
3315 GOTO(cleanup, rc = 0);
3317 if (!S_ISREG(new_inode->i_mode))
3320 if (!(body->valid & OBD_MD_FLEASIZE)) {
3321 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3322 OBD_MD_FLATIME | OBD_MD_FLMTIME);
3323 } else if (mds_log_op_unlink(obd, new_inode,
3324 lustre_msg_buf(req->rq_repmsg,1,0),
3325 req->rq_repmsg->buflens[1],
3326 lustre_msg_buf(req->rq_repmsg,2,0),
3327 req->rq_repmsg->buflens[2],
3329 body->valid |= OBD_MD_FLCOOKIE;
3335 rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3336 handle, req, rc, 0);
3338 switch (cleanup_phase) {
3340 up(&mds->mds_pending_dir->d_inode->i_sem);
3343 UP_READ_I_ALLOC_SEM(new_inode);
3346 if (dlm_handles[5].cookie != 0)
3347 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3348 if (dlm_handles[6].cookie != 0)
3349 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3352 ptlrpc_save_llog_lock(req, lcl);
3355 if (dlm_handles[3].cookie != 0)
3356 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3357 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3358 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3359 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3361 if (dlm_handles[3].cookie != 0)
3362 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3363 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3364 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3365 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3374 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3377 req->rq_status = rc;
3381 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3382 struct ptlrpc_request *, struct lustre_handle *);
3384 static mds_reinter reinters[REINT_MAX + 1] = {
3385 [REINT_SETATTR] mds_reint_setattr,
3386 [REINT_CREATE] mds_reint_create,
3387 [REINT_LINK] mds_reint_link,
3388 [REINT_UNLINK] mds_reint_unlink,
3389 [REINT_RENAME] mds_reint_rename,
3390 [REINT_OPEN] mds_open
3393 int mds_reint_rec(struct mds_update_record *rec, int offset,
3394 struct ptlrpc_request *req, struct lustre_handle *lockh)
3396 struct obd_device *obd = req->rq_export->exp_obd;
3397 struct lvfs_run_ctxt saved;
3400 /* checked by unpacker */
3401 LASSERT(rec->ur_opcode <= REINT_MAX &&
3402 reinters[rec->ur_opcode] != NULL);
3404 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3405 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3406 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);