1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/mds/mds_reint.c
5 * Lustre Metadata Server (mds) reintegration routines
7 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8 * Author: Peter Braam <braam@clusterfs.com>
9 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * Author: Phil Schwan <phil@clusterfs.com>
12 * This file is part of Lustre, http://www.lustre.org.
14 * Lustre is free software; you can redistribute it and/or
15 * modify it under the terms of version 2 of the GNU General Public
16 * License as published by the Free Software Foundation.
18 * Lustre is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with Lustre; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 # define EXPORT_SYMTAB
31 #define DEBUG_SUBSYSTEM S_MDS
34 #include <linux/jbd.h>
35 #include <linux/ext3_fs.h>
36 #include <linux/obd_support.h>
37 #include <linux/obd_class.h>
38 #include <linux/obd.h>
39 #include <linux/lustre_lib.h>
40 #include <linux/lustre_idl.h>
41 #include <linux/lustre_mds.h>
42 #include <linux/lustre_dlm.h>
43 #include <linux/lustre_log.h>
44 #include <linux/lustre_fsfilt.h>
45 #include "mds_internal.h"
47 struct mds_logcancel_data {
48 struct lov_mds_md *mlcd_lmm;
52 struct llog_cookie mlcd_cookies[0];
55 static void mds_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
56 void *cb_data, int error)
58 struct mds_logcancel_data *mlcd = cb_data;
59 struct lov_stripe_md *lsm = NULL;
60 struct llog_ctxt *ctxt;
63 obd_transno_commit_cb(obd, transno, error);
65 CDEBUG(D_HA, "cancelling %d cookies\n",
66 (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
68 rc = obd_unpackmd(obd->u.mds.mds_lov_exp, &lsm, mlcd->mlcd_lmm,
69 mlcd->mlcd_eadatalen);
71 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
72 (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
75 ///* XXX 0 normally, SENDNOW for debug */);
76 ctxt = llog_get_context(&obd->obd_llogs,
77 mlcd->mlcd_cookies[0].lgc_subsys + 1);
78 rc = llog_cancel(ctxt, mlcd->mlcd_cookielen /
79 sizeof(*mlcd->mlcd_cookies),
80 mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW, lsm);
82 CERROR("error cancelling %d log cookies: rc %d\n",
83 (int)(mlcd->mlcd_cookielen /
84 sizeof(*mlcd->mlcd_cookies)), rc);
87 OBD_FREE(mlcd, mlcd->mlcd_size);
90 /* Assumes caller has already pushed us into the kernel context. */
91 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
92 struct ptlrpc_request *req, int rc, __u32 op_data)
94 struct mds_export_data *med = &req->rq_export->exp_mds_data;
95 struct mds_client_data *mcd = med->med_mcd;
96 struct obd_device *obd = req->rq_export->exp_obd;
99 int err, log_pri = D_HA;
102 /* if the export has already been failed, we have no last_rcvd slot */
103 if (req->rq_export->exp_failed) {
104 CERROR("committing transaction for disconnected client\n");
106 GOTO(out_commit, rc);
113 if (handle == NULL) {
114 /* if we're starting our own xaction, use our own inode */
115 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
116 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
117 if (IS_ERR(handle)) {
118 CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
119 RETURN(PTR_ERR(handle));
125 transno = req->rq_reqmsg->transno;
127 LASSERTF(transno == 0, "BUG 3934, t"LPU64" rc %d\n", transno, rc);
128 } else if (transno == 0) {
129 spin_lock(&mds->mds_transno_lock);
130 transno = ++mds->mds_last_transno;
131 spin_unlock(&mds->mds_transno_lock);
133 spin_lock(&mds->mds_transno_lock);
134 if (transno > mds->mds_last_transno)
135 mds->mds_last_transno = transno;
136 spin_unlock(&mds->mds_transno_lock);
138 req->rq_repmsg->transno = req->rq_transno = transno;
139 mcd->mcd_last_transno = cpu_to_le64(transno);
140 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
141 mcd->mcd_last_result = cpu_to_le32(rc);
142 mcd->mcd_last_data = cpu_to_le32(op_data);
144 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb,
145 transno, handle, mds_commit_last_transno_cb, NULL);
147 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd, sizeof(*mcd),
156 DEBUG_REQ(log_pri, req,
157 "wrote trans #"LPU64" client %s at idx %u: err = %d",
158 transno, mcd->mcd_uuid, med->med_idx, err);
160 /* writing last fid. */
163 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb,
164 mds->mds_last_fid, handle, mds_commit_last_fid_cb,
167 err = fsfilt_write_record(obd, mds->mds_fid_filp, &mds->mds_last_fid,
168 sizeof(mds->mds_last_fid), &off, 0);
175 DEBUG_REQ(log_pri, req,
176 "wrote fid #"LPU64" client %s at idx %llu: err = %d",
177 mds->mds_last_fid, mcd->mcd_uuid, off, err);
179 err = mds_lov_write_objids(obd);
185 CDEBUG(log_pri, "wrote objids: err = %d\n", err);
188 err = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
190 CERROR("error committing transaction: %d\n", err);
198 /* this gives the same functionality as the code between
199 * sys_chmod and inode_setattr
200 * chown_common and inode_setattr
201 * utimes and inode_setattr
203 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
205 time_t now = LTIME_S(CURRENT_TIME);
206 struct iattr *attr = &rec->ur_iattr;
207 unsigned int ia_valid = attr->ia_valid;
211 /* only fix up attrs if the client VFS didn't already */
212 if (!(ia_valid & ATTR_RAW))
215 if (!(ia_valid & ATTR_CTIME_SET))
216 LTIME_S(attr->ia_ctime) = now;
217 if (!(ia_valid & ATTR_ATIME_SET))
218 LTIME_S(attr->ia_atime) = now;
219 if (!(ia_valid & ATTR_MTIME_SET))
220 LTIME_S(attr->ia_mtime) = now;
222 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
226 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
227 if (rec->ur_fsuid != inode->i_uid &&
228 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
232 if (ia_valid & ATTR_SIZE) {
233 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
237 if (ia_valid & ATTR_UID) {
240 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
242 if (attr->ia_uid == (uid_t) -1)
243 attr->ia_uid = inode->i_uid;
244 if (attr->ia_gid == (gid_t) -1)
245 attr->ia_gid = inode->i_gid;
246 attr->ia_mode = inode->i_mode;
248 * If the user or group of a non-directory has been
249 * changed by a non-root user, remove the setuid bit.
250 * 19981026 David C Niemi <niemi@tux.org>
252 * Changed this to apply to all users, including root,
253 * to avoid some races. This is the behavior we had in
254 * 2.0. The check for non-root was definitely wrong
255 * for 2.2 anyway, as it should have been using
256 * CAP_FSETID rather than fsuid -- 19990830 SD.
258 if ((inode->i_mode & S_ISUID) == S_ISUID &&
259 !S_ISDIR(inode->i_mode)) {
260 attr->ia_mode &= ~S_ISUID;
261 attr->ia_valid |= ATTR_MODE;
264 * Likewise, if the user or group of a non-directory
265 * has been changed by a non-root user, remove the
266 * setgid bit UNLESS there is no group execute bit
267 * (this would be a file marked for mandatory
268 * locking). 19981026 David C Niemi <niemi@tux.org>
270 * Removed the fsuid check (see the comment above) --
273 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
274 (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
275 attr->ia_mode &= ~S_ISGID;
276 attr->ia_valid |= ATTR_MODE;
278 } else if (ia_valid & ATTR_MODE) {
279 int mode = attr->ia_mode;
281 if (attr->ia_mode == (mode_t) -1)
282 attr->ia_mode = inode->i_mode;
284 (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
289 void mds_steal_ack_locks(struct ptlrpc_request *req)
291 struct obd_export *exp = req->rq_export;
292 struct list_head *tmp;
293 struct ptlrpc_reply_state *oldrep;
294 struct ptlrpc_service *svc;
295 struct llog_create_locks *lcl;
297 char str[PTL_NALFMT_SIZE];
300 /* CAVEAT EMPTOR: spinlock order */
301 spin_lock_irqsave (&exp->exp_lock, flags);
302 list_for_each (tmp, &exp->exp_outstanding_replies) {
303 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
305 if (oldrep->rs_xid != req->rq_xid)
308 if (oldrep->rs_msg.opc != req->rq_reqmsg->opc)
309 CERROR ("Resent req xid "LPX64" has mismatched opc: "
310 "new %d old %d\n", req->rq_xid,
311 req->rq_reqmsg->opc, oldrep->rs_msg.opc);
313 svc = oldrep->rs_srv_ni->sni_service;
314 spin_lock (&svc->srv_lock);
316 list_del_init (&oldrep->rs_exp_list);
318 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
320 oldrep->rs_nlocks, oldrep,
321 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg.opc,
322 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
324 for (i = 0; i < oldrep->rs_nlocks; i++)
325 ptlrpc_save_lock(req,
326 &oldrep->rs_locks[i],
327 oldrep->rs_modes[i]);
328 oldrep->rs_nlocks = 0;
330 lcl = oldrep->rs_llog_locks;
331 oldrep->rs_llog_locks = NULL;
333 ptlrpc_save_llog_lock(req, lcl);
335 DEBUG_REQ(D_HA, req, "stole locks for");
336 ptlrpc_schedule_difficult_reply (oldrep);
338 spin_unlock (&svc->srv_lock);
339 spin_unlock_irqrestore (&exp->exp_lock, flags);
342 spin_unlock_irqrestore (&exp->exp_lock, flags);
345 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
347 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
348 mcd->mcd_last_transno, mcd->mcd_last_result);
349 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
350 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
352 mds_steal_ack_locks(req);
355 static void reconstruct_reint_setattr(struct mds_update_record *rec,
356 int offset, struct ptlrpc_request *req)
358 struct mds_export_data *med = &req->rq_export->exp_mds_data;
359 struct mds_body *body;
362 mds_req_from_mcd(req, med->med_mcd);
364 de = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
366 LASSERT(PTR_ERR(de) == req->rq_status);
370 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
371 mds_pack_inode2body(req2obd(req), body, de->d_inode, 0);
373 /* Don't return OST-specific attributes if we didn't just set them */
374 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
375 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
376 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
377 body->valid |= OBD_MD_FLMTIME;
378 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
379 body->valid |= OBD_MD_FLATIME;
384 /* In the raw-setattr case, we lock the child inode.
385 * In the write-back case or if being called from open, the client holds a lock
388 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
389 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
390 struct ptlrpc_request *req, struct lustre_handle *lh)
392 struct mds_obd *mds = mds_req2mds(req);
393 struct obd_device *obd = req->rq_export->exp_obd;
394 struct mds_body *body;
396 struct inode *inode = NULL;
397 struct lustre_handle lockh[2] = {{0}, {0}};
400 struct mds_logcancel_data *mlcd = NULL;
401 int rc = 0, cleanup_phase = 0, err;
405 LASSERT(offset == 1);
407 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x",
408 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
409 rec->ur_iattr.ia_valid);
411 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
413 MDS_UPDATE_COUNTER(mds, MDS_SETATTR_COUNT);
415 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
416 de = mds_id2dentry(obd, rec->ur_id1, NULL);
418 GOTO(cleanup, rc = PTR_ERR(de));
420 __u64 lockpart = MDS_INODELOCK_UPDATE;
421 if (rec->ur_iattr.ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
422 lockpart |= MDS_INODELOCK_LOOKUP;
423 de = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
424 lockh, &parent_mode, NULL, 0, lockpart);
426 GOTO(cleanup, rc = PTR_ERR(de));
434 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
435 rec->ur_eadata != NULL)
438 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
440 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
442 GOTO(cleanup, rc = PTR_ERR(handle));
444 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
445 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
446 LTIME_S(rec->ur_iattr.ia_mtime),
447 LTIME_S(rec->ur_iattr.ia_ctime));
448 rc = mds_fix_attr(inode, rec);
452 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
453 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
454 (long)&rec->ur_iattr.ia_attr_flags);
456 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
458 if (rc == 0 && (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
459 rec->ur_eadata != NULL) {
460 struct lov_stripe_md *lsm = NULL;
462 rc = ll_permission(inode, MAY_WRITE, NULL);
466 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, mds->mds_lov_exp,
467 0, &lsm, rec->ur_eadata);
471 obd_free_memmd(mds->mds_lov_exp, &lsm);
473 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
479 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
480 mds_pack_inode2body(obd, body, inode, 0);
482 /* Don't return OST-specific attributes if we didn't just set them */
483 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
484 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
485 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
486 body->valid |= OBD_MD_FLMTIME;
487 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
488 body->valid |= OBD_MD_FLATIME;
490 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_lov_obd)) {
491 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
494 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
496 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
497 mlcd->mlcd_cookielen = rec->ur_cookielen;
498 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
499 mlcd->mlcd_cookielen;
500 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
501 mlcd->mlcd_cookielen);
502 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
503 mlcd->mlcd_eadatalen);
505 CERROR("unable to allocate log cancel data\n");
511 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
512 handle, mds_cancel_cookies_cb, mlcd);
513 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
514 switch (cleanup_phase) {
516 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
517 rec->ur_eadata != NULL)
522 if (lockh[1].cookie != 0)
523 ldlm_lock_decref(lockh + 1, parent_mode);
526 ldlm_lock_decref(lockh, LCK_PW);
528 ptlrpc_save_lock (req, lockh, LCK_PW);
543 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
544 struct ptlrpc_request *req)
546 struct mds_export_data *med = &req->rq_export->exp_mds_data;
547 struct dentry *parent, *child;
548 struct mds_body *body;
551 mds_req_from_mcd(req, med->med_mcd);
553 if (req->rq_status) {
558 parent = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
559 LASSERT(!IS_ERR(parent));
560 child = ll_lookup_one_len(rec->ur_name, parent,
561 rec->ur_namelen - 1);
562 LASSERT(!IS_ERR(child));
563 if ((child->d_flags & DCACHE_CROSS_REF)) {
564 LASSERTF(child->d_inode == NULL, "BUG 3869\n");
565 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
566 mds_pack_dentry2body(req2obd(req), body, child, 1);
567 } else if (child->d_inode == NULL) {
568 DEBUG_REQ(D_ERROR, req, "parent "DLID4" name %s mode %o",
569 OLID4(rec->ur_id1), rec->ur_name, rec->ur_mode);
570 LASSERTF(child->d_inode != NULL, "BUG 3869\n");
572 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
573 mds_pack_inode2body(req2obd(req), body, child->d_inode, 1);
580 static int mds_reint_create(struct mds_update_record *rec, int offset,
581 struct ptlrpc_request *req,
582 struct lustre_handle *lh)
584 struct dentry *dparent = NULL;
585 struct mds_obd *mds = mds_req2mds(req);
586 struct obd_device *obd = req->rq_export->exp_obd;
587 struct dentry *dchild = NULL;
588 struct inode *dir = NULL;
590 struct lustre_handle lockh[2] = {{0}, {0}};
592 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
594 struct dentry_params dp;
595 struct mea *mea = NULL;
599 LASSERT(offset == 1);
601 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
604 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
605 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
606 rec->ur_name, rec->ur_mode);
608 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
610 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
611 GOTO(cleanup, rc = -ESTALE);
613 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
614 lockh, &parent_mode, rec->ur_name,
615 rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
616 if (IS_ERR(dparent)) {
617 rc = PTR_ERR(dparent);
618 CERROR("parent lookup error %d\n", rc);
621 cleanup_phase = 1; /* locked parent dentry */
622 dir = dparent->d_inode;
625 ldlm_lock_dump_handle(D_OTHER, lockh);
627 /* try to retrieve MEA data for this dir */
628 rc = mds_get_lmv_attr(obd, dparent->d_inode, &mea, &mea_size);
634 * dir is already splitted, check is requested filename should
635 * live at this MDS or at another one.
637 int i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
638 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
639 CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
640 " should be %lu(%d)\n",
641 mea->mea_master, dparent->d_inode->i_ino,
642 dparent->d_inode->i_generation, rec->ur_name,
643 (unsigned long)id_group(&mea->mea_ids[i]), i);
644 GOTO(cleanup, rc = -ERESTART);
648 dchild = ll_lookup_one_len(rec->ur_name, dparent,
649 rec->ur_namelen - 1);
650 if (IS_ERR(dchild)) {
651 rc = PTR_ERR(dchild);
652 CERROR("Can't find "DLID4"/%s, error %d\n",
653 OLID4(rec->ur_id1), rec->ur_name, rc);
657 cleanup_phase = 2; /* child dentry */
659 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
661 if (type == S_IFREG || type == S_IFDIR) {
662 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
663 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
664 obd->obd_name, dparent->d_inode->i_ino,
665 dparent->d_inode->i_generation, rc, parent_mode);
667 /* dir got splitted */
668 GOTO(cleanup, rc = -ERESTART);
670 /* error happened during spitting. */
675 if (dir->i_mode & S_ISGID) {
676 if (S_ISDIR(rec->ur_mode))
677 rec->ur_mode |= S_ISGID;
681 * here inode number should be used only in the case of replaying. It is
682 * needed to check if object already created in the case of creating
685 dchild->d_fsdata = (void *)&dp;
686 dp.p_inum = (unsigned long)id_ino(rec->ur_id2);
691 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
693 GOTO(cleanup, rc = PTR_ERR(handle));
694 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
700 struct lustre_id sid;
703 * as Peter asked, mkdir() should distribute new directories
704 * over the whole cluster in order to distribute namespace
705 * processing load. first, we calculate which MDS to use to put
706 * new directory's inode in.
708 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1,
710 if (i == mds->mds_num) {
711 /* inode will be created locally */
712 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
714 GOTO(cleanup, rc = PTR_ERR(handle));
716 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
718 CERROR("Can't create dir %s, rc = %d\n",
719 dchild->d_name.name, rc);
723 down(&dchild->d_inode->i_sem);
725 rc = mds_update_inode_sid(obd, dchild->d_inode,
726 handle, rec->ur_id2);
728 CERROR("mds_update_inode_sid() failed, inode %lu, "
729 "rc %d\n", dchild->d_inode->i_ino, rc);
733 * check if fid from client is bigger than local
736 spin_lock(&mds->mds_fid_lock);
737 if (id_fid(rec->ur_id2) > mds->mds_last_fid)
738 mds->mds_last_fid = id_fid(rec->ur_id2);
739 spin_unlock(&mds->mds_fid_lock);
741 rc = mds_alloc_inode_sid(obd, dchild->d_inode,
744 CERROR("mds_alloc_inode_sid() failed, inode %lu, "
745 "rc %d\n", dchild->d_inode->i_ino, rc);
748 up(&dchild->d_inode->i_sem);
754 nstripes = *(u16 *)rec->ur_eadata;
756 if (rc == 0 && nstripes) {
757 /* we pass LCK_EX to split routine to signal,
758 * that we have exclusive access to the
759 * directory. Simple because nobody knows it
760 * already exists -bzzz */
761 rc = mds_try_to_split_dir(obd, dchild,
765 /* dir got splitted */
768 /* an error occured during
773 } else if (!DENTRY_VALID(dchild)) {
774 /* inode will be created on another MDS */
775 struct obdo *oa = NULL;
776 struct mds_body *body;
778 /* first, create that inode */
781 GOTO(cleanup, rc = -ENOMEM);
786 if (rec->ur_eadata) {
787 /* user asks for creating splitted dir */
788 oa->o_easize = *((u16 *) rec->ur_eadata);
791 obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
792 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
793 OBD_MD_FLUID | OBD_MD_FLGID);
795 oa->o_mode = dir->i_mode;
797 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
800 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
802 * here inode number and generation are
803 * important, as this is replay request and we
804 * need them to check if such an object is
807 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
808 obd->obd_name, rec->ur_namelen - 1,
809 rec->ur_name, (unsigned)id_ino(rec->ur_id2),
810 (unsigned)id_gen(rec->ur_id2));
811 oa->o_id = id_ino(rec->ur_id2);
812 oa->o_fid = id_fid(rec->ur_id2);
813 oa->o_generation = id_gen(rec->ur_id2);
814 oa->o_flags |= OBD_FL_RECREATE_OBJS;
817 /* before obd_create() is called, o_fid is not known. */
818 rc = obd_create(mds->mds_lmv_exp, oa, NULL, NULL);
820 CERROR("can't create remote inode: %d\n", rc);
821 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
822 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
823 rec->ur_name, rec->ur_mode);
828 /* now, add new dir entry for it */
829 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
830 if (IS_ERR(handle)) {
832 GOTO(cleanup, rc = PTR_ERR(handle));
835 /* creating local dentry for remote inode. */
836 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
837 rec->ur_namelen - 1, oa->o_id,
838 oa->o_generation, i, oa->o_fid);
841 CERROR("Can't create local entry %*s for "
842 "remote inode.\n", rec->ur_namelen - 1,
848 body = lustre_msg_buf(req->rq_repmsg,
850 body->valid |= OBD_MD_FLID | OBD_MD_MDS |
853 id_group(&body->id1) = i;
854 id_ino(&body->id1) = oa->o_id;
855 id_fid(&body->id1) = oa->o_fid;
856 id_gen(&body->id1) = oa->o_generation;
859 /* requested name exists in the directory */
866 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
868 GOTO(cleanup, rc = PTR_ERR(handle));
869 if (rec->ur_tgt == NULL) /* no target supplied */
870 rc = -EINVAL; /* -EPROTO? */
872 rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
880 int rdev = rec->ur_rdev;
881 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
883 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
884 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
889 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
890 dchild->d_fsdata = NULL;
891 GOTO(cleanup, rc = -EINVAL);
894 /* In case we stored the desired inum in here, we want to clean up. */
895 if (dchild->d_fsdata == (void *)(unsigned long)id_ino(rec->ur_id2))
896 dchild->d_fsdata = NULL;
899 CDEBUG(D_INODE, "error during create: %d\n", rc);
901 } else if (dchild->d_inode) {
903 struct mds_body *body;
904 struct inode *inode = dchild->d_inode;
907 iattr.ia_uid = rec->ur_fsuid;
908 LTIME_S(iattr.ia_atime) = LTIME_S(rec->ur_time);
909 LTIME_S(iattr.ia_ctime) = LTIME_S(rec->ur_time);
910 LTIME_S(iattr.ia_mtime) = LTIME_S(rec->ur_time);
912 if (dir->i_mode & S_ISGID)
913 iattr.ia_gid = dir->i_gid;
915 iattr.ia_gid = rec->ur_fsgid;
917 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
918 ATTR_MTIME | ATTR_CTIME;
920 if (id_ino(rec->ur_id2)) {
921 LASSERT(id_ino(rec->ur_id2) == inode->i_ino);
922 inode->i_generation = id_gen(rec->ur_id2);
924 if (type != S_IFDIR) {
926 * updating inode self id, as inode already
927 * exists and we should make sure, its sid will
928 * be the same as we reveived.
931 rc = mds_update_inode_sid(obd, inode,
932 handle, rec->ur_id2);
935 CERROR("Can't update inode self id, "
938 spin_lock(&mds->mds_fid_lock);
939 if (id_fid(rec->ur_id2) > mds->mds_last_fid)
940 mds->mds_last_fid = id_fid(rec->ur_id2);
941 spin_unlock(&mds->mds_fid_lock);
944 /* dirtied and committed by the upcoming setattr. */
945 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
946 inode->i_ino, inode->i_generation);
949 struct lustre_handle child_ino_lockh;
952 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
953 inode->i_ino, inode->i_generation);
957 * the inode we were allocated may have just been freed
958 * by an unlink operation. We take this lock to
959 * synchronize against the matching reply-ack-lock taken
960 * in unlink, to avoid replay problems if this reply
961 * makes it out to the client but the unlink's does not.
962 * See bug 2029 for more detail.
964 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
965 if (rc != ELDLM_OK) {
966 CERROR("error locking for unlink/create sync: "
969 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
972 if (type != S_IFDIR) {
973 struct lustre_id sid;
976 * allocate new id for @inode if it is not dir,
977 * because for dir it was already done.
980 rc = mds_alloc_inode_sid(obd, inode,
984 CERROR("mds_alloc_inode_sid() failed, "
985 "inode %lu, rc %d\n", inode->i_ino,
992 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
994 CERROR("error on child setattr: rc = %d\n", rc);
996 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
997 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
999 CERROR("error on parent setattr: rc = %d\n", rc);
1001 MDS_UPDATE_COUNTER(mds, MDS_CREATE_COUNT);
1003 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1004 mds_pack_inode2body(obd, body, inode, 1);
1009 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
1011 if (rc && created) {
1012 /* Destroy the file we just created. This should not need extra
1013 * journal credits, as we have already modified all of the
1014 * blocks needed in order to create the file in the first
1018 err = vfs_rmdir(dir, dchild);
1020 CERROR("rmdir in error path: %d\n", err);
1023 err = vfs_unlink(dir, dchild);
1025 CERROR("unlink in error path: %d\n", err);
1031 switch (cleanup_phase) {
1032 case 2: /* child dentry */
1034 case 1: /* locked parent dentry */
1036 if (lockh[1].cookie != 0)
1037 ldlm_lock_decref(lockh + 1, parent_mode);
1040 ldlm_lock_decref(lockh, LCK_PW);
1042 ptlrpc_save_lock(req, lockh, LCK_PW);
1048 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1052 OBD_FREE(mea, mea_size);
1053 req->rq_status = rc;
1058 res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
1059 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1063 for (i = 0; i < RES_NAME_SIZE; i++) {
1064 if (res1->name[i] > res2->name[i])
1066 if (res1->name[i] < res2->name[i])
1073 if (memcmp(p1, p2, sizeof(*p1)) < 0)
1079 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1080 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1081 * because they take the place of local semaphores.
1083 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
1084 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
1085 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
1086 struct lustre_handle *p1_lockh, int p1_lock_mode,
1087 ldlm_policy_data_t *p1_policy,
1088 struct ldlm_res_id *p2_res_id,
1089 struct lustre_handle *p2_lockh, int p2_lock_mode,
1090 ldlm_policy_data_t *p2_policy)
1092 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1093 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1094 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1095 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
1099 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1101 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1102 res_id[0]->name[0], res_id[1]->name[0]);
1104 if (p2_res_id->name[0] != 0 &&
1105 res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1106 handles[1] = p1_lockh;
1107 handles[0] = p2_lockh;
1108 res_id[1] = p1_res_id;
1109 res_id[0] = p2_res_id;
1110 lock_modes[1] = p1_lock_mode;
1111 lock_modes[0] = p2_lock_mode;
1112 policies[1] = p1_policy;
1113 policies[0] = p2_policy;
1116 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1117 res_id[0]->name[0], res_id[1]->name[0]);
1119 flags = LDLM_FL_LOCAL_ONLY;
1120 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1121 LDLM_IBITS, policies[0], lock_modes[0], &flags,
1122 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1123 NULL, 0, NULL, handles[0]);
1126 ldlm_lock_dump_handle(D_OTHER, handles[0]);
1128 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1129 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1130 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1131 ldlm_lock_addref(handles[1], lock_modes[1]);
1132 } else if (res_id[1]->name[0] != 0) {
1133 flags = LDLM_FL_LOCAL_ONLY;
1134 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1135 *res_id[1], LDLM_IBITS, policies[1],
1136 lock_modes[1], &flags, mds_blocking_ast,
1137 ldlm_completion_ast, NULL, NULL, NULL, 0,
1139 if (rc != ELDLM_OK) {
1140 ldlm_lock_decref(handles[0], lock_modes[0]);
1143 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1149 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1150 struct lustre_handle *p1_lockh, int p1_lock_mode,
1151 ldlm_policy_data_t *p1_policy,
1152 struct ldlm_res_id *p2_res_id,
1153 struct lustre_handle *p2_lockh, int p2_lock_mode,
1154 ldlm_policy_data_t *p2_policy,
1155 struct ldlm_res_id *c1_res_id,
1156 struct lustre_handle *c1_lockh, int c1_lock_mode,
1157 ldlm_policy_data_t *c1_policy,
1158 struct ldlm_res_id *c2_res_id,
1159 struct lustre_handle *c2_lockh, int c2_lock_mode,
1160 ldlm_policy_data_t *c2_policy)
1162 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1163 c1_res_id, c2_res_id };
1164 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1165 c1_lockh, c2_lockh };
1166 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1167 c1_lock_mode, c2_lock_mode };
1168 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1169 c1_policy, c2_policy};
1170 int rc, i, j, sorted, flags;
1173 CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1174 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1175 res_id[3]->name[0]);
1177 /* simple insertion sort - we have at most 4 elements */
1178 for (i = 1; i < 4; i++) {
1180 dlm_handles[4] = dlm_handles[i];
1181 res_id[4] = res_id[i];
1182 lock_modes[4] = lock_modes[i];
1183 policies[4] = policies[i];
1187 if (res_gt(res_id[j], res_id[4], policies[j],
1189 dlm_handles[j + 1] = dlm_handles[j];
1190 res_id[j + 1] = res_id[j];
1191 lock_modes[j + 1] = lock_modes[j];
1192 policies[j + 1] = policies[j];
1197 } while (j >= 0 && !sorted);
1199 dlm_handles[j + 1] = dlm_handles[4];
1200 res_id[j + 1] = res_id[4];
1201 lock_modes[j + 1] = lock_modes[4];
1202 policies[j + 1] = policies[4];
1205 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1206 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1207 res_id[3]->name[0]);
1209 /* XXX we could send ASTs on all these locks first before blocking? */
1210 for (i = 0; i < 4; i++) {
1212 if (res_id[i]->name[0] == 0)
1215 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1216 (policies[i]->l_inodebits.bits &
1217 policies[i-1]->l_inodebits.bits) ) {
1218 memcpy(dlm_handles[i], dlm_handles[i-1],
1219 sizeof(*(dlm_handles[i])));
1220 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1222 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1223 *res_id[i], LDLM_IBITS,
1225 lock_modes[i], &flags,
1227 ldlm_completion_ast, NULL, NULL,
1228 NULL, 0, NULL, dlm_handles[i]);
1230 GOTO(out_err, rc = -EIO);
1231 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1238 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1243 /* In the unlikely case that the child changed while we were waiting
1244 * on the lock, we need to drop the lock on the old child and either:
1245 * - if the child has a lower resource name, then we have to also
1246 * drop the parent lock and regain the locks in the right order
1247 * - in the rename case, if the child has a lower resource name than one of
1248 * the other parent/child resources (maxres) we also need to reget the locks
1249 * - if the child has a higher resource name (this is the common case)
1250 * we can just get the lock on the new child (still in lock order)
1252 * Returns 0 if the child did not change or if it changed but could be locked.
1253 * Returns 1 if the child changed and we need to re-lock (no locks held).
1254 * Returns -ve error with a valid dchild (no locks held). */
1255 static int mds_verify_child(struct obd_device *obd,
1256 struct ldlm_res_id *parent_res_id,
1257 struct lustre_handle *parent_lockh,
1258 struct dentry *dparent, int parent_mode,
1259 struct ldlm_res_id *child_res_id,
1260 struct lustre_handle *child_lockh,
1261 struct dentry **dchildp, int child_mode,
1262 ldlm_policy_data_t *child_policy,
1263 const char *name, int namelen,
1264 struct ldlm_res_id *maxres,
1265 unsigned long child_ino,
1268 struct lustre_id sid;
1269 struct dentry *vchild, *dchild = *dchildp;
1270 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1273 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1275 GOTO(cleanup, rc = PTR_ERR(vchild));
1277 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1278 if (child_gen == vchild->d_generation &&
1279 child_ino == vchild->d_inum) {
1288 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1289 (vchild->d_inode != NULL &&
1290 child_gen == vchild->d_inode->i_generation &&
1291 child_ino == vchild->d_inode->i_ino))) {
1299 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1300 vchild->d_inode, dchild ? dchild->d_inode : 0,
1301 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1302 child_res_id->name[0]);
1304 if (child_res_id->name[0] != 0)
1305 ldlm_lock_decref(child_lockh, child_mode);
1309 cleanup_phase = 1; /* parent lock only */
1310 *dchildp = dchild = vchild;
1312 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1315 if (dchild->d_inode) {
1316 down(&dchild->d_inode->i_sem);
1317 rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
1318 up(&dchild->d_inode->i_sem);
1320 CERROR("Can't read inode self id, inode %lu,"
1321 " rc %d\n", dchild->d_inode->i_ino, rc);
1324 child_res_id->name[0] = id_fid(&sid);
1325 child_res_id->name[1] = id_group(&sid);
1327 child_res_id->name[0] = dchild->d_fid;
1328 child_res_id->name[1] = dchild->d_mdsnum;
1331 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1332 res_gt(maxres, child_res_id, NULL, NULL)) {
1333 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1334 child_res_id->name[0], parent_res_id->name[0],
1336 GOTO(cleanup, rc = 1);
1339 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1340 *child_res_id, LDLM_IBITS, child_policy,
1341 child_mode, &flags, mds_blocking_ast,
1342 ldlm_completion_ast, NULL, NULL, NULL, 0,
1345 GOTO(cleanup, rc = -EIO);
1348 memset(child_res_id, 0, sizeof(*child_res_id));
1353 switch(cleanup_phase) {
1355 if (child_res_id->name[0] != 0)
1356 ldlm_lock_decref(child_lockh, child_mode);
1358 ldlm_lock_decref(parent_lockh, parent_mode);
1364 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1365 struct lustre_id *id,
1366 struct lustre_handle *parent_lockh,
1367 struct dentry **dparentp, int parent_mode,
1368 __u64 parent_lockpart, int *update_mode,
1369 char *name, int namelen,
1370 struct lustre_handle *child_lockh,
1371 struct dentry **dchildp, int child_mode,
1372 __u64 child_lockpart)
1374 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1375 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1376 struct ldlm_res_id parent_res_id = { .name = {0} };
1377 struct ldlm_res_id child_res_id = { .name = {0} };
1378 int rc = 0, cleanup_phase = 0;
1379 unsigned long child_ino;
1380 struct lustre_id sid;
1381 __u32 child_gen = 0;
1382 struct inode *inode;
1385 /* Step 1: Lookup parent */
1386 *dparentp = mds_id2dentry(obd, id, NULL);
1387 if (IS_ERR(*dparentp)) {
1388 rc = PTR_ERR(*dparentp);
1393 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1394 (*dparentp)->d_inode->i_ino, name);
1396 parent_res_id.name[0] = id_fid(id);
1397 parent_res_id.name[1] = id_group(id);
1400 parent_lockh[1].cookie = 0;
1401 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1402 struct ldlm_res_id res_id = { .name = {0} };
1403 ldlm_policy_data_t policy;
1406 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1408 res_id.name[0] = id_fid(id);
1409 res_id.name[1] = id_group(id);
1410 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1412 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1413 res_id, LDLM_IBITS, &policy,
1414 *update_mode, &flags,
1416 ldlm_completion_ast,
1417 NULL, NULL, NULL, 0, NULL,
1423 parent_res_id.name[2] = full_name_hash(name, namelen - 1);
1425 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1426 (unsigned long)id_fid(id), (unsigned long)id_group(id),
1427 parent_res_id.name[2]);
1431 cleanup_phase = 1; /* parent dentry */
1433 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1434 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1435 if (IS_ERR(*dchildp)) {
1436 rc = PTR_ERR(*dchildp);
1437 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1441 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1443 * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1444 * lock. Drop possible UPDATE lock!
1446 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1447 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1449 child_res_id.name[0] = (*dchildp)->d_fid;
1450 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1451 child_gen = (*dchildp)->d_generation;
1452 child_ino = (*dchildp)->d_inum;
1456 inode = (*dchildp)->d_inode;
1458 inode = igrab(inode);
1462 down(&inode->i_sem);
1463 rc = mds_read_inode_sid(obd, inode, &sid);
1466 CERROR("Can't read inode self id, inode %lu, "
1467 "rc %d\n", inode->i_ino, rc);
1471 child_res_id.name[0] = id_fid(&sid);
1472 child_res_id.name[1] = id_group(&sid);
1473 child_gen = inode->i_generation;
1474 child_ino = inode->i_ino;
1478 cleanup_phase = 2; /* child dentry */
1480 /* Step 3: Lock parent and child in resource order. If child doesn't
1481 * exist, we still have to lock the parent and re-lookup. */
1482 rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1483 &parent_policy, &child_res_id, child_lockh,
1484 child_mode, &child_policy);
1488 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1489 cleanup_phase = 4; /* child lock */
1491 cleanup_phase = 3; /* parent lock */
1493 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1494 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1495 parent_mode, &child_res_id, child_lockh,
1496 dchildp, child_mode, &child_policy,
1497 name, namelen, &parent_res_id,
1498 child_ino, child_gen);
1508 switch (cleanup_phase) {
1510 ldlm_lock_decref(child_lockh, child_mode);
1512 ldlm_lock_decref(parent_lockh, parent_mode);
1517 if (parent_lockh[1].cookie)
1518 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1526 void mds_reconstruct_generic(struct ptlrpc_request *req)
1528 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1530 mds_req_from_mcd(req, med->med_mcd);
1532 /* If we are unlinking an open file/dir (i.e. creating an orphan) then
1533 * we instead link the inode into the PENDING directory until it is
1534 * finally released. We can't simply call mds_reint_rename() or some
1535 * part thereof, because we don't have the inode to check for link
1536 * count/open status until after it is locked.
1538 * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1539 * before starting journal transaction.
1541 * returns 1 on success
1542 * returns 0 if we lost a race and didn't make a new link
1543 * returns negative on error
1545 static int mds_orphan_add_link(struct mds_update_record *rec,
1546 struct obd_device *obd, struct dentry *dentry)
1548 struct mds_obd *mds = &obd->u.mds;
1549 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1550 struct inode *inode = dentry->d_inode;
1551 struct dentry *pending_child;
1552 char idname[LL_ID_NAMELEN];
1553 int idlen = 0, rc, mode;
1556 LASSERT(inode != NULL);
1557 LASSERT(!mds_inode_is_orphan(inode));
1558 #ifndef HAVE_I_ALLOC_SEM
1559 LASSERT(down_trylock(&inode->i_sem) != 0);
1561 LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1563 idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1565 CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1566 mds_orphan_open_count(inode), inode->i_nlink,
1567 S_ISDIR(inode->i_mode) ? "dir" :
1568 S_ISREG(inode->i_mode) ? "file" : "other",
1569 rec->ur_name, idname);
1571 if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1574 pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1575 if (IS_ERR(pending_child))
1576 RETURN(PTR_ERR(pending_child));
1578 if (pending_child->d_inode != NULL) {
1579 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1580 LASSERT(pending_child->d_inode == inode);
1581 GOTO(out_dput, rc = 0);
1584 /* link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG
1585 * for linking and return real mode back then -bzzz */
1586 mode = inode->i_mode;
1587 inode->i_mode = S_IFREG;
1588 rc = vfs_link(dentry, pending_dir, pending_child);
1590 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1593 mds_inode_set_orphan(inode);
1595 /* return mode and correct i_nlink if inode is directory */
1596 inode->i_mode = mode;
1597 LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1598 S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1600 if (S_ISDIR(mode)) {
1602 pending_dir->i_nlink++;
1603 mark_inode_dirty(inode);
1604 mark_inode_dirty(pending_dir);
1607 GOTO(out_dput, rc = 1);
1609 l_dput(pending_child);
1613 int mds_create_local_dentry(struct mds_update_record *rec,
1614 struct obd_device *obd)
1616 struct mds_obd *mds = &obd->u.mds;
1617 struct inode *id_dir = mds->mds_id_dir->d_inode;
1618 int idlen = 0, rc, cleanup_phase = 0;
1619 struct dentry *new_child = NULL;
1620 char *idname = rec->ur_name;
1621 struct dentry *child = NULL;
1622 struct lustre_handle lockh[2] = {{0}, {0}};
1623 struct lustre_id sid;
1627 down(&id_dir->i_sem);
1628 idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1629 id_gen(rec->ur_id1));
1631 CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1632 idname, OLID4(rec->ur_id1));
1634 new_child = ll_lookup_one_len(idname, mds->mds_id_dir,
1637 if (IS_ERR(new_child)) {
1638 CERROR("can't lookup %s: %d\n", idname,
1639 (int) PTR_ERR(new_child));
1640 GOTO(cleanup, rc = PTR_ERR(new_child));
1644 down(&id_dir->i_sem);
1645 rc = mds_read_inode_sid(obd, id_dir, &sid);
1648 CERROR("Can't read inode self id, inode %lu, "
1649 "rc %d\n", id_dir->i_ino, rc);
1653 if (new_child->d_inode != NULL) {
1654 /* nice. we've already have local dentry! */
1655 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1656 (unsigned)new_child->d_inode->i_ino,
1657 (unsigned)new_child->d_inode->i_generation);
1659 id_ino(rec->ur_id1) = id_dir->i_ino;
1660 id_gen(rec->ur_id1) = id_dir->i_generation;
1661 rec->ur_namelen = idlen + 1;
1663 id_fid(rec->ur_id1) = id_fid(&sid);
1664 id_group(rec->ur_id1) = id_group(&sid);
1666 GOTO(cleanup, rc = 0);
1669 /* new, local dentry will be added soon. we need no aliases here */
1672 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1673 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1675 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1676 LCK_EX, lockh, NULL, NULL, 0,
1677 MDS_INODELOCK_UPDATE);
1680 if (IS_ERR(child)) {
1681 rc = PTR_ERR(child);
1682 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1683 CERROR("can't get victim: %d\n", rc);
1688 handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
1690 GOTO(cleanup, rc = PTR_ERR(handle));
1692 rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
1693 idlen, id_ino(rec->ur_id1),
1694 id_gen(rec->ur_id1), mds->mds_num,
1695 id_fid(rec->ur_id1));
1697 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1698 (unsigned long)child->d_inode->i_ino,
1699 (unsigned long)child->d_inode->i_generation, rc);
1701 if (S_ISDIR(child->d_inode->i_mode)) {
1703 mark_inode_dirty(id_dir);
1705 mark_inode_dirty(child->d_inode);
1707 fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
1709 id_ino(rec->ur_id1) = id_dir->i_ino;
1710 id_gen(rec->ur_id1) = id_dir->i_generation;
1711 rec->ur_namelen = idlen + 1;
1713 id_fid(rec->ur_id1) = id_fid(&sid);
1714 id_group(rec->ur_id1) = id_group(&sid);
1716 switch(cleanup_phase) {
1718 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1719 ldlm_lock_decref(lockh, LCK_EX);
1729 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1730 struct ptlrpc_request *slave)
1732 void *cookie, *cookie2;
1733 struct mds_body *body2;
1734 struct mds_body *body;
1738 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1739 LASSERT(body != NULL);
1741 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1742 LASSERT(body2 != NULL);
1744 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
1747 memcpy(body2, body, sizeof(*body));
1748 body2->valid &= ~OBD_MD_FLCOOKIE;
1750 if (!(body->valid & OBD_MD_FLEASIZE) &&
1751 !(body->valid & OBD_MD_FLDIREA))
1754 if (body->eadatasize == 0) {
1755 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1759 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1761 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1762 LASSERT(ea != NULL);
1764 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1765 LASSERT(ea2 != NULL);
1767 memcpy(ea2, ea, body->eadatasize);
1769 if (body->valid & OBD_MD_FLCOOKIE) {
1770 LASSERT(master->rq_repmsg->buflens[2] >=
1771 slave->rq_repmsg->buflens[2]);
1772 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1773 slave->rq_repmsg->buflens[2]);
1774 LASSERT(cookie != NULL);
1776 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1777 master->rq_repmsg->buflens[2]);
1778 LASSERT(cookie2 != NULL);
1779 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1780 body2->valid |= OBD_MD_FLCOOKIE;
1785 static int mds_reint_unlink_remote(struct mds_update_record *rec,
1786 int offset, struct ptlrpc_request *req,
1787 struct lustre_handle *parent_lockh,
1788 int update_mode, struct dentry *dparent,
1789 struct lustre_handle *child_lockh,
1790 struct dentry *dchild)
1792 struct obd_device *obd = req->rq_export->exp_obd;
1793 struct mds_obd *mds = mds_req2mds(req);
1794 struct mdc_op_data op_data;
1795 int rc = 0, cleanup_phase = 0;
1796 struct ptlrpc_request *request = NULL;
1800 LASSERT(offset == 1 || offset == 3);
1802 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode %u/%u/%u/%u)",
1803 rec->ur_namelen - 1, rec->ur_name,
1804 (unsigned)dchild->d_mdsnum,
1805 (unsigned)dchild->d_fid,
1806 (unsigned)dchild->d_inum,
1807 (unsigned)dchild->d_generation);
1809 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1810 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode %u/%u/%u/%u)",
1811 rec->ur_namelen - 1, rec->ur_name,
1812 (unsigned)dchild->d_mdsnum,
1813 (unsigned)dchild->d_fid,
1814 (unsigned)dchild->d_inum,
1815 (unsigned)dchild->d_generation);
1817 /* time to drop i_nlink on remote MDS */
1818 memset(&op_data, 0, sizeof(op_data));
1819 id_ino(&op_data.id1) = dchild->d_inum;
1820 id_fid(&op_data.id1) = dchild->d_fid;
1821 id_group(&op_data.id1) = dchild->d_mdsnum;
1822 id_gen(&op_data.id1) = dchild->d_generation;
1823 op_data.create_mode = rec->ur_mode;
1824 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1825 op_data.create_mode |= MDS_MODE_REPLAY;
1826 rc = md_unlink(mds->mds_lmv_exp, &op_data, &request);
1830 mds_copy_unlink_reply(req, request);
1831 ptlrpc_req_finished(request);
1834 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1837 GOTO(cleanup, rc = PTR_ERR(handle));
1838 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
1839 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1843 req->rq_status = rc;
1846 if (parent_lockh[1].cookie != 0)
1847 ldlm_lock_decref(parent_lockh + 1, update_mode);
1849 ldlm_lock_decref(child_lockh, LCK_EX);
1851 ldlm_lock_decref(parent_lockh, LCK_PW);
1853 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1860 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1861 struct ptlrpc_request *req,
1862 struct lustre_handle *lh)
1864 struct dentry *dparent = NULL, *dchild;
1865 struct mds_obd *mds = mds_req2mds(req);
1866 struct obd_device *obd = req->rq_export->exp_obd;
1867 struct mds_body *body = NULL;
1868 struct inode *child_inode = NULL;
1869 struct lustre_handle parent_lockh[2] = {{0}, {0}};
1870 struct lustre_handle child_lockh = {0};
1872 struct lustre_handle child_reuse_lockh = {0};
1874 struct lustre_handle *slave_lockh = NULL;
1875 char idname[LL_ID_NAMELEN];
1876 struct llog_create_locks *lcl = NULL;
1877 void *handle = NULL;
1878 int rc = 0, cleanup_phase = 0;
1879 int unlink_by_id = 0;
1883 LASSERT(offset == 1 || offset == 3);
1885 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1886 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
1889 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1891 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1892 DEBUG_REQ(D_HA, req, "unlink replay\n");
1893 LASSERT(offset == 1); /* should not come from intent */
1894 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
1895 lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
1896 req->rq_repmsg->buflens[2]);
1899 MDS_UPDATE_COUNTER(mds, MDS_UNLINK_COUNT);
1901 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1902 GOTO(cleanup, rc = -ENOENT);
1904 if (rec->ur_namelen == 1) {
1905 /* this is request to drop i_nlink on local inode */
1907 rec->ur_name = idname;
1908 rc = mds_create_local_dentry(rec, obd);
1909 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
1910 DEBUG_REQ(D_HA, req,
1911 "drop nlink on inode "DLID4" (replay)",
1912 OLID4(rec->ur_id1));
1918 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1919 /* master mds for directory asks slave removing inode is already
1921 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1922 LCK_PW, parent_lockh,
1923 &update_mode, rec->ur_name,
1925 MDS_INODELOCK_UPDATE);
1926 if (IS_ERR(dparent))
1927 GOTO(cleanup, rc = PTR_ERR(dparent));
1928 dchild = ll_lookup_one_len(rec->ur_name, dparent,
1929 rec->ur_namelen - 1);
1931 GOTO(cleanup, rc = PTR_ERR(dchild));
1932 child_lockh.cookie = 0;
1933 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
1934 LASSERT(dchild->d_inode != NULL);
1935 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
1937 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
1938 parent_lockh, &dparent,
1939 LCK_PW, MDS_INODELOCK_UPDATE,
1940 &update_mode, rec->ur_name,
1941 rec->ur_namelen, &child_lockh,
1943 MDS_INODELOCK_LOOKUP |
1944 MDS_INODELOCK_UPDATE);
1949 if (dchild->d_flags & DCACHE_CROSS_REF) {
1950 /* we should have parent lock only here */
1951 LASSERT(unlink_by_id == 0);
1952 LASSERT(dchild->d_mdsnum != mds->mds_num);
1953 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
1954 update_mode, dparent, &child_lockh, dchild);
1958 cleanup_phase = 1; /* dchild, dparent, locks */
1961 child_inode = dchild->d_inode;
1962 if (child_inode == NULL) {
1963 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1964 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
1965 GOTO(cleanup, rc = -ENOENT);
1968 cleanup_phase = 2; /* dchild has a lock */
1970 /* We have to do these checks ourselves, in case we are making an
1971 * orphan. The client tells us whether rmdir() or unlink() was called,
1972 * so we need to return appropriate errors (bug 72).
1974 * We don't have to check permissions, because vfs_rename (called from
1975 * mds_open_unlink_rename) also calls may_delete. */
1976 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1977 if (!S_ISDIR(child_inode->i_mode))
1978 GOTO(cleanup, rc = -ENOTDIR);
1980 if (S_ISDIR(child_inode->i_mode))
1981 GOTO(cleanup, rc = -EISDIR);
1984 /* handle splitted dir */
1985 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
1990 /* Step 4: Get a lock on the ino to sync with creation WRT inode
1991 * reuse (see bug 2029). */
1992 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
1996 cleanup_phase = 3; /* child inum lock */
1998 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2000 /* ldlm_reply in buf[0] if called via intent */
2006 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2007 LASSERT(body != NULL);
2009 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2010 DOWN_READ_I_ALLOC_SEM(child_inode);
2011 cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2013 /* If this is potentially the last reference to this inode, get the
2014 * OBD EA data first so the client can destroy OST objects. We
2015 * only do the object removal later if no open files/links remain. */
2016 if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2017 child_inode->i_nlink == 1) {
2018 if (mds_orphan_open_count(child_inode) > 0) {
2019 /* need to lock pending_dir before transaction */
2020 down(&mds->mds_pending_dir->d_inode->i_sem);
2021 cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2022 } else if (S_ISREG(child_inode->i_mode)) {
2023 mds_pack_inode2body(obd, body, child_inode, 0);
2024 mds_pack_md(obd, req->rq_repmsg, offset + 1,
2025 body, child_inode, MDS_PACK_MD_LOCK);
2029 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2030 switch (child_inode->i_mode & S_IFMT) {
2032 /* Drop any lingering child directories before we start our
2033 * transaction, to avoid doing multiple inode dirty/delete
2034 * in our compound transaction (bug 1321). */
2035 shrink_dcache_parent(dchild);
2036 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2039 GOTO(cleanup, rc = PTR_ERR(handle));
2040 rc = vfs_rmdir(dparent->d_inode, dchild);
2043 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2044 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2046 handle = fsfilt_start_log(obd, dparent->d_inode,
2047 FSFILT_OP_UNLINK, NULL,
2048 le32_to_cpu(lmm->lmm_stripe_count));
2050 GOTO(cleanup, rc = PTR_ERR(handle));
2051 rc = vfs_unlink(dparent->d_inode, dchild);
2059 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2062 GOTO(cleanup, rc = PTR_ERR(handle));
2063 rc = vfs_unlink(dparent->d_inode, dchild);
2066 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2069 GOTO(cleanup, rc = -EINVAL);
2072 if (rc == 0 && child_inode->i_nlink == 0) {
2073 if (mds_orphan_open_count(child_inode) > 0)
2074 rc = mds_orphan_add_link(rec, obd, dchild);
2077 GOTO(cleanup, rc = 0);
2079 if (!S_ISREG(child_inode->i_mode))
2082 if (!(body->valid & OBD_MD_FLEASIZE)) {
2083 body->valid |=(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2084 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2085 } else if (mds_log_op_unlink(obd, child_inode,
2086 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2087 req->rq_repmsg->buflens[offset + 1],
2088 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2089 req->rq_repmsg->buflens[offset+2],
2091 body->valid |= OBD_MD_FLCOOKIE;
2102 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2103 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2104 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2106 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2108 CERROR("error on parent setattr: rc = %d\n", err);
2110 rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2111 handle, req, rc, 0);
2113 (void)obd_set_info(mds->mds_lov_exp, strlen("unlinked"),
2114 "unlinked", 0, NULL);
2115 switch(cleanup_phase) {
2116 case 5: /* pending_dir semaphore */
2117 up(&mds->mds_pending_dir->d_inode->i_sem);
2118 case 4: /* child inode semaphore */
2119 UP_READ_I_ALLOC_SEM(child_inode);
2120 /* handle splitted dir */
2122 /* master directory can be non-empty or something else ... */
2123 mds_unlink_slave_objs(obd, dchild);
2126 ptlrpc_save_llog_lock(req, lcl);
2127 case 3: /* child ino-reuse lock */
2129 if (rc && body != NULL) {
2130 // Don't unlink the OST objects if the MDS unlink failed
2134 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2136 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2138 case 2: /* child lock */
2139 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2140 if (child_lockh.cookie)
2141 ldlm_lock_decref(&child_lockh, LCK_EX);
2142 case 1: /* child and parent dentry, parent lock */
2144 if (parent_lockh[1].cookie != 0)
2145 ldlm_lock_decref(parent_lockh + 1, update_mode);
2148 ldlm_lock_decref(parent_lockh, LCK_PW);
2150 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2157 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2160 req->rq_status = rc;
2165 * to service requests from remote MDS to increment i_nlink
2167 static int mds_reint_link_acquire(struct mds_update_record *rec,
2168 int offset, struct ptlrpc_request *req,
2169 struct lustre_handle *lh)
2171 struct obd_device *obd = req->rq_export->exp_obd;
2172 struct ldlm_res_id src_res_id = { .name = {0} };
2173 struct lustre_handle *handle = NULL, src_lockh = {0};
2174 struct mds_obd *mds = mds_req2mds(req);
2175 int rc = 0, cleanup_phase = 0;
2176 struct dentry *de_src = NULL;
2177 ldlm_policy_data_t policy;
2181 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2182 obd->obd_name, OLID4(rec->ur_id1));
2184 /* Step 1: Lookup the source inode and target directory by ID */
2185 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2187 GOTO(cleanup, rc = PTR_ERR(de_src));
2188 cleanup_phase = 1; /* source dentry */
2190 src_res_id.name[0] = id_fid(rec->ur_id1);
2191 src_res_id.name[1] = id_group(rec->ur_id1);
2192 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2194 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2195 src_res_id, LDLM_IBITS, &policy,
2196 LCK_EX, &flags, mds_blocking_ast,
2197 ldlm_completion_ast, NULL, NULL,
2198 NULL, 0, NULL, &src_lockh);
2200 GOTO(cleanup, rc = -ENOLCK);
2201 cleanup_phase = 2; /* lock */
2203 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2205 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2206 if (IS_ERR(handle)) {
2207 rc = PTR_ERR(handle);
2210 de_src->d_inode->i_nlink++;
2211 mark_inode_dirty(de_src->d_inode);
2214 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2215 handle, req, rc, 0);
2217 switch (cleanup_phase) {
2220 ldlm_lock_decref(&src_lockh, LCK_EX);
2222 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2228 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2231 req->rq_status = rc;
2236 * request to link to foreign inode:
2237 * - acquire i_nlinks on this inode
2240 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2241 int offset, struct ptlrpc_request *req,
2242 struct lustre_handle *lh)
2244 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2245 struct obd_device *obd = req->rq_export->exp_obd;
2246 struct dentry *de_tgt_dir = NULL;
2247 struct mds_obd *mds = mds_req2mds(req);
2248 int rc = 0, cleanup_phase = 0;
2249 struct mdc_op_data op_data;
2250 struct ptlrpc_request *request = NULL;
2254 DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2255 ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2256 OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2257 OLID4(rec->ur_id1));
2259 de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2260 tgt_dir_lockh, &update_mode,
2261 rec->ur_name, rec->ur_namelen - 1,
2262 MDS_INODELOCK_UPDATE);
2263 if (IS_ERR(de_tgt_dir))
2264 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2267 op_data.id1 = *(rec->ur_id1);
2268 op_data.namelen = 0;
2269 op_data.name = NULL;
2270 rc = md_link(mds->mds_lmv_exp, &op_data, &request);
2276 ptlrpc_req_finished(request);
2278 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2280 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2281 if (IS_ERR(handle)) {
2282 rc = PTR_ERR(handle);
2286 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2287 rec->ur_namelen - 1, id_ino(rec->ur_id1),
2288 id_gen(rec->ur_id1), id_group(rec->ur_id1),
2289 id_fid(rec->ur_id1));
2293 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2294 handle, req, rc, 0);
2297 switch (cleanup_phase) {
2300 /* FIXME: drop i_nlink on remote inode here */
2301 CERROR("MUST drop drop i_nlink here\n");
2306 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2308 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2311 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2313 ptlrpc_save_lock(req, tgt_dir_lockh+1, update_mode);
2319 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2322 req->rq_status = rc;
2326 static int mds_reint_link(struct mds_update_record *rec, int offset,
2327 struct ptlrpc_request *req,
2328 struct lustre_handle *lh)
2330 struct obd_device *obd = req->rq_export->exp_obd;
2331 struct dentry *de_src = NULL;
2332 struct dentry *de_tgt_dir = NULL;
2333 struct dentry *dchild = NULL;
2334 struct mds_obd *mds = mds_req2mds(req);
2335 struct lustre_handle *handle = NULL;
2336 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2337 struct ldlm_res_id src_res_id = { .name = {0} };
2338 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2339 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2340 ldlm_policy_data_t tgt_dir_policy =
2341 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2342 int rc = 0, cleanup_phase = 0;
2344 int update_mode = 0;
2348 LASSERT(offset == 1);
2350 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2351 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2352 id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2355 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2356 MDS_UPDATE_COUNTER(mds, MDS_LINK_COUNT);
2358 // memset(tgt_dir_lockh, 0, 2*sizeof(tgt_dir_lockh[0]));
2359 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2360 GOTO(cleanup, rc = -ENOENT);
2362 if (id_group(rec->ur_id1) != mds->mds_num) {
2363 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2367 if (rec->ur_namelen == 1) {
2368 rc = mds_reint_link_acquire(rec, offset, req, lh);
2372 /* Step 1: Lookup the source inode and target directory by ID */
2373 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2375 GOTO(cleanup, rc = PTR_ERR(de_src));
2377 cleanup_phase = 1; /* source dentry */
2379 de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2380 if (IS_ERR(de_tgt_dir)) {
2381 rc = PTR_ERR(de_tgt_dir);
2386 cleanup_phase = 2; /* target directory dentry */
2388 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2389 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2390 rec->ur_name, de_src->d_inode->i_ino);
2392 /* Step 2: Take the two locks */
2393 src_res_id.name[0] = id_fid(rec->ur_id1);
2394 src_res_id.name[1] = id_group(rec->ur_id1);
2395 tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2396 tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2399 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2401 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2403 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2404 tgt_dir_res_id, LDLM_IBITS,
2405 &src_policy, update_mode, &flags,
2407 ldlm_completion_ast, NULL, NULL,
2408 NULL, 0, NULL, tgt_dir_lockh + 1);
2410 GOTO(cleanup, rc = -ENOLCK);
2413 tgt_dir_res_id.name[2] = full_name_hash(rec->ur_name,
2414 rec->ur_namelen - 1);
2415 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2416 (unsigned long)id_fid(rec->ur_id2),
2417 (unsigned long)id_group(rec->ur_id2),
2418 tgt_dir_res_id.name[2]);
2421 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2422 &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2423 LCK_EX, &tgt_dir_policy);
2427 cleanup_phase = 3; /* locks */
2429 /* Step 3: Lookup the child */
2430 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir,
2431 rec->ur_namelen - 1);
2432 if (IS_ERR(dchild)) {
2433 rc = PTR_ERR(dchild);
2434 if (rc != -EPERM && rc != -EACCES)
2435 CERROR("child lookup error %d\n", rc);
2439 cleanup_phase = 4; /* child dentry */
2441 if (dchild->d_inode) {
2442 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2443 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2448 /* Step 4: Do it. */
2449 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2451 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2452 if (IS_ERR(handle)) {
2453 rc = PTR_ERR(handle);
2457 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2458 if (rc && rc != -EPERM && rc != -EACCES)
2459 CERROR("vfs_link error %d\n", rc);
2461 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2462 handle, req, rc, 0);
2465 switch (cleanup_phase) {
2466 case 4: /* child dentry */
2470 ldlm_lock_decref(&src_lockh, LCK_EX);
2471 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2473 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2474 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2476 case 2: /* target dentry */
2478 if (tgt_dir_lockh[1].cookie && update_mode)
2479 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2483 case 1: /* source dentry */
2488 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2491 req->rq_status = rc;
2495 /* The idea here is that we need to get four locks in the end:
2496 * one on each parent directory, one on each child. We need to take
2497 * these locks in some kind of order (to avoid deadlocks), and the order
2498 * I selected is "increasing resource number" order. We need to look up
2499 * the children, however, before we know what the resource number(s) are.
2500 * Thus the following plan:
2502 * 1,2. Look up the parents
2503 * 3,4. Look up the children
2504 * 5. Take locks on the parents and children, in order
2505 * 6. Verify that the children haven't changed since they were looked up
2507 * If there was a race and the children changed since they were first looked
2508 * up, it is possible that mds_verify_child() will be able to just grab the
2509 * lock on the new child resource (if it has a higher resource than any other)
2510 * but we need to compare against not only its parent, but also against the
2511 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2513 * We need the fancy igrab() on the child inodes because we aren't holding a
2514 * lock on the parent after the lookup is done, so dentry->d_inode may change
2515 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2517 static int mds_get_parents_children_locked(struct obd_device *obd,
2518 struct mds_obd *mds,
2519 struct lustre_id *p1_id,
2520 struct dentry **de_srcdirp,
2521 struct lustre_id *p2_id,
2522 struct dentry **de_tgtdirp,
2524 const char *old_name, int old_len,
2525 struct dentry **de_oldp,
2526 const char *new_name, int new_len,
2527 struct dentry **de_newp,
2528 struct lustre_handle *dlm_handles,
2531 struct ldlm_res_id p1_res_id = { .name = {0} };
2532 struct ldlm_res_id p2_res_id = { .name = {0} };
2533 struct ldlm_res_id c1_res_id = { .name = {0} };
2534 struct ldlm_res_id c2_res_id = { .name = {0} };
2535 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2536 /* Only dentry should change, but the inode itself would be
2538 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2539 /* If something is going to be replaced, both dentry and inode locks are
2541 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP|
2542 MDS_INODELOCK_UPDATE}};
2543 struct ldlm_res_id *maxres_src, *maxres_tgt;
2544 struct inode *inode;
2545 __u32 child1_gen = 0;
2546 __u32 child2_gen = 0;
2547 unsigned long child1_ino;
2548 unsigned long child2_ino;
2549 int rc = 0, cleanup_phase = 0;
2552 /* Step 1: Lookup the source directory */
2553 *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2554 if (IS_ERR(*de_srcdirp))
2555 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2557 cleanup_phase = 1; /* source directory dentry */
2559 p1_res_id.name[0] = id_fid(p1_id);
2560 p1_res_id.name[1] = id_group(p1_id);
2562 /* Step 2: Lookup the target directory */
2563 if (id_equal(p1_id, p2_id)) {
2564 *de_tgtdirp = dget(*de_srcdirp);
2566 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2567 if (IS_ERR(*de_tgtdirp)) {
2568 rc = PTR_ERR(*de_tgtdirp);
2574 cleanup_phase = 2; /* target directory dentry */
2576 p2_res_id.name[0] = id_fid(p2_id);
2577 p2_res_id.name[1] = id_group(p2_id);
2580 dlm_handles[5].cookie = 0;
2581 dlm_handles[6].cookie = 0;
2582 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2584 * get a temp lock on just fid, group to flush client cache and
2585 * to protect dirs from concurrent splitting.
2587 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2588 LCK_PW, &p_policy, &p2_res_id,
2589 &dlm_handles[6], LCK_PW, &p_policy);
2593 p1_res_id.name[2] = full_name_hash(old_name, old_len - 1);
2594 p2_res_id.name[2] = full_name_hash(new_name, new_len - 1);
2596 CDEBUG(D_INFO, "take locks on %lu:%u:"LPX64", %lu:%u:"LPX64"\n",
2597 (*de_srcdirp)->d_inode->i_ino,
2598 (*de_srcdirp)->d_inode->i_generation, p1_res_id.name[2],
2599 (*de_tgtdirp)->d_inode->i_ino,
2600 (*de_tgtdirp)->d_inode->i_generation, p2_res_id.name[2]);
2605 /* Step 3: Lookup the source child entry */
2606 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp,
2608 if (IS_ERR(*de_oldp)) {
2609 rc = PTR_ERR(*de_oldp);
2610 CERROR("old child lookup error (%*s): %d\n",
2611 old_len - 1, old_name, rc);
2615 cleanup_phase = 4; /* original name dentry */
2617 inode = (*de_oldp)->d_inode;
2619 if (inode != NULL) {
2620 struct lustre_id sid;
2622 inode = igrab(inode);
2624 GOTO(cleanup, rc = -ENOENT);
2626 down(&inode->i_sem);
2627 rc = mds_read_inode_sid(obd, inode, &sid);
2630 CERROR("Can't read inode self id, inode %lu, "
2631 "rc %d\n", inode->i_ino, rc);
2635 c1_res_id.name[0] = id_fid(&sid);
2636 c1_res_id.name[1] = id_group(&sid);
2637 child1_gen = inode->i_generation;
2638 child1_ino = inode->i_ino;
2640 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2641 c1_res_id.name[0] = (*de_oldp)->d_fid;
2642 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2643 child1_gen = (*de_oldp)->d_generation;
2644 child1_ino = (*de_oldp)->d_inum;
2646 /* src entry is not found. */
2647 GOTO(cleanup, rc = -ENOENT);
2650 /* Step 4: Lookup the target child entry */
2651 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp,
2653 if (IS_ERR(*de_newp)) {
2654 rc = PTR_ERR(*de_newp);
2655 CERROR("new child lookup error (%*s): %d\n",
2656 old_len - 1, old_name, rc);
2660 cleanup_phase = 5; /* target dentry */
2662 inode = (*de_newp)->d_inode;
2667 if (inode != NULL) {
2668 struct lustre_id sid;
2670 inode = igrab(inode);
2674 down(&inode->i_sem);
2675 rc = mds_read_inode_sid(obd, inode, &sid);
2678 CERROR("Can't read inode self id, inode %lu, "
2679 "rc %d\n", inode->i_ino, rc);
2683 c2_res_id.name[0] = id_fid(&sid);
2684 c2_res_id.name[1] = id_group(&sid);
2685 child2_gen = inode->i_generation;
2686 child2_ino = inode->i_ino;
2688 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2689 c2_res_id.name[0] = (*de_newp)->d_fid;
2690 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
2691 child2_gen = (*de_newp)->d_generation;
2692 child2_ino = (*de_newp)->d_inum;
2696 /* Step 5: Take locks on the parents and child(ren) */
2697 maxres_src = &p1_res_id;
2698 maxres_tgt = &p2_res_id;
2699 cleanup_phase = 5; /* target dentry */
2701 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
2702 maxres_src = &c1_res_id;
2703 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
2704 maxres_tgt = &c2_res_id;
2706 rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
2708 &p2_res_id, &dlm_handles[1], parent_mode,
2710 &c1_res_id, &dlm_handles[2], child_mode,
2712 &c2_res_id, &dlm_handles[3], child_mode,
2717 cleanup_phase = 6; /* parent and child(ren) locks */
2719 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2720 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2721 parent_mode, &c1_res_id, &dlm_handles[2],
2722 de_oldp, child_mode, &c1_policy, old_name,old_len,
2723 maxres_tgt, child1_ino, child1_gen);
2725 if (c2_res_id.name[0] != 0)
2726 ldlm_lock_decref(&dlm_handles[3], child_mode);
2727 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2734 if (!DENTRY_VALID(*de_oldp))
2735 GOTO(cleanup, rc = -ENOENT);
2737 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2738 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2739 parent_mode, &c2_res_id, &dlm_handles[3],
2740 de_newp, child_mode, &c2_policy, new_name,
2741 new_len, maxres_src, child2_ino, child2_gen);
2743 ldlm_lock_decref(&dlm_handles[2], child_mode);
2744 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2754 switch (cleanup_phase) {
2755 case 6: /* child lock(s) */
2756 if (c2_res_id.name[0] != 0)
2757 ldlm_lock_decref(&dlm_handles[3], child_mode);
2758 if (c1_res_id.name[0] != 0)
2759 ldlm_lock_decref(&dlm_handles[2], child_mode);
2760 if (dlm_handles[1].cookie != 0)
2761 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2762 if (dlm_handles[0].cookie != 0)
2763 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2764 case 5: /* target dentry */
2766 case 4: /* source dentry */
2770 if (dlm_handles[5].cookie != 0)
2771 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2772 if (dlm_handles[6].cookie != 0)
2773 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2775 case 2: /* target directory dentry */
2776 l_dput(*de_tgtdirp);
2777 case 1: /* source directry dentry */
2778 l_dput(*de_srcdirp);
2785 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
2786 struct ptlrpc_request *req, struct dentry *dentry,
2787 struct dentry *de_dir, struct dentry *de)
2789 struct obd_device *obd = req->rq_export->exp_obd;
2790 struct mds_obd *mds = mds_req2mds(req);
2791 void *handle = NULL;
2797 * name exists and points to local inode try to unlink this name
2798 * and create new one.
2800 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
2801 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
2802 (unsigned long)de->d_inode->i_generation);
2803 handle = fsfilt_start(obd, de_dir->d_inode,
2804 FSFILT_OP_RENAME, NULL);
2806 GOTO(cleanup, rc = PTR_ERR(handle));
2807 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
2810 } else if (de->d_flags & DCACHE_CROSS_REF) {
2811 /* name exists and points to remove inode */
2812 CDEBUG(D_OTHER, "%s: %s points to remote inode %lu/%lu/%lu\n",
2813 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_mdsnum,
2814 (unsigned long)de->d_inum, (unsigned long)de->d_generation);
2815 handle = fsfilt_start(obd, de_dir->d_inode,
2816 FSFILT_OP_RENAME, NULL);
2818 GOTO(cleanup, rc = PTR_ERR(handle));
2819 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
2823 /* name doesn't exist. the simplest case. */
2824 handle = fsfilt_start(obd, de_dir->d_inode,
2825 FSFILT_OP_LINK, NULL);
2827 GOTO(cleanup, rc = PTR_ERR(handle));
2830 rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
2831 rec->ur_tgtlen - 1, dentry->d_inum,
2832 dentry->d_generation, dentry->d_mdsnum,
2835 CERROR("add_dir_entry() returned error %d\n", rc);
2841 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
2842 handle, req, rc, 0);
2847 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
2848 struct ptlrpc_request *req,
2849 struct dentry *dentry, struct dentry *de_dir,
2852 struct obd_device *obd = req->rq_export->exp_obd;
2853 struct mds_obd *mds = mds_req2mds(req);
2854 void *handle = NULL;
2858 handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
2860 GOTO(cleanup, rc = PTR_ERR(handle));
2861 rc = fsfilt_del_dir_entry(obd, de);
2866 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
2867 handle, req, rc, 0);
2871 static int mds_reint_rename_create_name(struct mds_update_record *rec,
2872 int offset, struct ptlrpc_request *req)
2874 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2875 struct obd_device *obd = req->rq_export->exp_obd;
2876 struct mds_obd *mds = mds_req2mds(req);
2877 struct lustre_handle child_lockh = {0};
2878 struct dentry *de_srcdir = NULL;
2879 struct dentry *de_new = NULL;
2880 int cleanup_phase = 0;
2881 void *handle = NULL;
2882 int update_mode, rc = 0;
2886 * another MDS executing rename operation has asked us to create target
2887 * name. such a creation should destroy existing target name.
2890 CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
2891 obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
2893 /* first, lookup the target */
2894 child_lockh.cookie = 0;
2895 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
2896 &de_srcdir,LCK_PW,MDS_INODELOCK_UPDATE,
2897 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
2898 &child_lockh, &de_new, LCK_EX,
2899 MDS_INODELOCK_LOOKUP);
2906 LASSERT(de_srcdir->d_inode);
2909 if (de_new->d_inode) {
2911 * name exists and points to local inode try to unlink this name
2912 * and create new one.
2914 CERROR("%s: %s points to local inode %lu/%lu\n",
2915 obd->obd_name, rec->ur_tgt,
2916 (unsigned long)de_new->d_inode->i_ino,
2917 (unsigned long)de_new->d_inode->i_generation);
2918 handle = fsfilt_start(obd, de_srcdir->d_inode,
2919 FSFILT_OP_RENAME, NULL);
2921 GOTO(cleanup, rc = PTR_ERR(handle));
2922 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de_new);
2925 } else if (de_new->d_flags & DCACHE_CROSS_REF) {
2926 /* name exists adn points to remove inode */
2927 CERROR("%s: %s points to remote inode %lu/%lu/%lu\n",
2928 obd->obd_name, rec->ur_tgt,
2929 (unsigned long)de_new->d_mdsnum,
2930 (unsigned long)de_new->d_inum,
2931 (unsigned long)de_new->d_generation);
2933 /* name doesn't exist. the simplest case */
2934 handle = fsfilt_start(obd, de_srcdir->d_inode,
2935 FSFILT_OP_LINK, NULL);
2937 GOTO(cleanup, rc = PTR_ERR(handle));
2941 rc = fsfilt_add_dir_entry(obd, de_srcdir, rec->ur_tgt,
2942 rec->ur_tgtlen - 1, id_ino(rec->ur_id1),
2943 id_gen(rec->ur_id1), id_group(rec->ur_id1),
2944 id_fid(rec->ur_id1));
2946 CERROR("add_dir_entry() returned error %d\n", rc);
2949 rc = mds_finish_transno(mds, de_srcdir ? de_srcdir->d_inode : NULL,
2950 handle, req, rc, 0);
2951 switch(cleanup_phase) {
2955 if (parent_lockh[1].cookie != 0)
2956 ldlm_lock_decref(&parent_lockh[1], update_mode);
2958 ldlm_lock_decref(&parent_lockh[0], LCK_PW);
2959 if (child_lockh.cookie != 0)
2960 ldlm_lock_decref(&child_lockh, LCK_EX);
2968 req->rq_status = rc;
2972 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
2973 struct ptlrpc_request *req)
2975 struct obd_device *obd = req->rq_export->exp_obd;
2976 struct ptlrpc_request *req2 = NULL;
2977 struct dentry *de_srcdir = NULL;
2978 struct dentry *de_old = NULL;
2979 struct mds_obd *mds = mds_req2mds(req);
2980 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2981 struct lustre_handle child_lockh = {0};
2982 struct mdc_op_data opdata;
2983 int update_mode, rc = 0;
2986 CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
2987 obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
2988 memset(&opdata, 0, sizeof(opdata));
2990 child_lockh.cookie = 0;
2991 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
2992 &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
2993 &update_mode, rec->ur_name,
2994 rec->ur_namelen, &child_lockh, &de_old,
2995 LCK_EX, MDS_INODELOCK_LOOKUP);
2998 LASSERT(de_srcdir->d_inode);
3002 * we already know the target should be created on another MDS so, we
3003 * have to request that MDS to do it.
3006 /* prepare source id */
3007 if (de_old->d_flags & DCACHE_CROSS_REF) {
3008 LASSERT(de_old->d_inode == NULL);
3009 CDEBUG(D_OTHER, "request to move remote name\n");
3010 mds_pack_dentry2id(obd, &opdata.id1, de_old, 1);
3011 } else if (de_old->d_inode == NULL) {
3012 /* oh, source doesn't exist */
3013 GOTO(cleanup, rc = -ENOENT);
3015 struct lustre_id sid;
3016 struct inode *inode = de_old->d_inode;
3018 LASSERT(inode != NULL);
3019 CDEBUG(D_OTHER, "request to move local name\n");
3020 id_group(&opdata.id1) = mds->mds_num;
3021 id_ino(&opdata.id1) = inode->i_ino;
3022 id_gen(&opdata.id1) = inode->i_generation;
3024 down(&inode->i_sem);
3025 rc = mds_read_inode_sid(obd, inode, &sid);
3028 CERROR("Can't read inode self id, "
3029 "inode %lu, rc = %d\n",
3034 id_fid(&opdata.id1) = id_fid(&sid);
3037 opdata.id2 = *(rec->ur_id2);
3038 rc = md_rename(mds->mds_lmv_exp, &opdata, NULL, 0,
3039 rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3044 rc = mds_del_local_dentry(rec, offset, req, NULL,
3049 ptlrpc_req_finished(req2);
3052 if (parent_lockh[1].cookie != 0)
3053 ldlm_lock_decref(&parent_lockh[1], update_mode);
3055 ldlm_lock_decref(&parent_lockh[0], LCK_PW);
3056 if (child_lockh.cookie != 0)
3057 ldlm_lock_decref(&child_lockh, LCK_EX);
3062 req->rq_status = rc;
3066 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3067 struct ptlrpc_request *req, struct lustre_handle *lockh)
3069 struct obd_device *obd = req->rq_export->exp_obd;
3070 struct dentry *de_srcdir = NULL;
3071 struct dentry *de_tgtdir = NULL;
3072 struct dentry *de_old = NULL;
3073 struct dentry *de_new = NULL;
3074 struct inode *old_inode = NULL, *new_inode = NULL;
3075 struct mds_obd *mds = mds_req2mds(req);
3076 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3077 struct mds_body *body = NULL;
3078 struct llog_create_locks *lcl = NULL;
3079 struct lov_mds_md *lmm = NULL;
3080 int rc = 0, cleanup_phase = 0;
3081 void *handle = NULL;
3084 LASSERT(offset == 1);
3086 DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3087 OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3090 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3092 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3093 DEBUG_REQ(D_HA, req, "rename replay\n");
3094 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3095 lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3096 req->rq_repmsg->buflens[2]);
3099 MDS_UPDATE_COUNTER(mds, MDS_RENAME_COUNT);
3101 if (rec->ur_namelen == 1) {
3102 rc = mds_reint_rename_create_name(rec, offset, req);
3106 /* check if new name should be located on remote target. */
3107 if (id_group(rec->ur_id2) != mds->mds_num) {
3108 rc = mds_reint_rename_to_remote(rec, offset, req);
3112 rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3113 rec->ur_id2, &de_tgtdir, LCK_PW,
3114 rec->ur_name, rec->ur_namelen,
3115 &de_old, rec->ur_tgt,
3116 rec->ur_tgtlen, &de_new,
3117 dlm_handles, LCK_EX);
3121 cleanup_phase = 1; /* parent(s), children, locks */
3123 old_inode = de_old->d_inode;
3124 new_inode = de_new->d_inode;
3126 /* sanity check for src inode */
3127 if (de_old->d_flags & DCACHE_CROSS_REF) {
3128 LASSERT(de_old->d_inode == NULL);
3131 * in the case of cross-ref dir, we can perform this check only
3132 * if child and parent lie on the same mds. This is because
3133 * otherwise they can have the same inodes.
3135 if (de_old->d_mdsnum == mds->mds_num) {
3136 if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3137 de_old->d_inum == de_tgtdir->d_inode->i_ino)
3138 GOTO(cleanup, rc = -EINVAL);
3141 LASSERT(de_old->d_inode != NULL);
3142 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3143 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3144 GOTO(cleanup, rc = -EINVAL);
3147 /* sanity check for dest inode */
3148 if (de_new->d_flags & DCACHE_CROSS_REF) {
3149 LASSERT(new_inode == NULL);
3151 /* the same check about target dentry. */
3152 if (de_new->d_mdsnum == mds->mds_num) {
3153 if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3154 de_new->d_inum == de_tgtdir->d_inode->i_ino)
3155 GOTO(cleanup, rc = -EINVAL);
3159 * regular files usualy do not have ->rename() implemented. But
3160 * we handle only this case when @de_new is cross-ref entry,
3161 * because in other cases it will be handled by vfs_rename().
3163 if (de_old->d_inode && (!de_old->d_inode->i_op ||
3164 !de_old->d_inode->i_op->rename))
3165 GOTO(cleanup, rc = -EPERM);
3168 (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3169 new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3170 GOTO(cleanup, rc = -EINVAL);
3173 /* check if inodes point to each other. */
3174 if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3175 !(de_new->d_flags & DCACHE_CROSS_REF) &&
3176 old_inode == new_inode)
3177 GOTO(cleanup, rc = 0);
3180 * if we are about to remove the target at first, pass the EA of that
3181 * inode to client to perform and cleanup on OST.
3183 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3184 LASSERT(body != NULL);
3186 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3188 DOWN_READ_I_ALLOC_SEM(new_inode);
3189 cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3191 if (new_inode && ((S_ISDIR(new_inode->i_mode) &&
3192 new_inode->i_nlink == 2) ||
3193 new_inode->i_nlink == 1)) {
3194 if (mds_orphan_open_count(new_inode) > 0) {
3195 /* need to lock pending_dir before transaction */
3196 down(&mds->mds_pending_dir->d_inode->i_sem);
3197 cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3198 } else if (S_ISREG(new_inode->i_mode)) {
3199 mds_pack_inode2body(obd, body, new_inode, 0);
3200 mds_pack_md(obd, req->rq_repmsg, 1, body,
3201 new_inode, MDS_PACK_MD_LOCK);
3205 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3206 de_srcdir->d_inode->i_sb);
3208 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3209 /* Check if we are moving old entry into its child. 2.6 does not
3210 check for this in vfs_rename() anymore */
3211 if (is_subdir(de_new, de_old))
3212 GOTO(cleanup, rc = -EINVAL);
3214 if (de_old->d_flags & DCACHE_CROSS_REF) {
3215 rc = mds_add_local_dentry(rec, offset, req, de_old, de_tgtdir,
3220 rc = mds_del_local_dentry(rec, offset, req, de_old, de_srcdir,
3225 lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3226 handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3227 NULL, le32_to_cpu(lmm->lmm_stripe_count));
3230 GOTO(cleanup, rc = PTR_ERR(handle));
3233 de_old->d_fsdata = req;
3234 de_new->d_fsdata = req;
3235 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3238 if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3239 if (mds_orphan_open_count(new_inode) > 0)
3240 rc = mds_orphan_add_link(rec, obd, de_new);
3243 GOTO(cleanup, rc = 0);
3245 if (!S_ISREG(new_inode->i_mode))
3248 if (!(body->valid & OBD_MD_FLEASIZE)) {
3249 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3250 OBD_MD_FLATIME | OBD_MD_FLMTIME);
3251 } else if (mds_log_op_unlink(obd, new_inode,
3252 lustre_msg_buf(req->rq_repmsg,1,0),
3253 req->rq_repmsg->buflens[1],
3254 lustre_msg_buf(req->rq_repmsg,2,0),
3255 req->rq_repmsg->buflens[2],
3257 body->valid |= OBD_MD_FLCOOKIE;
3264 rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3265 handle, req, rc, 0);
3267 switch (cleanup_phase) {
3269 up(&mds->mds_pending_dir->d_inode->i_sem);
3272 UP_READ_I_ALLOC_SEM(new_inode);
3275 if (dlm_handles[5].cookie != 0)
3276 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3277 if (dlm_handles[6].cookie != 0)
3278 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3281 ptlrpc_save_llog_lock(req, lcl);
3284 if (dlm_handles[3].cookie != 0)
3285 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3286 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3287 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3288 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3290 if (dlm_handles[3].cookie != 0)
3291 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3292 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3293 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3294 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3303 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3306 req->rq_status = rc;
3310 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3311 struct ptlrpc_request *, struct lustre_handle *);
3313 static mds_reinter reinters[REINT_MAX + 1] = {
3314 [REINT_SETATTR] mds_reint_setattr,
3315 [REINT_CREATE] mds_reint_create,
3316 [REINT_LINK] mds_reint_link,
3317 [REINT_UNLINK] mds_reint_unlink,
3318 [REINT_RENAME] mds_reint_rename,
3319 [REINT_OPEN] mds_open
3322 int mds_reint_rec(struct mds_update_record *rec, int offset,
3323 struct ptlrpc_request *req, struct lustre_handle *lockh)
3325 struct obd_device *obd = req->rq_export->exp_obd;
3326 struct lvfs_run_ctxt saved;
3329 /* checked by unpacker */
3330 LASSERT(rec->ur_opcode <= REINT_MAX &&
3331 reinters[rec->ur_opcode] != NULL);
3333 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3334 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3335 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);