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/obd_support.h>
35 #include <linux/obd_class.h>
36 #include <linux/obd.h>
37 #include <linux/lustre_lib.h>
38 #include <linux/lustre_idl.h>
39 #include <linux/lustre_mds.h>
40 #include <linux/lustre_dlm.h>
41 #include <linux/lustre_fsfilt.h>
43 #include "mds_internal.h"
45 void mds_commit_cb(struct obd_device *obd, __u64 transno, void *data,
48 obd_transno_commit_cb(obd, transno, error);
51 struct mds_logcancel_data {
52 struct lov_mds_md *mlcd_lmm;
56 struct llog_cookie mlcd_cookies[0];
60 static void mds_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
61 void *cb_data, int error)
63 struct mds_logcancel_data *mlcd = cb_data;
64 struct lov_stripe_md *lsm = NULL;
65 struct llog_ctxt *ctxt;
68 obd_transno_commit_cb(obd, transno, error);
70 CDEBUG(D_HA, "cancelling %d cookies\n",
71 (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
73 rc = obd_unpackmd(obd->u.mds.mds_osc_exp, &lsm, mlcd->mlcd_lmm,
74 mlcd->mlcd_eadatalen);
76 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
77 (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
80 ///* XXX 0 normally, SENDNOW for debug */);
81 ctxt = llog_get_context(&obd->obd_llogs,
82 mlcd->mlcd_cookies[0].lgc_subsys + 1);
83 rc = llog_cancel(ctxt, lsm, mlcd->mlcd_cookielen /
84 sizeof(*mlcd->mlcd_cookies),
85 mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW);
87 CERROR("error cancelling %d log cookies: rc %d\n",
88 (int)(mlcd->mlcd_cookielen /
89 sizeof(*mlcd->mlcd_cookies)), rc);
92 OBD_FREE(mlcd, mlcd->mlcd_size);
95 /* Assumes caller has already pushed us into the kernel context. */
96 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
97 struct ptlrpc_request *req, int rc, __u32 op_data)
99 struct mds_export_data *med = &req->rq_export->exp_mds_data;
100 struct mds_client_data *mcd = med->med_mcd;
101 struct obd_device *obd = req->rq_export->exp_obd;
108 /* if the export has already been failed, we have no last_rcvd slot */
109 if (req->rq_export->exp_failed) {
110 CERROR("committing transaction for disconnected client\n");
119 if (handle == NULL) {
120 /* if we're starting our own xaction, use our own inode */
121 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
122 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
123 if (IS_ERR(handle)) {
124 CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
125 RETURN(PTR_ERR(handle));
131 transno = req->rq_reqmsg->transno;
133 LASSERT(transno == 0);
134 } else if (transno == 0) {
135 spin_lock(&mds->mds_transno_lock);
136 transno = ++mds->mds_last_transno;
137 spin_unlock(&mds->mds_transno_lock);
139 spin_lock(&mds->mds_transno_lock);
140 if (transno > mds->mds_last_transno)
141 mds->mds_last_transno = transno;
142 spin_unlock(&mds->mds_transno_lock);
144 req->rq_repmsg->transno = req->rq_transno = transno;
145 mcd->mcd_last_transno = cpu_to_le64(transno);
146 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
147 mcd->mcd_last_result = cpu_to_le32(rc);
148 mcd->mcd_last_data = cpu_to_le32(op_data);
150 fsfilt_add_journal_cb(req->rq_export->exp_obd, transno, handle,
151 mds_commit_cb, NULL);
152 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd, sizeof(*mcd),
161 DEBUG_REQ(log_pri, req,
162 "wrote trans #"LPU64" client %s at idx %u: err = %d",
163 transno, mcd->mcd_uuid, med->med_idx, err);
165 err = mds_lov_write_objids(obd);
171 CDEBUG(log_pri, "wrote objids: err = %d\n", err);
174 err = fsfilt_commit(obd, inode, handle, 0);
176 CERROR("error committing transaction: %d\n", err);
184 /* this gives the same functionality as the code between
185 * sys_chmod and inode_setattr
186 * chown_common and inode_setattr
187 * utimes and inode_setattr
189 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
191 time_t now = LTIME_S(CURRENT_TIME);
192 struct iattr *attr = &rec->ur_iattr;
193 unsigned int ia_valid = attr->ia_valid;
197 /* only fix up attrs if the client VFS didn't already */
198 if (!(ia_valid & ATTR_RAW))
201 if (!(ia_valid & ATTR_CTIME_SET))
202 LTIME_S(attr->ia_ctime) = now;
203 if (!(ia_valid & ATTR_ATIME_SET))
204 LTIME_S(attr->ia_atime) = now;
205 if (!(ia_valid & ATTR_MTIME_SET))
206 LTIME_S(attr->ia_mtime) = now;
208 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
212 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
213 if (rec->ur_fsuid != inode->i_uid &&
214 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
218 if (ia_valid & ATTR_SIZE) {
219 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
223 if (ia_valid & ATTR_UID) {
226 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
228 if (attr->ia_uid == (uid_t) -1)
229 attr->ia_uid = inode->i_uid;
230 if (attr->ia_gid == (gid_t) -1)
231 attr->ia_gid = inode->i_gid;
232 attr->ia_mode = inode->i_mode;
234 * If the user or group of a non-directory has been
235 * changed by a non-root user, remove the setuid bit.
236 * 19981026 David C Niemi <niemi@tux.org>
238 * Changed this to apply to all users, including root,
239 * to avoid some races. This is the behavior we had in
240 * 2.0. The check for non-root was definitely wrong
241 * for 2.2 anyway, as it should have been using
242 * CAP_FSETID rather than fsuid -- 19990830 SD.
244 if ((inode->i_mode & S_ISUID) == S_ISUID &&
245 !S_ISDIR(inode->i_mode)) {
246 attr->ia_mode &= ~S_ISUID;
247 attr->ia_valid |= ATTR_MODE;
250 * Likewise, if the user or group of a non-directory
251 * has been changed by a non-root user, remove the
252 * setgid bit UNLESS there is no group execute bit
253 * (this would be a file marked for mandatory
254 * locking). 19981026 David C Niemi <niemi@tux.org>
256 * Removed the fsuid check (see the comment above) --
259 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
260 (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
261 attr->ia_mode &= ~S_ISGID;
262 attr->ia_valid |= ATTR_MODE;
264 } else if (ia_valid & ATTR_MODE) {
265 int mode = attr->ia_mode;
267 if (attr->ia_mode == (mode_t) -1)
268 attr->ia_mode = inode->i_mode;
270 (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
275 void mds_steal_ack_locks(struct ptlrpc_request *req)
277 struct obd_export *exp = req->rq_export;
278 struct list_head *tmp;
279 struct ptlrpc_reply_state *oldrep;
280 struct ptlrpc_service *svc;
284 /* CAVEAT EMPTOR: spinlock order */
285 spin_lock_irqsave (&exp->exp_lock, flags);
286 list_for_each (tmp, &exp->exp_outstanding_replies) {
287 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
289 if (oldrep->rs_xid != req->rq_xid)
292 if (oldrep->rs_msg.opc != req->rq_reqmsg->opc)
293 CERROR ("Resent req xid "LPX64" has mismatched opc: "
294 "new %d old %d\n", req->rq_xid,
295 req->rq_reqmsg->opc, oldrep->rs_msg.opc);
297 svc = oldrep->rs_srv_ni->sni_service;
298 spin_lock (&svc->srv_lock);
300 list_del_init (&oldrep->rs_exp_list);
302 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
304 oldrep->rs_nlocks, oldrep,
305 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg.opc,
306 exp->exp_connection->c_peer.peer_nid);
308 for (i = 0; i < oldrep->rs_nlocks; i++)
309 ptlrpc_save_lock(req,
310 &oldrep->rs_locks[i],
311 oldrep->rs_modes[i]);
312 oldrep->rs_nlocks = 0;
314 DEBUG_REQ(D_HA, req, "stole locks for");
315 ptlrpc_schedule_difficult_reply (oldrep);
317 spin_unlock (&svc->srv_lock);
318 spin_unlock_irqrestore (&exp->exp_lock, flags);
321 spin_unlock_irqrestore (&exp->exp_lock, flags);
324 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
326 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
327 mcd->mcd_last_transno, mcd->mcd_last_result);
328 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
329 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
331 mds_steal_ack_locks(req);
334 static void reconstruct_reint_setattr(struct mds_update_record *rec,
335 int offset, struct ptlrpc_request *req)
337 struct mds_export_data *med = &req->rq_export->exp_mds_data;
338 struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
340 struct mds_body *body;
342 mds_req_from_mcd(req, med->med_mcd);
344 de = mds_fid2dentry(obd, rec->ur_fid1, NULL);
346 LASSERT(PTR_ERR(de) == req->rq_status);
350 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
351 mds_pack_inode2fid(req2obd(req), &body->fid1, de->d_inode);
352 mds_pack_inode2body(req2obd(req), body, de->d_inode);
354 /* Don't return OST-specific attributes if we didn't just set them */
355 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
356 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
357 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
358 body->valid |= OBD_MD_FLMTIME;
359 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
360 body->valid |= OBD_MD_FLATIME;
365 /* In the raw-setattr case, we lock the child inode.
366 * In the write-back case or if being called from open, the client holds a lock
369 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
370 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
371 struct ptlrpc_request *req,
372 struct lustre_handle *lh)
374 struct mds_obd *mds = mds_req2mds(req);
375 struct obd_device *obd = req->rq_export->exp_obd;
376 struct mds_body *body;
378 struct inode *inode = NULL;
379 struct lustre_handle lockh[2];
381 struct mds_logcancel_data *mlcd = NULL;
382 int rc = 0, cleanup_phase = 0, err, locked = 0;
385 LASSERT(offset == 0);
387 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x", rec->ur_fid1->id,
388 rec->ur_fid1->generation, rec->ur_iattr.ia_valid);
390 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
392 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
393 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
395 GOTO(cleanup, rc = PTR_ERR(de));
397 __u64 lockpart = MDS_INODELOCK_UPDATE;
398 if (rec->ur_iattr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID) )
399 lockpart |= MDS_INODELOCK_LOOKUP;
400 de = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW,
401 lockh, NULL, 0, lockpart);
403 GOTO(cleanup, rc = PTR_ERR(de));
411 if (S_ISREG(inode->i_mode) && rec->ur_eadata != NULL)
414 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
416 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
418 GOTO(cleanup, rc = PTR_ERR(handle));
420 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
421 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
422 LTIME_S(rec->ur_iattr.ia_mtime),
423 LTIME_S(rec->ur_iattr.ia_ctime));
424 rc = mds_fix_attr(inode, rec);
428 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
429 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
430 (long)&rec->ur_iattr.ia_attr_flags);
432 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
434 if (rc == 0 && S_ISREG(inode->i_mode) && rec->ur_eadata != NULL) {
435 rc = fsfilt_set_md(obd, inode, handle,
436 rec->ur_eadata, rec->ur_eadatalen);
439 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
440 mds_pack_inode2fid(obd, &body->fid1, inode);
441 mds_pack_inode2body(obd, body, inode);
443 /* Don't return OST-specific attributes if we didn't just set them */
444 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
445 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
446 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
447 body->valid |= OBD_MD_FLMTIME;
448 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
449 body->valid |= OBD_MD_FLATIME;
451 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_osc_obd)) {
452 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
455 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
457 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
458 mlcd->mlcd_cookielen = rec->ur_cookielen;
459 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
460 mlcd->mlcd_cookielen;
461 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
462 mlcd->mlcd_cookielen);
463 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
464 mlcd->mlcd_eadatalen);
466 CERROR("unable to allocate log cancel data\n");
472 fsfilt_add_journal_cb(req->rq_export->exp_obd, 0, handle,
473 mds_cancel_cookies_cb, mlcd);
474 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
475 switch (cleanup_phase) {
477 if (S_ISREG(inode->i_mode) && rec->ur_eadata != NULL)
482 if (lockh[1].cookie != 0)
483 ldlm_lock_decref(lockh + 1, LCK_CW);
486 ldlm_lock_decref(lockh, LCK_PW);
488 ptlrpc_save_lock (req, lockh, LCK_PW);
503 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
504 struct ptlrpc_request *req)
506 struct mds_export_data *med = &req->rq_export->exp_mds_data;
507 struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
508 struct dentry *parent, *child;
509 struct mds_body *body;
511 mds_req_from_mcd(req, med->med_mcd);
516 parent = mds_fid2dentry(obd, rec->ur_fid1, NULL);
517 LASSERT(!IS_ERR(parent));
518 child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
519 LASSERT(!IS_ERR(child));
520 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
521 mds_pack_inode2fid(req2obd(req), &body->fid1, child->d_inode);
522 mds_pack_inode2body(req2obd(req), body, child->d_inode);
527 static int mds_reint_create(struct mds_update_record *rec, int offset,
528 struct ptlrpc_request *req,
529 struct lustre_handle *lh)
531 struct dentry *dparent = NULL;
532 struct mds_obd *mds = mds_req2mds(req);
533 struct obd_device *obd = req->rq_export->exp_obd;
534 struct dentry *dchild = NULL;
535 struct inode *dir = NULL;
537 struct lustre_handle lockh[2];
538 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
540 struct dentry_params dp;
541 struct mea *mea = NULL;
545 LASSERT(offset == 0);
546 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
548 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
549 rec->ur_fid1->id, rec->ur_fid1->generation,
550 rec->ur_name, rec->ur_mode);
552 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
554 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
555 GOTO(cleanup, rc = -ESTALE);
557 dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW, lockh,
558 rec->ur_name, rec->ur_namelen - 1,
559 MDS_INODELOCK_UPDATE);
560 if (IS_ERR(dparent)) {
561 rc = PTR_ERR(dparent);
562 CERROR("parent lookup error %d\n", rc);
565 cleanup_phase = 1; /* locked parent dentry */
566 dir = dparent->d_inode;
569 ldlm_lock_dump_handle(D_OTHER, lockh);
571 /* try to retrieve MEA data for this dir */
572 rc = mds_get_lmv_attr(obd, dparent->d_inode, &mea, &mea_size);
574 /* dir is already splitted, check is requested filename
575 * should live at this MDS or at another one */
577 i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
578 if (mea->mea_master != i) {
579 CERROR("inapropriate MDS(%d) for %s. should be %d\n",
580 mea->mea_master, rec->ur_name, i);
581 GOTO(cleanup, rc = -ERESTART);
585 dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
586 if (IS_ERR(dchild)) {
587 rc = PTR_ERR(dchild);
588 CERROR("child lookup error %d\n", rc);
592 cleanup_phase = 2; /* child dentry */
594 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
596 if (type == S_IFREG || type == S_IFDIR) {
597 if ((rc = mds_try_to_split_dir(obd, dparent, &mea, 0))) {
599 /* dir got splitted */
600 GOTO(cleanup, rc = -ERESTART);
602 /* error happened during spitting */
608 if (dir->i_mode & S_ISGID) {
609 if (S_ISDIR(rec->ur_mode))
610 rec->ur_mode |= S_ISGID;
613 dchild->d_fsdata = (void *)&dp;
614 dp.p_inum = (unsigned long)rec->ur_fid2->id;
619 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
621 GOTO(cleanup, rc = PTR_ERR(handle));
622 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
630 /* as Peter asked, mkdir() should distribute new directories
631 * over the whole cluster in order to distribute namespace
632 * processing load. first, we calculate which MDS to use to
633 * put new directory's inode in */
634 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1);
635 if (i == mds->mds_num) {
636 /* inode will be created locally */
638 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
640 GOTO(cleanup, rc = PTR_ERR(handle));
642 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
645 nstripes = *(u16 *)rec->ur_eadata;
647 if (rc == 0 && nstripes) {
648 /* FIXME: error handling here */
649 mds_try_to_split_dir(obd, dchild,
652 } else if (!DENTRY_VALID(dchild)) {
653 /* inode will be created on another MDS */
654 struct obdo *oa = NULL;
655 struct mds_body *body;
657 /* first, create that inode */
661 obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
662 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
663 OBD_MD_FLUID | OBD_MD_FLGID);
664 oa->o_mode = dir->i_mode;
665 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
667 rc = obd_create(mds->mds_lmv_exp, oa, NULL, NULL);
670 /* now, add new dir entry for it */
671 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
673 GOTO(cleanup, rc = PTR_ERR(handle));
674 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
676 oa->o_id, oa->o_generation,
681 body = lustre_msg_buf(req->rq_repmsg,
682 offset, sizeof (*body));
683 body->valid |= OBD_MD_FLID | OBD_MD_MDS;
684 body->fid1.id = oa->o_id;
686 body->fid1.generation = oa->o_generation;
689 /* requested name exists in the directory */
696 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
698 GOTO(cleanup, rc = PTR_ERR(handle));
699 if (rec->ur_tgt == NULL) /* no target supplied */
700 rc = -EINVAL; /* -EPROTO? */
702 rc = vfs_symlink(dir, dchild, rec->ur_tgt);
710 int rdev = rec->ur_rdev;
711 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
713 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
714 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
719 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
720 dchild->d_fsdata = NULL;
721 GOTO(cleanup, rc = -EINVAL);
724 /* In case we stored the desired inum in here, we want to clean up. */
725 if (dchild->d_fsdata == (void *)(unsigned long)rec->ur_fid2->id)
726 dchild->d_fsdata = NULL;
729 CDEBUG(D_INODE, "error during create: %d\n", rc);
731 } else if (dchild->d_inode) {
733 struct inode *inode = dchild->d_inode;
734 struct mds_body *body;
737 LTIME_S(iattr.ia_atime) = rec->ur_time;
738 LTIME_S(iattr.ia_ctime) = rec->ur_time;
739 LTIME_S(iattr.ia_mtime) = rec->ur_time;
740 iattr.ia_uid = rec->ur_fsuid;
741 if (dir->i_mode & S_ISGID)
742 iattr.ia_gid = dir->i_gid;
744 iattr.ia_gid = rec->ur_fsgid;
745 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
746 ATTR_MTIME | ATTR_CTIME;
748 if (rec->ur_fid2->id) {
749 LASSERT(rec->ur_fid2->id == inode->i_ino);
750 inode->i_generation = rec->ur_fid2->generation;
751 /* Dirtied and committed by the upcoming setattr. */
752 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
753 inode->i_ino, inode->i_generation);
755 struct lustre_handle child_ino_lockh;
757 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
758 inode->i_ino, inode->i_generation);
760 /* The inode we were allocated may have just been freed
761 * by an unlink operation. We take this lock to
762 * synchronize against the matching reply-ack-lock taken
763 * in unlink, to avoid replay problems if this reply
764 * makes it out to the client but the unlink's does not.
765 * See bug 2029 for more detail.*/
766 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
767 if (rc != ELDLM_OK) {
768 CERROR("error locking for unlink/create sync: "
771 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
775 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
777 CERROR("error on child setattr: rc = %d\n", rc);
779 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
780 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
782 CERROR("error on parent setattr: rc = %d\n", rc);
784 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
785 mds_pack_inode2fid(obd, &body->fid1, inode);
786 mds_pack_inode2body(obd, body, inode);
791 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
794 /* Destroy the file we just created. This should not need
795 * extra journal credits, as we have already modified all of
796 * the blocks needed in order to create the file in the first
801 err = vfs_rmdir(dir, dchild);
803 CERROR("rmdir in error path: %d\n", err);
806 err = vfs_unlink(dir, dchild);
808 CERROR("unlink in error path: %d\n", err);
814 switch (cleanup_phase) {
815 case 2: /* child dentry */
817 case 1: /* locked parent dentry */
819 if (lockh[1].cookie != 0)
820 ldlm_lock_decref(lockh + 1, LCK_CW);
823 ldlm_lock_decref(lockh, LCK_PW);
825 ptlrpc_save_lock (req, lockh, LCK_PW);
831 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
835 OBD_FREE(mea, mea_size);
840 static int res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
841 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
845 for (i = 0; i < RES_NAME_SIZE; i++) {
846 /* return 1 here, because enqueue_ordered will skip resources
847 * of all zeroes if they're sorted to the end of the list. */
848 if (res1->name[i] == 0 && res2->name[i] != 0)
850 if (res2->name[i] == 0 && res1->name[i] != 0)
853 if (res1->name[i] > res2->name[i])
855 if (res1->name[i] < res2->name[i])
862 if (memcmp(p1, p2, sizeof(*p1)) < 0)
868 /* This function doesn't use ldlm_match_or_enqueue because we're always called
869 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
870 * because they take the place of local semaphores.
872 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
873 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
874 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
875 struct lustre_handle *p1_lockh, int p1_lock_mode,
876 ldlm_policy_data_t *p1_policy,
877 struct ldlm_res_id *p2_res_id,
878 struct lustre_handle *p2_lockh, int p2_lock_mode,
879 ldlm_policy_data_t *p2_policy)
881 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
882 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
883 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
884 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
888 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
890 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n", res_id[0]->name[0],
893 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
894 handles[1] = p1_lockh;
895 handles[0] = p2_lockh;
896 res_id[1] = p1_res_id;
897 res_id[0] = p2_res_id;
898 lock_modes[1] = p1_lock_mode;
899 lock_modes[0] = p2_lock_mode;
900 policies[1] = p1_policy;
901 policies[0] = p2_policy;
904 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
905 res_id[0]->name[0], res_id[1]->name[0]);
907 flags = LDLM_FL_LOCAL_ONLY;
908 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
909 LDLM_IBITS, policies[0], lock_modes[0], &flags,
910 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
911 NULL, 0, NULL, handles[0]);
914 ldlm_lock_dump_handle(D_OTHER, handles[0]);
916 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
917 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
918 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
919 ldlm_lock_addref(handles[1], lock_modes[1]);
920 } else if (res_id[1]->name[0] != 0) {
921 flags = LDLM_FL_LOCAL_ONLY;
922 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
923 *res_id[1], LDLM_IBITS, policies[1],
924 lock_modes[1], &flags, mds_blocking_ast,
925 ldlm_completion_ast, NULL, NULL, NULL, 0,
927 if (rc != ELDLM_OK) {
928 ldlm_lock_decref(handles[0], lock_modes[0]);
931 ldlm_lock_dump_handle(D_OTHER, handles[1]);
937 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
938 struct lustre_handle *p1_lockh, int p1_lock_mode,
939 ldlm_policy_data_t *p1_policy,
940 struct ldlm_res_id *p2_res_id,
941 struct lustre_handle *p2_lockh, int p2_lock_mode,
942 ldlm_policy_data_t *p2_policy,
943 struct ldlm_res_id *c1_res_id,
944 struct lustre_handle *c1_lockh, int c1_lock_mode,
945 ldlm_policy_data_t *c1_policy,
946 struct ldlm_res_id *c2_res_id,
947 struct lustre_handle *c2_lockh, int c2_lock_mode,
948 ldlm_policy_data_t *c2_policy)
950 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
951 c1_res_id, c2_res_id };
952 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
953 c1_lockh, c2_lockh };
954 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
955 c1_lock_mode, c2_lock_mode };
956 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
957 c1_policy, c2_policy};
958 int rc, i, j, sorted, flags;
962 "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
963 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
966 /* simple insertion sort - we have at most 4 elements */
967 for (i = 1; i < 4; i++) {
969 dlm_handles[4] = dlm_handles[i];
970 res_id[4] = res_id[i];
971 lock_modes[4] = lock_modes[i];
972 policies[4] = policies[i];
976 if (res_gt(res_id[j], res_id[4], policies[j],
978 dlm_handles[j + 1] = dlm_handles[j];
979 res_id[j + 1] = res_id[j];
980 lock_modes[j + 1] = lock_modes[j];
981 policies[j + 1] = policies[j];
986 } while (j >= 0 && !sorted);
988 dlm_handles[j + 1] = dlm_handles[4];
989 res_id[j + 1] = res_id[4];
990 lock_modes[j + 1] = lock_modes[4];
991 policies[j + 1] = policies[4];
995 "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
996 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
999 /* XXX we could send ASTs on all these locks first before blocking? */
1000 for (i = 0; i < 4; i++) {
1002 if (res_id[i]->name[0] == 0)
1005 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1006 (policies[i]->l_inodebits.bits &
1007 policies[i-1]->l_inodebits.bits) ) {
1008 memcpy(dlm_handles[i], dlm_handles[i-1],
1009 sizeof(*(dlm_handles[i])));
1010 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1012 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1013 *res_id[i], LDLM_IBITS,
1015 lock_modes[i], &flags,
1017 ldlm_completion_ast, NULL, NULL,
1018 NULL, 0, NULL, dlm_handles[i]);
1020 GOTO(out_err, rc = -EIO);
1021 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1028 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1033 /* In the unlikely case that the child changed while we were waiting
1034 * on the lock, we need to drop the lock on the old child and either:
1035 * - if the child has a lower resource name, then we have to also
1036 * drop the parent lock and regain the locks in the right order
1037 * - in the rename case, if the child has a lower resource name than one of
1038 * the other parent/child resources (maxres) we also need to reget the locks
1039 * - if the child has a higher resource name (this is the common case)
1040 * we can just get the lock on the new child (still in lock order)
1042 * Returns 0 if the child did not change or if it changed but could be locked.
1043 * Returns 1 if the child changed and we need to re-lock (no locks held).
1044 * Returns -ve error with a valid dchild (no locks held). */
1045 static int mds_verify_child(struct obd_device *obd,
1046 struct ldlm_res_id *parent_res_id,
1047 struct lustre_handle *parent_lockh,
1048 struct dentry *dparent, int parent_mode,
1049 struct ldlm_res_id *child_res_id,
1050 struct lustre_handle *child_lockh,
1051 struct dentry **dchildp, int child_mode,
1052 ldlm_policy_data_t *child_policy,
1053 const char *name, int namelen,
1054 struct ldlm_res_id *maxres)
1056 struct dentry *vchild, *dchild = *dchildp;
1057 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1060 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1062 GOTO(cleanup, rc = PTR_ERR(vchild));
1064 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1065 if (child_res_id->name[0] == vchild->d_inum &&
1066 child_res_id->name[1] == vchild->d_generation) {
1075 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1076 (vchild->d_inode != NULL &&
1077 child_res_id->name[0] == vchild->d_inode->i_ino &&
1078 child_res_id->name[1] == vchild->d_inode->i_generation))) {
1087 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1088 vchild->d_inode, dchild ? dchild->d_inode : 0,
1089 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1090 child_res_id->name[0]);
1091 if (child_res_id->name[0] != 0)
1092 ldlm_lock_decref(child_lockh, child_mode);
1096 cleanup_phase = 1; /* parent lock only */
1097 *dchildp = dchild = vchild;
1099 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1101 if (dchild->d_inode) {
1102 child_res_id->name[0] = dchild->d_inode->i_ino;
1103 child_res_id->name[1] = dchild->d_inode->i_generation;
1105 child_res_id->name[0] = dchild->d_inum;
1106 child_res_id->name[1] = dchild->d_generation;
1109 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1110 res_gt(maxres, child_res_id, NULL, NULL)) {
1111 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1112 child_res_id->name[0], parent_res_id->name[0],
1114 GOTO(cleanup, rc = 1);
1117 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1118 *child_res_id, LDLM_IBITS, child_policy,
1119 child_mode, &flags, mds_blocking_ast,
1120 ldlm_completion_ast, NULL, NULL, NULL, 0,
1123 GOTO(cleanup, rc = -EIO);
1126 memset(child_res_id, 0, sizeof(*child_res_id));
1132 switch(cleanup_phase) {
1134 if (child_res_id->name[0] != 0)
1135 ldlm_lock_decref(child_lockh, child_mode);
1137 ldlm_lock_decref(parent_lockh, parent_mode);
1143 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1145 struct lustre_handle *parent_lockh,
1146 struct dentry **dparentp, int parent_mode,
1147 __u64 parent_lockpart,
1148 char *name, int namelen,
1149 struct lustre_handle *child_lockh,
1150 struct dentry **dchildp, int child_mode,
1151 __u64 child_lockpart)
1153 struct ldlm_res_id child_res_id = { .name = {0} };
1154 struct ldlm_res_id parent_res_id = { .name = {0} };
1155 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1156 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1157 struct inode *inode;
1158 int rc = 0, cleanup_phase = 0;
1161 /* Step 1: Lookup parent */
1162 *dparentp = mds_fid2dentry(mds, fid, NULL);
1163 if (IS_ERR(*dparentp))
1164 RETURN(rc = PTR_ERR(*dparentp));
1165 LASSERT((*dparentp)->d_inode);
1167 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1168 (*dparentp)->d_inode->i_ino, name);
1170 parent_res_id.name[0] = (*dparentp)->d_inode->i_ino;
1171 parent_res_id.name[1] = (*dparentp)->d_inode->i_generation;
1173 parent_lockh[1].cookie = 0;
1174 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1175 /* lock just dir { ino, generation } to flush client cache */
1176 if (parent_mode == LCK_PW) {
1177 struct ldlm_res_id res_id = { .name = {0} };
1178 ldlm_policy_data_t policy;
1180 res_id.name[0] = (*dparentp)->d_inode->i_ino;
1181 res_id.name[1] = (*dparentp)->d_inode->i_generation;
1182 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1183 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1185 &policy, LCK_CW, &flags,
1187 ldlm_completion_ast, NULL, NULL,
1188 NULL, 0, NULL, parent_lockh+1);
1193 parent_res_id.name[2] = full_name_hash(name, namelen - 1);
1194 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
1195 (*dparentp)->d_inode->i_ino,
1196 (*dparentp)->d_inode->i_generation,
1197 parent_res_id.name[2]);
1201 cleanup_phase = 1; /* parent dentry */
1203 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1204 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1205 if (IS_ERR(*dchildp)) {
1206 rc = PTR_ERR(*dchildp);
1207 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1211 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1212 /* inode lives on another MDS: return * mds/ino/gen
1213 * and LOOKUP lock. drop possible UPDATE lock! */
1214 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1215 child_res_id.name[0] = (*dchildp)->d_inum;
1216 child_res_id.name[1] = (*dchildp)->d_generation;
1220 inode = (*dchildp)->d_inode;
1222 inode = igrab(inode);
1226 child_res_id.name[0] = inode->i_ino;
1227 child_res_id.name[1] = inode->i_generation;
1232 cleanup_phase = 2; /* child dentry */
1234 /* Step 3: Lock parent and child in resource order. If child doesn't
1235 * exist, we still have to lock the parent and re-lookup. */
1236 rc = enqueue_ordered_locks(obd,&parent_res_id,parent_lockh,parent_mode,
1237 &parent_policy, &child_res_id, child_lockh,
1238 child_mode, &child_policy);
1242 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1243 cleanup_phase = 4; /* child lock */
1245 cleanup_phase = 3; /* parent lock */
1247 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1248 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1249 parent_mode, &child_res_id, child_lockh,
1250 dchildp, child_mode, &child_policy,
1251 name, namelen, &parent_res_id);
1261 switch (cleanup_phase) {
1263 ldlm_lock_decref(child_lockh, child_mode);
1265 ldlm_lock_decref(parent_lockh, parent_mode);
1270 if (parent_lockh[1].cookie)
1271 ldlm_lock_decref(parent_lockh + 1, LCK_CW);
1280 void mds_reconstruct_generic(struct ptlrpc_request *req)
1282 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1284 mds_req_from_mcd(req, med->med_mcd);
1287 int mds_create_local_dentry(struct mds_update_record *rec,
1288 struct obd_device *obd)
1290 struct mds_obd *mds = &obd->u.mds;
1291 struct inode *fids_dir = mds->mds_fids_dir->d_inode;
1292 int fidlen = 0, rc, cleanup_phase = 0;
1293 struct dentry *new_child = NULL;
1294 char *fidname = rec->ur_name;
1295 struct dentry *child = NULL;
1296 struct lustre_handle lockh;
1300 down(&fids_dir->i_sem);
1301 fidlen = ll_fid2str(fidname, rec->ur_fid1->id, rec->ur_fid1->generation);
1302 CDEBUG(D_OTHER, "look for local dentry '%s' for %u/%u\n",
1303 fidname, (unsigned) rec->ur_fid1->id,
1304 (unsigned) rec->ur_fid1->generation);
1306 new_child = lookup_one_len(fidname, mds->mds_fids_dir, fidlen);
1307 up(&fids_dir->i_sem);
1308 if (IS_ERR(new_child)) {
1309 CERROR("can't lookup %s: %d\n", fidname,
1310 (int) PTR_ERR(new_child));
1311 GOTO(cleanup, rc = PTR_ERR(new_child));
1315 if (new_child->d_inode != NULL) {
1316 /* nice. we've already have local dentry! */
1317 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1318 (unsigned) new_child->d_inode->i_ino,
1319 (unsigned) new_child->d_inode->i_generation);
1320 rec->ur_fid1->id = fids_dir->i_ino;
1321 rec->ur_fid1->generation = fids_dir->i_generation;
1322 rec->ur_namelen = fidlen + 1;
1323 GOTO(cleanup, rc = 0);
1326 /* new, local dentry will be added soon. we need no aliases here */
1329 child = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_EX,
1330 &lockh, NULL, 0, MDS_INODELOCK_UPDATE);
1331 if (IS_ERR(child)) {
1332 CERROR("can't get victim\n");
1333 GOTO(cleanup, rc = PTR_ERR(child));
1337 handle = fsfilt_start(obd, fids_dir, FSFILT_OP_LINK, NULL);
1339 GOTO(cleanup, rc = PTR_ERR(handle));
1341 rc = fsfilt_add_dir_entry(obd, mds->mds_fids_dir, fidname, fidlen,
1342 rec->ur_fid1->id, rec->ur_fid1->generation,
1345 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1346 (unsigned long) child->d_inode->i_ino,
1347 (unsigned long) child->d_inode->i_generation, rc);
1349 if (S_ISDIR(child->d_inode->i_mode)) {
1350 fids_dir->i_nlink++;
1351 mark_inode_dirty(fids_dir);
1353 mark_inode_dirty(child->d_inode);
1355 fsfilt_commit(obd, fids_dir, handle, 0);
1357 rec->ur_fid1->id = fids_dir->i_ino;
1358 rec->ur_fid1->generation = fids_dir->i_generation;
1359 rec->ur_namelen = fidlen + 1;
1362 switch(cleanup_phase) {
1364 ldlm_lock_decref(&lockh, LCK_EX);
1374 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1375 struct ptlrpc_request *slave)
1377 void *cookie, *cookie2;
1378 struct mds_body *body2;
1379 struct mds_body *body;
1383 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1384 LASSERT(body != NULL);
1386 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1387 LASSERT(body2 != NULL);
1389 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER))) {
1393 memcpy(body2, body, sizeof(*body));
1394 body2->valid &= ~OBD_MD_FLCOOKIE;
1396 if (!(body->valid & OBD_MD_FLEASIZE))
1399 if (body->eadatasize == 0) {
1400 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1404 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1406 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1407 LASSERT(ea != NULL);
1409 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1410 LASSERT(ea2 != NULL);
1412 memcpy(ea2, ea, body->eadatasize);
1414 if (body->valid & OBD_MD_FLCOOKIE) {
1415 LASSERT(master->rq_repmsg->buflens[2] >=
1416 slave->rq_repmsg->buflens[2]);
1417 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1418 slave->rq_repmsg->buflens[2]);
1419 LASSERT(cookie != NULL);
1421 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1422 master->rq_repmsg->buflens[2]);
1423 LASSERT(cookie2 != NULL);
1424 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1425 body2->valid |= OBD_MD_FLCOOKIE;
1430 static int mds_reint_unlink_remote(struct mds_update_record *rec, int offset,
1431 struct ptlrpc_request *req,
1432 struct lustre_handle *parent_lockh,
1433 struct dentry *dparent,
1434 struct lustre_handle *child_lockh,
1435 struct dentry *dchild)
1437 struct mds_obd *mds = mds_req2mds(req);
1438 struct mdc_op_data op_data;
1439 int rc = 0, cleanup_phase = 0;
1440 struct ptlrpc_request *request = NULL;
1443 LASSERT(offset == 0 || offset == 2);
1445 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode %u/%u/%u)\n",
1446 rec->ur_namelen - 1, rec->ur_name, (unsigned)dchild->d_mdsnum,
1447 (unsigned) dchild->d_inum, (unsigned) dchild->d_generation);
1449 /* time to drop i_nlink on remote MDS */
1450 op_data.fid1.mds = dchild->d_mdsnum;
1451 op_data.fid1.id = dchild->d_inum;
1452 op_data.fid1.generation = dchild->d_generation;
1453 op_data.create_mode = rec->ur_mode;
1454 op_data.namelen = 0;
1455 op_data.name = NULL;
1456 rc = md_unlink(mds->mds_lmv_exp, &op_data, &request);
1459 mds_copy_unlink_reply(req, request);
1460 ptlrpc_req_finished(request);
1463 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
1464 req->rq_status = rc;
1467 if (parent_lockh[1].cookie != 0)
1468 ldlm_lock_decref(parent_lockh + 1, LCK_CW);
1470 ldlm_lock_decref(child_lockh, LCK_EX);
1472 ldlm_lock_decref(parent_lockh, LCK_PW);
1474 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1481 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1482 struct ptlrpc_request *req,
1483 struct lustre_handle *lh)
1485 struct dentry *dparent, *dchild;
1486 struct mds_obd *mds = mds_req2mds(req);
1487 struct obd_device *obd = req->rq_export->exp_obd;
1488 struct mds_body *body = NULL;
1489 struct inode *child_inode;
1490 struct lustre_handle parent_lockh[2], child_lockh, child_reuse_lockh;
1491 char fidname[LL_FID_NAMELEN];
1492 void *handle = NULL;
1493 int rc = 0, log_unlink = 0, cleanup_phase = 0;
1494 int unlink_by_fid = 0;
1497 LASSERT(offset == 0 || offset == 2);
1499 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1500 rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name);
1502 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1504 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1505 GOTO(cleanup, rc = -ENOENT);
1507 if (rec->ur_namelen == 1) {
1508 /* this is request to drop i_nlink on local inode */
1510 rec->ur_name = fidname;
1511 rc = mds_create_local_dentry(rec, obd);
1514 rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1,
1515 parent_lockh, &dparent, LCK_PW,
1516 MDS_INODELOCK_UPDATE,
1517 rec->ur_name, rec->ur_namelen,
1518 &child_lockh, &dchild, LCK_EX,
1519 MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE);
1523 if (dchild->d_flags & DCACHE_CROSS_REF) {
1524 /* we should have parent lock only here */
1525 LASSERT(unlink_by_fid == 0);
1526 LASSERT(dchild->d_mdsnum != mds->mds_num);
1527 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
1528 dparent, &child_lockh, dchild);
1532 cleanup_phase = 1; /* dchild, dparent, locks */
1535 child_inode = dchild->d_inode;
1536 if (child_inode == NULL) {
1537 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1538 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
1539 GOTO(cleanup, rc = -ENOENT);
1542 cleanup_phase = 2; /* dchild has a lock */
1544 /* Step 4: Get a lock on the ino to sync with creation WRT inode
1545 * reuse (see bug 2029). */
1546 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
1550 cleanup_phase = 3; /* child inum lock */
1552 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
1554 /* ldlm_reply in buf[0] if called via intent */
1558 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
1559 LASSERT(body != NULL);
1561 /* If this is the last reference to this inode, get the OBD EA
1562 * data first so the client can destroy OST objects.
1563 * we only do the object removal if no open files remain.
1564 * Nobody can get at this name anymore because of the locks so
1565 * we make decisions here as to whether to remove the inode */
1566 if (S_ISREG(child_inode->i_mode) && child_inode->i_nlink == 1 &&
1567 mds_open_orphan_count(child_inode) == 0) {
1568 mds_pack_inode2fid(obd, &body->fid1, child_inode);
1569 mds_pack_inode2body(obd, body, child_inode);
1570 mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
1572 if (!(body->valid & OBD_MD_FLEASIZE)) {
1573 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1574 OBD_MD_FLATIME | OBD_MD_FLMTIME);
1580 /* We have to do these checks ourselves, in case we are making an
1581 * orphan. The client tells us whether rmdir() or unlink() was called,
1582 * so we need to return appropriate errors (bug 72).
1584 * We don't have to check permissions, because vfs_rename (called from
1585 * mds_open_unlink_rename) also calls may_delete. */
1586 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1587 if (!S_ISDIR(child_inode->i_mode))
1588 GOTO(cleanup, rc = -ENOTDIR);
1590 if (S_ISDIR(child_inode->i_mode))
1591 GOTO(cleanup, rc = -EISDIR);
1594 if (child_inode->i_nlink == (S_ISDIR(child_inode->i_mode) ? 2 : 1) &&
1595 mds_open_orphan_count(child_inode) > 0) {
1596 rc = mds_open_unlink_rename(rec, obd, dparent, dchild, &handle);
1597 cleanup_phase = 4; /* transaction */
1601 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
1602 switch (child_inode->i_mode & S_IFMT) {
1604 /* Drop any lingering child directories before we start our
1605 * transaction, to avoid doing multiple inode dirty/delete
1606 * in our compound transaction (bug 1321). */
1607 shrink_dcache_parent(dchild);
1608 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1611 GOTO(cleanup, rc = PTR_ERR(handle));
1612 cleanup_phase = 4; /* transaction */
1613 rc = vfs_rmdir(dparent->d_inode, dchild);
1616 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
1617 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
1619 handle = fsfilt_start_log(obd, dparent->d_inode,
1620 FSFILT_OP_UNLINK, NULL,
1621 le32_to_cpu(lmm->lmm_stripe_count));
1623 GOTO(cleanup, rc = PTR_ERR(handle));
1625 cleanup_phase = 4; /* transaction */
1626 rc = vfs_unlink(dparent->d_inode, dchild);
1628 if (!rc && log_unlink)
1629 if (mds_log_op_unlink(obd, child_inode,
1630 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
1631 req->rq_repmsg->buflens[offset + 1],
1632 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
1633 req->rq_repmsg->buflens[offset + 2]) > 0)
1634 body->valid |= OBD_MD_FLCOOKIE;
1642 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
1645 GOTO(cleanup, rc = PTR_ERR(handle));
1646 cleanup_phase = 4; /* transaction */
1647 rc = vfs_unlink(dparent->d_inode, dchild);
1650 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
1653 GOTO(cleanup, rc = -EINVAL);
1661 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1662 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1663 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1665 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1667 CERROR("error on parent setattr: rc = %d\n", err);
1670 switch(cleanup_phase) {
1672 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1675 (void)obd_set_info(mds->mds_osc_exp, strlen("unlinked"),
1676 "unlinked", 0, NULL);
1677 case 3: /* child ino-reuse lock */
1678 if (rc && body != NULL) {
1679 // Don't unlink the OST objects if the MDS unlink failed
1683 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
1685 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
1686 case 2: /* child lock */
1687 ldlm_lock_decref(&child_lockh, LCK_EX);
1688 case 1: /* child and parent dentry, parent lock */
1690 if (parent_lockh[1].cookie != 0)
1691 ldlm_lock_decref(parent_lockh + 1, LCK_CW);
1694 ldlm_lock_decref(parent_lockh, LCK_PW);
1696 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1703 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1706 req->rq_status = rc;
1711 * to service requests from remote MDS to increment i_nlink
1713 static int mds_reint_link_acquire(struct mds_update_record *rec,
1714 int offset, struct ptlrpc_request *req,
1715 struct lustre_handle *lh)
1717 struct obd_device *obd = req->rq_export->exp_obd;
1718 struct ldlm_res_id src_res_id = { .name = {0} };
1719 struct lustre_handle *handle = NULL, src_lockh;
1720 struct mds_obd *mds = mds_req2mds(req);
1721 int rc = 0, cleanup_phase = 0;
1722 struct dentry *de_src = NULL;
1723 ldlm_policy_data_t policy;
1727 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks %u/%u/%u\n",
1728 obd->obd_name, (unsigned) rec->ur_fid1->mds,
1729 (unsigned) rec->ur_fid1->id,
1730 (unsigned) rec->ur_fid1->generation);
1732 /* Step 1: Lookup the source inode and target directory by FID */
1733 de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1735 GOTO(cleanup, rc = PTR_ERR(de_src));
1736 cleanup_phase = 1; /* source dentry */
1738 src_res_id.name[0] = de_src->d_inode->i_ino;
1739 src_res_id.name[1] = de_src->d_inode->i_generation;
1740 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1742 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1743 src_res_id, LDLM_IBITS, &policy,
1744 LCK_EX, &flags, mds_blocking_ast,
1745 ldlm_completion_ast, NULL, NULL,
1746 NULL, 0, NULL, &src_lockh);
1748 GOTO(cleanup, rc = -ENOLCK);
1749 cleanup_phase = 2; /* lock */
1751 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1753 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
1754 if (IS_ERR(handle)) {
1755 rc = PTR_ERR(handle);
1758 de_src->d_inode->i_nlink++;
1759 mark_inode_dirty(de_src->d_inode);
1762 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
1763 handle, req, rc, 0);
1765 switch (cleanup_phase) {
1768 ldlm_lock_decref(&src_lockh, LCK_EX);
1770 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
1776 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1779 req->rq_status = rc;
1784 * request to link to foreign inode:
1785 * - acquire i_nlinks on this inode
1788 static int mds_reint_link_to_remote(struct mds_update_record *rec,
1789 int offset, struct ptlrpc_request *req,
1790 struct lustre_handle *lh)
1792 struct lustre_handle *handle = NULL, tgt_dir_lockh[2];
1793 struct obd_device *obd = req->rq_export->exp_obd;
1794 struct dentry *de_tgt_dir = NULL;
1795 struct mds_obd *mds = mds_req2mds(req);
1796 int rc = 0, cleanup_phase = 0;
1797 struct mdc_op_data op_data;
1798 struct ptlrpc_request *request = NULL;
1801 #define fmt "%s: request to link %u/%u/%u:%*s to foreign inode %u/%u/%u\n"
1802 DEBUG_REQ(D_INODE, req, fmt, obd->obd_name,
1803 (unsigned) rec->ur_fid2->mds,
1804 (unsigned) rec->ur_fid2->id,
1805 (unsigned) rec->ur_fid2->generation,
1806 rec->ur_namelen - 1, rec->ur_name,
1807 (unsigned) rec->ur_fid1->mds,
1808 (unsigned) rec->ur_fid1->id,
1809 (unsigned)rec->ur_fid1->generation);
1811 de_tgt_dir = mds_fid2locked_dentry(obd, rec->ur_fid2, NULL, LCK_EX,
1812 tgt_dir_lockh, rec->ur_name,
1813 rec->ur_namelen - 1,
1814 MDS_INODELOCK_UPDATE);
1815 if (IS_ERR(de_tgt_dir))
1816 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
1819 op_data.fid1 = *(rec->ur_fid1);
1820 op_data.namelen = 0;
1821 op_data.name = NULL;
1822 rc = md_link(mds->mds_lmv_exp, &op_data, &request);
1826 ptlrpc_req_finished(request);
1828 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
1830 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
1831 if (IS_ERR(handle)) {
1832 rc = PTR_ERR(handle);
1836 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
1837 rec->ur_namelen - 1, rec->ur_fid1->id,
1838 rec->ur_fid1->generation, rec->ur_fid1->mds);
1842 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
1843 handle, req, rc, 0);
1846 switch (cleanup_phase) {
1849 /* FIXME: drop i_nlink on remote inode here */
1850 CERROR("MUST drop drop i_nlink here\n");
1855 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
1857 ldlm_lock_decref(tgt_dir_lockh + 1, LCK_CW);
1860 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
1862 ptlrpc_save_lock(req, tgt_dir_lockh + 1, LCK_CW);
1868 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1871 req->rq_status = rc;
1875 static int mds_reint_link(struct mds_update_record *rec, int offset,
1876 struct ptlrpc_request *req,
1877 struct lustre_handle *lh)
1879 struct obd_device *obd = req->rq_export->exp_obd;
1880 struct dentry *de_src = NULL;
1881 struct dentry *de_tgt_dir = NULL;
1882 struct dentry *dchild = NULL;
1883 struct mds_obd *mds = mds_req2mds(req);
1884 struct lustre_handle *handle = NULL, tgt_dir_lockh[2], src_lockh;
1885 struct ldlm_res_id src_res_id = { .name = {0} };
1886 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
1887 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
1888 ldlm_policy_data_t tgt_dir_policy =
1889 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
1891 int rc = 0, cleanup_phase = 0;
1894 LASSERT(offset == 0);
1896 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
1897 rec->ur_fid1->id, rec->ur_fid1->generation,
1898 rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_name);
1900 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1902 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1903 GOTO(cleanup, rc = -ENOENT);
1905 if (rec->ur_fid1->mds != mds->mds_num) {
1906 rc = mds_reint_link_to_remote(rec, offset, req, lh);
1910 if (rec->ur_namelen == 1) {
1911 rc = mds_reint_link_acquire(rec, offset, req, lh);
1915 /* Step 1: Lookup the source inode and target directory by FID */
1916 de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1918 GOTO(cleanup, rc = PTR_ERR(de_src));
1920 cleanup_phase = 1; /* source dentry */
1922 de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
1923 if (IS_ERR(de_tgt_dir))
1924 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
1926 cleanup_phase = 2; /* target directory dentry */
1928 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
1929 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name, rec->ur_name,
1930 de_src->d_inode->i_ino);
1932 /* Step 2: Take the two locks */
1933 src_res_id.name[0] = de_src->d_inode->i_ino;
1934 src_res_id.name[1] = de_src->d_inode->i_generation;
1935 tgt_dir_res_id.name[0] = de_tgt_dir->d_inode->i_ino;
1936 tgt_dir_res_id.name[1] = de_tgt_dir->d_inode->i_generation;
1938 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
1940 /* Get a temp lock on just ino, gen to flush client cache */
1941 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1942 tgt_dir_res_id, LDLM_IBITS, &src_policy,
1943 LCK_CW, &flags, mds_blocking_ast,
1944 ldlm_completion_ast, NULL, NULL,
1945 NULL, 0, NULL, tgt_dir_lockh + 1);
1947 GOTO(cleanup, rc = -ENOLCK);
1949 tgt_dir_res_id.name[2] = full_name_hash(rec->ur_name,
1950 rec->ur_namelen - 1);
1951 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
1952 de_tgt_dir->d_inode->i_ino,
1953 de_tgt_dir->d_inode->i_generation,
1954 tgt_dir_res_id.name[2]);
1957 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
1959 &tgt_dir_res_id, tgt_dir_lockh, LCK_EX,
1964 cleanup_phase = 3; /* locks */
1966 /* Step 3: Lookup the child */
1967 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen-1);
1968 if (IS_ERR(dchild)) {
1969 rc = PTR_ERR(dchild);
1970 if (rc != -EPERM && rc != -EACCES)
1971 CERROR("child lookup error %d\n", rc);
1975 cleanup_phase = 4; /* child dentry */
1977 if (dchild->d_inode) {
1978 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
1979 de_tgt_dir->d_inode->i_ino, rec->ur_name);
1984 /* Step 4: Do it. */
1985 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1987 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
1988 if (IS_ERR(handle)) {
1989 rc = PTR_ERR(handle);
1993 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
1994 if (rc && rc != -EPERM && rc != -EACCES)
1995 CERROR("vfs_link error %d\n", rc);
1997 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
1998 handle, req, rc, 0);
2001 switch (cleanup_phase) {
2002 case 4: /* child dentry */
2006 ldlm_lock_decref(&src_lockh, LCK_EX);
2007 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2009 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2010 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2012 case 2: /* target dentry */
2014 if (tgt_dir_lockh[1].cookie)
2015 ldlm_lock_decref(tgt_dir_lockh + 1, LCK_CW);
2019 case 1: /* source dentry */
2024 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2027 req->rq_status = rc;
2032 * add a hard link in the PENDING directory, only used by rename()
2034 static int mds_add_link_orphan(struct mds_update_record *rec,
2035 struct obd_device *obd,
2036 struct dentry *dentry)
2038 struct mds_obd *mds = &obd->u.mds;
2039 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
2040 struct dentry *pending_child;
2041 char fidname[LL_FID_NAMELEN];
2045 LASSERT(dentry->d_inode);
2046 LASSERT(!mds_inode_is_orphan(dentry->d_inode));
2048 down(&pending_dir->i_sem);
2049 fidlen = ll_fid2str(fidname, dentry->d_inode->i_ino,
2050 dentry->d_inode->i_generation);
2052 CDEBUG(D_ERROR, "pending destroy of %dx open file %s = %s\n",
2053 mds_open_orphan_count(dentry->d_inode),
2054 rec->ur_name, fidname);
2056 pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
2057 if (IS_ERR(pending_child))
2058 GOTO(out_lock, rc = PTR_ERR(pending_child));
2060 if (pending_child->d_inode != NULL) {
2061 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
2062 LASSERT(pending_child->d_inode == dentry->d_inode);
2063 GOTO(out_dput, rc = 0);
2067 rc = vfs_link(dentry, pending_dir, pending_child);
2070 CERROR("error addlink orphan %s to PENDING: rc = %d\n",
2073 mds_inode_set_orphan(dentry->d_inode);
2075 l_dput(pending_child);
2077 up(&pending_dir->i_sem);
2081 /* The idea here is that we need to get four locks in the end:
2082 * one on each parent directory, one on each child. We need to take
2083 * these locks in some kind of order (to avoid deadlocks), and the order
2084 * I selected is "increasing resource number" order. We need to look up
2085 * the children, however, before we know what the resource number(s) are.
2086 * Thus the following plan:
2088 * 1,2. Look up the parents
2089 * 3,4. Look up the children
2090 * 5. Take locks on the parents and children, in order
2091 * 6. Verify that the children haven't changed since they were looked up
2093 * If there was a race and the children changed since they were first looked
2094 * up, it is possible that mds_verify_child() will be able to just grab the
2095 * lock on the new child resource (if it has a higher resource than any other)
2096 * but we need to compare against not only its parent, but also against the
2097 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2099 * We need the fancy igrab() on the child inodes because we aren't holding a
2100 * lock on the parent after the lookup is done, so dentry->d_inode may change
2101 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2103 static int mds_get_parents_children_locked(struct obd_device *obd,
2104 struct mds_obd *mds,
2105 struct ll_fid *p1_fid,
2106 struct dentry **de_srcdirp,
2107 struct ll_fid *p2_fid,
2108 struct dentry **de_tgtdirp,
2110 const char *old_name, int old_len,
2111 struct dentry **de_oldp,
2112 const char *new_name, int new_len,
2113 struct dentry **de_newp,
2114 struct lustre_handle *dlm_handles,
2117 struct ldlm_res_id p1_res_id = { .name = {0} };
2118 struct ldlm_res_id p2_res_id = { .name = {0} };
2119 struct ldlm_res_id c1_res_id = { .name = {0} };
2120 struct ldlm_res_id c2_res_id = { .name = {0} };
2121 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2122 /* Only dentry should change, but the inode itself would be
2124 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2125 /* If something is going to be replaced, both dentry and inode locks are
2127 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP|
2128 MDS_INODELOCK_UPDATE}};
2129 struct ldlm_res_id *maxres_src, *maxres_tgt;
2130 struct inode *inode;
2131 int rc = 0, cleanup_phase = 0;
2134 /* Step 1: Lookup the source directory */
2135 *de_srcdirp = mds_fid2dentry(mds, p1_fid, NULL);
2136 if (IS_ERR(*de_srcdirp))
2137 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2139 cleanup_phase = 1; /* source directory dentry */
2141 p1_res_id.name[0] = (*de_srcdirp)->d_inode->i_ino;
2142 p1_res_id.name[1] = (*de_srcdirp)->d_inode->i_generation;
2144 /* Step 2: Lookup the target directory */
2145 if (memcmp(p1_fid, p2_fid, sizeof(*p1_fid)) == 0) {
2146 *de_tgtdirp = dget(*de_srcdirp);
2148 *de_tgtdirp = mds_fid2dentry(mds, p2_fid, NULL);
2149 if (IS_ERR(*de_tgtdirp))
2150 GOTO(cleanup, rc = PTR_ERR(*de_tgtdirp));
2153 cleanup_phase = 2; /* target directory dentry */
2155 p2_res_id.name[0] = (*de_tgtdirp)->d_inode->i_ino;
2156 p2_res_id.name[1] = (*de_tgtdirp)->d_inode->i_generation;
2159 dlm_handles[5].cookie = 0;
2160 dlm_handles[6].cookie = 0;
2161 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2162 /* Get a temp lock on just ino, gen to flush client cache */
2163 rc = enqueue_ordered_locks(obd, &p1_res_id, &(dlm_handles[5]),
2164 LCK_CW, &p_policy, &p2_res_id,
2165 &(dlm_handles[6]),LCK_CW,&p_policy);
2169 p1_res_id.name[2] = full_name_hash(old_name, old_len - 1);
2170 p2_res_id.name[2] = full_name_hash(new_name, new_len - 1);
2171 CDEBUG(D_INFO, "take locks on %lu:%u:"LPX64", %lu:%u:"LPX64"\n",
2172 (*de_srcdirp)->d_inode->i_ino,
2173 (*de_srcdirp)->d_inode->i_generation, p1_res_id.name[2],
2174 (*de_tgtdirp)->d_inode->i_ino,
2175 (*de_tgtdirp)->d_inode->i_generation, p2_res_id.name[2]);
2179 /* Step 3: Lookup the source child entry */
2180 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp, old_len - 1);
2181 if (IS_ERR(*de_oldp)) {
2182 rc = PTR_ERR(*de_oldp);
2183 CERROR("old child lookup error (%*s): %d\n",
2184 old_len - 1, old_name, rc);
2188 cleanup_phase = 3; /* original name dentry */
2190 inode = (*de_oldp)->d_inode;
2191 if (inode != NULL) {
2192 inode = igrab(inode);
2194 GOTO(cleanup, rc = -ENOENT);
2196 c1_res_id.name[0] = inode->i_ino;
2197 c1_res_id.name[1] = inode->i_generation;
2199 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2200 c1_res_id.name[0] = (*de_oldp)->d_inum;
2201 c1_res_id.name[1] = (*de_oldp)->d_generation;
2204 /* Step 4: Lookup the target child entry */
2205 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp, new_len - 1);
2206 if (IS_ERR(*de_newp)) {
2207 rc = PTR_ERR(*de_newp);
2208 CERROR("new child lookup error (%*s): %d\n",
2209 old_len - 1, old_name, rc);
2213 cleanup_phase = 4; /* target dentry */
2215 inode = (*de_newp)->d_inode;
2216 if (inode != NULL) {
2217 inode = igrab(inode);
2221 c2_res_id.name[0] = inode->i_ino;
2222 c2_res_id.name[1] = inode->i_generation;
2224 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2225 c2_res_id.name[0] = (*de_newp)->d_inum;
2226 c2_res_id.name[1] = (*de_newp)->d_generation;
2230 /* Step 5: Take locks on the parents and child(ren) */
2231 maxres_src = &p1_res_id;
2232 maxres_tgt = &p2_res_id;
2233 cleanup_phase = 4; /* target dentry */
2235 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL,NULL))
2236 maxres_src = &c1_res_id;
2237 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL,NULL))
2238 maxres_tgt = &c2_res_id;
2240 rc = enqueue_4ordered_locks(obd, &p1_res_id,&dlm_handles[0],parent_mode,
2242 &p2_res_id, &dlm_handles[1], parent_mode,
2244 &c1_res_id, &dlm_handles[2], child_mode,
2246 &c2_res_id, &dlm_handles[3], child_mode,
2251 cleanup_phase = 6; /* parent and child(ren) locks */
2253 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2254 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2255 parent_mode, &c1_res_id, &dlm_handles[2],
2256 de_oldp, child_mode, &c1_policy, old_name,old_len,
2259 if (c2_res_id.name[0] != 0)
2260 ldlm_lock_decref(&dlm_handles[3], child_mode);
2261 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2268 if (!DENTRY_VALID(*de_oldp))
2269 GOTO(cleanup, rc = -ENOENT);
2271 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2272 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2273 parent_mode, &c2_res_id, &dlm_handles[3],
2274 de_newp, child_mode, &c2_policy, new_name,
2275 new_len, maxres_src);
2277 ldlm_lock_decref(&dlm_handles[2], child_mode);
2278 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2288 switch (cleanup_phase) {
2289 case 6: /* child lock(s) */
2290 if (c2_res_id.name[0] != 0)
2291 ldlm_lock_decref(&dlm_handles[3], child_mode);
2292 if (c1_res_id.name[0] != 0)
2293 ldlm_lock_decref(&dlm_handles[2], child_mode);
2294 case 5: /* parent locks */
2295 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2296 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2297 case 4: /* target dentry */
2299 case 3: /* source dentry */
2301 case 2: /* target directory dentry */
2302 l_dput(*de_tgtdirp);
2303 case 1: /* source directry dentry */
2304 l_dput(*de_srcdirp);
2311 static int mds_reint_rename_create_name(struct mds_update_record *rec,
2312 int offset, struct ptlrpc_request *req)
2314 struct obd_device *obd = req->rq_export->exp_obd;
2315 struct dentry *de_srcdir = NULL;
2316 struct dentry *de_new = NULL;
2317 struct mds_obd *mds = mds_req2mds(req);
2318 struct lustre_handle parent_lockh[2];
2319 struct lustre_handle child_lockh;
2320 int cleanup_phase = 0;
2321 void *handle = NULL;
2325 /* another MDS executing rename operation has asked us
2326 * to create target name. such a creation should destroy
2327 * existing target name */
2329 CDEBUG(D_OTHER, "%s: request to create name %s for %lu/%lu/%lu\n",
2330 obd->obd_name, rec->ur_tgt,
2331 (unsigned long) rec->ur_fid1->mds,
2332 (unsigned long) rec->ur_fid1->id,
2333 (unsigned long) rec->ur_fid1->generation);
2335 /* first, lookup the target */
2336 child_lockh.cookie = 0;
2337 rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid2, parent_lockh,
2338 &de_srcdir,LCK_PW,MDS_INODELOCK_UPDATE,
2339 rec->ur_tgt, rec->ur_tgtlen,
2340 &child_lockh, &de_new, LCK_EX,
2341 MDS_INODELOCK_LOOKUP);
2348 LASSERT(de_srcdir->d_inode);
2351 if (de_new->d_inode) {
2352 /* name exists and points to local inode
2353 * try to unlink this name and create new one */
2354 CERROR("%s: %s points to local inode %lu/%lu\n",
2355 obd->obd_name, rec->ur_tgt,
2356 (unsigned long) de_new->d_inode->i_ino,
2357 (unsigned long) de_new->d_inode->i_generation);
2358 handle = fsfilt_start(obd, de_srcdir->d_inode,
2359 FSFILT_OP_RENAME, NULL);
2361 GOTO(cleanup, rc = PTR_ERR(handle));
2362 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de_new);
2365 } else if (de_new->d_flags & DCACHE_CROSS_REF) {
2366 /* name exists adn points to remove inode */
2367 CERROR("%s: %s points to remote inode %lu/%lu/%lu\n",
2368 obd->obd_name, rec->ur_tgt,
2369 (unsigned long) de_new->d_mdsnum,
2370 (unsigned long) de_new->d_inum,
2371 (unsigned long) de_new->d_generation);
2373 /* name doesn't exist. the simplest case */
2374 handle = fsfilt_start(obd, de_srcdir->d_inode,
2375 FSFILT_OP_LINK, NULL);
2377 GOTO(cleanup, rc = PTR_ERR(handle));
2381 rc = fsfilt_add_dir_entry(obd, de_srcdir, rec->ur_tgt,
2382 rec->ur_tgtlen - 1, rec->ur_fid1->id,
2383 rec->ur_fid1->generation, rec->ur_fid1->mds);
2385 CERROR("add_dir_entry() returned error %d\n", rc);
2388 rc = mds_finish_transno(mds, de_srcdir ? de_srcdir->d_inode : NULL,
2389 handle, req, rc, 0);
2390 switch(cleanup_phase) {
2394 if (parent_lockh[1].cookie != 0)
2395 ldlm_lock_decref(&parent_lockh[1], LCK_CW);
2397 ldlm_lock_decref(&parent_lockh[0], LCK_PW);
2398 if (child_lockh.cookie != 0)
2399 ldlm_lock_decref(&child_lockh, LCK_EX);
2407 req->rq_status = rc;
2412 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
2413 struct ptlrpc_request *req)
2415 struct obd_device *obd = req->rq_export->exp_obd;
2416 struct ptlrpc_request *req2 = NULL;
2417 struct dentry *de_srcdir = NULL;
2418 struct dentry *de_old = NULL;
2419 struct mds_obd *mds = mds_req2mds(req);
2420 struct lustre_handle parent_lockh[2];
2421 struct lustre_handle child_lockh;
2422 struct mdc_op_data opdata;
2423 int cleanup_phase = 0;
2424 void *handle = NULL;
2428 CDEBUG(D_OTHER, "%s: move name %s onto another mds%u\n",
2429 obd->obd_name, rec->ur_name, rec->ur_fid2->mds + 1);
2430 memset(&opdata, 0, sizeof(opdata));
2432 child_lockh.cookie = 0;
2433 rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1, parent_lockh,
2434 &de_srcdir,LCK_PW,MDS_INODELOCK_UPDATE,
2435 rec->ur_name, rec->ur_namelen,
2436 &child_lockh, &de_old, LCK_EX,
2437 MDS_INODELOCK_LOOKUP);
2440 LASSERT(de_srcdir->d_inode);
2443 /* we already know the target should be created on another MDS
2444 * so, we have to request that MDS to do it */
2446 /* prepare source fid */
2447 if (de_old->d_flags & DCACHE_CROSS_REF) {
2448 LASSERT(de_old->d_inode == NULL);
2449 CDEBUG(D_OTHER, "request to move remote name\n");
2450 opdata.fid1.mds = de_old->d_mdsnum;
2451 opdata.fid1.id = de_old->d_inum;
2452 opdata.fid1.generation = de_old->d_generation;
2453 } else if (de_old->d_inode == NULL) {
2454 /* oh, source doesn't exist */
2455 GOTO(cleanup, rc = -ENOENT);
2457 LASSERT(de_old->d_inode != NULL);
2458 CDEBUG(D_OTHER, "request to move local name\n");
2459 opdata.fid1.mds = mds->mds_num;
2460 opdata.fid1.id = de_old->d_inode->i_ino;
2461 opdata.fid1.generation = de_old->d_inode->i_generation;
2464 opdata.fid2 = *(rec->ur_fid2);
2465 rc = md_rename(mds->mds_lmv_exp, &opdata, NULL, 0, rec->ur_tgt,
2466 rec->ur_tgtlen - 1, &req2);
2471 handle = fsfilt_start(obd, de_srcdir->d_inode, FSFILT_OP_UNLINK, NULL);
2473 GOTO(cleanup, rc = PTR_ERR(handle));
2474 rc = fsfilt_del_dir_entry(obd, de_old);
2479 rc = mds_finish_transno(mds, de_srcdir ? de_srcdir->d_inode : NULL,
2480 handle, req, rc, 0);
2482 ptlrpc_req_finished(req2);
2485 if (parent_lockh[1].cookie != 0)
2486 ldlm_lock_decref(&parent_lockh[1], LCK_CW);
2488 ldlm_lock_decref(&parent_lockh[0], LCK_PW);
2489 if (child_lockh.cookie != 0)
2490 ldlm_lock_decref(&child_lockh, LCK_EX);
2495 req->rq_status = rc;
2500 static int mds_reint_rename(struct mds_update_record *rec, int offset,
2501 struct ptlrpc_request *req,
2502 struct lustre_handle *lockh)
2504 struct obd_device *obd = req->rq_export->exp_obd;
2505 struct dentry *de_srcdir = NULL;
2506 struct dentry *de_tgtdir = NULL;
2507 struct dentry *de_old = NULL;
2508 struct dentry *de_new = NULL;
2509 struct mds_obd *mds = mds_req2mds(req);
2510 struct lustre_handle dlm_handles[7];
2511 struct mds_body *body = NULL;
2512 int rc = 0, lock_count = 3;
2513 int cleanup_phase = 0;
2514 void *handle = NULL;
2517 LASSERT(offset == 0);
2519 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u %s to "LPU64"/%u %s",
2520 rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name,
2521 rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_tgt);
2523 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2525 if (rec->ur_namelen == 1) {
2526 rc = mds_reint_rename_create_name(rec, offset, req);
2530 if (rec->ur_fid2->mds != mds->mds_num) {
2531 rc = mds_reint_rename_to_remote(rec, offset, req);
2535 rc = mds_get_parents_children_locked(obd, mds, rec->ur_fid1, &de_srcdir,
2536 rec->ur_fid2, &de_tgtdir, LCK_PW,
2537 rec->ur_name, rec->ur_namelen,
2538 &de_old, rec->ur_tgt,
2539 rec->ur_tgtlen, &de_new,
2540 dlm_handles, LCK_EX);
2544 cleanup_phase = 1; /* parent(s), children, locks */
2546 if (de_new->d_inode)
2549 /* sanity check for src inode */
2550 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
2551 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
2552 GOTO(cleanup, rc = -EINVAL);
2554 /* sanity check for dest inode */
2555 if (de_new->d_inode &&
2556 (de_new->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
2557 de_new->d_inode->i_ino == de_tgtdir->d_inode->i_ino))
2558 GOTO(cleanup, rc = -EINVAL);
2560 if (de_old->d_inode == de_new->d_inode) {
2561 GOTO(cleanup, rc = 0);
2564 /* if we are about to remove the target at first, pass the EA of
2565 * that inode to client to perform and cleanup on OST */
2566 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
2567 LASSERT(body != NULL);
2569 if (de_new->d_inode &&
2570 S_ISREG(de_new->d_inode->i_mode) &&
2571 de_new->d_inode->i_nlink == 1 &&
2572 mds_open_orphan_count(de_new->d_inode) == 0) {
2573 mds_pack_inode2fid(obd, &body->fid1, de_new->d_inode);
2574 mds_pack_inode2body(obd, body, de_new->d_inode);
2575 mds_pack_md(obd, req->rq_repmsg, 1, body, de_new->d_inode, 1);
2576 if (!(body->valid & OBD_MD_FLEASIZE)) {
2577 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2578 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2580 /* XXX need log unlink? */
2584 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
2585 de_srcdir->d_inode->i_sb);
2587 handle = fsfilt_start(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME, NULL);
2589 GOTO(cleanup, rc = PTR_ERR(handle));
2591 /* FIXME need adjust the journal block count? */
2592 /* if the target should be moved to PENDING, we at first increase the
2593 * link and later vfs_rename() will decrease the link count again */
2594 if (de_new->d_inode &&
2595 S_ISREG(de_new->d_inode->i_mode) &&
2596 de_new->d_inode->i_nlink == 1 &&
2597 mds_open_orphan_count(de_new->d_inode) > 0) {
2598 rc = mds_add_link_orphan(rec, obd, de_new);
2604 de_old->d_fsdata = req;
2605 de_new->d_fsdata = req;
2606 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
2611 rc = mds_finish_transno(mds, de_tgtdir ? de_tgtdir->d_inode : NULL,
2612 handle, req, rc, 0);
2613 switch (cleanup_phase) {
2616 if (dlm_handles[5].cookie != 0)
2617 ldlm_lock_decref(&(dlm_handles[5]), LCK_CW);
2618 if (dlm_handles[6].cookie != 0)
2619 ldlm_lock_decref(&(dlm_handles[6]), LCK_CW);
2622 if (lock_count == 4)
2623 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
2624 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
2625 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
2626 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
2628 if (lock_count == 4)
2629 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
2630 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
2631 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
2632 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
2641 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2644 req->rq_status = rc;
2648 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
2649 struct ptlrpc_request *, struct lustre_handle *);
2651 static mds_reinter reinters[REINT_MAX + 1] = {
2652 [REINT_SETATTR] mds_reint_setattr,
2653 [REINT_CREATE] mds_reint_create,
2654 [REINT_LINK] mds_reint_link,
2655 [REINT_UNLINK] mds_reint_unlink,
2656 [REINT_RENAME] mds_reint_rename,
2657 [REINT_OPEN] mds_open
2660 int mds_reint_rec(struct mds_update_record *rec, int offset,
2661 struct ptlrpc_request *req, struct lustre_handle *lockh)
2663 struct obd_device *obd = req->rq_export->exp_obd;
2664 struct obd_run_ctxt saved;
2667 /* checked by unpacker */
2668 LASSERT(rec->ur_opcode <= REINT_MAX &&
2669 reinters[rec->ur_opcode] != NULL);
2671 push_ctxt(&saved, &obd->obd_ctxt, &rec->ur_uc);
2672 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
2673 pop_ctxt(&saved, &obd->obd_ctxt, &rec->ur_uc);