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_lite.h>
47 #include "mds_internal.h"
49 struct mds_logcancel_data {
50 struct lov_mds_md *mlcd_lmm;
54 struct llog_cookie mlcd_cookies[0];
57 static void mds_cancel_cookies_cb(struct obd_device *obd,
58 __u64 transno, void *cb_data,
61 struct mds_logcancel_data *mlcd = cb_data;
62 struct lov_stripe_md *lsm = NULL;
63 struct llog_ctxt *ctxt;
66 obd_transno_commit_cb(obd, transno, error);
68 CDEBUG(D_HA, "cancelling %d cookies\n",
69 (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
71 rc = obd_unpackmd(obd->u.mds.mds_dt_exp, &lsm, mlcd->mlcd_lmm,
72 mlcd->mlcd_eadatalen);
74 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
75 (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
78 ///* XXX 0 normally, SENDNOW for debug */);
79 ctxt = llog_get_context(&obd->obd_llogs,
80 mlcd->mlcd_cookies[0].lgc_subsys + 1);
81 rc = llog_cancel(ctxt, mlcd->mlcd_cookielen /
82 sizeof(*mlcd->mlcd_cookies),
83 mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW, lsm);
85 CERROR("error cancelling %d log cookies: rc %d\n",
86 (int)(mlcd->mlcd_cookielen /
87 sizeof(*mlcd->mlcd_cookies)), rc);
88 obd_free_memmd(obd->u.mds.mds_dt_exp, &lsm);
91 OBD_FREE(mlcd, mlcd->mlcd_size);
94 /* Assumes caller has already pushed us into the kernel context. */
95 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
96 struct ptlrpc_request *req, int rc, __u32 op_data)
98 struct mds_export_data *med = &req->rq_export->exp_mds_data;
99 struct obd_device *obd = req->rq_export->exp_obd;
100 struct mds_client_data *mcd = med->med_mcd;
101 int err, log_pri = D_HA;
106 /* if the export has already been failed, we have no last_rcvd slot */
107 if (req->rq_export->exp_failed) {
108 CERROR("committing transaction for disconnected client\n");
110 GOTO(out_commit, rc);
117 if (handle == NULL) {
118 /* if we're starting our own xaction, use our own inode */
119 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
120 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
121 if (IS_ERR(handle)) {
122 CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
123 RETURN(PTR_ERR(handle));
129 transno = req->rq_reqmsg->transno;
131 LASSERTF(transno == 0, "BUG 3934, t"LPU64" rc %d\n", transno, rc);
132 } else if (transno == 0) {
133 spin_lock(&mds->mds_transno_lock);
134 transno = ++mds->mds_last_transno;
135 spin_unlock(&mds->mds_transno_lock);
137 spin_lock(&mds->mds_transno_lock);
138 if (transno > mds->mds_last_transno)
139 mds->mds_last_transno = transno;
140 spin_unlock(&mds->mds_transno_lock);
142 req->rq_repmsg->transno = req->rq_transno = transno;
143 if (req->rq_reqmsg->opc == MDS_CLOSE) {
144 mcd->mcd_last_close_transno = cpu_to_le64(transno);
145 mcd->mcd_last_close_xid = cpu_to_le64(req->rq_xid);
146 mcd->mcd_last_close_result = cpu_to_le32(rc);
147 mcd->mcd_last_close_data = cpu_to_le32(op_data);
149 mcd->mcd_last_transno = cpu_to_le64(transno);
150 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
151 mcd->mcd_last_result = cpu_to_le32(rc);
152 mcd->mcd_last_data = cpu_to_le32(op_data);
155 fsfilt_add_journal_cb(obd, mds->mds_sb, transno, handle,
156 mds_commit_last_transno_cb, NULL);
158 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd,
159 sizeof(*mcd), &off, 0);
167 DEBUG_REQ(log_pri, req,
168 "wrote trans #"LPU64" client %s at idx %u: err = %d",
169 transno, mcd->mcd_uuid, med->med_idx, err);
171 err = mds_update_last_fid(obd, handle, 0);
178 err = mds_dt_write_objids(obd);
184 CDEBUG(log_pri, "wrote objids: err = %d\n", err);
188 err = fsfilt_commit(obd, mds->mds_sb, inode, handle,
189 req->rq_export->exp_sync);
191 CERROR("error committing transaction: %d\n", err);
199 /* this gives the same functionality as the code between
200 * sys_chmod and inode_setattr
201 * chown_common and inode_setattr
202 * utimes and inode_setattr
205 /* Just for the case if we have some clients that know about ATTR_RAW */
206 #define ATTR_RAW 8192
208 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
210 time_t now = LTIME_S(CURRENT_TIME);
211 struct iattr *attr = &rec->ur_iattr;
212 unsigned int ia_valid = attr->ia_valid;
216 /* only fix up attrs if the client VFS didn't already */
218 if (!(ia_valid & ATTR_RAW))
221 if (!(ia_valid & ATTR_CTIME_SET))
222 LTIME_S(attr->ia_ctime) = now;
223 if (!(ia_valid & ATTR_ATIME_SET))
224 LTIME_S(attr->ia_atime) = now;
225 if (!(ia_valid & ATTR_MTIME_SET))
226 LTIME_S(attr->ia_mtime) = now;
228 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
232 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
233 if (rec->ur_fsuid != inode->i_uid &&
234 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
238 if (ia_valid & ATTR_SIZE) {
239 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
243 if (ia_valid & ATTR_UID) {
246 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
248 if (attr->ia_uid == (uid_t) -1)
249 attr->ia_uid = inode->i_uid;
250 if (attr->ia_gid == (gid_t) -1)
251 attr->ia_gid = inode->i_gid;
252 attr->ia_mode = inode->i_mode;
254 * If the user or group of a non-directory has been
255 * changed by a non-root user, remove the setuid bit.
256 * 19981026 David C Niemi <niemi@tux.org>
258 * Changed this to apply to all users, including root,
259 * to avoid some races. This is the behavior we had in
260 * 2.0. The check for non-root was definitely wrong
261 * for 2.2 anyway, as it should have been using
262 * CAP_FSETID rather than fsuid -- 19990830 SD.
264 if ((inode->i_mode & S_ISUID) == S_ISUID &&
265 !S_ISDIR(inode->i_mode)) {
266 attr->ia_mode &= ~S_ISUID;
267 attr->ia_valid |= ATTR_MODE;
270 * Likewise, if the user or group of a non-directory
271 * has been changed by a non-root user, remove the
272 * setgid bit UNLESS there is no group execute bit
273 * (this would be a file marked for mandatory
274 * locking). 19981026 David C Niemi <niemi@tux.org>
276 * Removed the fsuid check (see the comment above) --
279 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
280 (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
281 attr->ia_mode &= ~S_ISGID;
282 attr->ia_valid |= ATTR_MODE;
284 } else if (ia_valid & ATTR_MODE) {
285 int mode = attr->ia_mode;
287 if (attr->ia_mode == (mode_t) -1)
288 attr->ia_mode = inode->i_mode;
290 (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
295 void mds_steal_ack_locks(struct ptlrpc_request *req)
297 struct obd_export *exp = req->rq_export;
298 char str[PTL_NALFMT_SIZE];
299 struct list_head *tmp;
300 struct ptlrpc_reply_state *oldrep;
301 struct ptlrpc_service *svc;
302 struct llog_create_locks *lcl;
306 /* CAVEAT EMPTOR: spinlock order */
307 spin_lock_irqsave (&exp->exp_lock, flags);
308 list_for_each (tmp, &exp->exp_outstanding_replies) {
309 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
311 if (oldrep->rs_xid != req->rq_xid)
314 if (oldrep->rs_msg->opc != req->rq_reqmsg->opc)
315 CERROR ("Resent req xid "LPX64" has mismatched opc: "
316 "new %d old %d\n", req->rq_xid,
317 req->rq_reqmsg->opc, oldrep->rs_msg->opc);
319 svc = oldrep->rs_srv_ni->sni_service;
320 spin_lock (&svc->srv_lock);
322 list_del_init (&oldrep->rs_exp_list);
324 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
325 " o%d NID %s\n", oldrep->rs_nlocks, oldrep,
326 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg->opc,
327 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
329 for (i = 0; i < oldrep->rs_nlocks; i++)
330 ptlrpc_save_lock(req,
331 &oldrep->rs_locks[i],
332 oldrep->rs_modes[i]);
333 oldrep->rs_nlocks = 0;
335 lcl = oldrep->rs_llog_locks;
336 oldrep->rs_llog_locks = NULL;
338 ptlrpc_save_llog_lock(req, lcl);
340 DEBUG_REQ(D_HA, req, "stole locks for");
341 ptlrpc_schedule_difficult_reply (oldrep);
343 spin_unlock (&svc->srv_lock);
344 spin_unlock_irqrestore (&exp->exp_lock, flags);
347 spin_unlock_irqrestore (&exp->exp_lock, flags);
350 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
352 if (req->rq_reqmsg->opc == MDS_CLOSE) {
353 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
354 mcd->mcd_last_close_transno, mcd->mcd_last_close_result);
355 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_close_transno;
356 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_close_result;
358 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
359 mcd->mcd_last_transno, mcd->mcd_last_result);
360 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
361 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
364 mds_steal_ack_locks(req);
367 static void reconstruct_reint_setattr(struct mds_update_record *rec,
368 int offset, struct ptlrpc_request *req)
370 struct mds_export_data *med = &req->rq_export->exp_mds_data;
371 struct mds_body *body;
374 mds_req_from_mcd(req, med->med_mcd);
376 de = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
378 LASSERT(PTR_ERR(de) == req->rq_status);
382 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
383 mds_pack_inode2body(req2obd(req), body, de->d_inode, 1);
385 /* Don't return OST-specific attributes if we didn't just set them */
386 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
387 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
388 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
389 body->valid |= OBD_MD_FLMTIME;
390 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
391 body->valid |= OBD_MD_FLATIME;
396 /* In the raw-setattr case, we lock the child inode.
397 * In the write-back case or if being called from open, the client holds a lock
400 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
401 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
402 struct ptlrpc_request *req, struct lustre_handle *lh)
404 struct mds_obd *mds = mds_req2mds(req);
405 struct obd_device *obd = req->rq_export->exp_obd;
406 struct mds_body *body;
408 struct inode *inode = NULL;
409 struct lustre_handle lockh[2] = {{0}, {0}};
412 struct mds_logcancel_data *mlcd = NULL;
413 int rc = 0, cleanup_phase = 0, err;
417 LASSERT(offset == 1);
419 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x",
420 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
421 rec->ur_iattr.ia_valid);
423 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
424 MD_COUNTER_INCREMENT(obd, setattr);
426 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
427 de = mds_id2dentry(obd, rec->ur_id1, NULL);
429 GOTO(cleanup, rc = PTR_ERR(de));
431 __u64 lockpart = MDS_INODELOCK_UPDATE;
432 if (rec->ur_iattr.ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
433 lockpart |= MDS_INODELOCK_LOOKUP;
434 de = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
435 lockh, &parent_mode, NULL, 0, lockpart);
437 GOTO(cleanup, rc = PTR_ERR(de));
445 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
446 rec->ur_eadata != NULL)
449 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
451 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
453 GOTO(cleanup, rc = PTR_ERR(handle));
455 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
456 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
457 LTIME_S(rec->ur_iattr.ia_mtime),
458 LTIME_S(rec->ur_iattr.ia_ctime));
459 rc = mds_fix_attr(inode, rec);
463 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
464 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
465 (long)&rec->ur_iattr.ia_attr_flags);
467 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
470 if (rec->ur_iattr.ia_valid & ATTR_EA) {
471 int flags = (int)rec->ur_iattr.ia_attr_flags;
474 if (inode->i_op && inode->i_op->setxattr)
475 rc = inode->i_op->setxattr(de, rec->ur_eadata,
476 rec->ur_ea2data, rec->ur_ea2datalen,
478 } else if (rec->ur_iattr.ia_valid & ATTR_EA_RM) {
480 if (inode->i_op && inode->i_op->removexattr)
481 rc = inode->i_op->removexattr(de,
483 } else if ((S_ISREG(inode->i_mode) ||
484 S_ISDIR(inode->i_mode)) && rec->ur_eadata != NULL) {
485 struct lov_stripe_md *lsm = NULL;
486 struct lov_user_md *lum = NULL;
488 rc = ll_permission(inode, MAY_WRITE, NULL);
492 lum = rec->ur_eadata;
493 /* if lmm_stripe_size is -1 delete default stripe from dir */
494 if (S_ISDIR(inode->i_mode) &&
495 lum->lmm_stripe_size == (typeof(lum->lmm_stripe_size))(-1)){
496 rc = fsfilt_set_md(obd, inode, handle, NULL, 0, EA_LOV);
500 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, mds->mds_dt_exp,
501 0, &lsm, rec->ur_eadata);
505 obd_free_memmd(mds->mds_dt_exp, &lsm);
506 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
507 rec->ur_eadatalen, EA_LOV);
514 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
515 mds_pack_inode2body(obd, body, inode, 1);
517 /* Don't return OST-specific attributes if we didn't just set them */
518 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
519 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
520 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
521 body->valid |= OBD_MD_FLMTIME;
522 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
523 body->valid |= OBD_MD_FLATIME;
525 /* The logcookie should be no use anymore, why nobody remove
526 * following code block?
528 LASSERT(rec->ur_cookielen == 0);
529 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_dt_obd)) {
530 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
533 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
535 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
536 mlcd->mlcd_cookielen = rec->ur_cookielen;
537 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
538 mlcd->mlcd_cookielen;
539 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
540 mlcd->mlcd_cookielen);
541 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
542 mlcd->mlcd_eadatalen);
544 CERROR("unable to allocate log cancel data\n");
550 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
551 handle, mds_cancel_cookies_cb, mlcd);
552 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
553 switch (cleanup_phase) {
555 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
556 rec->ur_eadata != NULL)
561 if (lockh[1].cookie != 0)
562 ldlm_lock_decref(lockh + 1, parent_mode);
565 ldlm_lock_decref(lockh, LCK_PW);
567 ptlrpc_save_lock (req, lockh, LCK_PW);
582 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
583 struct ptlrpc_request *req)
585 struct mds_export_data *med = &req->rq_export->exp_mds_data;
586 struct dentry *parent, *child;
587 struct mds_body *body;
590 mds_req_from_mcd(req, med->med_mcd);
592 if (req->rq_status) {
597 parent = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
598 LASSERT(!IS_ERR(parent));
599 child = ll_lookup_one_len(rec->ur_name, parent,
600 rec->ur_namelen - 1);
601 LASSERT(!IS_ERR(child));
602 if ((child->d_flags & DCACHE_CROSS_REF)) {
603 LASSERTF(child->d_inode == NULL, "BUG 3869\n");
604 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
605 mds_pack_dentry2body(req2obd(req), body, child, 1);
606 } else if (child->d_inode == NULL) {
607 DEBUG_REQ(D_ERROR, req, "parent "DLID4" name %s mode %o",
608 OLID4(rec->ur_id1), rec->ur_name, rec->ur_mode);
609 LASSERTF(child->d_inode != NULL, "BUG 3869\n");
611 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
612 mds_pack_inode2body(req2obd(req), body, child->d_inode, 1);
619 static int mds_reint_create(struct mds_update_record *rec, int offset,
620 struct ptlrpc_request *req,
621 struct lustre_handle *lh)
623 struct dentry *dparent = NULL;
624 struct mds_obd *mds = mds_req2mds(req);
625 struct obd_device *obd = req->rq_export->exp_obd;
626 struct dentry *dchild = NULL;
627 struct inode *dir = NULL;
629 struct lustre_handle lockh[2] = {{0}, {0}};
631 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
633 struct dentry_params dp;
634 struct mea *mea = NULL;
638 LASSERT(offset == 1);
640 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
643 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
644 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
645 rec->ur_name, rec->ur_mode);
647 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
649 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
650 GOTO(cleanup, rc = -ESTALE);
652 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
653 lockh, &parent_mode, rec->ur_name,
654 rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
655 if (IS_ERR(dparent)) {
656 rc = PTR_ERR(dparent);
657 CERROR("parent lookup error %d\n", rc);
660 cleanup_phase = 1; /* locked parent dentry */
661 dir = dparent->d_inode;
664 ldlm_lock_dump_handle(D_OTHER, lockh);
666 /* try to retrieve MEA data for this dir */
667 rc = mds_md_get_attr(obd, dparent->d_inode, &mea, &mea_size);
673 * dir is already splitted, check is requested filename should
674 * live at this MDS or at another one.
676 int i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
677 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
678 CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
679 " should be %lu(%d)\n",
680 mea->mea_master, dparent->d_inode->i_ino,
681 dparent->d_inode->i_generation, rec->ur_name,
682 (unsigned long)id_group(&mea->mea_ids[i]), i);
683 GOTO(cleanup, rc = -ERESTART);
687 dchild = ll_lookup_one_len(rec->ur_name, dparent,
688 rec->ur_namelen - 1);
689 if (IS_ERR(dchild)) {
690 rc = PTR_ERR(dchild);
691 CERROR("Can't find "DLID4"/%s, error %d\n",
692 OLID4(rec->ur_id1), rec->ur_name, rc);
696 cleanup_phase = 2; /* child dentry */
698 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
700 if (type == S_IFREG || type == S_IFDIR) {
701 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
702 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
703 obd->obd_name, dparent->d_inode->i_ino,
704 dparent->d_inode->i_generation, rc, parent_mode);
706 /* dir got splitted */
707 GOTO(cleanup, rc = -ERESTART);
709 /* error happened during spitting. */
714 if (dir->i_mode & S_ISGID) {
715 if (S_ISDIR(rec->ur_mode))
716 rec->ur_mode |= S_ISGID;
720 * here inode number should be used only in the case of replaying. It is
721 * needed to check if object already created in the case of creating
724 dchild->d_fsdata = (void *)&dp;
725 dp.p_inum = (unsigned long)id_ino(rec->ur_id2);
730 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
732 GOTO(cleanup, rc = PTR_ERR(handle));
733 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
741 * as Peter asked, mkdir() should distribute new directories
742 * over the whole cluster in order to distribute namespace
743 * processing load. first, we calculate which MDS to use to put
744 * new directory's inode in.
746 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1,
748 if (i == mds->mds_num) {
749 /* inode will be created locally */
750 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
752 GOTO(cleanup, rc = PTR_ERR(handle));
754 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
756 CERROR("Can't create dir \"%s\", rc = %d\n",
757 dchild->d_name.name, rc);
761 down(&dchild->d_inode->i_sem);
763 rc = mds_update_inode_sid(obd, dchild->d_inode,
764 handle, rec->ur_id2);
766 CERROR("mds_update_inode_sid() failed, inode %lu, "
767 "rc %d\n", dchild->d_inode->i_ino, rc);
771 * make sure, that fid is up-to-date.
773 mds_set_last_fid(obd, id_fid(rec->ur_id2));
775 rc = mds_alloc_inode_sid(obd, dchild->d_inode,
778 CERROR("mds_alloc_inode_sid() failed, inode %lu, "
779 "rc %d\n", dchild->d_inode->i_ino, rc);
782 up(&dchild->d_inode->i_sem);
788 nstripes = *(u16 *)rec->ur_eadata;
790 if (rc == 0 && nstripes) {
792 * we pass LCK_EX to split routine to signal,
793 * that we have exclusive access to the
794 * directory. Simple because nobody knows it
795 * already exists -bzzz
797 rc = mds_try_to_split_dir(obd, dchild,
801 /* dir got splitted */
804 /* an error occured during
809 } else if (!DENTRY_VALID(dchild)) {
810 /* inode will be created on another MDS */
811 struct obdo *oa = NULL;
812 struct mds_body *body;
814 /* first, create that inode */
817 GOTO(cleanup, rc = -ENOMEM);
822 if (rec->ur_eadata) {
823 /* user asks for creating splitted dir */
824 oa->o_easize = *((u16 *) rec->ur_eadata);
827 obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
828 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
829 OBD_MD_FLUID | OBD_MD_FLGID);
831 oa->o_mode = dir->i_mode;
833 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
836 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
838 * here inode number and generation are
839 * important, as this is replay request and we
840 * need them to check if such an object is
843 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
844 obd->obd_name, rec->ur_namelen - 1,
845 rec->ur_name, (unsigned)id_ino(rec->ur_id2),
846 (unsigned)id_gen(rec->ur_id2));
847 oa->o_id = id_ino(rec->ur_id2);
848 oa->o_fid = id_fid(rec->ur_id2);
849 oa->o_generation = id_gen(rec->ur_id2);
850 oa->o_flags |= OBD_FL_RECREATE_OBJS;
851 LASSERT(oa->o_fid != 0);
855 * before obd_create() is called, o_fid is not known if
856 * this is not recovery of cause.
858 rc = obd_create(mds->mds_md_exp, oa, NULL, NULL);
860 CERROR("can't create remote inode: %d\n", rc);
861 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
862 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
863 rec->ur_name, rec->ur_mode);
868 LASSERT(oa->o_fid != 0);
870 /* now, add new dir entry for it */
871 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
872 if (IS_ERR(handle)) {
874 GOTO(cleanup, rc = PTR_ERR(handle));
877 /* creating local dentry for remote inode. */
878 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
879 rec->ur_namelen - 1, oa->o_id,
880 oa->o_generation, i, oa->o_fid);
883 CERROR("Can't create local entry %*s for "
884 "remote inode.\n", rec->ur_namelen - 1,
890 body = lustre_msg_buf(req->rq_repmsg,
892 body->valid |= OBD_MD_FLID | OBD_MD_MDS | OBD_MD_FID;
894 obdo2id(&body->id1, oa);
897 /* requested name exists in the directory */
904 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
906 GOTO(cleanup, rc = PTR_ERR(handle));
907 if (rec->ur_tgt == NULL) /* no target supplied */
908 rc = -EINVAL; /* -EPROTO? */
910 rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
918 int rdev = rec->ur_rdev;
919 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
921 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
922 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
927 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
928 dchild->d_fsdata = NULL;
929 GOTO(cleanup, rc = -EINVAL);
932 /* In case we stored the desired inum in here, we want to clean up. */
933 if (dchild->d_fsdata == (void *)(unsigned long)id_ino(rec->ur_id2))
934 dchild->d_fsdata = NULL;
937 CDEBUG(D_INODE, "error during create: %d\n", rc);
939 } else if (dchild->d_inode) {
941 struct mds_body *body;
942 struct inode *inode = dchild->d_inode;
945 iattr.ia_uid = rec->ur_fsuid;
946 LTIME_S(iattr.ia_atime) = rec->ur_time;
947 LTIME_S(iattr.ia_ctime) = rec->ur_time;
948 LTIME_S(iattr.ia_mtime) = rec->ur_time;
950 if (dir->i_mode & S_ISGID)
951 iattr.ia_gid = dir->i_gid;
953 iattr.ia_gid = rec->ur_fsgid;
955 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
956 ATTR_MTIME | ATTR_CTIME;
958 if (id_ino(rec->ur_id2)) {
959 LASSERT(id_ino(rec->ur_id2) == inode->i_ino);
960 inode->i_generation = id_gen(rec->ur_id2);
962 if (type != S_IFDIR) {
964 rc = mds_update_inode_sid(obd, inode,
965 handle, rec->ur_id2);
968 CERROR("Can't update inode self id, "
973 * make sure, that fid is up-to-date.
975 mds_set_last_fid(obd, id_fid(rec->ur_id2));
978 /* dirtied and committed by the upcoming setattr. */
979 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
980 inode->i_ino, inode->i_generation);
982 struct lustre_handle child_ino_lockh;
984 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
985 inode->i_ino, inode->i_generation);
987 if (type != S_IFDIR) {
989 * allocate new id for @inode if it is not dir,
990 * because for dir it was already done.
993 rc = mds_alloc_inode_sid(obd, inode,
997 CERROR("mds_alloc_inode_sid() failed, "
998 "inode %lu, rc %d\n", inode->i_ino,
1005 * the inode we were allocated may have just
1006 * been freed by an unlink operation. We take
1007 * this lock to synchronize against the matching
1008 * reply-ack-lock taken in unlink, to avoid
1009 * replay problems if this reply makes it out to
1010 * the client but the unlink's does not. See
1011 * bug 2029 for more detail.
1013 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
1014 if (rc != ELDLM_OK) {
1015 CERROR("error locking for unlink/create sync: "
1018 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
1023 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
1025 CERROR("error on child setattr: rc = %d\n", rc);
1027 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1028 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1030 CERROR("error on parent setattr: rc = %d\n", rc);
1032 MD_COUNTER_INCREMENT(obd, create);
1034 /* take care of default stripe inheritance */
1035 if (type == S_IFDIR) {
1036 struct lov_mds_md lmm;
1037 int lmm_size = sizeof(lmm);
1039 rc = mds_get_md(obd, dir, &lmm, &lmm_size, 1, 0);
1041 down(&inode->i_sem);
1042 rc = fsfilt_set_md(obd, inode, handle,
1043 &lmm, lmm_size, EA_LOV);
1047 CERROR("error on copy stripe info: rc = %d\n",
1053 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1054 mds_pack_inode2body(obd, body, inode, 1);
1059 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
1061 if (rc && created) {
1062 /* Destroy the file we just created. This should not need extra
1063 * journal credits, as we have already modified all of the
1064 * blocks needed in order to create the file in the first
1068 err = vfs_rmdir(dir, dchild);
1070 CERROR("rmdir in error path: %d\n", err);
1073 err = vfs_unlink(dir, dchild);
1075 CERROR("unlink in error path: %d\n", err);
1081 switch (cleanup_phase) {
1082 case 2: /* child dentry */
1084 case 1: /* locked parent dentry */
1086 if (lockh[1].cookie != 0)
1087 ldlm_lock_decref(lockh + 1, parent_mode);
1090 ldlm_lock_decref(lockh, LCK_PW);
1092 ptlrpc_save_lock(req, lockh, LCK_PW);
1098 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1102 OBD_FREE(mea, mea_size);
1103 req->rq_status = rc;
1108 res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
1109 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1113 for (i = 0; i < RES_NAME_SIZE; i++) {
1115 * this is needed to make zeroed res_id entries to be put at the
1116 * end of list in *ordered_locks() .
1118 if (res1->name[i] == 0 && res2->name[i] != 0)
1120 if (res2->name[i] == 0 && res1->name[i] != 0)
1122 if (res1->name[i] > res2->name[i])
1124 if (res1->name[i] < res2->name[i])
1131 if (memcmp(p1, p2, sizeof(*p1)) < 0)
1137 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1138 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1139 * because they take the place of local semaphores.
1141 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
1142 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
1143 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
1144 struct lustre_handle *p1_lockh, int p1_lock_mode,
1145 ldlm_policy_data_t *p1_policy,
1146 struct ldlm_res_id *p2_res_id,
1147 struct lustre_handle *p2_lockh, int p2_lock_mode,
1148 ldlm_policy_data_t *p2_policy)
1150 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1151 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1152 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1153 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
1157 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1159 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1160 res_id[0]->name[0], res_id[1]->name[0]);
1162 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1163 handles[1] = p1_lockh;
1164 handles[0] = p2_lockh;
1165 res_id[1] = p1_res_id;
1166 res_id[0] = p2_res_id;
1167 lock_modes[1] = p1_lock_mode;
1168 lock_modes[0] = p2_lock_mode;
1169 policies[1] = p1_policy;
1170 policies[0] = p2_policy;
1173 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1174 res_id[0]->name[0], res_id[1]->name[0]);
1176 flags = LDLM_FL_LOCAL_ONLY;
1177 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1178 LDLM_IBITS, policies[0], lock_modes[0], &flags,
1179 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1180 NULL, 0, NULL, handles[0]);
1183 ldlm_lock_dump_handle(D_OTHER, handles[0]);
1185 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1186 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1187 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1188 ldlm_lock_addref(handles[1], lock_modes[1]);
1189 } else if (res_id[1]->name[0] != 0) {
1190 flags = LDLM_FL_LOCAL_ONLY;
1191 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1192 *res_id[1], LDLM_IBITS, policies[1],
1193 lock_modes[1], &flags, mds_blocking_ast,
1194 ldlm_completion_ast, NULL, NULL, NULL, 0,
1196 if (rc != ELDLM_OK) {
1197 ldlm_lock_decref(handles[0], lock_modes[0]);
1200 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1206 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1207 struct lustre_handle *p1_lockh, int p1_lock_mode,
1208 ldlm_policy_data_t *p1_policy,
1209 struct ldlm_res_id *p2_res_id,
1210 struct lustre_handle *p2_lockh, int p2_lock_mode,
1211 ldlm_policy_data_t *p2_policy,
1212 struct ldlm_res_id *c1_res_id,
1213 struct lustre_handle *c1_lockh, int c1_lock_mode,
1214 ldlm_policy_data_t *c1_policy,
1215 struct ldlm_res_id *c2_res_id,
1216 struct lustre_handle *c2_lockh, int c2_lock_mode,
1217 ldlm_policy_data_t *c2_policy)
1219 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1220 c1_res_id, c2_res_id };
1221 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1222 c1_lockh, c2_lockh };
1223 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1224 c1_lock_mode, c2_lock_mode };
1225 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1226 c1_policy, c2_policy};
1227 int rc, i, j, sorted, flags;
1230 CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1231 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1232 res_id[3]->name[0]);
1235 * simple insertion sort - we have at most 4 elements. Note, that zeroed
1236 * res_id should be at the end of list after sorting is finished.
1238 for (i = 1; i < 4; i++) {
1240 dlm_handles[4] = dlm_handles[i];
1241 res_id[4] = res_id[i];
1242 lock_modes[4] = lock_modes[i];
1243 policies[4] = policies[i];
1247 if (res_gt(res_id[j], res_id[4], policies[j],
1249 dlm_handles[j + 1] = dlm_handles[j];
1250 res_id[j + 1] = res_id[j];
1251 lock_modes[j + 1] = lock_modes[j];
1252 policies[j + 1] = policies[j];
1257 } while (j >= 0 && !sorted);
1259 dlm_handles[j + 1] = dlm_handles[4];
1260 res_id[j + 1] = res_id[4];
1261 lock_modes[j + 1] = lock_modes[4];
1262 policies[j + 1] = policies[4];
1265 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1266 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1267 res_id[3]->name[0]);
1269 /* XXX we could send ASTs on all these locks first before blocking? */
1270 for (i = 0; i < 4; i++) {
1274 * nevertheless zeroed res_ids should be at the end of list, and
1275 * could use break here, I think, that it is more correctly for
1276 * clear understanding of code to have continue here, as it
1277 * clearly means, that zeroed res_id should be skipped and does
1278 * not mean, that if we meet zeroed res_id we should stop
1281 if (res_id[i]->name[0] == 0)
1285 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1286 (policies[i]->l_inodebits.bits &
1287 policies[i-1]->l_inodebits.bits) ) {
1288 memcpy(dlm_handles[i], dlm_handles[i-1],
1289 sizeof(*(dlm_handles[i])));
1290 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1292 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1293 *res_id[i], LDLM_IBITS,
1295 lock_modes[i], &flags,
1297 ldlm_completion_ast, NULL, NULL,
1298 NULL, 0, NULL, dlm_handles[i]);
1300 GOTO(out_err, rc = -EIO);
1301 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1308 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1313 /* In the unlikely case that the child changed while we were waiting
1314 * on the lock, we need to drop the lock on the old child and either:
1315 * - if the child has a lower resource name, then we have to also
1316 * drop the parent lock and regain the locks in the right order
1317 * - in the rename case, if the child has a lower resource name than one of
1318 * the other parent/child resources (maxres) we also need to reget the locks
1319 * - if the child has a higher resource name (this is the common case)
1320 * we can just get the lock on the new child (still in lock order)
1322 * Returns 0 if the child did not change or if it changed but could be locked.
1323 * Returns 1 if the child changed and we need to re-lock (no locks held).
1324 * Returns -ve error with a valid dchild (no locks held). */
1325 static int mds_verify_child(struct obd_device *obd,
1326 struct ldlm_res_id *parent_res_id,
1327 struct lustre_handle *parent_lockh,
1328 struct dentry *dparent, int parent_mode,
1329 struct ldlm_res_id *child_res_id,
1330 struct lustre_handle *child_lockh,
1331 struct dentry **dchildp, int child_mode,
1332 ldlm_policy_data_t *child_policy,
1333 const char *name, int namelen,
1334 struct ldlm_res_id *maxres,
1335 unsigned long child_ino, __u32 child_gen)
1337 struct lustre_id sid;
1338 struct dentry *vchild, *dchild = *dchildp;
1339 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1342 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1344 GOTO(cleanup, rc = PTR_ERR(vchild));
1346 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1347 if (child_gen == vchild->d_generation &&
1348 child_ino == vchild->d_inum) {
1357 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1358 (vchild->d_inode != NULL &&
1359 child_gen == vchild->d_inode->i_generation &&
1360 child_ino == vchild->d_inode->i_ino))) {
1368 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1369 vchild->d_inode, dchild ? dchild->d_inode : 0,
1370 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1371 child_res_id->name[0]);
1373 if (child_res_id->name[0] != 0)
1374 ldlm_lock_decref(child_lockh, child_mode);
1378 cleanup_phase = 1; /* parent lock only */
1379 *dchildp = dchild = vchild;
1381 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1384 if (dchild->d_inode) {
1385 down(&dchild->d_inode->i_sem);
1386 rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
1387 up(&dchild->d_inode->i_sem);
1389 CERROR("Can't read inode self id, inode %lu,"
1390 " rc %d\n", dchild->d_inode->i_ino, rc);
1393 child_res_id->name[0] = id_fid(&sid);
1394 child_res_id->name[1] = id_group(&sid);
1396 child_res_id->name[0] = dchild->d_fid;
1397 child_res_id->name[1] = dchild->d_mdsnum;
1400 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1401 res_gt(maxres, child_res_id, NULL, NULL)) {
1402 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1403 child_res_id->name[0], parent_res_id->name[0],
1405 GOTO(cleanup, rc = 1);
1408 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1409 *child_res_id, LDLM_IBITS, child_policy,
1410 child_mode, &flags, mds_blocking_ast,
1411 ldlm_completion_ast, NULL, NULL, NULL, 0,
1414 GOTO(cleanup, rc = -EIO);
1417 memset(child_res_id, 0, sizeof(*child_res_id));
1423 switch(cleanup_phase) {
1425 if (child_res_id->name[0] != 0)
1426 ldlm_lock_decref(child_lockh, child_mode);
1428 ldlm_lock_decref(parent_lockh, parent_mode);
1434 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1435 struct lustre_id *id,
1436 struct lustre_handle *parent_lockh,
1437 struct dentry **dparentp, int parent_mode,
1438 __u64 parent_lockpart, int *update_mode,
1439 char *name, int namelen,
1440 struct lustre_handle *child_lockh,
1441 struct dentry **dchildp, int child_mode,
1442 __u64 child_lockpart)
1444 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1445 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1446 struct ldlm_res_id parent_res_id = { .name = {0} };
1447 struct ldlm_res_id child_res_id = { .name = {0} };
1448 unsigned long child_ino = 0; __u32 child_gen = 0;
1449 int rc = 0, cleanup_phase = 0;
1450 struct lustre_id sid;
1451 struct inode *inode;
1454 /* Step 1: Lookup parent */
1455 *dparentp = mds_id2dentry(obd, id, NULL);
1456 if (IS_ERR(*dparentp)) {
1457 rc = PTR_ERR(*dparentp);
1462 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1463 (*dparentp)->d_inode->i_ino, name);
1465 parent_res_id.name[0] = id_fid(id);
1466 parent_res_id.name[1] = id_group(id);
1469 parent_lockh[1].cookie = 0;
1470 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1471 struct ldlm_res_id res_id = { .name = {0} };
1472 ldlm_policy_data_t policy;
1475 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1477 res_id.name[0] = id_fid(id);
1478 res_id.name[1] = id_group(id);
1479 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1481 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1482 res_id, LDLM_IBITS, &policy,
1483 *update_mode, &flags,
1485 ldlm_completion_ast,
1486 NULL, NULL, NULL, 0, NULL,
1492 parent_res_id.name[2] = full_name_hash(name, namelen - 1);
1494 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1495 (unsigned long)id_fid(id), (unsigned long)id_group(id),
1496 parent_res_id.name[2]);
1500 cleanup_phase = 1; /* parent dentry */
1502 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1503 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1504 if (IS_ERR(*dchildp)) {
1505 rc = PTR_ERR(*dchildp);
1506 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1510 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1512 * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1513 * lock. Drop possible UPDATE lock!
1515 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1516 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1518 child_res_id.name[0] = (*dchildp)->d_fid;
1519 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1520 child_ino = (*dchildp)->d_inum;
1521 child_gen = (*dchildp)->d_generation;
1525 inode = (*dchildp)->d_inode;
1527 inode = igrab(inode);
1531 down(&inode->i_sem);
1532 rc = mds_read_inode_sid(obd, inode, &sid);
1535 CERROR("Can't read inode self id, inode %lu, "
1536 "rc %d\n", inode->i_ino, rc);
1541 child_ino = inode->i_ino;
1542 child_gen = inode->i_generation;
1543 child_res_id.name[0] = id_fid(&sid);
1544 child_res_id.name[1] = id_group(&sid);
1548 cleanup_phase = 2; /* child dentry */
1550 /* Step 3: Lock parent and child in resource order. If child doesn't
1551 * exist, we still have to lock the parent and re-lookup. */
1552 rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1553 &parent_policy, &child_res_id, child_lockh,
1554 child_mode, &child_policy);
1558 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1559 cleanup_phase = 4; /* child lock */
1561 cleanup_phase = 3; /* parent lock */
1563 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1564 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1565 parent_mode, &child_res_id, child_lockh,
1566 dchildp, child_mode, &child_policy,
1567 name, namelen, &parent_res_id, child_ino,
1579 switch (cleanup_phase) {
1581 ldlm_lock_decref(child_lockh, child_mode);
1583 ldlm_lock_decref(parent_lockh, parent_mode);
1588 if (parent_lockh[1].cookie)
1589 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1597 void mds_reconstruct_generic(struct ptlrpc_request *req)
1599 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1600 mds_req_from_mcd(req, med->med_mcd);
1603 /* If we are unlinking an open file/dir (i.e. creating an orphan) then
1604 * we instead link the inode into the PENDING directory until it is
1605 * finally released. We can't simply call mds_reint_rename() or some
1606 * part thereof, because we don't have the inode to check for link
1607 * count/open status until after it is locked.
1609 * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1610 * before starting journal transaction.
1612 * returns 1 on success
1613 * returns 0 if we lost a race and didn't make a new link
1614 * returns negative on error
1616 static int mds_orphan_add_link(struct mds_update_record *rec,
1617 struct obd_device *obd, struct dentry *dentry)
1619 struct mds_obd *mds = &obd->u.mds;
1620 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1621 struct inode *inode = dentry->d_inode;
1622 struct dentry *pending_child;
1623 char idname[LL_ID_NAMELEN];
1624 int idlen = 0, rc, mode;
1627 LASSERT(inode != NULL);
1628 LASSERT(!mds_inode_is_orphan(inode));
1629 #ifndef HAVE_I_ALLOC_SEM
1630 LASSERT(down_trylock(&inode->i_sem) != 0);
1632 LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1634 idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1636 CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1637 mds_orphan_open_count(inode), inode->i_nlink,
1638 S_ISDIR(inode->i_mode) ? "dir" :
1639 S_ISREG(inode->i_mode) ? "file" : "other",
1640 rec->ur_name, idname);
1642 if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1645 pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1646 if (IS_ERR(pending_child))
1647 RETURN(PTR_ERR(pending_child));
1649 if (pending_child->d_inode != NULL) {
1650 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1651 LASSERT(pending_child->d_inode == inode);
1652 GOTO(out_dput, rc = 0);
1655 /* link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG
1656 * for linking and return real mode back then -bzzz */
1657 mode = inode->i_mode;
1658 inode->i_mode = S_IFREG;
1659 rc = vfs_link(dentry, pending_dir, pending_child);
1661 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1664 mds_inode_set_orphan(inode);
1666 /* return mode and correct i_nlink if inode is directory */
1667 inode->i_mode = mode;
1668 LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1669 S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1671 if (S_ISDIR(mode)) {
1673 pending_dir->i_nlink++;
1674 mark_inode_dirty(inode);
1675 mark_inode_dirty(pending_dir);
1680 l_dput(pending_child);
1684 int mds_create_local_dentry(struct mds_update_record *rec,
1685 struct obd_device *obd)
1687 struct mds_obd *mds = &obd->u.mds;
1688 struct inode *id_dir = mds->mds_id_dir->d_inode;
1689 int idlen = 0, rc, cleanup_phase = 0;
1690 struct dentry *new_child = NULL;
1691 char *idname = rec->ur_name;
1692 struct dentry *child = NULL;
1693 struct lustre_handle lockh[2] = {{0}, {0}};
1694 struct lustre_id sid;
1698 down(&id_dir->i_sem);
1699 idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1700 id_gen(rec->ur_id1));
1702 CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1703 idname, OLID4(rec->ur_id1));
1705 new_child = ll_lookup_one_len(idname, mds->mds_id_dir,
1708 if (IS_ERR(new_child)) {
1709 CERROR("can't lookup %s: %d\n", idname,
1710 (int) PTR_ERR(new_child));
1711 GOTO(cleanup, rc = PTR_ERR(new_child));
1715 down(&id_dir->i_sem);
1716 rc = mds_read_inode_sid(obd, id_dir, &sid);
1719 CERROR("Can't read inode self id, inode %lu, "
1720 "rc %d\n", id_dir->i_ino, rc);
1724 if (new_child->d_inode != NULL) {
1725 /* nice. we've already have local dentry! */
1726 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1727 (unsigned)new_child->d_inode->i_ino,
1728 (unsigned)new_child->d_inode->i_generation);
1730 id_ino(rec->ur_id1) = id_dir->i_ino;
1731 id_gen(rec->ur_id1) = id_dir->i_generation;
1732 rec->ur_namelen = idlen + 1;
1734 id_fid(rec->ur_id1) = id_fid(&sid);
1735 id_group(rec->ur_id1) = id_group(&sid);
1737 GOTO(cleanup, rc = 0);
1740 /* new, local dentry will be added soon. we need no aliases here */
1743 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1744 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1746 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1747 LCK_EX, lockh, NULL, NULL, 0,
1748 MDS_INODELOCK_UPDATE);
1751 if (IS_ERR(child)) {
1752 rc = PTR_ERR(child);
1753 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1754 CERROR("can't get victim: %d\n", rc);
1759 handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
1761 GOTO(cleanup, rc = PTR_ERR(handle));
1763 rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
1764 idlen, id_ino(rec->ur_id1),
1765 id_gen(rec->ur_id1), mds->mds_num,
1766 id_fid(rec->ur_id1));
1768 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1769 (unsigned long)child->d_inode->i_ino,
1770 (unsigned long)child->d_inode->i_generation, rc);
1772 if (S_ISDIR(child->d_inode->i_mode)) {
1774 mark_inode_dirty(id_dir);
1776 mark_inode_dirty(child->d_inode);
1778 fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
1780 id_ino(rec->ur_id1) = id_dir->i_ino;
1781 id_gen(rec->ur_id1) = id_dir->i_generation;
1782 rec->ur_namelen = idlen + 1;
1784 id_fid(rec->ur_id1) = id_fid(&sid);
1785 id_group(rec->ur_id1) = id_group(&sid);
1789 switch(cleanup_phase) {
1791 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1792 ldlm_lock_decref(lockh, LCK_EX);
1802 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1803 struct ptlrpc_request *slave)
1805 void *cookie, *cookie2;
1806 struct mds_body *body2;
1807 struct mds_body *body;
1811 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1812 LASSERT(body != NULL);
1814 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1815 LASSERT(body2 != NULL);
1817 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
1820 memcpy(body2, body, sizeof(*body));
1821 body2->valid &= ~OBD_MD_FLCOOKIE;
1823 if (!(body->valid & OBD_MD_FLEASIZE) &&
1824 !(body->valid & OBD_MD_FLDIREA))
1827 if (body->eadatasize == 0) {
1828 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1832 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1834 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1835 LASSERT(ea != NULL);
1837 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1838 LASSERT(ea2 != NULL);
1840 memcpy(ea2, ea, body->eadatasize);
1842 if (body->valid & OBD_MD_FLCOOKIE) {
1843 LASSERT(master->rq_repmsg->buflens[2] >=
1844 slave->rq_repmsg->buflens[2]);
1845 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1846 slave->rq_repmsg->buflens[2]);
1847 LASSERT(cookie != NULL);
1849 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1850 master->rq_repmsg->buflens[2]);
1851 LASSERT(cookie2 != NULL);
1852 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1853 body2->valid |= OBD_MD_FLCOOKIE;
1858 static int mds_reint_unlink_remote(struct mds_update_record *rec,
1859 int offset, struct ptlrpc_request *req,
1860 struct lustre_handle *parent_lockh,
1861 int update_mode, struct dentry *dparent,
1862 struct lustre_handle *child_lockh,
1863 struct dentry *dchild)
1865 struct obd_device *obd = req->rq_export->exp_obd;
1866 struct mds_obd *mds = mds_req2mds(req);
1867 struct ptlrpc_request *request = NULL;
1868 int rc = 0, cleanup_phase = 0;
1869 struct mdc_op_data *op_data;
1873 LASSERT(offset == 1 || offset == 3);
1875 /* time to drop i_nlink on remote MDS */
1876 OBD_ALLOC(op_data, sizeof(*op_data));
1877 if (op_data == NULL)
1880 memset(op_data, 0, sizeof(*op_data));
1881 mds_pack_dentry2id(obd, &op_data->id1, dchild, 1);
1882 op_data->create_mode = rec->ur_mode;
1884 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode "DLID4")",
1885 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1887 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1888 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode "DLID4")",
1889 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1892 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1893 op_data->create_mode |= MDS_MODE_REPLAY;
1895 rc = md_unlink(mds->mds_md_exp, op_data, &request);
1896 OBD_FREE(op_data, sizeof(*op_data));
1901 mds_copy_unlink_reply(req, request);
1902 ptlrpc_req_finished(request);
1906 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1909 GOTO(cleanup, rc = PTR_ERR(handle));
1910 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
1911 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1916 req->rq_status = rc;
1919 if (parent_lockh[1].cookie != 0)
1920 ldlm_lock_decref(parent_lockh + 1, update_mode);
1922 ldlm_lock_decref(child_lockh, LCK_EX);
1924 ldlm_lock_decref(parent_lockh, LCK_PW);
1926 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1933 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1934 struct ptlrpc_request *req,
1935 struct lustre_handle *lh)
1937 struct dentry *dparent = NULL, *dchild;
1938 struct mds_obd *mds = mds_req2mds(req);
1939 struct obd_device *obd = req->rq_export->exp_obd;
1940 struct mds_body *body = NULL;
1941 struct inode *child_inode = NULL;
1942 struct lustre_handle parent_lockh[2] = {{0}, {0}};
1943 struct lustre_handle child_lockh = {0};
1944 struct lustre_handle child_reuse_lockh = {0};
1945 struct lustre_handle *slave_lockh = NULL;
1946 char idname[LL_ID_NAMELEN];
1947 struct llog_create_locks *lcl = NULL;
1948 void *handle = NULL;
1949 int rc = 0, cleanup_phase = 0;
1950 int unlink_by_id = 0;
1954 LASSERT(offset == 1 || offset == 3);
1956 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1957 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
1960 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1962 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1963 DEBUG_REQ(D_HA, req, "unlink replay");
1964 LASSERT(offset == 1); /* should not come from intent */
1965 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
1966 lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
1967 req->rq_repmsg->buflens[2]);
1970 MD_COUNTER_INCREMENT(obd, unlink);
1972 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1973 GOTO(cleanup, rc = -ENOENT);
1975 if (rec->ur_namelen == 1) {
1976 /* this is request to drop i_nlink on local inode */
1978 rec->ur_name = idname;
1979 rc = mds_create_local_dentry(rec, obd);
1980 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
1981 DEBUG_REQ(D_HA, req,
1982 "drop nlink on inode "DLID4" (replay)",
1983 OLID4(rec->ur_id1));
1989 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1990 /* master mds for directory asks slave removing inode is already
1992 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1993 LCK_PW, parent_lockh,
1994 &update_mode, rec->ur_name,
1996 MDS_INODELOCK_UPDATE);
1997 if (IS_ERR(dparent))
1998 GOTO(cleanup, rc = PTR_ERR(dparent));
1999 dchild = ll_lookup_one_len(rec->ur_name, dparent,
2000 rec->ur_namelen - 1);
2002 GOTO(cleanup, rc = PTR_ERR(dchild));
2003 child_lockh.cookie = 0;
2004 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
2005 LASSERT(dchild->d_inode != NULL);
2006 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
2008 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
2009 parent_lockh, &dparent,
2010 LCK_PW, MDS_INODELOCK_UPDATE,
2011 &update_mode, rec->ur_name,
2012 rec->ur_namelen, &child_lockh,
2014 MDS_INODELOCK_LOOKUP |
2015 MDS_INODELOCK_UPDATE);
2020 if (dchild->d_flags & DCACHE_CROSS_REF) {
2021 /* we should have parent lock only here */
2022 LASSERT(unlink_by_id == 0);
2023 LASSERT(dchild->d_mdsnum != mds->mds_num);
2024 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
2025 update_mode, dparent, &child_lockh, dchild);
2029 cleanup_phase = 1; /* dchild, dparent, locks */
2032 child_inode = dchild->d_inode;
2033 if (child_inode == NULL) {
2034 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
2035 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
2036 GOTO(cleanup, rc = -ENOENT);
2039 cleanup_phase = 2; /* dchild has a lock */
2041 /* We have to do these checks ourselves, in case we are making an
2042 * orphan. The client tells us whether rmdir() or unlink() was called,
2043 * so we need to return appropriate errors (bug 72).
2045 * We don't have to check permissions, because vfs_rename (called from
2046 * mds_open_unlink_rename) also calls may_delete. */
2047 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
2048 if (!S_ISDIR(child_inode->i_mode))
2049 GOTO(cleanup, rc = -ENOTDIR);
2051 if (S_ISDIR(child_inode->i_mode))
2052 GOTO(cleanup, rc = -EISDIR);
2055 /* handle splitted dir */
2056 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
2060 /* Step 4: Get a lock on the ino to sync with creation WRT inode
2061 * reuse (see bug 2029). */
2062 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
2065 cleanup_phase = 3; /* child inum lock */
2067 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2069 /* ldlm_reply in buf[0] if called via intent */
2075 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2076 LASSERT(body != NULL);
2078 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2079 DOWN_READ_I_ALLOC_SEM(child_inode);
2080 cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2082 /* If this is potentially the last reference to this inode, get the
2083 * OBD EA data first so the client can destroy OST objects. We
2084 * only do the object removal later if no open files/links remain. */
2085 if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2086 child_inode->i_nlink == 1) {
2087 if (mds_orphan_open_count(child_inode) > 0) {
2088 /* need to lock pending_dir before transaction */
2089 down(&mds->mds_pending_dir->d_inode->i_sem);
2090 cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2091 } else if (S_ISREG(child_inode->i_mode)) {
2092 mds_pack_inode2body(obd, body, child_inode, 0);
2093 mds_pack_md(obd, req->rq_repmsg, offset + 1,
2094 body, child_inode, MDS_PACK_MD_LOCK, 0);
2098 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2099 switch (child_inode->i_mode & S_IFMT) {
2101 /* Drop any lingering child directories before we start our
2102 * transaction, to avoid doing multiple inode dirty/delete
2103 * in our compound transaction (bug 1321). */
2104 shrink_dcache_parent(dchild);
2105 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2108 GOTO(cleanup, rc = PTR_ERR(handle));
2109 rc = vfs_rmdir(dparent->d_inode, dchild);
2112 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2113 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2115 handle = fsfilt_start_log(obd, dparent->d_inode,
2116 FSFILT_OP_UNLINK, NULL,
2117 le32_to_cpu(lmm->lmm_stripe_count));
2119 GOTO(cleanup, rc = PTR_ERR(handle));
2120 rc = vfs_unlink(dparent->d_inode, dchild);
2128 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2131 GOTO(cleanup, rc = PTR_ERR(handle));
2132 rc = vfs_unlink(dparent->d_inode, dchild);
2135 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2138 GOTO(cleanup, rc = -EINVAL);
2141 if (rc == 0 && child_inode->i_nlink == 0) {
2142 if (mds_orphan_open_count(child_inode) > 0)
2143 rc = mds_orphan_add_link(rec, obd, dchild);
2146 GOTO(cleanup, rc = 0);
2148 if (!S_ISREG(child_inode->i_mode))
2151 if (!(body->valid & OBD_MD_FLEASIZE)) {
2152 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2153 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2154 } else if (mds_log_op_unlink(obd, child_inode,
2155 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2156 req->rq_repmsg->buflens[offset + 1],
2157 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2158 req->rq_repmsg->buflens[offset+2],
2160 body->valid |= OBD_MD_FLCOOKIE;
2171 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2172 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2173 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2175 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2177 CERROR("error on parent setattr: rc = %d\n", err);
2179 rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2180 handle, req, rc, 0);
2182 (void)obd_set_info(mds->mds_dt_exp, strlen("unlinked"),
2183 "unlinked", 0, NULL);
2184 switch(cleanup_phase) {
2185 case 5: /* pending_dir semaphore */
2186 up(&mds->mds_pending_dir->d_inode->i_sem);
2187 case 4: /* child inode semaphore */
2188 UP_READ_I_ALLOC_SEM(child_inode);
2189 /* handle splitted dir */
2191 /* master directory can be non-empty or something else ... */
2192 mds_unlink_slave_objs(obd, dchild);
2195 ptlrpc_save_llog_lock(req, lcl);
2196 case 3: /* child ino-reuse lock */
2197 if (rc && body != NULL) {
2198 // Don't unlink the OST objects if the MDS unlink failed
2202 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2204 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2205 case 2: /* child lock */
2206 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2207 if (child_lockh.cookie)
2208 ldlm_lock_decref(&child_lockh, LCK_EX);
2209 case 1: /* child and parent dentry, parent lock */
2211 if (parent_lockh[1].cookie != 0)
2212 ldlm_lock_decref(parent_lockh + 1, update_mode);
2215 ldlm_lock_decref(parent_lockh, LCK_PW);
2217 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2224 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2227 req->rq_status = rc;
2232 * to service requests from remote MDS to increment i_nlink
2234 static int mds_reint_link_acquire(struct mds_update_record *rec,
2235 int offset, struct ptlrpc_request *req,
2236 struct lustre_handle *lh)
2238 struct obd_device *obd = req->rq_export->exp_obd;
2239 struct ldlm_res_id src_res_id = { .name = {0} };
2240 struct lustre_handle *handle = NULL, src_lockh = {0};
2241 struct mds_obd *mds = mds_req2mds(req);
2242 int rc = 0, cleanup_phase = 0;
2243 struct dentry *de_src = NULL;
2244 ldlm_policy_data_t policy;
2248 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2249 obd->obd_name, OLID4(rec->ur_id1));
2251 /* Step 1: Lookup the source inode and target directory by ID */
2252 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2254 GOTO(cleanup, rc = PTR_ERR(de_src));
2255 cleanup_phase = 1; /* source dentry */
2257 src_res_id.name[0] = id_fid(rec->ur_id1);
2258 src_res_id.name[1] = id_group(rec->ur_id1);
2259 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2261 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2262 src_res_id, LDLM_IBITS, &policy,
2263 LCK_EX, &flags, mds_blocking_ast,
2264 ldlm_completion_ast, NULL, NULL,
2265 NULL, 0, NULL, &src_lockh);
2267 GOTO(cleanup, rc = -ENOLCK);
2268 cleanup_phase = 2; /* lock */
2270 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2272 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2273 if (IS_ERR(handle)) {
2274 rc = PTR_ERR(handle);
2277 de_src->d_inode->i_nlink++;
2278 mark_inode_dirty(de_src->d_inode);
2282 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2283 handle, req, rc, 0);
2284 switch (cleanup_phase) {
2287 ldlm_lock_decref(&src_lockh, LCK_EX);
2289 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2295 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2298 req->rq_status = rc;
2303 * request to link to foreign inode:
2304 * - acquire i_nlinks on this inode
2307 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2308 int offset, struct ptlrpc_request *req,
2309 struct lustre_handle *lh)
2311 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2312 struct obd_device *obd = req->rq_export->exp_obd;
2313 struct dentry *de_tgt_dir = NULL;
2314 struct mds_obd *mds = mds_req2mds(req);
2315 int rc = 0, cleanup_phase = 0;
2316 struct mdc_op_data *op_data;
2317 struct ptlrpc_request *request = NULL;
2321 DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2322 ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2323 OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2324 OLID4(rec->ur_id1));
2326 de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2327 tgt_dir_lockh, &update_mode,
2328 rec->ur_name, rec->ur_namelen - 1,
2329 MDS_INODELOCK_UPDATE);
2330 if (IS_ERR(de_tgt_dir))
2331 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2334 OBD_ALLOC(op_data, sizeof(*op_data));
2335 if (op_data == NULL)
2336 GOTO(cleanup, rc = -ENOMEM);
2338 memset(op_data, 0, sizeof(*op_data));
2339 op_data->id1 = *(rec->ur_id1);
2340 rc = md_link(mds->mds_md_exp, op_data, &request);
2341 OBD_FREE(op_data, sizeof(*op_data));
2344 ptlrpc_req_finished(request);
2350 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2352 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2353 if (IS_ERR(handle)) {
2354 rc = PTR_ERR(handle);
2360 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2361 rec->ur_namelen - 1, id_ino(rec->ur_id1),
2362 id_gen(rec->ur_id1), id_group(rec->ur_id1),
2363 id_fid(rec->ur_id1));
2366 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2367 handle, req, rc, 0);
2369 switch (cleanup_phase) {
2372 OBD_ALLOC(op_data, sizeof(*op_data));
2373 if (op_data != NULL) {
2375 memset(op_data, 0, sizeof(*op_data));
2377 op_data->id1 = *(rec->ur_id1);
2378 op_data->create_mode = rec->ur_mode;
2380 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2381 OBD_FREE(op_data, sizeof(*op_data));
2383 ptlrpc_req_finished(request);
2385 CERROR("error %d while dropping i_nlink on "
2386 "remote inode\n", rc);
2389 CERROR("rc %d prevented dropping i_nlink on "
2390 "remote inode\n", -ENOMEM);
2396 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2398 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2401 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2403 ptlrpc_save_lock(req, tgt_dir_lockh + 1, update_mode);
2409 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2412 req->rq_status = rc;
2416 static int mds_reint_link(struct mds_update_record *rec, int offset,
2417 struct ptlrpc_request *req,
2418 struct lustre_handle *lh)
2420 struct obd_device *obd = req->rq_export->exp_obd;
2421 struct dentry *de_src = NULL;
2422 struct dentry *de_tgt_dir = NULL;
2423 struct dentry *dchild = NULL;
2424 struct mds_obd *mds = mds_req2mds(req);
2425 struct lustre_handle *handle = NULL;
2426 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2427 struct ldlm_res_id src_res_id = { .name = {0} };
2428 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2429 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2430 ldlm_policy_data_t tgt_dir_policy =
2431 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2432 int rc = 0, cleanup_phase = 0;
2434 int update_mode = 0;
2438 LASSERT(offset == 1);
2440 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2441 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2442 id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2445 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2446 MD_COUNTER_INCREMENT(obd, link);
2448 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2449 GOTO(cleanup, rc = -ENOENT);
2451 if (id_group(rec->ur_id1) != mds->mds_num) {
2452 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2456 if (rec->ur_namelen == 1) {
2457 rc = mds_reint_link_acquire(rec, offset, req, lh);
2461 /* Step 1: Lookup the source inode and target directory by ID */
2462 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2464 GOTO(cleanup, rc = PTR_ERR(de_src));
2466 cleanup_phase = 1; /* source dentry */
2468 de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2469 if (IS_ERR(de_tgt_dir)) {
2470 rc = PTR_ERR(de_tgt_dir);
2475 cleanup_phase = 2; /* target directory dentry */
2477 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2478 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2479 rec->ur_name, de_src->d_inode->i_ino);
2481 /* Step 2: Take the two locks */
2482 src_res_id.name[0] = id_fid(rec->ur_id1);
2483 src_res_id.name[1] = id_group(rec->ur_id1);
2484 tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2485 tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2488 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2490 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2492 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2493 tgt_dir_res_id, LDLM_IBITS,
2494 &src_policy, update_mode, &flags,
2496 ldlm_completion_ast, NULL, NULL,
2497 NULL, 0, NULL, tgt_dir_lockh + 1);
2499 GOTO(cleanup, rc = -ENOLCK);
2502 tgt_dir_res_id.name[2] = full_name_hash(rec->ur_name,
2503 rec->ur_namelen - 1);
2504 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2505 (unsigned long)id_fid(rec->ur_id2),
2506 (unsigned long)id_group(rec->ur_id2),
2507 tgt_dir_res_id.name[2]);
2510 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2511 &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2512 LCK_EX, &tgt_dir_policy);
2516 cleanup_phase = 3; /* locks */
2518 /* Step 3: Lookup the child */
2519 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir,
2520 rec->ur_namelen - 1);
2521 if (IS_ERR(dchild)) {
2522 rc = PTR_ERR(dchild);
2523 if (rc != -EPERM && rc != -EACCES)
2524 CERROR("child lookup error %d\n", rc);
2528 cleanup_phase = 4; /* child dentry */
2530 if (dchild->d_inode) {
2531 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2532 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2537 /* Step 4: Do it. */
2538 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2540 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2541 if (IS_ERR(handle)) {
2542 rc = PTR_ERR(handle);
2546 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2547 if (rc && rc != -EPERM && rc != -EACCES)
2548 CERROR("vfs_link error %d\n", rc);
2550 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2551 handle, req, rc, 0);
2554 switch (cleanup_phase) {
2555 case 4: /* child dentry */
2559 ldlm_lock_decref(&src_lockh, LCK_EX);
2560 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2562 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2563 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2565 case 2: /* target dentry */
2567 if (tgt_dir_lockh[1].cookie && update_mode)
2568 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2572 case 1: /* source dentry */
2577 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2580 req->rq_status = rc;
2584 /* The idea here is that we need to get four locks in the end:
2585 * one on each parent directory, one on each child. We need to take
2586 * these locks in some kind of order (to avoid deadlocks), and the order
2587 * I selected is "increasing resource number" order. We need to look up
2588 * the children, however, before we know what the resource number(s) are.
2589 * Thus the following plan:
2591 * 1,2. Look up the parents
2592 * 3,4. Look up the children
2593 * 5. Take locks on the parents and children, in order
2594 * 6. Verify that the children haven't changed since they were looked up
2596 * If there was a race and the children changed since they were first looked
2597 * up, it is possible that mds_verify_child() will be able to just grab the
2598 * lock on the new child resource (if it has a higher resource than any other)
2599 * but we need to compare against not only its parent, but also against the
2600 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2602 * We need the fancy igrab() on the child inodes because we aren't holding a
2603 * lock on the parent after the lookup is done, so dentry->d_inode may change
2604 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2606 static int mds_get_parents_children_locked(struct obd_device *obd,
2607 struct mds_obd *mds,
2608 struct lustre_id *p1_id,
2609 struct dentry **de_srcdirp,
2610 struct lustre_id *p2_id,
2611 struct dentry **de_tgtdirp,
2613 const char *old_name, int old_len,
2614 struct dentry **de_oldp,
2615 const char *new_name, int new_len,
2616 struct dentry **de_newp,
2617 struct lustre_handle *dlm_handles,
2620 struct ldlm_res_id p1_res_id = { .name = {0} };
2621 struct ldlm_res_id p2_res_id = { .name = {0} };
2622 struct ldlm_res_id c1_res_id = { .name = {0} };
2623 struct ldlm_res_id c2_res_id = { .name = {0} };
2624 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2625 /* Only dentry should disappear, but the inode itself would be
2626 intact otherwise. */
2627 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2628 /* If something is going to be replaced, both dentry and inode locks are
2630 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_FULL}};
2631 struct ldlm_res_id *maxres_src, *maxres_tgt;
2632 struct inode *inode;
2633 int rc = 0, cleanup_phase = 0;
2634 __u32 child_gen1 = 0;
2635 __u32 child_gen2 = 0;
2636 unsigned long child_ino1 = 0;
2637 unsigned long child_ino2 = 0;
2640 /* Step 1: Lookup the source directory */
2641 *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2642 if (IS_ERR(*de_srcdirp))
2643 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2645 cleanup_phase = 1; /* source directory dentry */
2647 p1_res_id.name[0] = id_fid(p1_id);
2648 p1_res_id.name[1] = id_group(p1_id);
2650 /* Step 2: Lookup the target directory */
2651 if (id_equal_stc(p1_id, p2_id)) {
2652 *de_tgtdirp = dget(*de_srcdirp);
2654 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2655 if (IS_ERR(*de_tgtdirp)) {
2656 rc = PTR_ERR(*de_tgtdirp);
2662 cleanup_phase = 2; /* target directory dentry */
2664 p2_res_id.name[0] = id_fid(p2_id);
2665 p2_res_id.name[1] = id_group(p2_id);
2668 dlm_handles[5].cookie = 0;
2669 dlm_handles[6].cookie = 0;
2671 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2673 * get a temp lock on just fid, group to flush client cache and
2674 * to protect dirs from concurrent splitting.
2676 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2677 LCK_PW, &p_policy, &p2_res_id,
2678 &dlm_handles[6], LCK_PW, &p_policy);
2682 p1_res_id.name[2] = full_name_hash(old_name, old_len - 1);
2683 p2_res_id.name[2] = full_name_hash(new_name, new_len - 1);
2685 CDEBUG(D_INFO, "take locks on "
2686 LPX64":"LPX64":"LPX64", "LPX64":"LPX64":"LPX64"\n",
2687 p1_res_id.name[0], p1_res_id.name[1], p1_res_id.name[2],
2688 p2_res_id.name[0], p2_res_id.name[1], p2_res_id.name[2]);
2693 /* Step 3: Lookup the source child entry */
2694 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp,
2696 if (IS_ERR(*de_oldp)) {
2697 rc = PTR_ERR(*de_oldp);
2698 CERROR("old child lookup error (%.*s): %d\n",
2699 old_len - 1, old_name, rc);
2703 cleanup_phase = 4; /* original name dentry */
2705 inode = (*de_oldp)->d_inode;
2706 if (inode != NULL) {
2707 struct lustre_id sid;
2709 inode = igrab(inode);
2711 GOTO(cleanup, rc = -ENOENT);
2713 down(&inode->i_sem);
2714 rc = mds_read_inode_sid(obd, inode, &sid);
2717 CERROR("Can't read inode self id, inode %lu, "
2718 "rc %d\n", inode->i_ino, rc);
2723 child_ino1 = inode->i_ino;
2724 child_gen1 = inode->i_generation;
2725 c1_res_id.name[0] = id_fid(&sid);
2726 c1_res_id.name[1] = id_group(&sid);
2728 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2729 child_ino1 = (*de_oldp)->d_inum;
2730 child_gen1 = (*de_oldp)->d_generation;
2731 c1_res_id.name[0] = (*de_oldp)->d_fid;
2732 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2734 GOTO(cleanup, rc = -ENOENT);
2737 /* Step 4: Lookup the target child entry */
2738 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp,
2740 if (IS_ERR(*de_newp)) {
2741 rc = PTR_ERR(*de_newp);
2742 CERROR("new child lookup error (%.*s): %d\n",
2743 old_len - 1, old_name, rc);
2747 cleanup_phase = 5; /* target dentry */
2749 inode = (*de_newp)->d_inode;
2750 if (inode != NULL) {
2751 struct lustre_id sid;
2753 inode = igrab(inode);
2757 down(&inode->i_sem);
2758 rc = mds_read_inode_sid(obd, inode, &sid);
2761 CERROR("Can't read inode self id, inode %lu, "
2762 "rc %d\n", inode->i_ino, rc);
2766 child_ino2 = inode->i_ino;
2767 child_gen2 = inode->i_generation;
2768 c2_res_id.name[0] = id_fid(&sid);
2769 c2_res_id.name[1] = id_group(&sid);
2771 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2772 child_ino2 = (*de_newp)->d_inum;
2773 child_gen2 = (*de_newp)->d_generation;
2774 c2_res_id.name[0] = (*de_newp)->d_fid;
2775 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
2779 /* Step 5: Take locks on the parents and child(ren) */
2780 maxres_src = &p1_res_id;
2781 maxres_tgt = &p2_res_id;
2782 cleanup_phase = 5; /* target dentry */
2784 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
2785 maxres_src = &c1_res_id;
2786 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
2787 maxres_tgt = &c2_res_id;
2789 rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
2791 &p2_res_id, &dlm_handles[1], parent_mode,
2793 &c1_res_id, &dlm_handles[2], child_mode,
2795 &c2_res_id, &dlm_handles[3], child_mode,
2800 cleanup_phase = 6; /* parent and child(ren) locks */
2802 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2803 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2804 parent_mode, &c1_res_id, &dlm_handles[2],
2805 de_oldp, child_mode, &c1_policy, old_name, old_len,
2806 maxres_tgt, child_ino1, child_gen1);
2808 if (c2_res_id.name[0] != 0)
2809 ldlm_lock_decref(&dlm_handles[3], child_mode);
2810 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2817 if (!DENTRY_VALID(*de_oldp))
2818 GOTO(cleanup, rc = -ENOENT);
2820 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2821 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2822 parent_mode, &c2_res_id, &dlm_handles[3],
2823 de_newp, child_mode, &c2_policy, new_name,
2824 new_len, maxres_src, child_ino2, child_gen2);
2826 ldlm_lock_decref(&dlm_handles[2], child_mode);
2827 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2837 switch (cleanup_phase) {
2838 case 6: /* child lock(s) */
2839 if (c2_res_id.name[0] != 0)
2840 ldlm_lock_decref(&dlm_handles[3], child_mode);
2841 if (c1_res_id.name[0] != 0)
2842 ldlm_lock_decref(&dlm_handles[2], child_mode);
2843 if (dlm_handles[1].cookie != 0)
2844 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2845 if (dlm_handles[0].cookie != 0)
2846 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2847 case 5: /* target dentry */
2849 case 4: /* source dentry */
2853 if (dlm_handles[5].cookie != 0)
2854 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2855 if (dlm_handles[6].cookie != 0)
2856 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2858 case 2: /* target directory dentry */
2859 l_dput(*de_tgtdirp);
2860 case 1: /* source directry dentry */
2861 l_dput(*de_srcdirp);
2869 * checks if dentry can be removed. This function also handles cross-ref
2872 static int mds_check_for_rename(struct obd_device *obd,
2873 struct dentry *dentry)
2875 struct mds_obd *mds = &obd->u.mds;
2876 struct lustre_handle *rlockh;
2877 struct ptlrpc_request *req;
2878 struct mdc_op_data *op_data;
2879 struct lookup_intent it;
2880 int handle_size, rc = 0;
2883 LASSERT(dentry != NULL);
2885 if (dentry->d_inode) {
2886 if (S_ISDIR(dentry->d_inode->i_mode) &&
2887 !mds_is_dir_empty(obd, dentry))
2890 LASSERT((dentry->d_flags & DCACHE_CROSS_REF));
2891 handle_size = sizeof(struct lustre_handle);
2893 OBD_ALLOC(rlockh, handle_size);
2897 memset(rlockh, 0, handle_size);
2898 OBD_ALLOC(op_data, sizeof(*op_data));
2899 if (op_data == NULL) {
2900 OBD_FREE(rlockh, handle_size);
2903 memset(op_data, 0, sizeof(*op_data));
2904 mds_pack_dentry2id(obd, &op_data->id1, dentry, 1);
2906 it.it_op = IT_UNLINK;
2907 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
2910 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
2911 op_data, rlockh, NULL, 0, ldlm_completion_ast,
2912 mds_blocking_ast, NULL);
2913 OBD_FREE(op_data, sizeof(*op_data));
2917 OBD_FREE(it.d.fs_data,
2918 sizeof(struct lustre_intent_data));
2921 if (rlockh->cookie != 0)
2922 ldlm_lock_decref(rlockh, LCK_EX);
2924 if (LUSTRE_IT(&it)->it_data) {
2925 req = (struct ptlrpc_request *)LUSTRE_IT(&it)->it_data;
2926 ptlrpc_req_finished(req);
2929 if (LUSTRE_IT(&it)->it_status)
2930 rc = LUSTRE_IT(&it)->it_status;
2931 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
2932 OBD_FREE(rlockh, handle_size);
2937 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
2938 struct ptlrpc_request *req, struct lustre_id *id,
2939 struct dentry *de_dir, struct dentry *de)
2941 struct obd_device *obd = req->rq_export->exp_obd;
2942 struct mds_obd *mds = mds_req2mds(req);
2943 void *handle = NULL;
2949 * name exists and points to local inode try to unlink this name
2950 * and create new one.
2952 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
2953 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
2954 (unsigned long)de->d_inode->i_generation);
2956 /* checking if we can remove local dentry. */
2957 rc = mds_check_for_rename(obd, de);
2961 handle = fsfilt_start(obd, de_dir->d_inode,
2962 FSFILT_OP_RENAME, NULL);
2964 GOTO(cleanup, rc = PTR_ERR(handle));
2965 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
2968 } else if (de->d_flags & DCACHE_CROSS_REF) {
2969 CDEBUG(D_OTHER, "%s: %s points to remote inode %lu/%lu\n",
2970 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_mdsnum,
2971 (unsigned long)de->d_fid);
2973 /* checking if we can remove local dentry. */
2974 rc = mds_check_for_rename(obd, de);
2979 * to be fully POSIX compatible, we should add one more check:
2981 * if de_new is subdir of dir rec->ur_id1. If so - return
2984 * I do not know how to implement it right now, because
2985 * inodes/dentries for new and old names lie on different MDS,
2986 * so add this notice here just to make it visible for the rest
2987 * of developers and do not forget about. And when this check
2988 * will be added, del_cross_ref should gone, that is local
2989 * dentry is able to be removed if all checks passed. --umka
2992 handle = fsfilt_start(obd, de_dir->d_inode,
2993 FSFILT_OP_RENAME, NULL);
2995 GOTO(cleanup, rc = PTR_ERR(handle));
2996 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3000 /* name doesn't exist. the simplest case. */
3001 handle = fsfilt_start(obd, de_dir->d_inode,
3002 FSFILT_OP_LINK, NULL);
3004 GOTO(cleanup, rc = PTR_ERR(handle));
3007 rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
3008 rec->ur_tgtlen - 1, id_ino(id),
3009 id_gen(id), id_group(id), id_fid(id));
3011 CERROR("add_dir_entry() returned error %d\n", rc);
3017 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3018 handle, req, rc, 0);
3023 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
3024 struct ptlrpc_request *req, struct dentry *de_dir,
3027 struct obd_device *obd = req->rq_export->exp_obd;
3028 struct mds_obd *mds = mds_req2mds(req);
3029 void *handle = NULL;
3033 handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
3035 GOTO(cleanup, rc = PTR_ERR(handle));
3036 rc = fsfilt_del_dir_entry(obd, de);
3041 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3042 handle, req, rc, 0);
3046 static int mds_reint_rename_create_name(struct mds_update_record *rec,
3047 int offset, struct ptlrpc_request *req)
3049 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3050 struct obd_device *obd = req->rq_export->exp_obd;
3051 struct mds_obd *mds = mds_req2mds(req);
3052 struct lustre_handle child_lockh = {0};
3053 struct dentry *de_tgtdir = NULL;
3054 struct dentry *de_new = NULL;
3055 int cleanup_phase = 0;
3056 int update_mode, rc = 0;
3060 * another MDS executing rename operation has asked us to create target
3061 * name. such a creation should destroy existing target name.
3063 CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
3064 obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
3066 /* first, lookup the target */
3067 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
3068 &de_tgtdir, LCK_PW, MDS_INODELOCK_UPDATE,
3069 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
3070 &child_lockh, &de_new, LCK_EX,
3071 MDS_INODELOCK_LOOKUP);
3078 LASSERT(de_tgtdir->d_inode);
3081 rc = mds_add_local_dentry(rec, offset, req, rec->ur_id1,
3087 if (cleanup_phase == 1) {
3089 if (parent_lockh[1].cookie != 0)
3090 ldlm_lock_decref(parent_lockh + 1, update_mode);
3092 ldlm_lock_decref(parent_lockh, LCK_PW);
3093 if (child_lockh.cookie != 0)
3094 ldlm_lock_decref(&child_lockh, LCK_EX);
3099 req->rq_status = rc;
3103 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
3104 struct ptlrpc_request *req)
3106 struct obd_device *obd = req->rq_export->exp_obd;
3107 struct ptlrpc_request *req2 = NULL;
3108 struct dentry *de_srcdir = NULL;
3109 struct dentry *de_old = NULL;
3110 struct mds_obd *mds = mds_req2mds(req);
3111 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3112 struct lustre_handle child_lockh = {0};
3113 struct mdc_op_data *op_data;
3114 int update_mode, rc = 0;
3117 CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
3118 obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
3120 OBD_ALLOC(op_data, sizeof(*op_data));
3121 if (op_data == NULL)
3123 memset(op_data, 0, sizeof(*op_data));
3125 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
3126 &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
3127 &update_mode, rec->ur_name,
3128 rec->ur_namelen, &child_lockh, &de_old,
3129 LCK_EX, MDS_INODELOCK_LOOKUP);
3131 OBD_FREE(op_data, sizeof(*op_data));
3136 LASSERT(de_srcdir->d_inode);
3140 * we already know the target should be created on another MDS so, we
3141 * have to request that MDS to do it.
3144 /* prepare source id */
3145 if (de_old->d_flags & DCACHE_CROSS_REF) {
3146 LASSERT(de_old->d_inode == NULL);
3147 CDEBUG(D_OTHER, "request to move remote name\n");
3148 mds_pack_dentry2id(obd, &op_data->id1, de_old, 1);
3149 } else if (de_old->d_inode == NULL) {
3150 /* oh, source doesn't exist */
3151 OBD_FREE(op_data, sizeof(*op_data));
3152 GOTO(cleanup, rc = -ENOENT);
3154 struct lustre_id sid;
3155 struct inode *inode = de_old->d_inode;
3157 LASSERT(inode != NULL);
3158 CDEBUG(D_OTHER, "request to move local name\n");
3159 id_ino(&op_data->id1) = inode->i_ino;
3160 id_group(&op_data->id1) = mds->mds_num;
3161 id_gen(&op_data->id1) = inode->i_generation;
3163 down(&inode->i_sem);
3164 rc = mds_read_inode_sid(obd, inode, &sid);
3167 CERROR("Can't read inode self id, "
3168 "inode %lu, rc = %d\n",
3173 id_fid(&op_data->id1) = id_fid(&sid);
3176 op_data->id2 = *rec->ur_id2;
3177 rc = md_rename(mds->mds_md_exp, op_data, NULL, 0,
3178 rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3179 OBD_FREE(op_data, sizeof(*op_data));
3184 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3190 ptlrpc_req_finished(req2);
3193 if (parent_lockh[1].cookie != 0)
3194 ldlm_lock_decref(parent_lockh + 1, update_mode);
3196 ldlm_lock_decref(parent_lockh, LCK_PW);
3197 if (child_lockh.cookie != 0)
3198 ldlm_lock_decref(&child_lockh, LCK_EX);
3203 req->rq_status = rc;
3207 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3208 struct ptlrpc_request *req, struct lustre_handle *lockh)
3210 struct obd_device *obd = req->rq_export->exp_obd;
3211 struct dentry *de_srcdir = NULL;
3212 struct dentry *de_tgtdir = NULL;
3213 struct dentry *de_old = NULL;
3214 struct dentry *de_new = NULL;
3215 struct inode *old_inode = NULL, *new_inode = NULL;
3216 struct mds_obd *mds = mds_req2mds(req);
3217 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3218 struct mds_body *body = NULL;
3219 struct llog_create_locks *lcl = NULL;
3220 struct lov_mds_md *lmm = NULL;
3221 int rc = 0, cleanup_phase = 0;
3222 void *handle = NULL;
3225 LASSERT(offset == 1);
3227 DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3228 OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3231 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3233 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3234 DEBUG_REQ(D_HA, req, "rename replay\n");
3235 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3236 lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3237 req->rq_repmsg->buflens[2]);
3240 MD_COUNTER_INCREMENT(obd, rename);
3242 if (rec->ur_namelen == 1) {
3243 rc = mds_reint_rename_create_name(rec, offset, req);
3247 /* check if new name should be located on remote target. */
3248 if (id_group(rec->ur_id2) != mds->mds_num) {
3249 rc = mds_reint_rename_to_remote(rec, offset, req);
3253 rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3254 rec->ur_id2, &de_tgtdir, LCK_PW,
3255 rec->ur_name, rec->ur_namelen,
3256 &de_old, rec->ur_tgt,
3257 rec->ur_tgtlen, &de_new,
3258 dlm_handles, LCK_EX);
3262 cleanup_phase = 1; /* parent(s), children, locks */
3263 old_inode = de_old->d_inode;
3264 new_inode = de_new->d_inode;
3266 /* sanity check for src inode */
3267 if (de_old->d_flags & DCACHE_CROSS_REF) {
3268 LASSERT(de_old->d_inode == NULL);
3271 * in the case of cross-ref dir, we can perform this check only
3272 * if child and parent lie on the same mds. This is because
3273 * otherwise they can have the same inode numbers.
3275 if (de_old->d_mdsnum == mds->mds_num) {
3276 if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3277 de_old->d_inum == de_tgtdir->d_inode->i_ino)
3278 GOTO(cleanup, rc = -EINVAL);
3281 LASSERT(de_old->d_inode != NULL);
3282 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3283 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3284 GOTO(cleanup, rc = -EINVAL);
3287 /* sanity check for dest inode */
3288 if (de_new->d_flags & DCACHE_CROSS_REF) {
3289 LASSERT(new_inode == NULL);
3291 /* the same check about target dentry. */
3292 if (de_new->d_mdsnum == mds->mds_num) {
3293 if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3294 de_new->d_inum == de_tgtdir->d_inode->i_ino)
3295 GOTO(cleanup, rc = -EINVAL);
3299 * regular files usualy do not have ->rename() implemented. But
3300 * we handle only this case when @de_new is cross-ref entry,
3301 * because in other cases it will be handled by vfs_rename().
3303 if (de_old->d_inode && (!de_old->d_inode->i_op ||
3304 !de_old->d_inode->i_op->rename))
3305 GOTO(cleanup, rc = -EPERM);
3308 (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3309 new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3310 GOTO(cleanup, rc = -EINVAL);
3315 * check if inodes point to each other. This should be checked before
3316 * is_subdir() check, as for the same entries it will think that they
3319 if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3320 !(de_new->d_flags & DCACHE_CROSS_REF) &&
3321 old_inode == new_inode)
3322 GOTO(cleanup, rc = 0);
3324 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3326 * check if we are moving old entry into its child. 2.6 does not check
3327 * for this in vfs_rename() anymore.
3329 if (is_subdir(de_new, de_old))
3330 GOTO(cleanup, rc = -EINVAL);
3334 * if we are about to remove the target at first, pass the EA of that
3335 * inode to client to perform and cleanup on OST.
3337 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3338 LASSERT(body != NULL);
3340 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3342 DOWN_READ_I_ALLOC_SEM(new_inode);
3344 cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3346 if (new_inode && ((S_ISDIR(new_inode->i_mode) &&
3347 new_inode->i_nlink == 2) ||
3348 new_inode->i_nlink == 1)) {
3349 if (mds_orphan_open_count(new_inode) > 0) {
3350 /* need to lock pending_dir before transaction */
3351 down(&mds->mds_pending_dir->d_inode->i_sem);
3352 cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3353 } else if (S_ISREG(new_inode->i_mode)) {
3354 mds_pack_inode2body(obd, body, new_inode, 0);
3355 mds_pack_md(obd, req->rq_repmsg, 1, body,
3356 new_inode, MDS_PACK_MD_LOCK, 0);
3360 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3361 de_srcdir->d_inode->i_sb);
3363 if (de_old->d_flags & DCACHE_CROSS_REF) {
3364 struct lustre_id old_id;
3366 mds_pack_dentry2id(obd, &old_id, de_old, 1);
3368 rc = mds_add_local_dentry(rec, offset, req, &old_id,
3373 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3378 lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3379 handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3380 NULL, le32_to_cpu(lmm->lmm_stripe_count));
3383 GOTO(cleanup, rc = PTR_ERR(handle));
3386 de_old->d_fsdata = req;
3387 de_new->d_fsdata = req;
3388 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3391 if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3392 if (mds_orphan_open_count(new_inode) > 0)
3393 rc = mds_orphan_add_link(rec, obd, de_new);
3396 GOTO(cleanup, rc = 0);
3398 if (!S_ISREG(new_inode->i_mode))
3401 if (!(body->valid & OBD_MD_FLEASIZE)) {
3402 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3403 OBD_MD_FLATIME | OBD_MD_FLMTIME);
3404 } else if (mds_log_op_unlink(obd, new_inode,
3405 lustre_msg_buf(req->rq_repmsg,1,0),
3406 req->rq_repmsg->buflens[1],
3407 lustre_msg_buf(req->rq_repmsg,2,0),
3408 req->rq_repmsg->buflens[2],
3410 body->valid |= OBD_MD_FLCOOKIE;
3416 rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3417 handle, req, rc, 0);
3419 switch (cleanup_phase) {
3421 up(&mds->mds_pending_dir->d_inode->i_sem);
3424 UP_READ_I_ALLOC_SEM(new_inode);
3427 if (dlm_handles[5].cookie != 0)
3428 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3429 if (dlm_handles[6].cookie != 0)
3430 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3433 ptlrpc_save_llog_lock(req, lcl);
3436 if (dlm_handles[3].cookie != 0)
3437 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3438 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3439 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3440 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3442 if (dlm_handles[3].cookie != 0)
3443 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3444 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3445 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3446 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3455 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3458 req->rq_status = rc;
3462 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3463 struct ptlrpc_request *, struct lustre_handle *);
3465 static mds_reinter reinters[REINT_MAX + 1] = {
3466 [REINT_SETATTR] mds_reint_setattr,
3467 [REINT_CREATE] mds_reint_create,
3468 [REINT_LINK] mds_reint_link,
3469 [REINT_UNLINK] mds_reint_unlink,
3470 [REINT_RENAME] mds_reint_rename,
3471 [REINT_OPEN] mds_open
3474 int mds_reint_rec(struct mds_update_record *rec, int offset,
3475 struct ptlrpc_request *req, struct lustre_handle *lockh)
3477 struct obd_device *obd = req->rq_export->exp_obd;
3478 struct lvfs_run_ctxt saved;
3481 /* checked by unpacker */
3482 LASSERT(rec->ur_opcode <= REINT_MAX &&
3483 reinters[rec->ur_opcode] != NULL);
3485 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3486 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3487 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);