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 mcd->mcd_last_transno = cpu_to_le64(transno);
144 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
145 mcd->mcd_last_result = cpu_to_le32(rc);
146 mcd->mcd_last_data = cpu_to_le32(op_data);
148 fsfilt_add_journal_cb(obd, mds->mds_sb, transno, handle,
149 mds_commit_last_transno_cb, NULL);
151 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd,
152 sizeof(*mcd), &off, 0);
160 DEBUG_REQ(log_pri, req,
161 "wrote trans #"LPU64" client %s at idx %u: err = %d",
162 transno, mcd->mcd_uuid, med->med_idx, err);
164 err = mds_update_last_fid(obd, handle, 0);
171 err = mds_dt_write_objids(obd);
177 CDEBUG(log_pri, "wrote objids: err = %d\n", err);
181 err = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
183 CERROR("error committing transaction: %d\n", err);
191 /* this gives the same functionality as the code between
192 * sys_chmod and inode_setattr
193 * chown_common and inode_setattr
194 * utimes and inode_setattr
197 /* Just for the case if we have some clients that know about ATTR_RAW */
198 #define ATTR_RAW 8192
200 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
202 time_t now = LTIME_S(CURRENT_TIME);
203 struct iattr *attr = &rec->ur_iattr;
204 unsigned int ia_valid = attr->ia_valid;
208 /* only fix up attrs if the client VFS didn't already */
210 if (!(ia_valid & ATTR_RAW))
213 if (!(ia_valid & ATTR_CTIME_SET))
214 LTIME_S(attr->ia_ctime) = now;
215 if (!(ia_valid & ATTR_ATIME_SET))
216 LTIME_S(attr->ia_atime) = now;
217 if (!(ia_valid & ATTR_MTIME_SET))
218 LTIME_S(attr->ia_mtime) = now;
220 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
224 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
225 if (rec->ur_fsuid != inode->i_uid &&
226 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
230 if (ia_valid & ATTR_SIZE) {
231 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
235 if (ia_valid & ATTR_UID) {
238 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
240 if (attr->ia_uid == (uid_t) -1)
241 attr->ia_uid = inode->i_uid;
242 if (attr->ia_gid == (gid_t) -1)
243 attr->ia_gid = inode->i_gid;
244 attr->ia_mode = inode->i_mode;
246 * If the user or group of a non-directory has been
247 * changed by a non-root user, remove the setuid bit.
248 * 19981026 David C Niemi <niemi@tux.org>
250 * Changed this to apply to all users, including root,
251 * to avoid some races. This is the behavior we had in
252 * 2.0. The check for non-root was definitely wrong
253 * for 2.2 anyway, as it should have been using
254 * CAP_FSETID rather than fsuid -- 19990830 SD.
256 if ((inode->i_mode & S_ISUID) == S_ISUID &&
257 !S_ISDIR(inode->i_mode)) {
258 attr->ia_mode &= ~S_ISUID;
259 attr->ia_valid |= ATTR_MODE;
262 * Likewise, if the user or group of a non-directory
263 * has been changed by a non-root user, remove the
264 * setgid bit UNLESS there is no group execute bit
265 * (this would be a file marked for mandatory
266 * locking). 19981026 David C Niemi <niemi@tux.org>
268 * Removed the fsuid check (see the comment above) --
271 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
272 (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
273 attr->ia_mode &= ~S_ISGID;
274 attr->ia_valid |= ATTR_MODE;
276 } else if (ia_valid & ATTR_MODE) {
277 int mode = attr->ia_mode;
279 if (attr->ia_mode == (mode_t) -1)
280 attr->ia_mode = inode->i_mode;
282 (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
287 void mds_steal_ack_locks(struct ptlrpc_request *req)
289 struct obd_export *exp = req->rq_export;
290 char str[PTL_NALFMT_SIZE];
291 struct list_head *tmp;
292 struct ptlrpc_reply_state *oldrep;
293 struct ptlrpc_service *svc;
294 struct llog_create_locks *lcl;
298 /* CAVEAT EMPTOR: spinlock order */
299 spin_lock_irqsave (&exp->exp_lock, flags);
300 list_for_each (tmp, &exp->exp_outstanding_replies) {
301 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
303 if (oldrep->rs_xid != req->rq_xid)
306 if (oldrep->rs_msg->opc != req->rq_reqmsg->opc)
307 CERROR ("Resent req xid "LPX64" has mismatched opc: "
308 "new %d old %d\n", req->rq_xid,
309 req->rq_reqmsg->opc, oldrep->rs_msg->opc);
311 svc = oldrep->rs_srv_ni->sni_service;
312 spin_lock (&svc->srv_lock);
314 list_del_init (&oldrep->rs_exp_list);
316 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
317 " o%d NID %s\n", oldrep->rs_nlocks, oldrep,
318 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg->opc,
319 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
321 for (i = 0; i < oldrep->rs_nlocks; i++)
322 ptlrpc_save_lock(req,
323 &oldrep->rs_locks[i],
324 oldrep->rs_modes[i]);
325 oldrep->rs_nlocks = 0;
327 lcl = oldrep->rs_llog_locks;
328 oldrep->rs_llog_locks = NULL;
330 ptlrpc_save_llog_lock(req, lcl);
332 DEBUG_REQ(D_HA, req, "stole locks for");
333 ptlrpc_schedule_difficult_reply (oldrep);
335 spin_unlock (&svc->srv_lock);
336 spin_unlock_irqrestore (&exp->exp_lock, flags);
339 spin_unlock_irqrestore (&exp->exp_lock, flags);
342 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
344 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
345 mcd->mcd_last_transno, mcd->mcd_last_result);
346 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
347 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
349 mds_steal_ack_locks(req);
352 static void reconstruct_reint_setattr(struct mds_update_record *rec,
353 int offset, struct ptlrpc_request *req)
355 struct mds_export_data *med = &req->rq_export->exp_mds_data;
356 struct mds_body *body;
359 mds_req_from_mcd(req, med->med_mcd);
361 de = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
363 LASSERT(PTR_ERR(de) == req->rq_status);
367 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
368 mds_pack_inode2body(req2obd(req), body, de->d_inode, 1);
370 /* Don't return OST-specific attributes if we didn't just set them */
371 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
372 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
373 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
374 body->valid |= OBD_MD_FLMTIME;
375 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
376 body->valid |= OBD_MD_FLATIME;
381 /* In the raw-setattr case, we lock the child inode.
382 * In the write-back case or if being called from open, the client holds a lock
385 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
386 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
387 struct ptlrpc_request *req, struct lustre_handle *lh)
389 struct mds_obd *mds = mds_req2mds(req);
390 struct obd_device *obd = req->rq_export->exp_obd;
391 struct mds_body *body;
393 struct inode *inode = NULL;
394 struct lustre_handle lockh[2] = {{0}, {0}};
397 struct mds_logcancel_data *mlcd = NULL;
398 int rc = 0, cleanup_phase = 0, err;
402 LASSERT(offset == 1);
404 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x",
405 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
406 rec->ur_iattr.ia_valid);
408 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
409 MD_COUNTER_INCREMENT(obd, setattr);
411 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
412 de = mds_id2dentry(obd, rec->ur_id1, NULL);
414 GOTO(cleanup, rc = PTR_ERR(de));
416 __u64 lockpart = MDS_INODELOCK_UPDATE;
417 if (rec->ur_iattr.ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
418 lockpart |= MDS_INODELOCK_LOOKUP;
419 de = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
420 lockh, &parent_mode, NULL, 0, lockpart);
422 GOTO(cleanup, rc = PTR_ERR(de));
430 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
431 rec->ur_eadata != NULL)
434 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
436 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
438 GOTO(cleanup, rc = PTR_ERR(handle));
440 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
441 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
442 LTIME_S(rec->ur_iattr.ia_mtime),
443 LTIME_S(rec->ur_iattr.ia_ctime));
444 rc = mds_fix_attr(inode, rec);
448 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
449 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
450 (long)&rec->ur_iattr.ia_attr_flags);
452 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
455 if (rec->ur_iattr.ia_valid & ATTR_EA) {
456 int flags = (int)rec->ur_iattr.ia_attr_flags;
459 if (inode->i_op && inode->i_op->setxattr)
460 rc = inode->i_op->setxattr(de, rec->ur_eadata,
461 rec->ur_ea2data, rec->ur_ea2datalen,
463 } else if (rec->ur_iattr.ia_valid & ATTR_EA_RM) {
465 if (inode->i_op && inode->i_op->removexattr)
466 rc = inode->i_op->removexattr(de,
468 } else if ((S_ISREG(inode->i_mode) ||
469 S_ISDIR(inode->i_mode)) && rec->ur_eadata != NULL) {
470 struct lov_stripe_md *lsm = NULL;
472 rc = ll_permission(inode, MAY_WRITE, NULL);
476 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, mds->mds_dt_exp,
477 0, &lsm, rec->ur_eadata);
481 obd_free_memmd(mds->mds_dt_exp, &lsm);
483 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
490 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
491 mds_pack_inode2body(obd, body, inode, 1);
493 /* Don't return OST-specific attributes if we didn't just set them */
494 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
495 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
496 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
497 body->valid |= OBD_MD_FLMTIME;
498 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
499 body->valid |= OBD_MD_FLATIME;
501 /* The logcookie should be no use anymore, why nobody remove
502 * following code block?
504 LASSERT(rec->ur_cookielen == 0);
505 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_dt_obd)) {
506 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
509 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
511 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
512 mlcd->mlcd_cookielen = rec->ur_cookielen;
513 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
514 mlcd->mlcd_cookielen;
515 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
516 mlcd->mlcd_cookielen);
517 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
518 mlcd->mlcd_eadatalen);
520 CERROR("unable to allocate log cancel data\n");
526 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
527 handle, mds_cancel_cookies_cb, mlcd);
528 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
529 switch (cleanup_phase) {
531 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
532 rec->ur_eadata != NULL)
537 if (lockh[1].cookie != 0)
538 ldlm_lock_decref(lockh + 1, parent_mode);
541 ldlm_lock_decref(lockh, LCK_PW);
543 ptlrpc_save_lock (req, lockh, LCK_PW);
558 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
559 struct ptlrpc_request *req)
561 struct mds_export_data *med = &req->rq_export->exp_mds_data;
562 struct dentry *parent, *child;
563 struct mds_body *body;
566 mds_req_from_mcd(req, med->med_mcd);
568 if (req->rq_status) {
573 parent = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
574 LASSERT(!IS_ERR(parent));
575 child = ll_lookup_one_len(rec->ur_name, parent,
576 rec->ur_namelen - 1);
577 LASSERT(!IS_ERR(child));
578 if ((child->d_flags & DCACHE_CROSS_REF)) {
579 LASSERTF(child->d_inode == NULL, "BUG 3869\n");
580 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
581 mds_pack_dentry2body(req2obd(req), body, child, 1);
582 } else if (child->d_inode == NULL) {
583 DEBUG_REQ(D_ERROR, req, "parent "DLID4" name %s mode %o",
584 OLID4(rec->ur_id1), rec->ur_name, rec->ur_mode);
585 LASSERTF(child->d_inode != NULL, "BUG 3869\n");
587 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
588 mds_pack_inode2body(req2obd(req), body, child->d_inode, 1);
595 static int mds_reint_create(struct mds_update_record *rec, int offset,
596 struct ptlrpc_request *req,
597 struct lustre_handle *lh)
599 struct dentry *dparent = NULL;
600 struct mds_obd *mds = mds_req2mds(req);
601 struct obd_device *obd = req->rq_export->exp_obd;
602 struct dentry *dchild = NULL;
603 struct inode *dir = NULL;
605 struct lustre_handle lockh[2] = {{0}, {0}};
607 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
609 struct dentry_params dp;
610 struct mea *mea = NULL;
614 LASSERT(offset == 1);
616 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
619 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
620 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
621 rec->ur_name, rec->ur_mode);
623 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
625 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
626 GOTO(cleanup, rc = -ESTALE);
628 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
629 lockh, &parent_mode, rec->ur_name,
630 rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
631 if (IS_ERR(dparent)) {
632 rc = PTR_ERR(dparent);
633 CERROR("parent lookup error %d\n", rc);
636 cleanup_phase = 1; /* locked parent dentry */
637 dir = dparent->d_inode;
640 ldlm_lock_dump_handle(D_OTHER, lockh);
642 /* try to retrieve MEA data for this dir */
643 rc = mds_md_get_attr(obd, dparent->d_inode, &mea, &mea_size);
649 * dir is already splitted, check is requested filename should
650 * live at this MDS or at another one.
652 int i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
653 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
654 CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
655 " should be %lu(%d)\n",
656 mea->mea_master, dparent->d_inode->i_ino,
657 dparent->d_inode->i_generation, rec->ur_name,
658 (unsigned long)id_group(&mea->mea_ids[i]), i);
659 GOTO(cleanup, rc = -ERESTART);
663 dchild = ll_lookup_one_len(rec->ur_name, dparent,
664 rec->ur_namelen - 1);
665 if (IS_ERR(dchild)) {
666 rc = PTR_ERR(dchild);
667 CERROR("Can't find "DLID4"/%s, error %d\n",
668 OLID4(rec->ur_id1), rec->ur_name, rc);
672 cleanup_phase = 2; /* child dentry */
674 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
676 if (type == S_IFREG || type == S_IFDIR) {
677 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
678 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
679 obd->obd_name, dparent->d_inode->i_ino,
680 dparent->d_inode->i_generation, rc, parent_mode);
682 /* dir got splitted */
683 GOTO(cleanup, rc = -ERESTART);
685 /* error happened during spitting. */
690 if (dir->i_mode & S_ISGID) {
691 if (S_ISDIR(rec->ur_mode))
692 rec->ur_mode |= S_ISGID;
696 * here inode number should be used only in the case of replaying. It is
697 * needed to check if object already created in the case of creating
700 dchild->d_fsdata = (void *)&dp;
701 dp.p_inum = (unsigned long)id_ino(rec->ur_id2);
706 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
708 GOTO(cleanup, rc = PTR_ERR(handle));
709 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
717 * as Peter asked, mkdir() should distribute new directories
718 * over the whole cluster in order to distribute namespace
719 * processing load. first, we calculate which MDS to use to put
720 * new directory's inode in.
722 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1,
724 if (i == mds->mds_num) {
725 /* inode will be created locally */
726 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
728 GOTO(cleanup, rc = PTR_ERR(handle));
730 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
732 CERROR("Can't create dir \"%s\", rc = %d\n",
733 dchild->d_name.name, rc);
737 down(&dchild->d_inode->i_sem);
739 rc = mds_update_inode_sid(obd, dchild->d_inode,
740 handle, rec->ur_id2);
742 CERROR("mds_update_inode_sid() failed, inode %lu, "
743 "rc %d\n", dchild->d_inode->i_ino, rc);
747 * make sure, that fid is up-to-date.
749 mds_set_last_fid(obd, id_fid(rec->ur_id2));
751 rc = mds_alloc_inode_sid(obd, dchild->d_inode,
754 CERROR("mds_alloc_inode_sid() failed, inode %lu, "
755 "rc %d\n", dchild->d_inode->i_ino, rc);
758 up(&dchild->d_inode->i_sem);
764 nstripes = *(u16 *)rec->ur_eadata;
766 if (rc == 0 && nstripes) {
768 * we pass LCK_EX to split routine to signal,
769 * that we have exclusive access to the
770 * directory. Simple because nobody knows it
771 * already exists -bzzz
773 rc = mds_try_to_split_dir(obd, dchild,
777 /* dir got splitted */
780 /* an error occured during
785 } else if (!DENTRY_VALID(dchild)) {
786 /* inode will be created on another MDS */
787 struct obdo *oa = NULL;
788 struct mds_body *body;
790 /* first, create that inode */
793 GOTO(cleanup, rc = -ENOMEM);
798 if (rec->ur_eadata) {
799 /* user asks for creating splitted dir */
800 oa->o_easize = *((u16 *) rec->ur_eadata);
803 obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
804 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
805 OBD_MD_FLUID | OBD_MD_FLGID);
807 oa->o_mode = dir->i_mode;
809 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
812 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
814 * here inode number and generation are
815 * important, as this is replay request and we
816 * need them to check if such an object is
819 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
820 obd->obd_name, rec->ur_namelen - 1,
821 rec->ur_name, (unsigned)id_ino(rec->ur_id2),
822 (unsigned)id_gen(rec->ur_id2));
823 oa->o_id = id_ino(rec->ur_id2);
824 oa->o_fid = id_fid(rec->ur_id2);
825 oa->o_generation = id_gen(rec->ur_id2);
826 oa->o_flags |= OBD_FL_RECREATE_OBJS;
829 * fid should be defined here. It should be
832 LASSERT(oa->o_fid != 0);
836 * before obd_create() is called, o_fid is not known if
837 * this is not recovery of cause.
839 rc = obd_create(mds->mds_md_exp, oa, NULL, NULL);
841 CERROR("can't create remote inode: %d\n", rc);
842 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
843 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
844 rec->ur_name, rec->ur_mode);
849 LASSERT(oa->o_fid != 0);
851 /* now, add new dir entry for it */
852 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
853 if (IS_ERR(handle)) {
855 GOTO(cleanup, rc = PTR_ERR(handle));
858 /* creating local dentry for remote inode. */
859 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
860 rec->ur_namelen - 1, oa->o_id,
861 oa->o_generation, i, oa->o_fid);
864 CERROR("Can't create local entry %*s for "
865 "remote inode.\n", rec->ur_namelen - 1,
871 body = lustre_msg_buf(req->rq_repmsg,
873 body->valid |= OBD_MD_FLID | OBD_MD_MDS | OBD_MD_FID;
875 obdo2id(&body->id1, oa);
878 /* requested name exists in the directory */
885 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
887 GOTO(cleanup, rc = PTR_ERR(handle));
888 if (rec->ur_tgt == NULL) /* no target supplied */
889 rc = -EINVAL; /* -EPROTO? */
891 rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
899 int rdev = rec->ur_rdev;
900 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
902 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
903 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
908 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
909 dchild->d_fsdata = NULL;
910 GOTO(cleanup, rc = -EINVAL);
913 /* In case we stored the desired inum in here, we want to clean up. */
914 if (dchild->d_fsdata == (void *)(unsigned long)id_ino(rec->ur_id2))
915 dchild->d_fsdata = NULL;
918 CDEBUG(D_INODE, "error during create: %d\n", rc);
920 } else if (dchild->d_inode) {
922 struct mds_body *body;
923 struct inode *inode = dchild->d_inode;
926 iattr.ia_uid = rec->ur_fsuid;
927 LTIME_S(iattr.ia_atime) = rec->ur_time;
928 LTIME_S(iattr.ia_ctime) = rec->ur_time;
929 LTIME_S(iattr.ia_mtime) = rec->ur_time;
931 if (dir->i_mode & S_ISGID)
932 iattr.ia_gid = dir->i_gid;
934 iattr.ia_gid = rec->ur_fsgid;
936 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
937 ATTR_MTIME | ATTR_CTIME;
939 if (id_ino(rec->ur_id2)) {
940 LASSERT(id_ino(rec->ur_id2) == inode->i_ino);
941 inode->i_generation = id_gen(rec->ur_id2);
943 if (type != S_IFDIR) {
945 * updating inode self id, as inode already
946 * exists and we should make sure, its sid will
947 * be the same as we reveived.
950 rc = mds_update_inode_sid(obd, inode,
951 handle, rec->ur_id2);
954 CERROR("Can't update inode self id, "
959 * make sure, that fid is up-to-date.
961 mds_set_last_fid(obd, id_fid(rec->ur_id2));
964 /* dirtied and committed by the upcoming setattr. */
965 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
966 inode->i_ino, inode->i_generation);
968 struct lustre_handle child_ino_lockh;
970 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
971 inode->i_ino, inode->i_generation);
973 if (type != S_IFDIR) {
975 * allocate new id for @inode if it is not dir,
976 * because for dir it was already done.
979 rc = mds_alloc_inode_sid(obd, inode,
983 CERROR("mds_alloc_inode_sid() failed, "
984 "inode %lu, rc %d\n", inode->i_ino,
991 * the inode we were allocated may have just
992 * been freed by an unlink operation. We take
993 * this lock to synchronize against the matching
994 * reply-ack-lock taken in unlink, to avoid
995 * replay problems if this reply makes it out to
996 * the client but the unlink's does not. See
997 * bug 2029 for more detail.
999 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
1000 if (rc != ELDLM_OK) {
1001 CERROR("error locking for unlink/create sync: "
1004 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
1009 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
1011 CERROR("error on child setattr: rc = %d\n", rc);
1013 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1014 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1016 CERROR("error on parent setattr: rc = %d\n", rc);
1018 MD_COUNTER_INCREMENT(obd, create);
1020 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1021 mds_pack_inode2body(obd, body, inode, 1);
1026 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
1028 if (rc && created) {
1029 /* Destroy the file we just created. This should not need extra
1030 * journal credits, as we have already modified all of the
1031 * blocks needed in order to create the file in the first
1035 err = vfs_rmdir(dir, dchild);
1037 CERROR("rmdir in error path: %d\n", err);
1040 err = vfs_unlink(dir, dchild);
1042 CERROR("unlink in error path: %d\n", err);
1048 switch (cleanup_phase) {
1049 case 2: /* child dentry */
1051 case 1: /* locked parent dentry */
1053 if (lockh[1].cookie != 0)
1054 ldlm_lock_decref(lockh + 1, parent_mode);
1057 ldlm_lock_decref(lockh, LCK_PW);
1059 ptlrpc_save_lock(req, lockh, LCK_PW);
1065 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1069 OBD_FREE(mea, mea_size);
1070 req->rq_status = rc;
1075 res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
1076 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1080 for (i = 0; i < RES_NAME_SIZE; i++) {
1082 * this is needed to make zeroed res_id entries to be put at the
1083 * end of list in *ordered_locks() .
1085 if (res1->name[i] == 0 && res2->name[i] != 0)
1087 if (res2->name[i] == 0 && res1->name[i] != 0)
1089 if (res1->name[i] > res2->name[i])
1091 if (res1->name[i] < res2->name[i])
1098 if (memcmp(p1, p2, sizeof(*p1)) < 0)
1104 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1105 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1106 * because they take the place of local semaphores.
1108 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
1109 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
1110 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
1111 struct lustre_handle *p1_lockh, int p1_lock_mode,
1112 ldlm_policy_data_t *p1_policy,
1113 struct ldlm_res_id *p2_res_id,
1114 struct lustre_handle *p2_lockh, int p2_lock_mode,
1115 ldlm_policy_data_t *p2_policy)
1117 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1118 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1119 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1120 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
1124 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1126 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1127 res_id[0]->name[0], res_id[1]->name[0]);
1129 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1130 handles[1] = p1_lockh;
1131 handles[0] = p2_lockh;
1132 res_id[1] = p1_res_id;
1133 res_id[0] = p2_res_id;
1134 lock_modes[1] = p1_lock_mode;
1135 lock_modes[0] = p2_lock_mode;
1136 policies[1] = p1_policy;
1137 policies[0] = p2_policy;
1140 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1141 res_id[0]->name[0], res_id[1]->name[0]);
1143 flags = LDLM_FL_LOCAL_ONLY;
1144 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1145 LDLM_IBITS, policies[0], lock_modes[0], &flags,
1146 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1147 NULL, 0, NULL, handles[0]);
1150 ldlm_lock_dump_handle(D_OTHER, handles[0]);
1152 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1153 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1154 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1155 ldlm_lock_addref(handles[1], lock_modes[1]);
1156 } else if (res_id[1]->name[0] != 0) {
1157 flags = LDLM_FL_LOCAL_ONLY;
1158 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1159 *res_id[1], LDLM_IBITS, policies[1],
1160 lock_modes[1], &flags, mds_blocking_ast,
1161 ldlm_completion_ast, NULL, NULL, NULL, 0,
1163 if (rc != ELDLM_OK) {
1164 ldlm_lock_decref(handles[0], lock_modes[0]);
1167 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1173 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1174 struct lustre_handle *p1_lockh, int p1_lock_mode,
1175 ldlm_policy_data_t *p1_policy,
1176 struct ldlm_res_id *p2_res_id,
1177 struct lustre_handle *p2_lockh, int p2_lock_mode,
1178 ldlm_policy_data_t *p2_policy,
1179 struct ldlm_res_id *c1_res_id,
1180 struct lustre_handle *c1_lockh, int c1_lock_mode,
1181 ldlm_policy_data_t *c1_policy,
1182 struct ldlm_res_id *c2_res_id,
1183 struct lustre_handle *c2_lockh, int c2_lock_mode,
1184 ldlm_policy_data_t *c2_policy)
1186 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1187 c1_res_id, c2_res_id };
1188 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1189 c1_lockh, c2_lockh };
1190 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1191 c1_lock_mode, c2_lock_mode };
1192 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1193 c1_policy, c2_policy};
1194 int rc, i, j, sorted, flags;
1197 CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1198 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1199 res_id[3]->name[0]);
1202 * simple insertion sort - we have at most 4 elements. Note, that zeroed
1203 * res_id should be at the end of list after sorting is finished.
1205 for (i = 1; i < 4; i++) {
1207 dlm_handles[4] = dlm_handles[i];
1208 res_id[4] = res_id[i];
1209 lock_modes[4] = lock_modes[i];
1210 policies[4] = policies[i];
1214 if (res_gt(res_id[j], res_id[4], policies[j],
1216 dlm_handles[j + 1] = dlm_handles[j];
1217 res_id[j + 1] = res_id[j];
1218 lock_modes[j + 1] = lock_modes[j];
1219 policies[j + 1] = policies[j];
1224 } while (j >= 0 && !sorted);
1226 dlm_handles[j + 1] = dlm_handles[4];
1227 res_id[j + 1] = res_id[4];
1228 lock_modes[j + 1] = lock_modes[4];
1229 policies[j + 1] = policies[4];
1232 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1233 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1234 res_id[3]->name[0]);
1236 /* XXX we could send ASTs on all these locks first before blocking? */
1237 for (i = 0; i < 4; i++) {
1241 * nevertheless zeroed res_ids should be at the end of list, and
1242 * could use break here, I think, that it is more correctly for
1243 * clear understanding of code to have continue here, as it
1244 * clearly means, that zeroed res_id should be skipped and does
1245 * not mean, that if we meet zeroed res_id we should stop
1248 if (res_id[i]->name[0] == 0)
1252 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1253 (policies[i]->l_inodebits.bits &
1254 policies[i-1]->l_inodebits.bits) ) {
1255 memcpy(dlm_handles[i], dlm_handles[i-1],
1256 sizeof(*(dlm_handles[i])));
1257 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1259 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1260 *res_id[i], LDLM_IBITS,
1262 lock_modes[i], &flags,
1264 ldlm_completion_ast, NULL, NULL,
1265 NULL, 0, NULL, dlm_handles[i]);
1267 GOTO(out_err, rc = -EIO);
1268 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1275 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1280 /* In the unlikely case that the child changed while we were waiting
1281 * on the lock, we need to drop the lock on the old child and either:
1282 * - if the child has a lower resource name, then we have to also
1283 * drop the parent lock and regain the locks in the right order
1284 * - in the rename case, if the child has a lower resource name than one of
1285 * the other parent/child resources (maxres) we also need to reget the locks
1286 * - if the child has a higher resource name (this is the common case)
1287 * we can just get the lock on the new child (still in lock order)
1289 * Returns 0 if the child did not change or if it changed but could be locked.
1290 * Returns 1 if the child changed and we need to re-lock (no locks held).
1291 * Returns -ve error with a valid dchild (no locks held). */
1292 static int mds_verify_child(struct obd_device *obd,
1293 struct ldlm_res_id *parent_res_id,
1294 struct lustre_handle *parent_lockh,
1295 struct dentry *dparent, int parent_mode,
1296 struct ldlm_res_id *child_res_id,
1297 struct lustre_handle *child_lockh,
1298 struct dentry **dchildp, int child_mode,
1299 ldlm_policy_data_t *child_policy,
1300 const char *name, int namelen,
1301 struct ldlm_res_id *maxres,
1302 unsigned long child_ino, __u32 child_gen)
1304 struct lustre_id sid;
1305 struct dentry *vchild, *dchild = *dchildp;
1306 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1309 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1311 GOTO(cleanup, rc = PTR_ERR(vchild));
1313 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1314 if (child_gen == vchild->d_generation &&
1315 child_ino == vchild->d_inum) {
1324 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1325 (vchild->d_inode != NULL &&
1326 child_gen == vchild->d_inode->i_generation &&
1327 child_ino == vchild->d_inode->i_ino))) {
1335 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1336 vchild->d_inode, dchild ? dchild->d_inode : 0,
1337 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1338 child_res_id->name[0]);
1340 if (child_res_id->name[0] != 0)
1341 ldlm_lock_decref(child_lockh, child_mode);
1345 cleanup_phase = 1; /* parent lock only */
1346 *dchildp = dchild = vchild;
1348 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1351 if (dchild->d_inode) {
1352 down(&dchild->d_inode->i_sem);
1353 rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
1354 up(&dchild->d_inode->i_sem);
1356 CERROR("Can't read inode self id, inode %lu,"
1357 " rc %d\n", dchild->d_inode->i_ino, rc);
1360 child_res_id->name[0] = id_fid(&sid);
1361 child_res_id->name[1] = id_group(&sid);
1363 child_res_id->name[0] = dchild->d_fid;
1364 child_res_id->name[1] = dchild->d_mdsnum;
1367 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1368 res_gt(maxres, child_res_id, NULL, NULL)) {
1369 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1370 child_res_id->name[0], parent_res_id->name[0],
1372 GOTO(cleanup, rc = 1);
1375 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1376 *child_res_id, LDLM_IBITS, child_policy,
1377 child_mode, &flags, mds_blocking_ast,
1378 ldlm_completion_ast, NULL, NULL, NULL, 0,
1381 GOTO(cleanup, rc = -EIO);
1384 memset(child_res_id, 0, sizeof(*child_res_id));
1390 switch(cleanup_phase) {
1392 if (child_res_id->name[0] != 0)
1393 ldlm_lock_decref(child_lockh, child_mode);
1395 ldlm_lock_decref(parent_lockh, parent_mode);
1401 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1402 struct lustre_id *id,
1403 struct lustre_handle *parent_lockh,
1404 struct dentry **dparentp, int parent_mode,
1405 __u64 parent_lockpart, int *update_mode,
1406 char *name, int namelen,
1407 struct lustre_handle *child_lockh,
1408 struct dentry **dchildp, int child_mode,
1409 __u64 child_lockpart)
1411 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1412 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1413 struct ldlm_res_id parent_res_id = { .name = {0} };
1414 struct ldlm_res_id child_res_id = { .name = {0} };
1415 unsigned long child_ino = 0; __u32 child_gen = 0;
1416 int rc = 0, cleanup_phase = 0;
1417 struct lustre_id sid;
1418 struct inode *inode;
1421 /* Step 1: Lookup parent */
1422 *dparentp = mds_id2dentry(obd, id, NULL);
1423 if (IS_ERR(*dparentp)) {
1424 rc = PTR_ERR(*dparentp);
1429 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1430 (*dparentp)->d_inode->i_ino, name);
1432 parent_res_id.name[0] = id_fid(id);
1433 parent_res_id.name[1] = id_group(id);
1436 parent_lockh[1].cookie = 0;
1437 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1438 struct ldlm_res_id res_id = { .name = {0} };
1439 ldlm_policy_data_t policy;
1442 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1444 res_id.name[0] = id_fid(id);
1445 res_id.name[1] = id_group(id);
1446 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1448 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1449 res_id, LDLM_IBITS, &policy,
1450 *update_mode, &flags,
1452 ldlm_completion_ast,
1453 NULL, NULL, NULL, 0, NULL,
1459 parent_res_id.name[2] = full_name_hash(name, namelen - 1);
1461 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1462 (unsigned long)id_fid(id), (unsigned long)id_group(id),
1463 parent_res_id.name[2]);
1467 cleanup_phase = 1; /* parent dentry */
1469 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1470 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1471 if (IS_ERR(*dchildp)) {
1472 rc = PTR_ERR(*dchildp);
1473 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1477 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1479 * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1480 * lock. Drop possible UPDATE lock!
1482 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1483 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1485 child_res_id.name[0] = (*dchildp)->d_fid;
1486 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1487 child_ino = (*dchildp)->d_inum;
1488 child_gen = (*dchildp)->d_generation;
1492 inode = (*dchildp)->d_inode;
1494 inode = igrab(inode);
1498 down(&inode->i_sem);
1499 rc = mds_read_inode_sid(obd, inode, &sid);
1502 CERROR("Can't read inode self id, inode %lu, "
1503 "rc %d\n", inode->i_ino, rc);
1508 child_ino = inode->i_ino;
1509 child_gen = inode->i_generation;
1510 child_res_id.name[0] = id_fid(&sid);
1511 child_res_id.name[1] = id_group(&sid);
1515 cleanup_phase = 2; /* child dentry */
1517 /* Step 3: Lock parent and child in resource order. If child doesn't
1518 * exist, we still have to lock the parent and re-lookup. */
1519 rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1520 &parent_policy, &child_res_id, child_lockh,
1521 child_mode, &child_policy);
1525 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1526 cleanup_phase = 4; /* child lock */
1528 cleanup_phase = 3; /* parent lock */
1530 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1531 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1532 parent_mode, &child_res_id, child_lockh,
1533 dchildp, child_mode, &child_policy,
1534 name, namelen, &parent_res_id, child_ino,
1546 switch (cleanup_phase) {
1548 ldlm_lock_decref(child_lockh, child_mode);
1550 ldlm_lock_decref(parent_lockh, parent_mode);
1555 if (parent_lockh[1].cookie)
1556 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1564 void mds_reconstruct_generic(struct ptlrpc_request *req)
1566 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1567 mds_req_from_mcd(req, med->med_mcd);
1570 /* If we are unlinking an open file/dir (i.e. creating an orphan) then
1571 * we instead link the inode into the PENDING directory until it is
1572 * finally released. We can't simply call mds_reint_rename() or some
1573 * part thereof, because we don't have the inode to check for link
1574 * count/open status until after it is locked.
1576 * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1577 * before starting journal transaction.
1579 * returns 1 on success
1580 * returns 0 if we lost a race and didn't make a new link
1581 * returns negative on error
1583 static int mds_orphan_add_link(struct mds_update_record *rec,
1584 struct obd_device *obd, struct dentry *dentry)
1586 struct mds_obd *mds = &obd->u.mds;
1587 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1588 struct inode *inode = dentry->d_inode;
1589 struct dentry *pending_child;
1590 char idname[LL_ID_NAMELEN];
1591 int idlen = 0, rc, mode;
1594 LASSERT(inode != NULL);
1595 LASSERT(!mds_inode_is_orphan(inode));
1596 #ifndef HAVE_I_ALLOC_SEM
1597 LASSERT(down_trylock(&inode->i_sem) != 0);
1599 LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1601 idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1603 CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1604 mds_orphan_open_count(inode), inode->i_nlink,
1605 S_ISDIR(inode->i_mode) ? "dir" :
1606 S_ISREG(inode->i_mode) ? "file" : "other",
1607 rec->ur_name, idname);
1609 if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1612 pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1613 if (IS_ERR(pending_child))
1614 RETURN(PTR_ERR(pending_child));
1616 if (pending_child->d_inode != NULL) {
1617 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1618 LASSERT(pending_child->d_inode == inode);
1619 GOTO(out_dput, rc = 0);
1622 /* link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG
1623 * for linking and return real mode back then -bzzz */
1624 mode = inode->i_mode;
1625 inode->i_mode = S_IFREG;
1626 rc = vfs_link(dentry, pending_dir, pending_child);
1628 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1631 mds_inode_set_orphan(inode);
1633 /* return mode and correct i_nlink if inode is directory */
1634 inode->i_mode = mode;
1635 LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1636 S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1638 if (S_ISDIR(mode)) {
1640 pending_dir->i_nlink++;
1641 mark_inode_dirty(inode);
1642 mark_inode_dirty(pending_dir);
1647 l_dput(pending_child);
1651 int mds_create_local_dentry(struct mds_update_record *rec,
1652 struct obd_device *obd)
1654 struct mds_obd *mds = &obd->u.mds;
1655 struct inode *id_dir = mds->mds_id_dir->d_inode;
1656 int idlen = 0, rc, cleanup_phase = 0;
1657 struct dentry *new_child = NULL;
1658 char *idname = rec->ur_name;
1659 struct dentry *child = NULL;
1660 struct lustre_handle lockh[2] = {{0}, {0}};
1661 struct lustre_id sid;
1665 down(&id_dir->i_sem);
1666 idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1667 id_gen(rec->ur_id1));
1669 CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1670 idname, OLID4(rec->ur_id1));
1672 new_child = ll_lookup_one_len(idname, mds->mds_id_dir,
1675 if (IS_ERR(new_child)) {
1676 CERROR("can't lookup %s: %d\n", idname,
1677 (int) PTR_ERR(new_child));
1678 GOTO(cleanup, rc = PTR_ERR(new_child));
1682 down(&id_dir->i_sem);
1683 rc = mds_read_inode_sid(obd, id_dir, &sid);
1686 CERROR("Can't read inode self id, inode %lu, "
1687 "rc %d\n", id_dir->i_ino, rc);
1691 if (new_child->d_inode != NULL) {
1692 /* nice. we've already have local dentry! */
1693 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1694 (unsigned)new_child->d_inode->i_ino,
1695 (unsigned)new_child->d_inode->i_generation);
1697 id_ino(rec->ur_id1) = id_dir->i_ino;
1698 id_gen(rec->ur_id1) = id_dir->i_generation;
1699 rec->ur_namelen = idlen + 1;
1701 id_fid(rec->ur_id1) = id_fid(&sid);
1702 id_group(rec->ur_id1) = id_group(&sid);
1704 GOTO(cleanup, rc = 0);
1707 /* new, local dentry will be added soon. we need no aliases here */
1710 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1711 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1713 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1714 LCK_EX, lockh, NULL, NULL, 0,
1715 MDS_INODELOCK_UPDATE);
1718 if (IS_ERR(child)) {
1719 rc = PTR_ERR(child);
1720 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1721 CERROR("can't get victim: %d\n", rc);
1726 handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
1728 GOTO(cleanup, rc = PTR_ERR(handle));
1730 rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
1731 idlen, id_ino(rec->ur_id1),
1732 id_gen(rec->ur_id1), mds->mds_num,
1733 id_fid(rec->ur_id1));
1735 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1736 (unsigned long)child->d_inode->i_ino,
1737 (unsigned long)child->d_inode->i_generation, rc);
1739 if (S_ISDIR(child->d_inode->i_mode)) {
1741 mark_inode_dirty(id_dir);
1743 mark_inode_dirty(child->d_inode);
1745 fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
1747 id_ino(rec->ur_id1) = id_dir->i_ino;
1748 id_gen(rec->ur_id1) = id_dir->i_generation;
1749 rec->ur_namelen = idlen + 1;
1751 id_fid(rec->ur_id1) = id_fid(&sid);
1752 id_group(rec->ur_id1) = id_group(&sid);
1756 switch(cleanup_phase) {
1758 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1759 ldlm_lock_decref(lockh, LCK_EX);
1769 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1770 struct ptlrpc_request *slave)
1772 void *cookie, *cookie2;
1773 struct mds_body *body2;
1774 struct mds_body *body;
1778 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1779 LASSERT(body != NULL);
1781 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1782 LASSERT(body2 != NULL);
1784 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
1787 memcpy(body2, body, sizeof(*body));
1788 body2->valid &= ~OBD_MD_FLCOOKIE;
1790 if (!(body->valid & OBD_MD_FLEASIZE) &&
1791 !(body->valid & OBD_MD_FLDIREA))
1794 if (body->eadatasize == 0) {
1795 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1799 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1801 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1802 LASSERT(ea != NULL);
1804 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1805 LASSERT(ea2 != NULL);
1807 memcpy(ea2, ea, body->eadatasize);
1809 if (body->valid & OBD_MD_FLCOOKIE) {
1810 LASSERT(master->rq_repmsg->buflens[2] >=
1811 slave->rq_repmsg->buflens[2]);
1812 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1813 slave->rq_repmsg->buflens[2]);
1814 LASSERT(cookie != NULL);
1816 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1817 master->rq_repmsg->buflens[2]);
1818 LASSERT(cookie2 != NULL);
1819 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1820 body2->valid |= OBD_MD_FLCOOKIE;
1825 static int mds_reint_unlink_remote(struct mds_update_record *rec,
1826 int offset, struct ptlrpc_request *req,
1827 struct lustre_handle *parent_lockh,
1828 int update_mode, struct dentry *dparent,
1829 struct lustre_handle *child_lockh,
1830 struct dentry *dchild)
1832 struct obd_device *obd = req->rq_export->exp_obd;
1833 struct mds_obd *mds = mds_req2mds(req);
1834 struct ptlrpc_request *request = NULL;
1835 int rc = 0, cleanup_phase = 0;
1836 struct mdc_op_data *op_data;
1840 LASSERT(offset == 1 || offset == 3);
1842 /* time to drop i_nlink on remote MDS */
1843 OBD_ALLOC(op_data, sizeof(*op_data));
1844 if (op_data == NULL)
1847 memset(op_data, 0, sizeof(*op_data));
1848 mds_pack_dentry2id(obd, &op_data->id1, dchild, 1);
1849 op_data->create_mode = rec->ur_mode;
1851 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode "DLID4")",
1852 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1854 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1855 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode "DLID4")",
1856 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1859 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1860 op_data->create_mode |= MDS_MODE_REPLAY;
1862 rc = md_unlink(mds->mds_md_exp, op_data, &request);
1863 OBD_FREE(op_data, sizeof(*op_data));
1868 mds_copy_unlink_reply(req, request);
1869 ptlrpc_req_finished(request);
1873 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1876 GOTO(cleanup, rc = PTR_ERR(handle));
1877 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
1878 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1883 req->rq_status = rc;
1886 if (parent_lockh[1].cookie != 0)
1887 ldlm_lock_decref(parent_lockh + 1, update_mode);
1889 ldlm_lock_decref(child_lockh, LCK_EX);
1891 ldlm_lock_decref(parent_lockh, LCK_PW);
1893 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1900 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1901 struct ptlrpc_request *req,
1902 struct lustre_handle *lh)
1904 struct dentry *dparent = NULL, *dchild;
1905 struct mds_obd *mds = mds_req2mds(req);
1906 struct obd_device *obd = req->rq_export->exp_obd;
1907 struct mds_body *body = NULL;
1908 struct inode *child_inode = NULL;
1909 struct lustre_handle parent_lockh[2] = {{0}, {0}};
1910 struct lustre_handle child_lockh = {0};
1911 struct lustre_handle child_reuse_lockh = {0};
1912 struct lustre_handle *slave_lockh = NULL;
1913 char idname[LL_ID_NAMELEN];
1914 struct llog_create_locks *lcl = NULL;
1915 void *handle = NULL;
1916 int rc = 0, cleanup_phase = 0;
1917 int unlink_by_id = 0;
1921 LASSERT(offset == 1 || offset == 3);
1923 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1924 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
1927 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1929 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1930 DEBUG_REQ(D_HA, req, "unlink replay");
1931 LASSERT(offset == 1); /* should not come from intent */
1932 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
1933 lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
1934 req->rq_repmsg->buflens[2]);
1937 MD_COUNTER_INCREMENT(obd, unlink);
1939 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1940 GOTO(cleanup, rc = -ENOENT);
1942 if (rec->ur_namelen == 1) {
1943 /* this is request to drop i_nlink on local inode */
1945 rec->ur_name = idname;
1946 rc = mds_create_local_dentry(rec, obd);
1947 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
1948 DEBUG_REQ(D_HA, req,
1949 "drop nlink on inode "DLID4" (replay)",
1950 OLID4(rec->ur_id1));
1956 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1957 /* master mds for directory asks slave removing inode is already
1959 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1960 LCK_PW, parent_lockh,
1961 &update_mode, rec->ur_name,
1963 MDS_INODELOCK_UPDATE);
1964 if (IS_ERR(dparent))
1965 GOTO(cleanup, rc = PTR_ERR(dparent));
1966 dchild = ll_lookup_one_len(rec->ur_name, dparent,
1967 rec->ur_namelen - 1);
1969 GOTO(cleanup, rc = PTR_ERR(dchild));
1970 child_lockh.cookie = 0;
1971 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
1972 LASSERT(dchild->d_inode != NULL);
1973 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
1975 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
1976 parent_lockh, &dparent,
1977 LCK_PW, MDS_INODELOCK_UPDATE,
1978 &update_mode, rec->ur_name,
1979 rec->ur_namelen, &child_lockh,
1981 MDS_INODELOCK_LOOKUP |
1982 MDS_INODELOCK_UPDATE);
1987 if (dchild->d_flags & DCACHE_CROSS_REF) {
1988 /* we should have parent lock only here */
1989 LASSERT(unlink_by_id == 0);
1990 LASSERT(dchild->d_mdsnum != mds->mds_num);
1991 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
1992 update_mode, dparent, &child_lockh, dchild);
1996 cleanup_phase = 1; /* dchild, dparent, locks */
1999 child_inode = dchild->d_inode;
2000 if (child_inode == NULL) {
2001 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
2002 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
2003 GOTO(cleanup, rc = -ENOENT);
2006 cleanup_phase = 2; /* dchild has a lock */
2008 /* We have to do these checks ourselves, in case we are making an
2009 * orphan. The client tells us whether rmdir() or unlink() was called,
2010 * so we need to return appropriate errors (bug 72).
2012 * We don't have to check permissions, because vfs_rename (called from
2013 * mds_open_unlink_rename) also calls may_delete. */
2014 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
2015 if (!S_ISDIR(child_inode->i_mode))
2016 GOTO(cleanup, rc = -ENOTDIR);
2018 if (S_ISDIR(child_inode->i_mode))
2019 GOTO(cleanup, rc = -EISDIR);
2022 /* handle splitted dir */
2023 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
2027 /* Step 4: Get a lock on the ino to sync with creation WRT inode
2028 * reuse (see bug 2029). */
2029 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
2032 cleanup_phase = 3; /* child inum lock */
2034 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2036 /* ldlm_reply in buf[0] if called via intent */
2042 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2043 LASSERT(body != NULL);
2045 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2046 DOWN_READ_I_ALLOC_SEM(child_inode);
2047 cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2049 /* If this is potentially the last reference to this inode, get the
2050 * OBD EA data first so the client can destroy OST objects. We
2051 * only do the object removal later if no open files/links remain. */
2052 if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2053 child_inode->i_nlink == 1) {
2054 if (mds_orphan_open_count(child_inode) > 0) {
2055 /* need to lock pending_dir before transaction */
2056 down(&mds->mds_pending_dir->d_inode->i_sem);
2057 cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2058 } else if (S_ISREG(child_inode->i_mode)) {
2059 mds_pack_inode2body(obd, body, child_inode, 0);
2060 mds_pack_md(obd, req->rq_repmsg, offset + 1,
2061 body, child_inode, MDS_PACK_MD_LOCK);
2065 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2066 switch (child_inode->i_mode & S_IFMT) {
2068 /* Drop any lingering child directories before we start our
2069 * transaction, to avoid doing multiple inode dirty/delete
2070 * in our compound transaction (bug 1321). */
2071 shrink_dcache_parent(dchild);
2072 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2075 GOTO(cleanup, rc = PTR_ERR(handle));
2076 rc = vfs_rmdir(dparent->d_inode, dchild);
2079 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2080 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2082 handle = fsfilt_start_log(obd, dparent->d_inode,
2083 FSFILT_OP_UNLINK, NULL,
2084 le32_to_cpu(lmm->lmm_stripe_count));
2086 GOTO(cleanup, rc = PTR_ERR(handle));
2087 rc = vfs_unlink(dparent->d_inode, dchild);
2095 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2098 GOTO(cleanup, rc = PTR_ERR(handle));
2099 rc = vfs_unlink(dparent->d_inode, dchild);
2102 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2105 GOTO(cleanup, rc = -EINVAL);
2108 if (rc == 0 && child_inode->i_nlink == 0) {
2109 if (mds_orphan_open_count(child_inode) > 0)
2110 rc = mds_orphan_add_link(rec, obd, dchild);
2113 GOTO(cleanup, rc = 0);
2115 if (!S_ISREG(child_inode->i_mode))
2118 if (!(body->valid & OBD_MD_FLEASIZE)) {
2119 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2120 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2121 } else if (mds_log_op_unlink(obd, child_inode,
2122 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2123 req->rq_repmsg->buflens[offset + 1],
2124 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2125 req->rq_repmsg->buflens[offset+2],
2127 body->valid |= OBD_MD_FLCOOKIE;
2138 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2139 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2140 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2142 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2144 CERROR("error on parent setattr: rc = %d\n", err);
2146 rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2147 handle, req, rc, 0);
2149 (void)obd_set_info(mds->mds_dt_exp, strlen("unlinked"),
2150 "unlinked", 0, NULL);
2151 switch(cleanup_phase) {
2152 case 5: /* pending_dir semaphore */
2153 up(&mds->mds_pending_dir->d_inode->i_sem);
2154 case 4: /* child inode semaphore */
2155 UP_READ_I_ALLOC_SEM(child_inode);
2156 /* handle splitted dir */
2158 /* master directory can be non-empty or something else ... */
2159 mds_unlink_slave_objs(obd, dchild);
2162 ptlrpc_save_llog_lock(req, lcl);
2163 case 3: /* child ino-reuse lock */
2164 if (rc && body != NULL) {
2165 // Don't unlink the OST objects if the MDS unlink failed
2169 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2171 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2172 case 2: /* child lock */
2173 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2174 if (child_lockh.cookie)
2175 ldlm_lock_decref(&child_lockh, LCK_EX);
2176 case 1: /* child and parent dentry, parent lock */
2178 if (parent_lockh[1].cookie != 0)
2179 ldlm_lock_decref(parent_lockh + 1, update_mode);
2182 ldlm_lock_decref(parent_lockh, LCK_PW);
2184 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2191 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2194 req->rq_status = rc;
2199 * to service requests from remote MDS to increment i_nlink
2201 static int mds_reint_link_acquire(struct mds_update_record *rec,
2202 int offset, struct ptlrpc_request *req,
2203 struct lustre_handle *lh)
2205 struct obd_device *obd = req->rq_export->exp_obd;
2206 struct ldlm_res_id src_res_id = { .name = {0} };
2207 struct lustre_handle *handle = NULL, src_lockh = {0};
2208 struct mds_obd *mds = mds_req2mds(req);
2209 int rc = 0, cleanup_phase = 0;
2210 struct dentry *de_src = NULL;
2211 ldlm_policy_data_t policy;
2215 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2216 obd->obd_name, OLID4(rec->ur_id1));
2218 /* Step 1: Lookup the source inode and target directory by ID */
2219 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2221 GOTO(cleanup, rc = PTR_ERR(de_src));
2222 cleanup_phase = 1; /* source dentry */
2224 src_res_id.name[0] = id_fid(rec->ur_id1);
2225 src_res_id.name[1] = id_group(rec->ur_id1);
2226 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2228 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2229 src_res_id, LDLM_IBITS, &policy,
2230 LCK_EX, &flags, mds_blocking_ast,
2231 ldlm_completion_ast, NULL, NULL,
2232 NULL, 0, NULL, &src_lockh);
2234 GOTO(cleanup, rc = -ENOLCK);
2235 cleanup_phase = 2; /* lock */
2237 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2239 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2240 if (IS_ERR(handle)) {
2241 rc = PTR_ERR(handle);
2244 de_src->d_inode->i_nlink++;
2245 mark_inode_dirty(de_src->d_inode);
2249 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2250 handle, req, rc, 0);
2251 switch (cleanup_phase) {
2254 ldlm_lock_decref(&src_lockh, LCK_EX);
2256 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2262 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2265 req->rq_status = rc;
2270 * request to link to foreign inode:
2271 * - acquire i_nlinks on this inode
2274 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2275 int offset, struct ptlrpc_request *req,
2276 struct lustre_handle *lh)
2278 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2279 struct obd_device *obd = req->rq_export->exp_obd;
2280 struct dentry *de_tgt_dir = NULL;
2281 struct mds_obd *mds = mds_req2mds(req);
2282 int rc = 0, cleanup_phase = 0;
2283 struct mdc_op_data *op_data;
2284 struct ptlrpc_request *request = NULL;
2288 DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2289 ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2290 OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2291 OLID4(rec->ur_id1));
2293 de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2294 tgt_dir_lockh, &update_mode,
2295 rec->ur_name, rec->ur_namelen - 1,
2296 MDS_INODELOCK_UPDATE);
2297 if (IS_ERR(de_tgt_dir))
2298 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2301 OBD_ALLOC(op_data, sizeof(*op_data));
2302 if (op_data == NULL)
2303 GOTO(cleanup, rc = -ENOMEM);
2305 memset(op_data, 0, sizeof(*op_data));
2306 op_data->id1 = *(rec->ur_id1);
2307 rc = md_link(mds->mds_md_exp, op_data, &request);
2308 OBD_FREE(op_data, sizeof(*op_data));
2314 ptlrpc_req_finished(request);
2316 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2318 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2319 if (IS_ERR(handle)) {
2320 rc = PTR_ERR(handle);
2326 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2327 rec->ur_namelen - 1, id_ino(rec->ur_id1),
2328 id_gen(rec->ur_id1), id_group(rec->ur_id1),
2329 id_fid(rec->ur_id1));
2332 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2333 handle, req, rc, 0);
2335 switch (cleanup_phase) {
2338 OBD_ALLOC(op_data, sizeof(*op_data));
2339 if (op_data != NULL) {
2341 memset(op_data, 0, sizeof(*op_data));
2343 op_data->id1 = *(rec->ur_id1);
2344 op_data->create_mode = rec->ur_mode;
2346 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2347 OBD_FREE(op_data, sizeof(*op_data));
2349 ptlrpc_req_finished(request);
2351 CERROR("error %d while dropping i_nlink on "
2352 "remote inode\n", rc);
2355 CERROR("rc %d prevented dropping i_nlink on "
2356 "remote inode\n", -ENOMEM);
2362 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2364 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2367 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2369 ptlrpc_save_lock(req, tgt_dir_lockh + 1, update_mode);
2375 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2378 req->rq_status = rc;
2382 static int mds_reint_link(struct mds_update_record *rec, int offset,
2383 struct ptlrpc_request *req,
2384 struct lustre_handle *lh)
2386 struct obd_device *obd = req->rq_export->exp_obd;
2387 struct dentry *de_src = NULL;
2388 struct dentry *de_tgt_dir = NULL;
2389 struct dentry *dchild = NULL;
2390 struct mds_obd *mds = mds_req2mds(req);
2391 struct lustre_handle *handle = NULL;
2392 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2393 struct ldlm_res_id src_res_id = { .name = {0} };
2394 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2395 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2396 ldlm_policy_data_t tgt_dir_policy =
2397 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2398 int rc = 0, cleanup_phase = 0;
2400 int update_mode = 0;
2404 LASSERT(offset == 1);
2406 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2407 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2408 id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2411 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2412 MD_COUNTER_INCREMENT(obd, link);
2414 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2415 GOTO(cleanup, rc = -ENOENT);
2417 if (id_group(rec->ur_id1) != mds->mds_num) {
2418 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2422 if (rec->ur_namelen == 1) {
2423 rc = mds_reint_link_acquire(rec, offset, req, lh);
2427 /* Step 1: Lookup the source inode and target directory by ID */
2428 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2430 GOTO(cleanup, rc = PTR_ERR(de_src));
2432 cleanup_phase = 1; /* source dentry */
2434 de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2435 if (IS_ERR(de_tgt_dir)) {
2436 rc = PTR_ERR(de_tgt_dir);
2441 cleanup_phase = 2; /* target directory dentry */
2443 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2444 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2445 rec->ur_name, de_src->d_inode->i_ino);
2447 /* Step 2: Take the two locks */
2448 src_res_id.name[0] = id_fid(rec->ur_id1);
2449 src_res_id.name[1] = id_group(rec->ur_id1);
2450 tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2451 tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2454 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2456 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2458 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2459 tgt_dir_res_id, LDLM_IBITS,
2460 &src_policy, update_mode, &flags,
2462 ldlm_completion_ast, NULL, NULL,
2463 NULL, 0, NULL, tgt_dir_lockh + 1);
2465 GOTO(cleanup, rc = -ENOLCK);
2468 tgt_dir_res_id.name[2] = full_name_hash(rec->ur_name,
2469 rec->ur_namelen - 1);
2470 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2471 (unsigned long)id_fid(rec->ur_id2),
2472 (unsigned long)id_group(rec->ur_id2),
2473 tgt_dir_res_id.name[2]);
2476 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2477 &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2478 LCK_EX, &tgt_dir_policy);
2482 cleanup_phase = 3; /* locks */
2484 /* Step 3: Lookup the child */
2485 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir,
2486 rec->ur_namelen - 1);
2487 if (IS_ERR(dchild)) {
2488 rc = PTR_ERR(dchild);
2489 if (rc != -EPERM && rc != -EACCES)
2490 CERROR("child lookup error %d\n", rc);
2494 cleanup_phase = 4; /* child dentry */
2496 if (dchild->d_inode) {
2497 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2498 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2503 /* Step 4: Do it. */
2504 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2506 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2507 if (IS_ERR(handle)) {
2508 rc = PTR_ERR(handle);
2512 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2513 if (rc && rc != -EPERM && rc != -EACCES)
2514 CERROR("vfs_link error %d\n", rc);
2516 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2517 handle, req, rc, 0);
2520 switch (cleanup_phase) {
2521 case 4: /* child dentry */
2525 ldlm_lock_decref(&src_lockh, LCK_EX);
2526 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2528 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2529 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2531 case 2: /* target dentry */
2533 if (tgt_dir_lockh[1].cookie && update_mode)
2534 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2538 case 1: /* source dentry */
2543 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2546 req->rq_status = rc;
2550 /* The idea here is that we need to get four locks in the end:
2551 * one on each parent directory, one on each child. We need to take
2552 * these locks in some kind of order (to avoid deadlocks), and the order
2553 * I selected is "increasing resource number" order. We need to look up
2554 * the children, however, before we know what the resource number(s) are.
2555 * Thus the following plan:
2557 * 1,2. Look up the parents
2558 * 3,4. Look up the children
2559 * 5. Take locks on the parents and children, in order
2560 * 6. Verify that the children haven't changed since they were looked up
2562 * If there was a race and the children changed since they were first looked
2563 * up, it is possible that mds_verify_child() will be able to just grab the
2564 * lock on the new child resource (if it has a higher resource than any other)
2565 * but we need to compare against not only its parent, but also against the
2566 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2568 * We need the fancy igrab() on the child inodes because we aren't holding a
2569 * lock on the parent after the lookup is done, so dentry->d_inode may change
2570 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2572 static int mds_get_parents_children_locked(struct obd_device *obd,
2573 struct mds_obd *mds,
2574 struct lustre_id *p1_id,
2575 struct dentry **de_srcdirp,
2576 struct lustre_id *p2_id,
2577 struct dentry **de_tgtdirp,
2579 const char *old_name, int old_len,
2580 struct dentry **de_oldp,
2581 const char *new_name, int new_len,
2582 struct dentry **de_newp,
2583 struct lustre_handle *dlm_handles,
2586 struct ldlm_res_id p1_res_id = { .name = {0} };
2587 struct ldlm_res_id p2_res_id = { .name = {0} };
2588 struct ldlm_res_id c1_res_id = { .name = {0} };
2589 struct ldlm_res_id c2_res_id = { .name = {0} };
2590 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2591 /* Only dentry should change, but the inode itself would be
2593 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2594 /* If something is going to be replaced, both dentry and inode locks are
2596 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP|
2597 MDS_INODELOCK_UPDATE}};
2598 struct ldlm_res_id *maxres_src, *maxres_tgt;
2599 struct inode *inode;
2600 int rc = 0, cleanup_phase = 0;
2601 __u32 child_gen1 = 0;
2602 __u32 child_gen2 = 0;
2603 unsigned long child_ino1 = 0;
2604 unsigned long child_ino2 = 0;
2607 /* Step 1: Lookup the source directory */
2608 *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2609 if (IS_ERR(*de_srcdirp))
2610 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2612 cleanup_phase = 1; /* source directory dentry */
2614 p1_res_id.name[0] = id_fid(p1_id);
2615 p1_res_id.name[1] = id_group(p1_id);
2617 /* Step 2: Lookup the target directory */
2618 if (id_equal_stc(p1_id, p2_id)) {
2619 *de_tgtdirp = dget(*de_srcdirp);
2621 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2622 if (IS_ERR(*de_tgtdirp)) {
2623 rc = PTR_ERR(*de_tgtdirp);
2629 cleanup_phase = 2; /* target directory dentry */
2631 p2_res_id.name[0] = id_fid(p2_id);
2632 p2_res_id.name[1] = id_group(p2_id);
2635 dlm_handles[5].cookie = 0;
2636 dlm_handles[6].cookie = 0;
2638 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2640 * get a temp lock on just fid, group to flush client cache and
2641 * to protect dirs from concurrent splitting.
2643 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2644 LCK_PW, &p_policy, &p2_res_id,
2645 &dlm_handles[6], LCK_PW, &p_policy);
2649 p1_res_id.name[2] = full_name_hash(old_name, old_len - 1);
2650 p2_res_id.name[2] = full_name_hash(new_name, new_len - 1);
2652 CDEBUG(D_INFO, "take locks on "
2653 LPX64":"LPX64":"LPX64", "LPX64":"LPX64":"LPX64"\n",
2654 p1_res_id.name[0], p1_res_id.name[1], p1_res_id.name[2],
2655 p2_res_id.name[0], p2_res_id.name[1], p2_res_id.name[2]);
2660 /* Step 3: Lookup the source child entry */
2661 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp,
2663 if (IS_ERR(*de_oldp)) {
2664 rc = PTR_ERR(*de_oldp);
2665 CERROR("old child lookup error (%*s): %d\n",
2666 old_len - 1, old_name, rc);
2670 cleanup_phase = 4; /* original name dentry */
2672 inode = (*de_oldp)->d_inode;
2673 if (inode != NULL) {
2674 struct lustre_id sid;
2676 inode = igrab(inode);
2678 GOTO(cleanup, rc = -ENOENT);
2680 down(&inode->i_sem);
2681 rc = mds_read_inode_sid(obd, inode, &sid);
2684 CERROR("Can't read inode self id, inode %lu, "
2685 "rc %d\n", inode->i_ino, rc);
2690 child_ino1 = inode->i_ino;
2691 child_gen1 = inode->i_generation;
2692 c1_res_id.name[0] = id_fid(&sid);
2693 c1_res_id.name[1] = id_group(&sid);
2695 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2696 child_ino1 = (*de_oldp)->d_inum;
2697 child_gen1 = (*de_oldp)->d_generation;
2698 c1_res_id.name[0] = (*de_oldp)->d_fid;
2699 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2701 GOTO(cleanup, rc = -ENOENT);
2704 /* Step 4: Lookup the target child entry */
2705 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp,
2707 if (IS_ERR(*de_newp)) {
2708 rc = PTR_ERR(*de_newp);
2709 CERROR("new child lookup error (%*s): %d\n",
2710 old_len - 1, old_name, rc);
2714 cleanup_phase = 5; /* target dentry */
2716 inode = (*de_newp)->d_inode;
2717 if (inode != NULL) {
2718 struct lustre_id sid;
2720 inode = igrab(inode);
2724 down(&inode->i_sem);
2725 rc = mds_read_inode_sid(obd, inode, &sid);
2728 CERROR("Can't read inode self id, inode %lu, "
2729 "rc %d\n", inode->i_ino, rc);
2733 child_ino2 = inode->i_ino;
2734 child_gen2 = inode->i_generation;
2735 c2_res_id.name[0] = id_fid(&sid);
2736 c2_res_id.name[1] = id_group(&sid);
2738 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2739 child_ino2 = (*de_newp)->d_inum;
2740 child_gen2 = (*de_newp)->d_generation;
2741 c2_res_id.name[0] = (*de_newp)->d_fid;
2742 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
2746 /* Step 5: Take locks on the parents and child(ren) */
2747 maxres_src = &p1_res_id;
2748 maxres_tgt = &p2_res_id;
2749 cleanup_phase = 5; /* target dentry */
2751 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
2752 maxres_src = &c1_res_id;
2753 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
2754 maxres_tgt = &c2_res_id;
2756 rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
2758 &p2_res_id, &dlm_handles[1], parent_mode,
2760 &c1_res_id, &dlm_handles[2], child_mode,
2762 &c2_res_id, &dlm_handles[3], child_mode,
2767 cleanup_phase = 6; /* parent and child(ren) locks */
2769 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2770 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2771 parent_mode, &c1_res_id, &dlm_handles[2],
2772 de_oldp, child_mode, &c1_policy, old_name, old_len,
2773 maxres_tgt, child_ino1, child_gen1);
2775 if (c2_res_id.name[0] != 0)
2776 ldlm_lock_decref(&dlm_handles[3], child_mode);
2777 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2784 if (!DENTRY_VALID(*de_oldp))
2785 GOTO(cleanup, rc = -ENOENT);
2787 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2788 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2789 parent_mode, &c2_res_id, &dlm_handles[3],
2790 de_newp, child_mode, &c2_policy, new_name,
2791 new_len, maxres_src, child_ino2, child_gen2);
2793 ldlm_lock_decref(&dlm_handles[2], child_mode);
2794 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2804 switch (cleanup_phase) {
2805 case 6: /* child lock(s) */
2806 if (c2_res_id.name[0] != 0)
2807 ldlm_lock_decref(&dlm_handles[3], child_mode);
2808 if (c1_res_id.name[0] != 0)
2809 ldlm_lock_decref(&dlm_handles[2], child_mode);
2810 if (dlm_handles[1].cookie != 0)
2811 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2812 if (dlm_handles[0].cookie != 0)
2813 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2814 case 5: /* target dentry */
2816 case 4: /* source dentry */
2820 if (dlm_handles[5].cookie != 0)
2821 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2822 if (dlm_handles[6].cookie != 0)
2823 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2825 case 2: /* target directory dentry */
2826 l_dput(*de_tgtdirp);
2827 case 1: /* source directry dentry */
2828 l_dput(*de_srcdirp);
2836 * checks if dentry can be removed. This function also handles cross-ref
2839 static int mds_check_for_rename(struct obd_device *obd,
2840 struct dentry *dentry)
2842 struct mds_obd *mds = &obd->u.mds;
2843 struct lustre_handle *rlockh;
2844 struct ptlrpc_request *req;
2845 struct mdc_op_data *op_data;
2846 struct lookup_intent it;
2847 int handle_size, rc = 0;
2850 LASSERT(dentry != NULL);
2852 if (dentry->d_inode) {
2853 if (S_ISDIR(dentry->d_inode->i_mode) &&
2854 !mds_is_dir_empty(obd, dentry))
2857 LASSERT((dentry->d_flags & DCACHE_CROSS_REF));
2858 handle_size = sizeof(struct lustre_handle);
2860 OBD_ALLOC(rlockh, handle_size);
2864 memset(rlockh, 0, handle_size);
2865 OBD_ALLOC(op_data, sizeof(*op_data));
2866 if (op_data == NULL) {
2867 OBD_FREE(rlockh, handle_size);
2870 memset(op_data, 0, sizeof(*op_data));
2871 mds_pack_dentry2id(obd, &op_data->id1, dentry, 1);
2873 it.it_op = IT_UNLINK;
2874 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
2877 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
2878 op_data, rlockh, NULL, 0, ldlm_completion_ast,
2879 mds_blocking_ast, NULL);
2880 OBD_FREE(op_data, sizeof(*op_data));
2884 OBD_FREE(it.d.fs_data,
2885 sizeof(struct lustre_intent_data));
2888 if (rlockh->cookie != 0)
2889 ldlm_lock_decref(rlockh, LCK_EX);
2891 if (LUSTRE_IT(&it)->it_data) {
2892 req = (struct ptlrpc_request *)LUSTRE_IT(&it)->it_data;
2893 ptlrpc_req_finished(req);
2896 if (LUSTRE_IT(&it)->it_status)
2897 rc = LUSTRE_IT(&it)->it_status;
2898 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
2899 OBD_FREE(rlockh, handle_size);
2904 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
2905 struct ptlrpc_request *req, struct lustre_id *id,
2906 struct dentry *de_dir, struct dentry *de)
2908 struct obd_device *obd = req->rq_export->exp_obd;
2909 struct mds_obd *mds = mds_req2mds(req);
2910 void *handle = NULL;
2916 * name exists and points to local inode try to unlink this name
2917 * and create new one.
2919 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
2920 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
2921 (unsigned long)de->d_inode->i_generation);
2923 /* checking if we can remove local dentry. */
2924 rc = mds_check_for_rename(obd, de);
2928 handle = fsfilt_start(obd, de_dir->d_inode,
2929 FSFILT_OP_RENAME, NULL);
2931 GOTO(cleanup, rc = PTR_ERR(handle));
2932 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
2935 } else if (de->d_flags & DCACHE_CROSS_REF) {
2936 CDEBUG(D_OTHER, "%s: %s points to remote inode %lu/%lu\n",
2937 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_mdsnum,
2938 (unsigned long)de->d_fid);
2940 /* checking if we can remove local dentry. */
2941 rc = mds_check_for_rename(obd, de);
2946 * to be fully POSIX compatible, we should add one more check:
2948 * if de_new is subdir of dir rec->ur_id1. If so - return
2951 * I do not know how to implement it right now, because
2952 * inodes/dentries for new and old names lie on different MDS,
2953 * so add this notice here just to make it visible for the rest
2954 * of developers and do not forget about. And when this check
2955 * will be added, del_cross_ref should gone, that is local
2956 * dentry is able to be removed if all checks passed. --umka
2959 handle = fsfilt_start(obd, de_dir->d_inode,
2960 FSFILT_OP_RENAME, NULL);
2962 GOTO(cleanup, rc = PTR_ERR(handle));
2963 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
2967 /* name doesn't exist. the simplest case. */
2968 handle = fsfilt_start(obd, de_dir->d_inode,
2969 FSFILT_OP_LINK, NULL);
2971 GOTO(cleanup, rc = PTR_ERR(handle));
2974 rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
2975 rec->ur_tgtlen - 1, id_ino(id),
2976 id_gen(id), id_group(id), id_fid(id));
2978 CERROR("add_dir_entry() returned error %d\n", rc);
2984 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
2985 handle, req, rc, 0);
2990 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
2991 struct ptlrpc_request *req, struct dentry *de_dir,
2994 struct obd_device *obd = req->rq_export->exp_obd;
2995 struct mds_obd *mds = mds_req2mds(req);
2996 void *handle = NULL;
3000 handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
3002 GOTO(cleanup, rc = PTR_ERR(handle));
3003 rc = fsfilt_del_dir_entry(obd, de);
3008 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3009 handle, req, rc, 0);
3013 static int mds_reint_rename_create_name(struct mds_update_record *rec,
3014 int offset, struct ptlrpc_request *req)
3016 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3017 struct obd_device *obd = req->rq_export->exp_obd;
3018 struct mds_obd *mds = mds_req2mds(req);
3019 struct lustre_handle child_lockh = {0};
3020 struct dentry *de_tgtdir = NULL;
3021 struct dentry *de_new = NULL;
3022 int cleanup_phase = 0;
3023 int update_mode, rc = 0;
3027 * another MDS executing rename operation has asked us to create target
3028 * name. such a creation should destroy existing target name.
3030 CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
3031 obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
3033 /* first, lookup the target */
3034 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
3035 &de_tgtdir, LCK_PW, MDS_INODELOCK_UPDATE,
3036 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
3037 &child_lockh, &de_new, LCK_EX,
3038 MDS_INODELOCK_LOOKUP);
3045 LASSERT(de_tgtdir->d_inode);
3048 rc = mds_add_local_dentry(rec, offset, req, rec->ur_id1,
3054 if (cleanup_phase == 1) {
3056 if (parent_lockh[1].cookie != 0)
3057 ldlm_lock_decref(parent_lockh + 1, update_mode);
3059 ldlm_lock_decref(parent_lockh, LCK_PW);
3060 if (child_lockh.cookie != 0)
3061 ldlm_lock_decref(&child_lockh, LCK_EX);
3066 req->rq_status = rc;
3070 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
3071 struct ptlrpc_request *req)
3073 struct obd_device *obd = req->rq_export->exp_obd;
3074 struct ptlrpc_request *req2 = NULL;
3075 struct dentry *de_srcdir = NULL;
3076 struct dentry *de_old = NULL;
3077 struct mds_obd *mds = mds_req2mds(req);
3078 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3079 struct lustre_handle child_lockh = {0};
3080 struct mdc_op_data *op_data;
3081 int update_mode, rc = 0;
3084 CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
3085 obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
3087 OBD_ALLOC(op_data, sizeof(*op_data));
3088 if (op_data == NULL)
3090 memset(op_data, 0, sizeof(*op_data));
3092 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
3093 &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
3094 &update_mode, rec->ur_name,
3095 rec->ur_namelen, &child_lockh, &de_old,
3096 LCK_EX, MDS_INODELOCK_LOOKUP);
3099 LASSERT(de_srcdir->d_inode);
3103 * we already know the target should be created on another MDS so, we
3104 * have to request that MDS to do it.
3107 /* prepare source id */
3108 if (de_old->d_flags & DCACHE_CROSS_REF) {
3109 LASSERT(de_old->d_inode == NULL);
3110 CDEBUG(D_OTHER, "request to move remote name\n");
3111 mds_pack_dentry2id(obd, &op_data->id1, de_old, 1);
3112 } else if (de_old->d_inode == NULL) {
3113 /* oh, source doesn't exist */
3114 OBD_FREE(op_data, sizeof(*op_data));
3115 GOTO(cleanup, rc = -ENOENT);
3117 struct lustre_id sid;
3118 struct inode *inode = de_old->d_inode;
3120 LASSERT(inode != NULL);
3121 CDEBUG(D_OTHER, "request to move local name\n");
3122 id_ino(&op_data->id1) = inode->i_ino;
3123 id_group(&op_data->id1) = mds->mds_num;
3124 id_gen(&op_data->id1) = inode->i_generation;
3126 down(&inode->i_sem);
3127 rc = mds_read_inode_sid(obd, inode, &sid);
3130 CERROR("Can't read inode self id, "
3131 "inode %lu, rc = %d\n",
3136 id_fid(&op_data->id1) = id_fid(&sid);
3139 op_data->id2 = *rec->ur_id2;
3140 rc = md_rename(mds->mds_md_exp, op_data, NULL, 0,
3141 rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3142 OBD_FREE(op_data, sizeof(*op_data));
3147 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3153 ptlrpc_req_finished(req2);
3156 if (parent_lockh[1].cookie != 0)
3157 ldlm_lock_decref(parent_lockh + 1, update_mode);
3159 ldlm_lock_decref(parent_lockh, LCK_PW);
3160 if (child_lockh.cookie != 0)
3161 ldlm_lock_decref(&child_lockh, LCK_EX);
3166 req->rq_status = rc;
3170 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3171 struct ptlrpc_request *req, struct lustre_handle *lockh)
3173 struct obd_device *obd = req->rq_export->exp_obd;
3174 struct dentry *de_srcdir = NULL;
3175 struct dentry *de_tgtdir = NULL;
3176 struct dentry *de_old = NULL;
3177 struct dentry *de_new = NULL;
3178 struct inode *old_inode = NULL, *new_inode = NULL;
3179 struct mds_obd *mds = mds_req2mds(req);
3180 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3181 struct mds_body *body = NULL;
3182 struct llog_create_locks *lcl = NULL;
3183 struct lov_mds_md *lmm = NULL;
3184 int rc = 0, cleanup_phase = 0;
3185 void *handle = NULL;
3188 LASSERT(offset == 1);
3190 DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3191 OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3194 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3196 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3197 DEBUG_REQ(D_HA, req, "rename replay\n");
3198 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3199 lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3200 req->rq_repmsg->buflens[2]);
3203 MD_COUNTER_INCREMENT(obd, rename);
3205 if (rec->ur_namelen == 1) {
3206 rc = mds_reint_rename_create_name(rec, offset, req);
3210 /* check if new name should be located on remote target. */
3211 if (id_group(rec->ur_id2) != mds->mds_num) {
3212 rc = mds_reint_rename_to_remote(rec, offset, req);
3216 rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3217 rec->ur_id2, &de_tgtdir, LCK_PW,
3218 rec->ur_name, rec->ur_namelen,
3219 &de_old, rec->ur_tgt,
3220 rec->ur_tgtlen, &de_new,
3221 dlm_handles, LCK_EX);
3225 cleanup_phase = 1; /* parent(s), children, locks */
3226 old_inode = de_old->d_inode;
3227 new_inode = de_new->d_inode;
3229 /* sanity check for src inode */
3230 if (de_old->d_flags & DCACHE_CROSS_REF) {
3231 LASSERT(de_old->d_inode == NULL);
3234 * in the case of cross-ref dir, we can perform this check only
3235 * if child and parent lie on the same mds. This is because
3236 * otherwise they can have the same inode numbers.
3238 if (de_old->d_mdsnum == mds->mds_num) {
3239 if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3240 de_old->d_inum == de_tgtdir->d_inode->i_ino)
3241 GOTO(cleanup, rc = -EINVAL);
3244 LASSERT(de_old->d_inode != NULL);
3245 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3246 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3247 GOTO(cleanup, rc = -EINVAL);
3250 /* sanity check for dest inode */
3251 if (de_new->d_flags & DCACHE_CROSS_REF) {
3252 LASSERT(new_inode == NULL);
3254 /* the same check about target dentry. */
3255 if (de_new->d_mdsnum == mds->mds_num) {
3256 if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3257 de_new->d_inum == de_tgtdir->d_inode->i_ino)
3258 GOTO(cleanup, rc = -EINVAL);
3262 * regular files usualy do not have ->rename() implemented. But
3263 * we handle only this case when @de_new is cross-ref entry,
3264 * because in other cases it will be handled by vfs_rename().
3266 if (de_old->d_inode && (!de_old->d_inode->i_op ||
3267 !de_old->d_inode->i_op->rename))
3268 GOTO(cleanup, rc = -EPERM);
3271 (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3272 new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3273 GOTO(cleanup, rc = -EINVAL);
3277 /* check if inodes point to each other. */
3278 if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3279 !(de_new->d_flags & DCACHE_CROSS_REF) &&
3280 old_inode == new_inode)
3281 GOTO(cleanup, rc = 0);
3283 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3285 * check if we are moving old entry into its child. 2.6 does not check
3286 * for this in vfs_rename() anymore.
3288 if (is_subdir(de_new, de_old))
3289 GOTO(cleanup, rc = -EINVAL);
3293 * if we are about to remove the target at first, pass the EA of that
3294 * inode to client to perform and cleanup on OST.
3296 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3297 LASSERT(body != NULL);
3299 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3301 DOWN_READ_I_ALLOC_SEM(new_inode);
3303 cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3305 if (new_inode && ((S_ISDIR(new_inode->i_mode) &&
3306 new_inode->i_nlink == 2) ||
3307 new_inode->i_nlink == 1)) {
3308 if (mds_orphan_open_count(new_inode) > 0) {
3309 /* need to lock pending_dir before transaction */
3310 down(&mds->mds_pending_dir->d_inode->i_sem);
3311 cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3312 } else if (S_ISREG(new_inode->i_mode)) {
3313 mds_pack_inode2body(obd, body, new_inode, 0);
3314 mds_pack_md(obd, req->rq_repmsg, 1, body,
3315 new_inode, MDS_PACK_MD_LOCK);
3319 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3320 de_srcdir->d_inode->i_sb);
3322 if (de_old->d_flags & DCACHE_CROSS_REF) {
3323 struct lustre_id old_id;
3325 mds_pack_dentry2id(obd, &old_id, de_old, 1);
3327 rc = mds_add_local_dentry(rec, offset, req, &old_id,
3332 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3337 lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3338 handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3339 NULL, le32_to_cpu(lmm->lmm_stripe_count));
3342 GOTO(cleanup, rc = PTR_ERR(handle));
3345 de_old->d_fsdata = req;
3346 de_new->d_fsdata = req;
3347 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3350 if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3351 if (mds_orphan_open_count(new_inode) > 0)
3352 rc = mds_orphan_add_link(rec, obd, de_new);
3355 GOTO(cleanup, rc = 0);
3357 if (!S_ISREG(new_inode->i_mode))
3360 if (!(body->valid & OBD_MD_FLEASIZE)) {
3361 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3362 OBD_MD_FLATIME | OBD_MD_FLMTIME);
3363 } else if (mds_log_op_unlink(obd, new_inode,
3364 lustre_msg_buf(req->rq_repmsg,1,0),
3365 req->rq_repmsg->buflens[1],
3366 lustre_msg_buf(req->rq_repmsg,2,0),
3367 req->rq_repmsg->buflens[2],
3369 body->valid |= OBD_MD_FLCOOKIE;
3375 rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3376 handle, req, rc, 0);
3378 switch (cleanup_phase) {
3380 up(&mds->mds_pending_dir->d_inode->i_sem);
3383 UP_READ_I_ALLOC_SEM(new_inode);
3386 if (dlm_handles[5].cookie != 0)
3387 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3388 if (dlm_handles[6].cookie != 0)
3389 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3392 ptlrpc_save_llog_lock(req, lcl);
3395 if (dlm_handles[3].cookie != 0)
3396 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3397 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3398 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3399 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3401 if (dlm_handles[3].cookie != 0)
3402 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3403 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3404 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3405 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3414 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3417 req->rq_status = rc;
3421 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3422 struct ptlrpc_request *, struct lustre_handle *);
3424 static mds_reinter reinters[REINT_MAX + 1] = {
3425 [REINT_SETATTR] mds_reint_setattr,
3426 [REINT_CREATE] mds_reint_create,
3427 [REINT_LINK] mds_reint_link,
3428 [REINT_UNLINK] mds_reint_unlink,
3429 [REINT_RENAME] mds_reint_rename,
3430 [REINT_OPEN] mds_open
3433 int mds_reint_rec(struct mds_update_record *rec, int offset,
3434 struct ptlrpc_request *req, struct lustre_handle *lockh)
3436 struct obd_device *obd = req->rq_export->exp_obd;
3437 struct lvfs_run_ctxt saved;
3440 /* checked by unpacker */
3441 LASSERT(rec->ur_opcode <= REINT_MAX &&
3442 reinters[rec->ur_opcode] != NULL);
3444 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3445 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3446 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);