Whamcloud - gitweb
- debug message correction
[fs/lustre-release.git] / lustre / mds / mds_reint.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/mds/mds_reint.c
5  *  Lustre Metadata Server (mds) reintegration routines
6  *
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>
11  *
12  *   This file is part of Lustre, http://www.lustre.org.
13  *
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.
17  *
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.
22  *
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.
26  */
27
28 #ifndef EXPORT_SYMTAB
29 # define EXPORT_SYMTAB
30 #endif
31 #define DEBUG_SUBSYSTEM S_MDS
32
33 #include <linux/fs.h>
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"
46
47 struct mds_logcancel_data {
48         struct lov_mds_md      *mlcd_lmm;
49         int                     mlcd_size;
50         int                     mlcd_cookielen;
51         int                     mlcd_eadatalen;
52         struct llog_cookie      mlcd_cookies[0];
53 };
54
55 static void mds_cancel_cookies_cb(struct obd_device *obd,
56                                   __u64 transno, void *cb_data,
57                                   int error)
58 {
59         struct mds_logcancel_data *mlcd = cb_data;
60         struct lov_stripe_md *lsm = NULL;
61         struct llog_ctxt *ctxt;
62         int rc;
63
64         obd_transno_commit_cb(obd, transno, error);
65
66         CDEBUG(D_HA, "cancelling %d cookies\n",
67                (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
68
69         rc = obd_unpackmd(obd->u.mds.mds_lov_exp, &lsm, mlcd->mlcd_lmm,
70                           mlcd->mlcd_eadatalen);
71         if (rc < 0) {
72                 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
73                        (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
74                        rc);
75         } else {
76                 ///* XXX 0 normally, SENDNOW for debug */);
77                 ctxt = llog_get_context(&obd->obd_llogs,
78                                         mlcd->mlcd_cookies[0].lgc_subsys + 1);
79                 rc = llog_cancel(ctxt, mlcd->mlcd_cookielen /
80                                  sizeof(*mlcd->mlcd_cookies),
81                                  mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW, lsm);
82                 if (rc)
83                         CERROR("error cancelling %d log cookies: rc %d\n",
84                                (int)(mlcd->mlcd_cookielen /
85                                      sizeof(*mlcd->mlcd_cookies)), rc);
86         }
87
88         OBD_FREE(mlcd, mlcd->mlcd_size);
89 }
90
91 /* Assumes caller has already pushed us into the kernel context. */
92 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
93                        struct ptlrpc_request *req, int rc, __u32 op_data)
94 {
95         struct mds_export_data *med = &req->rq_export->exp_mds_data;
96         struct obd_device *obd = req->rq_export->exp_obd;
97         struct mds_client_data *mcd = med->med_mcd;
98         int err, log_pri = D_HA;
99         __u64 transno;
100         loff_t off;
101         ENTRY;
102
103         /* if the export has already been failed, we have no last_rcvd slot */
104         if (req->rq_export->exp_failed) {
105                 CERROR("committing transaction for disconnected client\n");
106                 if (handle)
107                         GOTO(out_commit, rc);
108                 RETURN(rc);
109         }
110
111         if (IS_ERR(handle))
112                 RETURN(rc);
113
114         if (handle == NULL) {
115                 /* if we're starting our own xaction, use our own inode */
116                 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
117                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
118                 if (IS_ERR(handle)) {
119                         CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
120                         RETURN(PTR_ERR(handle));
121                 }
122         }
123
124         off = med->med_off;
125
126         transno = req->rq_reqmsg->transno;
127         if (rc != 0) {
128                 LASSERTF(transno == 0, "BUG 3934, t"LPU64" rc %d\n", transno, rc);
129         } else if (transno == 0) {
130                 spin_lock(&mds->mds_transno_lock);
131                 transno = ++mds->mds_last_transno;
132                 spin_unlock(&mds->mds_transno_lock);
133         } else {
134                 spin_lock(&mds->mds_transno_lock);
135                 if (transno > mds->mds_last_transno)
136                         mds->mds_last_transno = transno;
137                 spin_unlock(&mds->mds_transno_lock);
138         }
139         req->rq_repmsg->transno = req->rq_transno = transno;
140         mcd->mcd_last_transno = cpu_to_le64(transno);
141         mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
142         mcd->mcd_last_result = cpu_to_le32(rc);
143         mcd->mcd_last_data = cpu_to_le32(op_data);
144
145         fsfilt_add_journal_cb(obd, mds->mds_sb, transno, handle,
146                               mds_commit_last_transno_cb, NULL);
147         
148         err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd,
149                                   sizeof(*mcd), &off, 0);
150
151         if (err) {
152                 log_pri = D_ERROR;
153                 if (rc == 0)
154                         rc = err;
155         }
156
157         DEBUG_REQ(log_pri, req,
158                   "wrote trans #"LPU64" client %s at idx %u: err = %d",
159                   transno, mcd->mcd_uuid, med->med_idx, err);
160
161         err = mds_update_last_fid(obd, handle, 0);
162         if (err) {
163                 log_pri = D_ERROR;
164                 if (rc == 0)
165                         rc = err;
166         }
167                 
168         err = mds_lov_write_objids(obd);
169         if (err) {
170                 log_pri = D_ERROR;
171                 if (rc == 0)
172                         rc = err;
173         }
174         CDEBUG(log_pri, "wrote objids: err = %d\n", err);
175
176         EXIT;
177 out_commit:
178         err = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
179         if (err) {
180                 CERROR("error committing transaction: %d\n", err);
181                 if (!rc)
182                         rc = err;
183         }
184
185         return rc;
186 }
187
188 /* this gives the same functionality as the code between
189  * sys_chmod and inode_setattr
190  * chown_common and inode_setattr
191  * utimes and inode_setattr
192  */
193 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
194 {
195         time_t now = LTIME_S(CURRENT_TIME);
196         struct iattr *attr = &rec->ur_iattr;
197         unsigned int ia_valid = attr->ia_valid;
198         int error;
199         ENTRY;
200
201         /* only fix up attrs if the client VFS didn't already */
202         if (!(ia_valid & ATTR_RAW))
203                 RETURN(0);
204
205         if (!(ia_valid & ATTR_CTIME_SET))
206                 LTIME_S(attr->ia_ctime) = now;
207         if (!(ia_valid & ATTR_ATIME_SET))
208                 LTIME_S(attr->ia_atime) = now;
209         if (!(ia_valid & ATTR_MTIME_SET))
210                 LTIME_S(attr->ia_mtime) = now;
211
212         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
213                 RETURN(-EPERM);
214
215         /* times */
216         if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
217                 if (rec->ur_fsuid != inode->i_uid &&
218                     (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
219                         RETURN(error);
220         }
221
222         if (ia_valid & ATTR_SIZE) {
223                 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
224                         RETURN(error);
225         }
226
227         if (ia_valid & ATTR_UID) {
228                 /* chown */
229                 error = -EPERM;
230                 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
231                         RETURN(-EPERM);
232                 if (attr->ia_uid == (uid_t) -1)
233                         attr->ia_uid = inode->i_uid;
234                 if (attr->ia_gid == (gid_t) -1)
235                         attr->ia_gid = inode->i_gid;
236                 attr->ia_mode = inode->i_mode;
237                 /*
238                  * If the user or group of a non-directory has been
239                  * changed by a non-root user, remove the setuid bit.
240                  * 19981026 David C Niemi <niemi@tux.org>
241                  *
242                  * Changed this to apply to all users, including root,
243                  * to avoid some races. This is the behavior we had in
244                  * 2.0. The check for non-root was definitely wrong
245                  * for 2.2 anyway, as it should have been using
246                  * CAP_FSETID rather than fsuid -- 19990830 SD.
247                  */
248                 if ((inode->i_mode & S_ISUID) == S_ISUID &&
249                     !S_ISDIR(inode->i_mode)) {
250                         attr->ia_mode &= ~S_ISUID;
251                         attr->ia_valid |= ATTR_MODE;
252                 }
253                 /*
254                  * Likewise, if the user or group of a non-directory
255                  * has been changed by a non-root user, remove the
256                  * setgid bit UNLESS there is no group execute bit
257                  * (this would be a file marked for mandatory
258                  * locking).  19981026 David C Niemi <niemi@tux.org>
259                  *
260                  * Removed the fsuid check (see the comment above) --
261                  * 19990830 SD.
262                  */
263                 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
264                      (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
265                         attr->ia_mode &= ~S_ISGID;
266                         attr->ia_valid |= ATTR_MODE;
267                 }
268         } else if (ia_valid & ATTR_MODE) {
269                 int mode = attr->ia_mode;
270                 /* chmod */
271                 if (attr->ia_mode == (mode_t) -1)
272                         attr->ia_mode = inode->i_mode;
273                 attr->ia_mode =
274                         (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
275         }
276         RETURN(0);
277 }
278
279 void mds_steal_ack_locks(struct ptlrpc_request *req)
280 {
281         struct obd_export         *exp = req->rq_export;
282         struct list_head          *tmp;
283         struct ptlrpc_reply_state *oldrep;
284         struct ptlrpc_service     *svc;
285         struct llog_create_locks  *lcl;
286         unsigned long              flags;
287         char                       str[PTL_NALFMT_SIZE];
288         int                        i;
289
290         /* CAVEAT EMPTOR: spinlock order */
291         spin_lock_irqsave (&exp->exp_lock, flags);
292         list_for_each (tmp, &exp->exp_outstanding_replies) {
293                 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
294
295                 if (oldrep->rs_xid != req->rq_xid)
296                         continue;
297
298                 if (oldrep->rs_msg.opc != req->rq_reqmsg->opc)
299                         CERROR ("Resent req xid "LPX64" has mismatched opc: "
300                                 "new %d old %d\n", req->rq_xid,
301                                 req->rq_reqmsg->opc, oldrep->rs_msg.opc);
302
303                 svc = oldrep->rs_srv_ni->sni_service;
304                 spin_lock (&svc->srv_lock);
305
306                 list_del_init (&oldrep->rs_exp_list);
307
308                 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
309                       " o%d NID %s\n",
310                       oldrep->rs_nlocks, oldrep,
311                       oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg.opc,
312                       ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
313
314                 for (i = 0; i < oldrep->rs_nlocks; i++)
315                         ptlrpc_save_lock(req,
316                                          &oldrep->rs_locks[i],
317                                          oldrep->rs_modes[i]);
318                 oldrep->rs_nlocks = 0;
319
320                 lcl = oldrep->rs_llog_locks;
321                 oldrep->rs_llog_locks = NULL;
322                 if (lcl != NULL)
323                         ptlrpc_save_llog_lock(req, lcl);
324
325                 DEBUG_REQ(D_HA, req, "stole locks for");
326                 ptlrpc_schedule_difficult_reply (oldrep);
327
328                 spin_unlock (&svc->srv_lock);
329                 spin_unlock_irqrestore (&exp->exp_lock, flags);
330                 return;
331         }
332         spin_unlock_irqrestore (&exp->exp_lock, flags);
333 }
334
335 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
336 {
337         DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
338                   mcd->mcd_last_transno, mcd->mcd_last_result);
339         req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
340         req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
341
342         mds_steal_ack_locks(req);
343 }
344
345 static void reconstruct_reint_setattr(struct mds_update_record *rec,
346                                       int offset, struct ptlrpc_request *req)
347 {
348         struct mds_export_data *med = &req->rq_export->exp_mds_data;
349         struct mds_body *body;
350         struct dentry *de;
351
352         mds_req_from_mcd(req, med->med_mcd);
353
354         de = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
355         if (IS_ERR(de)) {
356                 LASSERT(PTR_ERR(de) == req->rq_status);
357                 return;
358         }
359
360         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
361         mds_pack_inode2body(req2obd(req), body, de->d_inode, 0);
362
363         /* Don't return OST-specific attributes if we didn't just set them */
364         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
365                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
366         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
367                 body->valid |= OBD_MD_FLMTIME;
368         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
369                 body->valid |= OBD_MD_FLATIME;
370
371         l_dput(de);
372 }
373
374 /* In the raw-setattr case, we lock the child inode.
375  * In the write-back case or if being called from open, the client holds a lock
376  * already.
377  *
378  * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
379 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
380                              struct ptlrpc_request *req, struct lustre_handle *lh)
381 {
382         struct mds_obd *mds = mds_req2mds(req);
383         struct obd_device *obd = req->rq_export->exp_obd;
384         struct mds_body *body;
385         struct dentry *de;
386         struct inode *inode = NULL;
387         struct lustre_handle lockh[2] = {{0}, {0}};
388         int parent_mode;
389         void *handle = NULL;
390         struct mds_logcancel_data *mlcd = NULL;
391         int rc = 0, cleanup_phase = 0, err;
392         int locked = 0;
393         ENTRY;
394
395         LASSERT(offset == 1);
396
397         DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x",
398                   id_ino(rec->ur_id1), id_gen(rec->ur_id1),
399                   rec->ur_iattr.ia_valid);
400
401         MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
402         MD_COUNTER_INCREMENT(obd, setattr);
403
404         if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
405                 de = mds_id2dentry(obd, rec->ur_id1, NULL);
406                 if (IS_ERR(de))
407                         GOTO(cleanup, rc = PTR_ERR(de));
408         } else {
409                 __u64 lockpart = MDS_INODELOCK_UPDATE;
410                 if (rec->ur_iattr.ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
411                         lockpart |= MDS_INODELOCK_LOOKUP;
412                 de = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
413                                           lockh, &parent_mode, NULL, 0, lockpart);
414                 if (IS_ERR(de))
415                         GOTO(cleanup, rc = PTR_ERR(de));
416                 locked = 1;
417         }
418
419         cleanup_phase = 1;
420
421         inode = de->d_inode;
422         LASSERT(inode);
423         if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
424             rec->ur_eadata != NULL)
425                 down(&inode->i_sem);
426
427         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
428
429         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
430         if (IS_ERR(handle))
431                 GOTO(cleanup, rc = PTR_ERR(handle));
432
433         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
434                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
435                        LTIME_S(rec->ur_iattr.ia_mtime),
436                        LTIME_S(rec->ur_iattr.ia_ctime));
437         rc = mds_fix_attr(inode, rec);
438         if (rc)
439                 GOTO(cleanup, rc);
440
441         if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG)    /* ioctl */
442                 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
443                                       (long)&rec->ur_iattr.ia_attr_flags);
444         else                                            /* setattr */
445                 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
446
447         if (rc == 0 && (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
448             rec->ur_eadata != NULL) {
449                 struct lov_stripe_md *lsm = NULL;
450
451                 rc = ll_permission(inode, MAY_WRITE, NULL);
452                 if (rc < 0)
453                         GOTO(cleanup, rc);
454
455                 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, mds->mds_lov_exp,
456                                    0, &lsm, rec->ur_eadata);
457                 if (rc)
458                         GOTO(cleanup, rc);
459
460                 obd_free_memmd(mds->mds_lov_exp, &lsm);
461
462                 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
463                                    rec->ur_eadatalen);
464                 if (rc)
465                         GOTO(cleanup, rc);
466         }
467
468         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
469         mds_pack_inode2body(obd, body, inode, 0);
470         body->id1 = *rec->ur_id1;
471
472         /* Don't return OST-specific attributes if we didn't just set them */
473         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
474                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
475         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
476                 body->valid |= OBD_MD_FLMTIME;
477         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
478                 body->valid |= OBD_MD_FLATIME;
479
480         if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_lov_obd)) {
481                 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
482                           rec->ur_eadatalen);
483                 if (mlcd) {
484                         mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
485                                 rec->ur_eadatalen;
486                         mlcd->mlcd_eadatalen = rec->ur_eadatalen;
487                         mlcd->mlcd_cookielen = rec->ur_cookielen;
488                         mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
489                                 mlcd->mlcd_cookielen;
490                         memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
491                                mlcd->mlcd_cookielen);
492                         memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
493                                mlcd->mlcd_eadatalen);
494                 } else {
495                         CERROR("unable to allocate log cancel data\n");
496                 }
497         }
498         EXIT;
499  cleanup:
500         if (mlcd != NULL)
501                 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
502                                       handle, mds_cancel_cookies_cb, mlcd);
503         err = mds_finish_transno(mds, inode, handle, req, rc, 0);
504         switch (cleanup_phase) {
505         case 1:
506                 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
507                     rec->ur_eadata != NULL)
508                         up(&inode->i_sem);
509                 l_dput(de);
510                 if (locked) {
511 #ifdef S_PDIROPS
512                         if (lockh[1].cookie != 0)
513                                 ldlm_lock_decref(lockh + 1, parent_mode);
514 #endif
515                         if (rc) {
516                                 ldlm_lock_decref(lockh, LCK_PW);
517                         } else {
518                                 ptlrpc_save_lock (req, lockh, LCK_PW);
519                         }
520                 }
521         case 0:
522                 break;
523         default:
524                 LBUG();
525         }
526         if (err && !rc)
527                 rc = err;
528
529         req->rq_status = rc;
530         return 0;
531 }
532
533 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
534                                      struct ptlrpc_request *req)
535 {
536         struct mds_export_data *med = &req->rq_export->exp_mds_data;
537         struct dentry *parent, *child;
538         struct mds_body *body;
539         ENTRY;
540
541         mds_req_from_mcd(req, med->med_mcd);
542
543         if (req->rq_status) {
544                 EXIT;
545                 return;
546         }
547
548         parent = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
549         LASSERT(!IS_ERR(parent));
550         child = ll_lookup_one_len(rec->ur_name, parent,
551                                   rec->ur_namelen - 1);
552         LASSERT(!IS_ERR(child));
553         if ((child->d_flags & DCACHE_CROSS_REF)) {
554                 LASSERTF(child->d_inode == NULL, "BUG 3869\n");
555                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
556                 mds_pack_dentry2body(req2obd(req), body, child, 1);
557         } else if (child->d_inode == NULL) {
558                 DEBUG_REQ(D_ERROR, req, "parent "DLID4" name %s mode %o",
559                           OLID4(rec->ur_id1), rec->ur_name, rec->ur_mode);
560                 LASSERTF(child->d_inode != NULL, "BUG 3869\n");
561         } else {
562                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
563                 mds_pack_inode2body(req2obd(req), body, child->d_inode, 1);
564         }
565         l_dput(parent);
566         l_dput(child);
567         EXIT;
568 }
569
570 static int mds_reint_create(struct mds_update_record *rec, int offset,
571                             struct ptlrpc_request *req,
572                             struct lustre_handle *lh)
573 {
574         struct dentry *dparent = NULL;
575         struct mds_obd *mds = mds_req2mds(req);
576         struct obd_device *obd = req->rq_export->exp_obd;
577         struct dentry *dchild = NULL;
578         struct inode *dir = NULL;
579         void *handle = NULL;
580         struct lustre_handle lockh[2] = {{0}, {0}};
581         int parent_mode;
582         int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
583         int created = 0;
584         struct dentry_params dp;
585         struct mea *mea = NULL;
586         int mea_size;
587         ENTRY;
588
589         LASSERT(offset == 1);
590         
591         LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
592                         LUSTRE_MDS_NAME));
593
594         DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
595                   id_ino(rec->ur_id1), id_gen(rec->ur_id1),
596                   rec->ur_name, rec->ur_mode);
597
598         MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
599
600         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
601                 GOTO(cleanup, rc = -ESTALE);
602
603         dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
604                                        lockh, &parent_mode, rec->ur_name,
605                                        rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
606         if (IS_ERR(dparent)) {
607                 rc = PTR_ERR(dparent);
608                 CERROR("parent lookup error %d\n", rc);
609                 GOTO(cleanup, rc);
610         }
611         cleanup_phase = 1; /* locked parent dentry */
612         dir = dparent->d_inode;
613         LASSERT(dir);
614
615         ldlm_lock_dump_handle(D_OTHER, lockh);
616
617         /* try to retrieve MEA data for this dir */
618         rc = mds_get_lmv_attr(obd, dparent->d_inode, &mea, &mea_size);
619         if (rc)
620                 GOTO(cleanup, rc);
621
622         if (mea != NULL) {
623                 /*
624                  * dir is already splitted, check is requested filename should
625                  * live at this MDS or at another one.
626                  */
627                 int i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
628                 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
629                         CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
630                                " should be %lu(%d)\n",
631                                mea->mea_master, dparent->d_inode->i_ino,
632                                dparent->d_inode->i_generation, rec->ur_name,
633                                (unsigned long)id_group(&mea->mea_ids[i]), i);
634                         GOTO(cleanup, rc = -ERESTART);
635                 }
636         }
637
638         dchild = ll_lookup_one_len(rec->ur_name, dparent, 
639                                    rec->ur_namelen - 1);
640         if (IS_ERR(dchild)) {
641                 rc = PTR_ERR(dchild);
642                 CERROR("Can't find "DLID4"/%s, error %d\n",
643                        OLID4(rec->ur_id1), rec->ur_name, rc);
644                 GOTO(cleanup, rc);
645         }
646
647         cleanup_phase = 2; /* child dentry */
648
649         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
650
651         if (type == S_IFREG || type == S_IFDIR) {
652                 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
653                 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
654                        obd->obd_name, dparent->d_inode->i_ino,
655                        dparent->d_inode->i_generation, rc, parent_mode);
656                 if (rc > 0) {
657                         /* dir got splitted */
658                         GOTO(cleanup, rc = -ERESTART);
659                 } else if (rc < 0) {
660                         /* error happened during spitting. */
661                         GOTO(cleanup, rc);
662                 }
663         }
664
665         if (dir->i_mode & S_ISGID) {
666                 if (S_ISDIR(rec->ur_mode))
667                         rec->ur_mode |= S_ISGID;
668         }
669
670         /*
671          * here inode number should be used only in the case of replaying. It is
672          * needed to check if object already created in the case of creating
673          * remote inode.
674          */
675         dchild->d_fsdata = (void *)&dp;
676         dp.p_inum = (unsigned long)id_ino(rec->ur_id2);
677         dp.p_ptr = req;
678
679         switch (type) {
680         case S_IFREG:{
681                 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
682                 if (IS_ERR(handle))
683                         GOTO(cleanup, rc = PTR_ERR(handle));
684                 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
685                 EXIT;
686                 break;
687         }
688         case S_IFDIR:{
689                 int i, nstripes = 0;
690                 struct lustre_id sid;
691                 
692                 /*
693                  * as Peter asked, mkdir() should distribute new directories
694                  * over the whole cluster in order to distribute namespace
695                  * processing load. first, we calculate which MDS to use to put
696                  * new directory's inode in.
697                  */
698                 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1, 
699                                       rec->ur_flags);
700                 if (i == mds->mds_num) {
701                         /* inode will be created locally */
702                         handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
703                         if (IS_ERR(handle))
704                                 GOTO(cleanup, rc = PTR_ERR(handle));
705
706                         rc = vfs_mkdir(dir, dchild, rec->ur_mode);
707                         if (rc) {
708                                 CERROR("Can't create dir %s, rc = %d\n",
709                                        dchild->d_name.name, rc);
710                                 GOTO(cleanup, rc);
711                         }
712
713                         down(&dchild->d_inode->i_sem);
714                         if (dp.p_inum) {
715                                 rc = mds_update_inode_sid(obd, dchild->d_inode,
716                                                           handle, rec->ur_id2);
717                                 if (rc) {
718                                         CERROR("mds_update_inode_sid() failed, inode %lu, "
719                                                "rc %d\n", dchild->d_inode->i_ino, rc);
720                                 }
721
722                                 /* 
723                                  * make sure, that fid is up-to-date.
724                                  */
725                                 mds_set_last_fid(obd, id_fid(rec->ur_id2));
726                         } else {
727                                 rc = mds_alloc_inode_sid(obd, dchild->d_inode,
728                                                          handle, &sid);
729                                 if (rc) {
730                                         CERROR("mds_alloc_inode_sid() failed, inode %lu, "
731                                                "rc %d\n", dchild->d_inode->i_ino, rc);
732                                 }
733                         }
734                         up(&dchild->d_inode->i_sem);
735                         
736                         if (rc)
737                                 GOTO(cleanup, rc);
738                         
739                         if (rec->ur_eadata)
740                                 nstripes = *(u16 *)rec->ur_eadata;
741
742                         if (rc == 0 && nstripes) {
743                                 /*
744                                  * we pass LCK_EX to split routine to signal,
745                                  * that we have exclusive access to the
746                                  * directory. Simple because nobody knows it
747                                  * already exists -bzzz
748                                  */
749                                 rc = mds_try_to_split_dir(obd, dchild,
750                                                           NULL, nstripes,
751                                                           LCK_EX);
752                                 if (rc > 0) {
753                                         /* dir got splitted */
754                                         rc = 0;
755                                 } else if (rc < 0) {
756                                         /* an error occured during
757                                          * splitting. */
758                                         GOTO(cleanup, rc);
759                                 }
760                         }
761                 } else if (!DENTRY_VALID(dchild)) {
762                         /* inode will be created on another MDS */
763                         struct obdo *oa = NULL;
764                         struct mds_body *body;
765                         
766                         /* first, create that inode */
767                         oa = obdo_alloc();
768                         if (!oa)
769                                 GOTO(cleanup, rc = -ENOMEM);
770
771                         oa->o_mds = i;
772                         oa->o_easize = 0;
773
774                         if (rec->ur_eadata) {
775                                 /* user asks for creating splitted dir */
776                                 oa->o_easize = *((u16 *) rec->ur_eadata);
777                         }
778
779                         obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
780                                         OBD_MD_FLMTIME | OBD_MD_FLCTIME |
781                                         OBD_MD_FLUID | OBD_MD_FLGID);
782                         
783                         oa->o_mode = dir->i_mode;
784                         
785                         CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
786                                obd->obd_name, i);
787
788                         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
789                                 /*
790                                  * here inode number and generation are
791                                  * important, as this is replay request and we
792                                  * need them to check if such an object is
793                                  * already created.
794                                  */
795                                 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
796                                        obd->obd_name, rec->ur_namelen - 1,
797                                        rec->ur_name, (unsigned)id_ino(rec->ur_id2),
798                                        (unsigned)id_gen(rec->ur_id2));
799                                 oa->o_id = id_ino(rec->ur_id2);
800                                 oa->o_fid = id_fid(rec->ur_id2);
801                                 oa->o_generation = id_gen(rec->ur_id2);
802                                 oa->o_flags |= OBD_FL_RECREATE_OBJS;
803
804                                 /* 
805                                  * fid should be defined here. It should be
806                                  * passedfrom client.
807                                  */
808                                 LASSERT(oa->o_fid != 0);
809                         }
810
811                         /* 
812                          * before obd_create() is called, o_fid is not known if
813                          * this is not recovery of cause.
814                          */
815                         rc = obd_create(mds->mds_lmv_exp, oa, NULL, NULL);
816                         if (rc) {
817                                 CERROR("can't create remote inode: %d\n", rc);
818                                 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
819                                           id_ino(rec->ur_id1), id_gen(rec->ur_id1),
820                                           rec->ur_name, rec->ur_mode);
821                                 obdo_free(oa);
822                                 GOTO(cleanup, rc);
823                         }
824
825                         LASSERT(oa->o_fid != 0);
826                         
827                         /* now, add new dir entry for it */
828                         handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
829                         if (IS_ERR(handle)) {
830                                 obdo_free(oa);
831                                 GOTO(cleanup, rc = PTR_ERR(handle));
832                         }
833
834                         /* creating local dentry for remote inode. */
835                         rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
836                                                   rec->ur_namelen - 1, oa->o_id,
837                                                   oa->o_generation, i, oa->o_fid);
838
839                         if (rc) {
840                                 CERROR("Can't create local entry %*s for "
841                                        "remote inode.\n", rec->ur_namelen - 1,
842                                         rec->ur_name);
843                                 GOTO(cleanup, rc);
844                         }
845
846                         /* fill reply */
847                         body = lustre_msg_buf(req->rq_repmsg,
848                                               0, sizeof(*body));
849                         body->valid |= OBD_MD_FLID | OBD_MD_MDS |
850                                 OBD_MD_FID;
851
852                         obdo2id(&body->id1, oa);
853                         obdo_free(oa);
854                 } else {
855                         /* requested name exists in the directory */
856                         rc = -EEXIST;
857                 }
858                 EXIT;
859                 break;
860         }
861         case S_IFLNK:{
862                 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
863                 if (IS_ERR(handle))
864                         GOTO(cleanup, rc = PTR_ERR(handle));
865                 if (rec->ur_tgt == NULL)        /* no target supplied */
866                         rc = -EINVAL;           /* -EPROTO? */
867                 else
868                         rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
869                 EXIT;
870                 break;
871         }
872         case S_IFCHR:
873         case S_IFBLK:
874         case S_IFIFO:
875         case S_IFSOCK:{
876                 int rdev = rec->ur_rdev;
877                 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
878                 if (IS_ERR(handle))
879                         GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
880                 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
881                 EXIT;
882                 break;
883         }
884         default:
885                 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
886                 dchild->d_fsdata = NULL;
887                 GOTO(cleanup, rc = -EINVAL);
888         }
889
890         /* In case we stored the desired inum in here, we want to clean up. */
891         if (dchild->d_fsdata == (void *)(unsigned long)id_ino(rec->ur_id2))
892                 dchild->d_fsdata = NULL;
893
894         if (rc) {
895                 CDEBUG(D_INODE, "error during create: %d\n", rc);
896                 GOTO(cleanup, rc);
897         } else if (dchild->d_inode) {
898                 struct iattr iattr;
899                 struct mds_body *body;
900                 struct inode *inode = dchild->d_inode;
901
902                 created = 1;
903                 iattr.ia_uid = rec->ur_fsuid;
904                 LTIME_S(iattr.ia_atime) = rec->ur_time;
905                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
906                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
907
908                 if (dir->i_mode & S_ISGID)
909                         iattr.ia_gid = dir->i_gid;
910                 else
911                         iattr.ia_gid = rec->ur_fsgid;
912
913                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
914                         ATTR_MTIME | ATTR_CTIME;
915
916                 if (id_ino(rec->ur_id2)) {
917                         LASSERT(id_ino(rec->ur_id2) == inode->i_ino);
918                         inode->i_generation = id_gen(rec->ur_id2);
919
920                         if (type != S_IFDIR) {
921                                 /* 
922                                  * updating inode self id, as inode already
923                                  * exists and we should make sure, its sid will
924                                  * be the same as we reveived.
925                                  */
926                                 down(&inode->i_sem);
927                                 rc = mds_update_inode_sid(obd, inode,
928                                                           handle, rec->ur_id2);
929                                 up(&inode->i_sem);
930                                 if (rc) {
931                                         CERROR("Can't update inode self id, "
932                                                "rc = %d.\n", rc);
933                                 }
934
935                                 /* 
936                                  * make sure, that fid is up-to-date.
937                                  */
938                                 mds_set_last_fid(obd, id_fid(rec->ur_id2));
939                         }
940                         
941                         /* dirtied and committed by the upcoming setattr. */
942                         CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
943                                inode->i_ino, inode->i_generation);
944                 } else {
945                         struct lustre_handle child_ino_lockh;
946
947                         CDEBUG(D_INODE, "created ino %lu with gen %x\n",
948                                inode->i_ino, inode->i_generation);
949
950                         if (type != S_IFDIR) {
951                                 struct lustre_id sid;
952                                 
953                                 /* 
954                                  * allocate new id for @inode if it is not dir,
955                                  * because for dir it was already done.
956                                  */
957                                 down(&inode->i_sem);
958                                 rc = mds_alloc_inode_sid(obd, inode,
959                                                          handle, &sid);
960                                 up(&inode->i_sem);
961                                 if (rc) {
962                                         CERROR("mds_alloc_inode_sid() failed, "
963                                                "inode %lu, rc %d\n", inode->i_ino,
964                                                rc);
965                                 }
966                         }
967
968                         if (rc == 0) {
969                                 /*
970                                  * the inode we were allocated may have just
971                                  * been freed by an unlink operation.  We take
972                                  * this lock to synchronize against the matching
973                                  * reply-ack-lock taken in unlink, to avoid
974                                  * replay problems if this reply makes it out to
975                                  * the client but the unlink's does not.  See
976                                  * bug 2029 for more detail.
977                                  */
978                                 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
979                                 if (rc != ELDLM_OK) {
980                                         CERROR("error locking for unlink/create sync: "
981                                                "%d\n", rc);
982                                 } else {
983                                         ldlm_lock_decref(&child_ino_lockh, LCK_EX);
984                                 }
985                         }
986                 }
987
988                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
989                 if (rc)
990                         CERROR("error on child setattr: rc = %d\n", rc);
991
992                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
993                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
994                 if (rc)
995                         CERROR("error on parent setattr: rc = %d\n", rc);
996                 else
997                         MD_COUNTER_INCREMENT(obd, create);
998
999                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1000                 mds_pack_inode2body(obd, body, inode, 1);
1001         }
1002
1003         EXIT;
1004 cleanup:
1005         err = mds_finish_transno(mds, dir, handle, req, rc, 0);
1006
1007         if (rc && created) {
1008                 /* Destroy the file we just created. This should not need extra
1009                  * journal credits, as we have already modified all of the
1010                  * blocks needed in order to create the file in the first
1011                  * place. */
1012                 switch (type) {
1013                 case S_IFDIR:
1014                         err = vfs_rmdir(dir, dchild);
1015                         if (err)
1016                                 CERROR("rmdir in error path: %d\n", err);
1017                         break;
1018                 default:
1019                         err = vfs_unlink(dir, dchild);
1020                         if (err)
1021                                 CERROR("unlink in error path: %d\n", err);
1022                         break;
1023                 }
1024         } else {
1025                 rc = err;
1026         }
1027         switch (cleanup_phase) {
1028         case 2: /* child dentry */
1029                 l_dput(dchild);
1030         case 1: /* locked parent dentry */
1031 #ifdef S_PDIROPS
1032                 if (lockh[1].cookie != 0)
1033                         ldlm_lock_decref(lockh + 1, parent_mode);
1034 #endif
1035                 if (rc) {
1036                         ldlm_lock_decref(lockh, LCK_PW);
1037                 } else {
1038                         ptlrpc_save_lock(req, lockh, LCK_PW);
1039                 }
1040                 l_dput(dparent);
1041         case 0:
1042                 break;
1043         default:
1044                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1045                 LBUG();
1046         }
1047         if (mea)
1048                 OBD_FREE(mea, mea_size);
1049         req->rq_status = rc;
1050         return 0;
1051 }
1052
1053 static inline int
1054 res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
1055        ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1056 {
1057         int i;
1058
1059         for (i = 0; i < RES_NAME_SIZE; i++) {
1060                 /* 
1061                  * this is needed to make zeroed res_id entries to be put at the
1062                  * end of list in *ordered_locks() .
1063                  */
1064                 if (res1->name[i] == 0 && res2->name[i] != 0)
1065                         return 1;
1066                 if (res2->name[i] == 0 && res1->name[i] != 0)
1067                         return 0;
1068                 if (res1->name[i] > res2->name[i])
1069                         return 1;
1070                 if (res1->name[i] < res2->name[i])
1071                         return 0;
1072         }
1073
1074         if (!p1 || !p2)
1075                 return 0;
1076
1077         if (memcmp(p1, p2, sizeof(*p1)) < 0)
1078                 return 1;
1079
1080         return 0;
1081 }
1082
1083 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1084  * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1085  * because they take the place of local semaphores.
1086  *
1087  * One or two locks are taken in numerical order.  A res_id->name[0] of 0 means
1088  * no lock is taken for that res_id.  Must be at least one non-zero res_id. */
1089 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
1090                           struct lustre_handle *p1_lockh, int p1_lock_mode,
1091                           ldlm_policy_data_t *p1_policy,
1092                           struct ldlm_res_id *p2_res_id,
1093                           struct lustre_handle *p2_lockh, int p2_lock_mode,
1094                           ldlm_policy_data_t *p2_policy)
1095 {
1096         int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1097         struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1098         struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1099         ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
1100         int rc, flags;
1101         ENTRY;
1102
1103         LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1104
1105         CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1106                res_id[0]->name[0], res_id[1]->name[0]);
1107
1108         if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1109                 handles[1] = p1_lockh;
1110                 handles[0] = p2_lockh;
1111                 res_id[1] = p1_res_id;
1112                 res_id[0] = p2_res_id;
1113                 lock_modes[1] = p1_lock_mode;
1114                 lock_modes[0] = p2_lock_mode;
1115                 policies[1] = p1_policy;
1116                 policies[0] = p2_policy;
1117         }
1118
1119         CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1120                res_id[0]->name[0], res_id[1]->name[0]);
1121
1122         flags = LDLM_FL_LOCAL_ONLY;
1123         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1124                               LDLM_IBITS, policies[0], lock_modes[0], &flags,
1125                               mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1126                               NULL, 0, NULL, handles[0]);
1127         if (rc != ELDLM_OK)
1128                 RETURN(-EIO);
1129         ldlm_lock_dump_handle(D_OTHER, handles[0]);
1130
1131         if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1132             (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1133                 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1134                 ldlm_lock_addref(handles[1], lock_modes[1]);
1135         } else if (res_id[1]->name[0] != 0) {
1136                 flags = LDLM_FL_LOCAL_ONLY;
1137                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1138                                       *res_id[1], LDLM_IBITS, policies[1],
1139                                       lock_modes[1], &flags, mds_blocking_ast,
1140                                       ldlm_completion_ast, NULL, NULL, NULL, 0,
1141                                       NULL, handles[1]);
1142                 if (rc != ELDLM_OK) {
1143                         ldlm_lock_decref(handles[0], lock_modes[0]);
1144                         RETURN(-EIO);
1145                 }
1146                 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1147         }
1148
1149         RETURN(0);
1150 }
1151
1152 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1153                            struct lustre_handle *p1_lockh, int p1_lock_mode,
1154                            ldlm_policy_data_t *p1_policy,
1155                            struct ldlm_res_id *p2_res_id,
1156                            struct lustre_handle *p2_lockh, int p2_lock_mode,
1157                            ldlm_policy_data_t *p2_policy,
1158                            struct ldlm_res_id *c1_res_id,
1159                            struct lustre_handle *c1_lockh, int c1_lock_mode,
1160                            ldlm_policy_data_t *c1_policy,
1161                            struct ldlm_res_id *c2_res_id,
1162                            struct lustre_handle *c2_lockh, int c2_lock_mode,
1163                            ldlm_policy_data_t *c2_policy)
1164 {
1165         struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1166                                           c1_res_id, c2_res_id };
1167         struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1168                                                  c1_lockh, c2_lockh };
1169         int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1170                               c1_lock_mode, c2_lock_mode };
1171         ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1172                                             c1_policy, c2_policy};
1173         int rc, i, j, sorted, flags;
1174         ENTRY;
1175
1176         CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1177                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1178                res_id[3]->name[0]);
1179
1180         /* 
1181          * simple insertion sort - we have at most 4 elements. Note, that zeroed
1182          * res_id should be at the end of list after sorting is finished.
1183          */
1184         for (i = 1; i < 4; i++) {
1185                 j = i - 1;
1186                 dlm_handles[4] = dlm_handles[i];
1187                 res_id[4] = res_id[i];
1188                 lock_modes[4] = lock_modes[i];
1189                 policies[4] = policies[i];
1190
1191                 sorted = 0;
1192                 do {
1193                         if (res_gt(res_id[j], res_id[4], policies[j],
1194                                    policies[4])) {
1195                                 dlm_handles[j + 1] = dlm_handles[j];
1196                                 res_id[j + 1] = res_id[j];
1197                                 lock_modes[j + 1] = lock_modes[j];
1198                                 policies[j + 1] = policies[j];
1199                                 j--;
1200                         } else {
1201                                 sorted = 1;
1202                         }
1203                 } while (j >= 0 && !sorted);
1204
1205                 dlm_handles[j + 1] = dlm_handles[4];
1206                 res_id[j + 1] = res_id[4];
1207                 lock_modes[j + 1] = lock_modes[4];
1208                 policies[j + 1] = policies[4];
1209         }
1210
1211         CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1212                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1213                res_id[3]->name[0]);
1214
1215         /* XXX we could send ASTs on all these locks first before blocking? */
1216         for (i = 0; i < 4; i++) {
1217                 flags = 0;
1218
1219                 /* 
1220                  * nevertheless zeroed res_ids should be at the end of list, and
1221                  * could use break here, I think, that it is more correctly for
1222                  * clear understanding of code to have continue here, as it
1223                  * clearly means, that zeroed res_id should be skipped and does
1224                  * not mean, that if we meet zeroed res_id we should stop
1225                  * locking loop.
1226                  */
1227                 if (res_id[i]->name[0] == 0)
1228                         continue;
1229                 
1230                 if (i != 0 &&
1231                     !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1232                     (policies[i]->l_inodebits.bits &
1233                      policies[i-1]->l_inodebits.bits) ) {
1234                         memcpy(dlm_handles[i], dlm_handles[i-1],
1235                                sizeof(*(dlm_handles[i])));
1236                         ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1237                 } else {
1238                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1239                                               *res_id[i], LDLM_IBITS,
1240                                               policies[i],
1241                                               lock_modes[i], &flags,
1242                                               mds_blocking_ast,
1243                                               ldlm_completion_ast, NULL, NULL,
1244                                               NULL, 0, NULL, dlm_handles[i]);
1245                         if (rc != ELDLM_OK)
1246                                 GOTO(out_err, rc = -EIO);
1247                         ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1248                 }
1249         }
1250
1251         RETURN(0);
1252 out_err:
1253         while (i-- > 0)
1254                 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1255
1256         return rc;
1257 }
1258
1259 /* In the unlikely case that the child changed while we were waiting
1260  * on the lock, we need to drop the lock on the old child and either:
1261  * - if the child has a lower resource name, then we have to also
1262  *   drop the parent lock and regain the locks in the right order
1263  * - in the rename case, if the child has a lower resource name than one of
1264  *   the other parent/child resources (maxres) we also need to reget the locks
1265  * - if the child has a higher resource name (this is the common case)
1266  *   we can just get the lock on the new child (still in lock order)
1267  *
1268  * Returns 0 if the child did not change or if it changed but could be locked.
1269  * Returns 1 if the child changed and we need to re-lock (no locks held).
1270  * Returns -ve error with a valid dchild (no locks held). */
1271 static int mds_verify_child(struct obd_device *obd,
1272                             struct ldlm_res_id *parent_res_id,
1273                             struct lustre_handle *parent_lockh,
1274                             struct dentry *dparent, int parent_mode,
1275                             struct ldlm_res_id *child_res_id,
1276                             struct lustre_handle *child_lockh,
1277                             struct dentry **dchildp, int child_mode,
1278                             ldlm_policy_data_t *child_policy,
1279                             const char *name, int namelen,
1280                             struct ldlm_res_id *maxres,
1281                             unsigned long child_ino,
1282                             __u32 child_gen)
1283 {
1284         struct lustre_id sid;
1285         struct dentry *vchild, *dchild = *dchildp;
1286         int rc = 0, cleanup_phase = 2; /* parent, child locks */
1287         ENTRY;
1288
1289         vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1290         if (IS_ERR(vchild))
1291                 GOTO(cleanup, rc = PTR_ERR(vchild));
1292
1293         if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1294                 if (child_gen == vchild->d_generation &&
1295                     child_ino == vchild->d_inum) {
1296                         if (dchild)
1297                                 l_dput(dchild);
1298                         *dchildp = vchild;
1299                         RETURN(0);
1300                 }
1301                 goto changed;
1302         }
1303
1304         if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1305                    (vchild->d_inode != NULL &&
1306                     child_gen == vchild->d_inode->i_generation &&
1307                     child_ino == vchild->d_inode->i_ino))) {
1308                 if (dchild)
1309                         l_dput(dchild);
1310                 *dchildp = vchild;
1311                 RETURN(0);
1312         }
1313
1314 changed:
1315         CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1316                vchild->d_inode, dchild ? dchild->d_inode : 0,
1317                vchild->d_inode ? vchild->d_inode->i_ino : 0,
1318                child_res_id->name[0]);
1319
1320         if (child_res_id->name[0] != 0)
1321                 ldlm_lock_decref(child_lockh, child_mode);
1322         if (dchild)
1323                 l_dput(dchild);
1324
1325         cleanup_phase = 1; /* parent lock only */
1326         *dchildp = dchild = vchild;
1327
1328         if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1329                 int flags = 0;
1330                 
1331                 if (dchild->d_inode) {
1332                         down(&dchild->d_inode->i_sem);
1333                         rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
1334                         up(&dchild->d_inode->i_sem);
1335                         if (rc) {
1336                                 CERROR("Can't read inode self id, inode %lu,"
1337                                        " rc %d\n",  dchild->d_inode->i_ino, rc);
1338                                 GOTO(cleanup, rc);
1339                         }
1340                         child_res_id->name[0] = id_fid(&sid);
1341                         child_res_id->name[1] = id_group(&sid);
1342                 } else {
1343                         child_res_id->name[0] = dchild->d_fid;
1344                         child_res_id->name[1] = dchild->d_mdsnum;
1345                 }
1346
1347                 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1348                     res_gt(maxres, child_res_id, NULL, NULL)) {
1349                         CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1350                                child_res_id->name[0], parent_res_id->name[0],
1351                                maxres->name[0]);
1352                         GOTO(cleanup, rc = 1);
1353                 }
1354
1355                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1356                                       *child_res_id, LDLM_IBITS, child_policy,
1357                                       child_mode, &flags, mds_blocking_ast,
1358                                       ldlm_completion_ast, NULL, NULL, NULL, 0,
1359                                       NULL, child_lockh);
1360                 if (rc != ELDLM_OK)
1361                         GOTO(cleanup, rc = -EIO);
1362
1363         } else {
1364                 memset(child_res_id, 0, sizeof(*child_res_id));
1365         }
1366
1367         EXIT;
1368 cleanup:
1369         if (rc) {
1370                 switch(cleanup_phase) {
1371                 case 2:
1372                         if (child_res_id->name[0] != 0)
1373                                 ldlm_lock_decref(child_lockh, child_mode);
1374                 case 1:
1375                         ldlm_lock_decref(parent_lockh, parent_mode);
1376                 }
1377         }
1378         return rc;
1379 }
1380
1381 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1382                                 struct lustre_id *id,
1383                                 struct lustre_handle *parent_lockh,
1384                                 struct dentry **dparentp, int parent_mode,
1385                                 __u64 parent_lockpart, int *update_mode,
1386                                 char *name, int namelen,
1387                                 struct lustre_handle *child_lockh,
1388                                 struct dentry **dchildp, int child_mode,
1389                                 __u64 child_lockpart)
1390 {
1391         ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1392         ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1393         struct ldlm_res_id parent_res_id = { .name = {0} };
1394         struct ldlm_res_id child_res_id = { .name = {0} };
1395         int rc = 0, cleanup_phase = 0;
1396         unsigned long child_ino;
1397         struct lustre_id sid;
1398         __u32 child_gen = 0;
1399         struct inode *inode;
1400         ENTRY;
1401
1402         /* Step 1: Lookup parent */
1403         *dparentp = mds_id2dentry(obd, id, NULL);
1404         if (IS_ERR(*dparentp)) {
1405                 rc = PTR_ERR(*dparentp);
1406                 *dparentp = NULL;
1407                 RETURN(rc);
1408         }
1409
1410         CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1411                (*dparentp)->d_inode->i_ino, name);
1412
1413         parent_res_id.name[0] = id_fid(id);
1414         parent_res_id.name[1] = id_group(id);
1415         
1416 #ifdef S_PDIROPS
1417         parent_lockh[1].cookie = 0;
1418         if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1419                 struct ldlm_res_id res_id = { .name = {0} };
1420                 ldlm_policy_data_t policy;
1421                 int flags = 0;
1422
1423                 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1424                 if (*update_mode) {
1425                         res_id.name[0] = id_fid(id);
1426                         res_id.name[1] = id_group(id);
1427                         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1428
1429                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1430                                               res_id, LDLM_IBITS, &policy,
1431                                               *update_mode, &flags,
1432                                               mds_blocking_ast,
1433                                               ldlm_completion_ast,
1434                                               NULL, NULL, NULL, 0, NULL,
1435                                               parent_lockh + 1);
1436                         if (rc != ELDLM_OK)
1437                                 RETURN(-ENOLCK);
1438                 }
1439
1440                 parent_res_id.name[2] = full_name_hash(name, namelen - 1);
1441                 
1442                 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1443                        (unsigned long)id_fid(id), (unsigned long)id_group(id),
1444                        parent_res_id.name[2]);
1445         }
1446 #endif
1447
1448         cleanup_phase = 1; /* parent dentry */
1449
1450         /* Step 2: Lookup child (without DLM lock, to get resource name) */
1451         *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1452         if (IS_ERR(*dchildp)) {
1453                 rc = PTR_ERR(*dchildp);
1454                 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1455                 GOTO(cleanup, rc);
1456         }
1457
1458         if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1459                 /*
1460                  * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1461                  * lock. Drop possible UPDATE lock!
1462                  */
1463                 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1464                 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1465
1466                 child_res_id.name[0] = (*dchildp)->d_fid;
1467                 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1468                 child_gen = (*dchildp)->d_generation;
1469                 child_ino = (*dchildp)->d_inum;
1470                 goto retry_locks;
1471         }
1472
1473         inode = (*dchildp)->d_inode;
1474         if (inode != NULL)
1475                 inode = igrab(inode);
1476         if (inode == NULL)
1477                 goto retry_locks;
1478
1479         down(&inode->i_sem);
1480         rc = mds_read_inode_sid(obd, inode, &sid);
1481         up(&inode->i_sem);
1482         if (rc) {
1483                 CERROR("Can't read inode self id, inode %lu, "
1484                        "rc %d\n", inode->i_ino, rc);
1485                 iput(inode);
1486                 GOTO(cleanup, rc);
1487         }
1488         
1489         child_res_id.name[0] = id_fid(&sid);
1490         child_res_id.name[1] = id_group(&sid);
1491         child_gen = inode->i_generation;
1492         child_ino = inode->i_ino;
1493         iput(inode);
1494
1495 retry_locks:
1496         cleanup_phase = 2; /* child dentry */
1497
1498         /* Step 3: Lock parent and child in resource order.  If child doesn't
1499          * exist, we still have to lock the parent and re-lookup. */
1500         rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1501                                    &parent_policy, &child_res_id, child_lockh,
1502                                    child_mode, &child_policy);
1503         if (rc)
1504                 GOTO(cleanup, rc);
1505
1506         if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1507                 cleanup_phase = 4; /* child lock */
1508         else
1509                 cleanup_phase = 3; /* parent lock */
1510
1511         /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1512         rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1513                               parent_mode, &child_res_id, child_lockh, 
1514                               dchildp, child_mode, &child_policy,
1515                               name, namelen, &parent_res_id,
1516                               child_ino, child_gen);
1517         if (rc > 0)
1518                 goto retry_locks;
1519         if (rc < 0) {
1520                 cleanup_phase = 3;
1521                 GOTO(cleanup, rc);
1522         }
1523
1524         EXIT;
1525 cleanup:
1526         if (rc) {
1527                 switch (cleanup_phase) {
1528                 case 4:
1529                         ldlm_lock_decref(child_lockh, child_mode);
1530                 case 3:
1531                         ldlm_lock_decref(parent_lockh, parent_mode);
1532                 case 2:
1533                         l_dput(*dchildp);
1534                 case 1:
1535 #ifdef S_PDIROPS
1536                         if (parent_lockh[1].cookie)
1537                                 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1538 #endif
1539                         l_dput(*dparentp);
1540                 }
1541         }
1542         return rc;
1543 }
1544
1545 void mds_reconstruct_generic(struct ptlrpc_request *req)
1546 {
1547         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1548         mds_req_from_mcd(req, med->med_mcd);
1549 }
1550
1551 /* If we are unlinking an open file/dir (i.e. creating an orphan) then
1552  * we instead link the inode into the PENDING directory until it is
1553  * finally released.  We can't simply call mds_reint_rename() or some
1554  * part thereof, because we don't have the inode to check for link
1555  * count/open status until after it is locked.
1556  *
1557  * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1558  * before starting journal transaction.
1559  *
1560  * returns 1 on success
1561  * returns 0 if we lost a race and didn't make a new link
1562  * returns negative on error
1563  */
1564 static int mds_orphan_add_link(struct mds_update_record *rec,
1565                                struct obd_device *obd, struct dentry *dentry)
1566 {
1567         struct mds_obd *mds = &obd->u.mds;
1568         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1569         struct inode *inode = dentry->d_inode;
1570         struct dentry *pending_child;
1571         char idname[LL_ID_NAMELEN];
1572         int idlen = 0, rc, mode;
1573         ENTRY;
1574
1575         LASSERT(inode != NULL);
1576         LASSERT(!mds_inode_is_orphan(inode));
1577 #ifndef HAVE_I_ALLOC_SEM
1578         LASSERT(down_trylock(&inode->i_sem) != 0);
1579 #endif
1580         LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1581
1582         idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1583
1584         CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1585                mds_orphan_open_count(inode), inode->i_nlink,
1586                S_ISDIR(inode->i_mode) ? "dir" :
1587                S_ISREG(inode->i_mode) ? "file" : "other",
1588                rec->ur_name, idname);
1589
1590         if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1591                 RETURN(0);
1592
1593         pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1594         if (IS_ERR(pending_child))
1595                 RETURN(PTR_ERR(pending_child));
1596
1597         if (pending_child->d_inode != NULL) {
1598                 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1599                 LASSERT(pending_child->d_inode == inode);
1600                 GOTO(out_dput, rc = 0);
1601         }
1602
1603         /* link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG
1604          * for linking and return real mode back then -bzzz */
1605         mode = inode->i_mode;
1606         inode->i_mode = S_IFREG;
1607         rc = vfs_link(dentry, pending_dir, pending_child);
1608         if (rc)
1609                 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1610                        rec->ur_name, rc);
1611         else
1612                 mds_inode_set_orphan(inode);
1613
1614         /* return mode and correct i_nlink if inode is directory */
1615         inode->i_mode = mode;
1616         LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1617                  S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1618                  inode->i_nlink);
1619         if (S_ISDIR(mode)) {
1620                 inode->i_nlink++;
1621                 pending_dir->i_nlink++;
1622                 mark_inode_dirty(inode);
1623                 mark_inode_dirty(pending_dir);
1624         }
1625
1626         EXIT;
1627 out_dput:
1628         l_dput(pending_child);
1629         return rc;
1630 }
1631
1632 int mds_create_local_dentry(struct mds_update_record *rec,
1633                             struct obd_device *obd)
1634 {
1635         struct mds_obd *mds = &obd->u.mds;
1636         struct inode *id_dir = mds->mds_id_dir->d_inode;
1637         int idlen = 0, rc, cleanup_phase = 0;
1638         struct dentry *new_child = NULL;
1639         char *idname = rec->ur_name;
1640         struct dentry *child = NULL;
1641         struct lustre_handle lockh[2] = {{0}, {0}};
1642         struct lustre_id sid;
1643         void *handle;
1644         ENTRY;
1645
1646         down(&id_dir->i_sem);
1647         idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1648                           id_gen(rec->ur_id1));
1649         
1650         CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1651                idname, OLID4(rec->ur_id1));
1652
1653         new_child = ll_lookup_one_len(idname, mds->mds_id_dir, 
1654                                       idlen);
1655         up(&id_dir->i_sem);
1656         if (IS_ERR(new_child)) {
1657                 CERROR("can't lookup %s: %d\n", idname,
1658                        (int) PTR_ERR(new_child));
1659                 GOTO(cleanup, rc = PTR_ERR(new_child));
1660         }
1661         cleanup_phase = 1;
1662
1663         down(&id_dir->i_sem);
1664         rc = mds_read_inode_sid(obd, id_dir, &sid);
1665         up(&id_dir->i_sem);
1666         if (rc) {
1667                 CERROR("Can't read inode self id, inode %lu, "
1668                        "rc %d\n", id_dir->i_ino, rc);
1669                 GOTO(cleanup, rc);
1670         }
1671         
1672         if (new_child->d_inode != NULL) {
1673                 /* nice. we've already have local dentry! */
1674                 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n", 
1675                        (unsigned)new_child->d_inode->i_ino,
1676                        (unsigned)new_child->d_inode->i_generation);
1677                 
1678                 id_ino(rec->ur_id1) = id_dir->i_ino;
1679                 id_gen(rec->ur_id1) = id_dir->i_generation;
1680                 rec->ur_namelen = idlen + 1;
1681
1682                 id_fid(rec->ur_id1) = id_fid(&sid);
1683                 id_group(rec->ur_id1) = id_group(&sid);
1684                 
1685                 GOTO(cleanup, rc = 0);
1686         }
1687
1688         /* new, local dentry will be added soon. we need no aliases here */
1689         d_drop(new_child);
1690
1691         if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1692                 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1693         } else {
1694                 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1695                                              LCK_EX, lockh, NULL, NULL, 0,
1696                                              MDS_INODELOCK_UPDATE);
1697         }
1698
1699         if (IS_ERR(child)) {
1700                 rc = PTR_ERR(child);
1701                 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1702                         CERROR("can't get victim: %d\n", rc);
1703                 GOTO(cleanup, rc);
1704         }
1705         cleanup_phase = 2;
1706
1707         handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
1708         if (IS_ERR(handle))
1709                 GOTO(cleanup, rc = PTR_ERR(handle));
1710
1711         rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
1712                                   idlen, id_ino(rec->ur_id1),
1713                                   id_gen(rec->ur_id1), mds->mds_num,
1714                                   id_fid(rec->ur_id1));
1715         if (rc)
1716                 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1717                        (unsigned long)child->d_inode->i_ino,
1718                        (unsigned long)child->d_inode->i_generation, rc);
1719         else {
1720                 if (S_ISDIR(child->d_inode->i_mode)) {
1721                         id_dir->i_nlink++;
1722                         mark_inode_dirty(id_dir);
1723                 }
1724                 mark_inode_dirty(child->d_inode);
1725         }
1726         fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
1727
1728         id_ino(rec->ur_id1) = id_dir->i_ino;
1729         id_gen(rec->ur_id1) = id_dir->i_generation;
1730         rec->ur_namelen = idlen + 1;
1731
1732         id_fid(rec->ur_id1) = id_fid(&sid);
1733         id_group(rec->ur_id1) = id_group(&sid);
1734
1735         EXIT;
1736 cleanup:
1737         switch(cleanup_phase) {
1738                 case 2:
1739                         if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1740                                 ldlm_lock_decref(lockh, LCK_EX);
1741                         dput(child);
1742                 case 1:
1743                         dput(new_child);
1744                 case 0:
1745                        break; 
1746         }
1747         return rc;
1748 }
1749
1750 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1751                                  struct ptlrpc_request *slave)
1752 {
1753         void *cookie, *cookie2;
1754         struct mds_body *body2;
1755         struct mds_body *body;
1756         void *ea, *ea2;
1757         ENTRY;
1758
1759         body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1760         LASSERT(body != NULL);
1761
1762         body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1763         LASSERT(body2 != NULL);
1764
1765         if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
1766                 RETURN(0);
1767
1768         memcpy(body2, body, sizeof(*body));
1769         body2->valid &= ~OBD_MD_FLCOOKIE;
1770
1771         if (!(body->valid & OBD_MD_FLEASIZE) &&
1772             !(body->valid & OBD_MD_FLDIREA))
1773                 RETURN(0);
1774
1775         if (body->eadatasize == 0) {
1776                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1777                 RETURN(0);
1778         }
1779
1780         LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1781         
1782         ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1783         LASSERT(ea != NULL);
1784         
1785         ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1786         LASSERT(ea2 != NULL);
1787
1788         memcpy(ea2, ea, body->eadatasize);
1789
1790         if (body->valid & OBD_MD_FLCOOKIE) {
1791                 LASSERT(master->rq_repmsg->buflens[2] >=
1792                                 slave->rq_repmsg->buflens[2]);
1793                 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1794                                 slave->rq_repmsg->buflens[2]);
1795                 LASSERT(cookie != NULL);
1796
1797                 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1798                                 master->rq_repmsg->buflens[2]);
1799                 LASSERT(cookie2 != NULL);
1800                 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1801                 body2->valid |= OBD_MD_FLCOOKIE;
1802         }
1803         RETURN(0);
1804 }
1805
1806 static int mds_reint_unlink_remote(struct mds_update_record *rec,
1807                                    int offset, struct ptlrpc_request *req,
1808                                    struct lustre_handle *parent_lockh,
1809                                    int update_mode, struct dentry *dparent,
1810                                    struct lustre_handle *child_lockh,
1811                                    struct dentry *dchild)
1812 {
1813         struct obd_device *obd = req->rq_export->exp_obd;
1814         struct mds_obd *mds = mds_req2mds(req);
1815         struct ptlrpc_request *request = NULL;
1816         int rc = 0, cleanup_phase = 0;
1817         struct mdc_op_data op_data;
1818         void *handle;
1819         ENTRY;
1820
1821         LASSERT(offset == 1 || offset == 3);
1822
1823         /* time to drop i_nlink on remote MDS */
1824         memset(&op_data, 0, sizeof(op_data));
1825         mds_pack_dentry2id(obd, &op_data.id1, dchild, 1);
1826         op_data.create_mode = rec->ur_mode;
1827
1828         DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode "DLID4")",
1829                   rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data.id1));
1830         
1831         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1832                 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode "DLID4")",
1833                           rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data.id1));
1834         }
1835
1836         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1837                 op_data.create_mode |= MDS_MODE_REPLAY;
1838         
1839         rc = md_unlink(mds->mds_lmv_exp, &op_data, &request);
1840         cleanup_phase = 2;
1841
1842         if (request) {
1843                 if (rc == 0)
1844                         mds_copy_unlink_reply(req, request);
1845                 ptlrpc_req_finished(request);
1846         }
1847
1848         if (rc == 0) {
1849                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1850                                       NULL);
1851                 if (IS_ERR(handle))
1852                         GOTO(cleanup, rc = PTR_ERR(handle));
1853                 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
1854                 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1855                                         rc, 0);
1856         }
1857         EXIT;
1858 cleanup:
1859         req->rq_status = rc;
1860
1861 #ifdef S_PDIROPS
1862         if (parent_lockh[1].cookie != 0)
1863                 ldlm_lock_decref(parent_lockh + 1, update_mode);
1864 #endif
1865         ldlm_lock_decref(child_lockh, LCK_EX);
1866         if (rc)
1867                 ldlm_lock_decref(parent_lockh, LCK_PW);
1868         else
1869                 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1870         l_dput(dchild);
1871         l_dput(dparent);
1872
1873         return 0;
1874 }
1875
1876 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1877                             struct ptlrpc_request *req,
1878                             struct lustre_handle *lh)
1879 {
1880         struct dentry *dparent = NULL, *dchild;
1881         struct mds_obd *mds = mds_req2mds(req);
1882         struct obd_device *obd = req->rq_export->exp_obd;
1883         struct mds_body *body = NULL;
1884         struct inode *child_inode = NULL;
1885         struct lustre_handle parent_lockh[2] = {{0}, {0}}; 
1886         struct lustre_handle child_lockh = {0};
1887         struct lustre_handle child_reuse_lockh = {0};
1888         struct lustre_handle *slave_lockh = NULL;
1889         char idname[LL_ID_NAMELEN];
1890         struct llog_create_locks *lcl = NULL;
1891         void *handle = NULL;
1892         int rc = 0, cleanup_phase = 0;
1893         int unlink_by_id = 0;
1894         int update_mode;
1895         ENTRY;
1896
1897         LASSERT(offset == 1 || offset == 3);
1898
1899         DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1900                   id_ino(rec->ur_id1), id_gen(rec->ur_id1),
1901                   rec->ur_name);
1902
1903         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1904
1905         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1906                 DEBUG_REQ(D_HA, req, "unlink replay");
1907                 LASSERT(offset == 1); /* should not come from intent */
1908                 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
1909                        lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
1910                        req->rq_repmsg->buflens[2]);
1911         }
1912
1913         MD_COUNTER_INCREMENT(obd, unlink);
1914
1915         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1916                 GOTO(cleanup, rc = -ENOENT);
1917
1918         if (rec->ur_namelen == 1) {
1919                 /* this is request to drop i_nlink on local inode */
1920                 unlink_by_id = 1;
1921                 rec->ur_name = idname;
1922                 rc = mds_create_local_dentry(rec, obd);
1923                 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
1924                         DEBUG_REQ(D_HA, req,
1925                                   "drop nlink on inode "DLID4" (replay)",
1926                                   OLID4(rec->ur_id1));
1927                         req->rq_status = 0;
1928                         RETURN(0);
1929                 }
1930         }
1931
1932         if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1933                 /* master mds for directory asks slave removing inode is already
1934                  * locked */
1935                 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1936                                                LCK_PW, parent_lockh,
1937                                                &update_mode, rec->ur_name,
1938                                                rec->ur_namelen,
1939                                                MDS_INODELOCK_UPDATE);
1940                 if (IS_ERR(dparent))
1941                         GOTO(cleanup, rc = PTR_ERR(dparent));
1942                 dchild = ll_lookup_one_len(rec->ur_name, dparent, 
1943                                            rec->ur_namelen - 1);
1944                 if (IS_ERR(dchild))
1945                         GOTO(cleanup, rc = PTR_ERR(dchild));
1946                 child_lockh.cookie = 0;
1947                 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
1948                 LASSERT(dchild->d_inode != NULL);
1949                 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
1950         } else {
1951                 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
1952                                                  parent_lockh, &dparent,
1953                                                  LCK_PW, MDS_INODELOCK_UPDATE,
1954                                                  &update_mode, rec->ur_name,
1955                                                  rec->ur_namelen, &child_lockh,
1956                                                  &dchild, LCK_EX,
1957                                                  MDS_INODELOCK_LOOKUP |
1958                                                  MDS_INODELOCK_UPDATE);
1959         }
1960         if (rc)
1961                 GOTO(cleanup, rc);
1962
1963         if (dchild->d_flags & DCACHE_CROSS_REF) {
1964                 /* we should have parent lock only here */
1965                 LASSERT(unlink_by_id == 0);
1966                 LASSERT(dchild->d_mdsnum != mds->mds_num);
1967                 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
1968                                         update_mode, dparent, &child_lockh, dchild);
1969                 RETURN(0);
1970         }
1971
1972         cleanup_phase = 1; /* dchild, dparent, locks */
1973
1974         dget(dchild);
1975         child_inode = dchild->d_inode;
1976         if (child_inode == NULL) {
1977                 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1978                        dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
1979                 GOTO(cleanup, rc = -ENOENT);
1980         }
1981
1982         cleanup_phase = 2; /* dchild has a lock */
1983
1984         /* We have to do these checks ourselves, in case we are making an
1985          * orphan.  The client tells us whether rmdir() or unlink() was called,
1986          * so we need to return appropriate errors (bug 72).
1987          *
1988          * We don't have to check permissions, because vfs_rename (called from
1989          * mds_open_unlink_rename) also calls may_delete. */
1990         if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1991                 if (!S_ISDIR(child_inode->i_mode))
1992                         GOTO(cleanup, rc = -ENOTDIR);
1993         } else {
1994                 if (S_ISDIR(child_inode->i_mode))
1995                         GOTO(cleanup, rc = -EISDIR);
1996         }
1997
1998         /* handle splitted dir */
1999         rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
2000         if (rc)
2001                 GOTO(cleanup, rc);
2002
2003         /* Step 4: Get a lock on the ino to sync with creation WRT inode
2004          * reuse (see bug 2029). */
2005         rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
2006         if (rc != ELDLM_OK)
2007                 GOTO(cleanup, rc);
2008         cleanup_phase = 3; /* child inum lock */
2009
2010         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2011
2012         /* ldlm_reply in buf[0] if called via intent */
2013         if (offset == 3)
2014                 offset = 1;
2015         else
2016                 offset = 0;
2017
2018         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2019         LASSERT(body != NULL);
2020
2021         /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2022         DOWN_READ_I_ALLOC_SEM(child_inode);
2023         cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2024
2025         /* If this is potentially the last reference to this inode, get the
2026          * OBD EA data first so the client can destroy OST objects.  We
2027          * only do the object removal later if no open files/links remain. */
2028         if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2029             child_inode->i_nlink == 1) {
2030                 if (mds_orphan_open_count(child_inode) > 0) {
2031                         /* need to lock pending_dir before transaction */
2032                         down(&mds->mds_pending_dir->d_inode->i_sem);
2033                         cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2034                 } else if (S_ISREG(child_inode->i_mode)) {
2035                         mds_pack_inode2body(obd, body, child_inode, 0);
2036                         mds_pack_md(obd, req->rq_repmsg, offset + 1,
2037                                     body, child_inode, MDS_PACK_MD_LOCK);
2038                 }
2039         }
2040
2041         /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2042         switch (child_inode->i_mode & S_IFMT) {
2043         case S_IFDIR:
2044                 /* Drop any lingering child directories before we start our
2045                  * transaction, to avoid doing multiple inode dirty/delete
2046                  * in our compound transaction (bug 1321). */
2047                 shrink_dcache_parent(dchild);
2048                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2049                                       NULL);
2050                 if (IS_ERR(handle))
2051                         GOTO(cleanup, rc = PTR_ERR(handle));
2052                 rc = vfs_rmdir(dparent->d_inode, dchild);
2053                 break;
2054         case S_IFREG: {
2055 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2056                 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2057                                                         offset + 1, 0);
2058                 handle = fsfilt_start_log(obd, dparent->d_inode,
2059                                           FSFILT_OP_UNLINK, NULL,
2060                                           le32_to_cpu(lmm->lmm_stripe_count));
2061                 if (IS_ERR(handle))
2062                         GOTO(cleanup, rc = PTR_ERR(handle));
2063                 rc = vfs_unlink(dparent->d_inode, dchild);
2064                 break;
2065         }
2066         case S_IFLNK:
2067         case S_IFCHR:
2068         case S_IFBLK:
2069         case S_IFIFO:
2070         case S_IFSOCK:
2071                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2072                                       NULL);
2073                 if (IS_ERR(handle))
2074                         GOTO(cleanup, rc = PTR_ERR(handle));
2075                 rc = vfs_unlink(dparent->d_inode, dchild);
2076                 break;
2077         default:
2078                 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2079                        rec->ur_name);
2080                 LBUG();
2081                 GOTO(cleanup, rc = -EINVAL);
2082         }
2083
2084         if (rc == 0 && child_inode->i_nlink == 0) {
2085                 if (mds_orphan_open_count(child_inode) > 0)
2086                         rc = mds_orphan_add_link(rec, obd, dchild);
2087
2088                 if (rc == 1)
2089                         GOTO(cleanup, rc = 0);
2090
2091                 if (!S_ISREG(child_inode->i_mode))
2092                         GOTO(cleanup, rc);
2093
2094                 if (!(body->valid & OBD_MD_FLEASIZE)) {
2095                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2096                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
2097                 } else if (mds_log_op_unlink(obd, child_inode,
2098                                 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2099                                         req->rq_repmsg->buflens[offset + 1],
2100                                 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2101                                         req->rq_repmsg->buflens[offset+2], 
2102                                 &lcl) > 0){
2103                         body->valid |= OBD_MD_FLCOOKIE;
2104                 }
2105         }
2106
2107         GOTO(cleanup, rc);
2108
2109 cleanup:
2110         if (rc == 0) {
2111                 struct iattr iattr;
2112                 int err;
2113
2114                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2115                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2116                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2117
2118                 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2119                 if (err)
2120                         CERROR("error on parent setattr: rc = %d\n", err);
2121         }
2122         rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2123                                 handle, req, rc, 0);
2124         if (!rc)
2125                 (void)obd_set_info(mds->mds_lov_exp, strlen("unlinked"),
2126                                    "unlinked", 0, NULL);
2127         switch(cleanup_phase) {
2128         case 5: /* pending_dir semaphore */
2129                 up(&mds->mds_pending_dir->d_inode->i_sem);
2130         case 4: /* child inode semaphore */
2131                 UP_READ_I_ALLOC_SEM(child_inode);
2132                  /* handle splitted dir */
2133                 if (rc == 0) {
2134                         /* master directory can be non-empty or something else ... */
2135                         mds_unlink_slave_objs(obd, dchild);
2136                 }
2137                 if (lcl != NULL)
2138                         ptlrpc_save_llog_lock(req, lcl);
2139         case 3: /* child ino-reuse lock */
2140                 if (rc && body != NULL) {
2141                         // Don't unlink the OST objects if the MDS unlink failed
2142                         body->valid = 0;
2143                 }
2144                 if (rc)
2145                         ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2146                 else
2147                         ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2148         case 2: /* child lock */
2149                 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2150                 if (child_lockh.cookie)
2151                         ldlm_lock_decref(&child_lockh, LCK_EX);
2152         case 1: /* child and parent dentry, parent lock */
2153 #ifdef S_PDIROPS
2154                 if (parent_lockh[1].cookie != 0)
2155                         ldlm_lock_decref(parent_lockh + 1, update_mode);
2156 #endif
2157                 if (rc)
2158                         ldlm_lock_decref(parent_lockh, LCK_PW);
2159                 else
2160                         ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2161                 l_dput(dchild);
2162                 l_dput(dchild);
2163                 l_dput(dparent);
2164         case 0:
2165                 break;
2166         default:
2167                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2168                 LBUG();
2169         }
2170         req->rq_status = rc;
2171         return 0;
2172 }
2173
2174 /*
2175  * to service requests from remote MDS to increment i_nlink
2176  */
2177 static int mds_reint_link_acquire(struct mds_update_record *rec,
2178                                   int offset, struct ptlrpc_request *req,
2179                                   struct lustre_handle *lh)
2180 {
2181         struct obd_device *obd = req->rq_export->exp_obd;
2182         struct ldlm_res_id src_res_id = { .name = {0} };
2183         struct lustre_handle *handle = NULL, src_lockh = {0};
2184         struct mds_obd *mds = mds_req2mds(req);
2185         int rc = 0, cleanup_phase = 0;
2186         struct dentry *de_src = NULL;
2187         ldlm_policy_data_t policy;
2188         int flags = 0;
2189         ENTRY;
2190
2191         DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2192                   obd->obd_name, OLID4(rec->ur_id1));
2193
2194         /* Step 1: Lookup the source inode and target directory by ID */
2195         de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2196         if (IS_ERR(de_src))
2197                 GOTO(cleanup, rc = PTR_ERR(de_src));
2198         cleanup_phase = 1; /* source dentry */
2199
2200         src_res_id.name[0] = id_fid(rec->ur_id1);
2201         src_res_id.name[1] = id_group(rec->ur_id1);
2202         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2203
2204         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2205                               src_res_id, LDLM_IBITS, &policy,
2206                               LCK_EX, &flags, mds_blocking_ast,
2207                               ldlm_completion_ast, NULL, NULL,
2208                               NULL, 0, NULL, &src_lockh);
2209         if (rc != ELDLM_OK)
2210                 GOTO(cleanup, rc = -ENOLCK);
2211         cleanup_phase = 2; /* lock */
2212
2213         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2214
2215         handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2216         if (IS_ERR(handle)) {
2217                 rc = PTR_ERR(handle);
2218                 GOTO(cleanup, rc);
2219         }
2220         de_src->d_inode->i_nlink++;
2221         mark_inode_dirty(de_src->d_inode);
2222
2223 cleanup:
2224         rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2225                                         handle, req, rc, 0);
2226         EXIT;
2227         switch (cleanup_phase) {
2228                 case 2:
2229                         if (rc)
2230                                 ldlm_lock_decref(&src_lockh, LCK_EX);
2231                         else
2232                                 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2233                 case 1:
2234                         l_dput(de_src);
2235                 case 0:
2236                         break;
2237                 default:
2238                         CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2239                         LBUG();
2240         }
2241         req->rq_status = rc;
2242         return 0;
2243 }
2244
2245 /*
2246  * request to link to foreign inode:
2247  *  - acquire i_nlinks on this inode
2248  *  - add dentry
2249  */
2250 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2251                                     int offset, struct ptlrpc_request *req,
2252                                     struct lustre_handle *lh)
2253 {
2254         struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2255         struct obd_device *obd = req->rq_export->exp_obd;
2256         struct dentry *de_tgt_dir = NULL;
2257         struct mds_obd *mds = mds_req2mds(req);
2258         int rc = 0, cleanup_phase = 0;
2259         struct mdc_op_data op_data;
2260         struct ptlrpc_request *request = NULL;
2261         int update_mode;
2262         ENTRY;
2263
2264         DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2265                   ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2266                   OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2267                   OLID4(rec->ur_id1));
2268
2269         de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2270                                           tgt_dir_lockh, &update_mode,
2271                                           rec->ur_name, rec->ur_namelen - 1,
2272                                           MDS_INODELOCK_UPDATE);
2273         if (IS_ERR(de_tgt_dir))
2274                 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2275         cleanup_phase = 1;
2276
2277         op_data.id1 = *(rec->ur_id1);
2278         op_data.namelen = 0;
2279         op_data.name = NULL;
2280         rc = md_link(mds->mds_lmv_exp, &op_data, &request);
2281         if (rc)
2282                 GOTO(cleanup, rc);
2283
2284         cleanup_phase = 2;
2285         if (request)
2286                 ptlrpc_req_finished(request);
2287
2288         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2289
2290         handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2291         if (IS_ERR(handle)) {
2292                 rc = PTR_ERR(handle);
2293                 GOTO(cleanup, rc);
2294         }
2295         
2296         rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2297                                   rec->ur_namelen - 1, id_ino(rec->ur_id1),
2298                                   id_gen(rec->ur_id1), id_group(rec->ur_id1),
2299                                   id_fid(rec->ur_id1));
2300         cleanup_phase = 3;
2301
2302 cleanup:
2303         rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2304                                 handle, req, rc, 0);
2305         EXIT;
2306
2307         switch (cleanup_phase) {
2308                 case 3:
2309                         if (rc) {
2310                                 /* FIXME: drop i_nlink on remote inode here */
2311                                 CERROR("MUST drop drop i_nlink here\n");
2312                         }
2313                 case 2:
2314                 case 1:
2315                         if (rc) {
2316                                 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2317 #ifdef S_PDIROPS
2318                                 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2319 #endif
2320                         } else {
2321                                 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2322 #ifdef S_PDIROPS
2323                                 ptlrpc_save_lock(req, tgt_dir_lockh+1, update_mode);
2324 #endif
2325                         }
2326                         l_dput(de_tgt_dir);
2327                         break;
2328                 default:
2329                         CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2330                         LBUG();
2331         }
2332         req->rq_status = rc;
2333         return 0;
2334 }
2335
2336 static int mds_reint_link(struct mds_update_record *rec, int offset,
2337                           struct ptlrpc_request *req,
2338                           struct lustre_handle *lh)
2339 {
2340         struct obd_device *obd = req->rq_export->exp_obd;
2341         struct dentry *de_src = NULL;
2342         struct dentry *de_tgt_dir = NULL;
2343         struct dentry *dchild = NULL;
2344         struct mds_obd *mds = mds_req2mds(req);
2345         struct lustre_handle *handle = NULL;
2346         struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2347         struct ldlm_res_id src_res_id = { .name = {0} };
2348         struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2349         ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2350         ldlm_policy_data_t tgt_dir_policy =
2351                                        {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2352         int rc = 0, cleanup_phase = 0;
2353 #ifdef S_PDIROPS
2354         int update_mode = 0;
2355 #endif
2356         ENTRY;
2357
2358         LASSERT(offset == 1);
2359
2360         DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2361                   id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2362                   id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2363                   rec->ur_name);
2364
2365         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2366         MD_COUNTER_INCREMENT(obd, link);
2367         
2368 //      memset(tgt_dir_lockh, 0, 2*sizeof(tgt_dir_lockh[0]));
2369         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2370                 GOTO(cleanup, rc = -ENOENT);
2371
2372         if (id_group(rec->ur_id1) != mds->mds_num) {
2373                 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2374                 RETURN(rc);
2375         }
2376         
2377         if (rec->ur_namelen == 1) {
2378                 rc = mds_reint_link_acquire(rec, offset, req, lh);
2379                 RETURN(rc);
2380         }
2381
2382         /* Step 1: Lookup the source inode and target directory by ID */
2383         de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2384         if (IS_ERR(de_src))
2385                 GOTO(cleanup, rc = PTR_ERR(de_src));
2386
2387         cleanup_phase = 1; /* source dentry */
2388
2389         de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2390         if (IS_ERR(de_tgt_dir)) {
2391                 rc = PTR_ERR(de_tgt_dir);
2392                 de_tgt_dir = NULL;
2393                 GOTO(cleanup, rc);
2394         }
2395
2396         cleanup_phase = 2; /* target directory dentry */
2397
2398         CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2399                de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2400                rec->ur_name, de_src->d_inode->i_ino);
2401
2402         /* Step 2: Take the two locks */
2403         src_res_id.name[0] = id_fid(rec->ur_id1);
2404         src_res_id.name[1] = id_group(rec->ur_id1);
2405         tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2406         tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2407         
2408 #ifdef S_PDIROPS
2409         if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2410                 int flags = 0;
2411                 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2412                 if (update_mode) {
2413                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2414                                               tgt_dir_res_id, LDLM_IBITS,
2415                                               &src_policy, update_mode, &flags,
2416                                               mds_blocking_ast,
2417                                               ldlm_completion_ast, NULL, NULL,
2418                                               NULL, 0, NULL, tgt_dir_lockh + 1);
2419                         if (rc != ELDLM_OK)
2420                                 GOTO(cleanup, rc = -ENOLCK);
2421                 }
2422
2423                 tgt_dir_res_id.name[2] = full_name_hash(rec->ur_name,
2424                                                         rec->ur_namelen - 1);
2425                 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2426                        (unsigned long)id_fid(rec->ur_id2),
2427                        (unsigned long)id_group(rec->ur_id2),
2428                        tgt_dir_res_id.name[2]);
2429         }
2430 #endif
2431         rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2432                                    &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2433                                    LCK_EX, &tgt_dir_policy);
2434         if (rc)
2435                 GOTO(cleanup, rc);
2436
2437         cleanup_phase = 3; /* locks */
2438
2439         /* Step 3: Lookup the child */
2440         dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, 
2441                                    rec->ur_namelen - 1);
2442         if (IS_ERR(dchild)) {
2443                 rc = PTR_ERR(dchild);
2444                 if (rc != -EPERM && rc != -EACCES)
2445                         CERROR("child lookup error %d\n", rc);
2446                 GOTO(cleanup, rc);
2447         }
2448
2449         cleanup_phase = 4; /* child dentry */
2450
2451         if (dchild->d_inode) {
2452                 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2453                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
2454                 rc = -EEXIST;
2455                 GOTO(cleanup, rc);
2456         }
2457
2458         /* Step 4: Do it. */
2459         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2460
2461         handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2462         if (IS_ERR(handle)) {
2463                 rc = PTR_ERR(handle);
2464                 GOTO(cleanup, rc);
2465         }
2466
2467         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2468         if (rc && rc != -EPERM && rc != -EACCES)
2469                 CERROR("vfs_link error %d\n", rc);
2470 cleanup:
2471         rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2472                                 handle, req, rc, 0);
2473         EXIT;
2474
2475         switch (cleanup_phase) {
2476         case 4: /* child dentry */
2477                 l_dput(dchild);
2478         case 3: /* locks */
2479                 if (rc) {
2480                         ldlm_lock_decref(&src_lockh, LCK_EX);
2481                         ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2482                 } else {
2483                         ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2484                         ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2485                 }
2486         case 2: /* target dentry */
2487 #ifdef S_PDIROPS
2488                 if (tgt_dir_lockh[1].cookie && update_mode)
2489                         ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2490 #endif
2491                 if (de_tgt_dir)
2492                         l_dput(de_tgt_dir);
2493         case 1: /* source dentry */
2494                 l_dput(de_src);
2495         case 0:
2496                 break;
2497         default:
2498                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2499                 LBUG();
2500         }
2501         req->rq_status = rc;
2502         return 0;
2503 }
2504
2505 /* The idea here is that we need to get four locks in the end:
2506  * one on each parent directory, one on each child.  We need to take
2507  * these locks in some kind of order (to avoid deadlocks), and the order
2508  * I selected is "increasing resource number" order.  We need to look up
2509  * the children, however, before we know what the resource number(s) are.
2510  * Thus the following plan:
2511  *
2512  * 1,2. Look up the parents
2513  * 3,4. Look up the children
2514  * 5. Take locks on the parents and children, in order
2515  * 6. Verify that the children haven't changed since they were looked up
2516  *
2517  * If there was a race and the children changed since they were first looked
2518  * up, it is possible that mds_verify_child() will be able to just grab the
2519  * lock on the new child resource (if it has a higher resource than any other)
2520  * but we need to compare against not only its parent, but also against the
2521  * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2522  *
2523  * We need the fancy igrab() on the child inodes because we aren't holding a
2524  * lock on the parent after the lookup is done, so dentry->d_inode may change
2525  * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2526  */
2527 static int mds_get_parents_children_locked(struct obd_device *obd,
2528                                            struct mds_obd *mds,
2529                                            struct lustre_id *p1_id,
2530                                            struct dentry **de_srcdirp,
2531                                            struct lustre_id *p2_id,
2532                                            struct dentry **de_tgtdirp,
2533                                            int parent_mode,
2534                                            const char *old_name, int old_len,
2535                                            struct dentry **de_oldp,
2536                                            const char *new_name, int new_len,
2537                                            struct dentry **de_newp,
2538                                            struct lustre_handle *dlm_handles,
2539                                            int child_mode)
2540 {
2541         struct ldlm_res_id p1_res_id = { .name = {0} };
2542         struct ldlm_res_id p2_res_id = { .name = {0} };
2543         struct ldlm_res_id c1_res_id = { .name = {0} };
2544         struct ldlm_res_id c2_res_id = { .name = {0} };
2545         ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2546         /* Only dentry should change, but the inode itself would be
2547            intact otherwise */
2548         ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2549         /* If something is going to be replaced, both dentry and inode locks are
2550            needed */
2551         ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP|
2552                                                         MDS_INODELOCK_UPDATE}};
2553         struct ldlm_res_id *maxres_src, *maxres_tgt;
2554         struct inode *inode;
2555         __u32 child1_gen = 0;
2556         __u32 child2_gen = 0;
2557         unsigned long child1_ino;
2558         unsigned long child2_ino;
2559         int rc = 0, cleanup_phase = 0;
2560         ENTRY;
2561
2562         /* Step 1: Lookup the source directory */
2563         *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2564         if (IS_ERR(*de_srcdirp))
2565                 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2566
2567         cleanup_phase = 1; /* source directory dentry */
2568
2569         p1_res_id.name[0] = id_fid(p1_id);
2570         p1_res_id.name[1] = id_group(p1_id);
2571
2572         /* Step 2: Lookup the target directory */
2573         if (id_equal_stc(p1_id, p2_id)) {
2574                 *de_tgtdirp = dget(*de_srcdirp);
2575         } else {
2576                 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2577                 if (IS_ERR(*de_tgtdirp)) {
2578                         rc = PTR_ERR(*de_tgtdirp);
2579                         *de_tgtdirp = NULL;
2580                         GOTO(cleanup, rc);
2581                 }
2582         }
2583
2584         cleanup_phase = 2; /* target directory dentry */
2585
2586         p2_res_id.name[0] = id_fid(p2_id);
2587         p2_res_id.name[1] = id_group(p2_id);
2588
2589 #ifdef S_PDIROPS
2590         dlm_handles[5].cookie = 0;
2591         dlm_handles[6].cookie = 0;
2592         
2593         if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2594                 /*
2595                  * get a temp lock on just fid, group to flush client cache and
2596                  * to protect dirs from concurrent splitting.
2597                  */
2598                 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2599                                            LCK_PW, &p_policy, &p2_res_id,
2600                                            &dlm_handles[6], LCK_PW, &p_policy);
2601                 if (rc != ELDLM_OK)
2602                         GOTO(cleanup, rc);
2603                 
2604                 p1_res_id.name[2] = full_name_hash(old_name, old_len - 1);
2605                 p2_res_id.name[2] = full_name_hash(new_name, new_len - 1);
2606
2607                 CDEBUG(D_INFO, "take locks on "
2608                        LPX64":"LPX64":"LPX64", "LPX64":"LPX64":"LPX64"\n",
2609                        p1_res_id.name[0], p1_res_id.name[1], p1_res_id.name[2],
2610                        p2_res_id.name[0], p2_res_id.name[1], p2_res_id.name[2]);
2611         }
2612         cleanup_phase = 3;
2613 #endif
2614
2615         /* Step 3: Lookup the source child entry */
2616         *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp, 
2617                                      old_len - 1);
2618         if (IS_ERR(*de_oldp)) {
2619                 rc = PTR_ERR(*de_oldp);
2620                 CERROR("old child lookup error (%*s): %d\n",
2621                        old_len - 1, old_name, rc);
2622                 GOTO(cleanup, rc);
2623         }
2624
2625         cleanup_phase = 4; /* original name dentry */
2626
2627         inode = (*de_oldp)->d_inode;
2628         if (inode != NULL) {
2629                 struct lustre_id sid;
2630                 
2631                 inode = igrab(inode);
2632                 if (inode == NULL)
2633                         GOTO(cleanup, rc = -ENOENT);
2634
2635                 down(&inode->i_sem);
2636                 rc = mds_read_inode_sid(obd, inode, &sid);
2637                 up(&inode->i_sem);
2638                 if (rc) {
2639                         CERROR("Can't read inode self id, inode %lu, "
2640                                "rc %d\n", inode->i_ino, rc);
2641                         iput(inode);
2642                         GOTO(cleanup, rc);
2643                 }
2644
2645                 c1_res_id.name[0] = id_fid(&sid);
2646                 c1_res_id.name[1] = id_group(&sid);
2647                 child1_gen = inode->i_generation;
2648                 child1_ino = inode->i_ino;
2649                 iput(inode);
2650         } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2651                 c1_res_id.name[0] = (*de_oldp)->d_fid;
2652                 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2653                 child1_gen = (*de_oldp)->d_generation;
2654                 child1_ino = (*de_oldp)->d_inum;
2655         } else {
2656                 GOTO(cleanup, rc = -ENOENT);
2657         }
2658
2659         /* Step 4: Lookup the target child entry */
2660         *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp, 
2661                                      new_len - 1);
2662         if (IS_ERR(*de_newp)) {
2663                 rc = PTR_ERR(*de_newp);
2664                 CERROR("new child lookup error (%*s): %d\n",
2665                        old_len - 1, old_name, rc);
2666                 GOTO(cleanup, rc);
2667         }
2668
2669         cleanup_phase = 5; /* target dentry */
2670
2671         inode = (*de_newp)->d_inode;
2672         if (inode != NULL) {
2673                 struct lustre_id sid;
2674
2675                 inode = igrab(inode);
2676                 if (inode == NULL)
2677                         goto retry_locks;
2678
2679                 down(&inode->i_sem);
2680                 rc = mds_read_inode_sid(obd, inode, &sid);
2681                 up(&inode->i_sem);
2682                 if (rc) {
2683                         CERROR("Can't read inode self id, inode %lu, "
2684                                "rc %d\n", inode->i_ino, rc);
2685                         GOTO(cleanup, rc);
2686                 }
2687
2688                 c2_res_id.name[0] = id_fid(&sid);
2689                 c2_res_id.name[1] = id_group(&sid);
2690                 child2_gen = inode->i_generation;
2691                 child2_ino = inode->i_ino;
2692                 iput(inode);
2693         } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2694                 c2_res_id.name[0] = (*de_newp)->d_fid;
2695                 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
2696                 child2_gen = (*de_newp)->d_generation;
2697                 child2_ino = (*de_newp)->d_inum;
2698         }
2699
2700 retry_locks:
2701         /* Step 5: Take locks on the parents and child(ren) */
2702         maxres_src = &p1_res_id;
2703         maxres_tgt = &p2_res_id;
2704         cleanup_phase = 5; /* target dentry */
2705
2706         if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
2707                 maxres_src = &c1_res_id;
2708         if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
2709                 maxres_tgt = &c2_res_id;
2710
2711         rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
2712                                     &p_policy,
2713                                     &p2_res_id, &dlm_handles[1], parent_mode,
2714                                     &p_policy,
2715                                     &c1_res_id, &dlm_handles[2], child_mode,
2716                                     &c1_policy,
2717                                     &c2_res_id, &dlm_handles[3], child_mode,
2718                                     &c2_policy);
2719         if (rc)
2720                 GOTO(cleanup, rc);
2721
2722         cleanup_phase = 6; /* parent and child(ren) locks */
2723
2724         /* Step 6a: Re-lookup source child to verify it hasn't changed */
2725         rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2726                               parent_mode, &c1_res_id, &dlm_handles[2],
2727                               de_oldp, child_mode, &c1_policy, old_name,old_len,
2728                               maxres_tgt, child1_ino, child1_gen);
2729         if (rc) {
2730                 if (c2_res_id.name[0] != 0)
2731                         ldlm_lock_decref(&dlm_handles[3], child_mode);
2732                 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2733                 cleanup_phase = 5;
2734                 if (rc > 0)
2735                         goto retry_locks;
2736                 GOTO(cleanup, rc);
2737         }
2738
2739         if (!DENTRY_VALID(*de_oldp))
2740                 GOTO(cleanup, rc = -ENOENT);
2741
2742         /* Step 6b: Re-lookup target child to verify it hasn't changed */
2743         rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2744                               parent_mode, &c2_res_id, &dlm_handles[3],
2745                               de_newp, child_mode, &c2_policy, new_name,
2746                               new_len, maxres_src, child2_ino, child2_gen);
2747         if (rc) {
2748                 ldlm_lock_decref(&dlm_handles[2], child_mode);
2749                 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2750                 cleanup_phase = 5;
2751                 if (rc > 0)
2752                         goto retry_locks;
2753                 GOTO(cleanup, rc);
2754         }
2755
2756         EXIT;
2757 cleanup:
2758         if (rc) {
2759                 switch (cleanup_phase) {
2760                 case 6: /* child lock(s) */
2761                         if (c2_res_id.name[0] != 0)
2762                                 ldlm_lock_decref(&dlm_handles[3], child_mode);
2763                         if (c1_res_id.name[0] != 0)
2764                                 ldlm_lock_decref(&dlm_handles[2], child_mode);
2765                         if (dlm_handles[1].cookie != 0)
2766                                 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2767                         if (dlm_handles[0].cookie != 0)
2768                                 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2769                 case 5: /* target dentry */
2770                         l_dput(*de_newp);
2771                 case 4: /* source dentry */
2772                         l_dput(*de_oldp);
2773                 case 3:
2774 #ifdef S_PDIROPS
2775                         if (dlm_handles[5].cookie != 0)
2776                                 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2777                         if (dlm_handles[6].cookie != 0)
2778                                 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2779 #endif
2780                 case 2: /* target directory dentry */
2781                         l_dput(*de_tgtdirp);
2782                 case 1: /* source directry dentry */
2783                         l_dput(*de_srcdirp);
2784                 }
2785         }
2786
2787         return rc;
2788 }
2789
2790 /*
2791  * checks if dentry can be removed. This function also handles cross-ref
2792  * dentries.
2793  */
2794 static int mds_check_for_rename(struct obd_device *obd,
2795                                 struct dentry *dentry)
2796 {
2797         struct mds_obd *mds = &obd->u.mds;
2798         struct lustre_handle *rlockh;
2799         struct ptlrpc_request *req;
2800         struct mdc_op_data op_data;
2801         struct lookup_intent it;
2802         int handle_size, rc = 0;
2803         ENTRY;
2804
2805         LASSERT(dentry != NULL);
2806
2807         if (dentry->d_inode) {
2808                 if (S_ISDIR(dentry->d_inode->i_mode) &&
2809                     !mds_is_dir_empty(obd, dentry))
2810                         rc = -ENOTEMPTY;
2811         } else {
2812                 LASSERT((dentry->d_flags & DCACHE_CROSS_REF));
2813                 handle_size = sizeof(struct lustre_handle);
2814         
2815                 OBD_ALLOC(rlockh, handle_size);
2816                 if (rlockh == NULL)
2817                         RETURN(-ENOMEM);
2818
2819                 memset(rlockh, 0, handle_size);
2820                 memset(&op_data, 0, sizeof(op_data));
2821                 mds_pack_dentry2id(obd, &op_data.id1, dentry, 1);
2822
2823                 it.it_op = IT_UNLINK;
2824                 rc = md_enqueue(mds->mds_lmv_exp, LDLM_IBITS, &it, LCK_EX,
2825                                 &op_data, rlockh, NULL, 0, ldlm_completion_ast,
2826                                 mds_blocking_ast, NULL);
2827
2828                 if (rc)
2829                         RETURN(rc);
2830
2831                 if (rlockh->cookie != 0)
2832                         ldlm_lock_decref(rlockh, LCK_EX);
2833                 
2834                 if (it.d.lustre.it_data) {
2835                         req = (struct ptlrpc_request *)it.d.lustre.it_data;
2836                         ptlrpc_req_finished(req);
2837                 }
2838
2839                 if (it.d.lustre.it_status)
2840                         rc = it.d.lustre.it_status;
2841                 OBD_FREE(rlockh, handle_size);
2842         }
2843         RETURN(rc);
2844 }
2845
2846 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
2847                                 struct ptlrpc_request *req, struct lustre_id *id,
2848                                 struct dentry *de_dir, struct dentry *de,
2849                                 int del_cross_ref)
2850 {
2851         struct obd_device *obd = req->rq_export->exp_obd;
2852         struct mds_obd *mds = mds_req2mds(req);
2853         void *handle = NULL;
2854         int rc = 0;
2855         ENTRY;
2856
2857         if (de->d_inode) {
2858                 /*
2859                  * name exists and points to local inode try to unlink this name
2860                  * and create new one.
2861                  */
2862                 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
2863                        obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
2864                        (unsigned long)de->d_inode->i_generation);
2865
2866                 /* checking if we can remove local dentry. */
2867                 rc = mds_check_for_rename(obd, de);
2868                 if (rc)
2869                         GOTO(cleanup, rc);
2870
2871                 handle = fsfilt_start(obd, de_dir->d_inode,
2872                                       FSFILT_OP_RENAME, NULL);
2873                 if (IS_ERR(handle))
2874                         GOTO(cleanup, rc = PTR_ERR(handle));
2875                 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
2876                 if (rc)
2877                         GOTO(cleanup, rc);
2878         } else if (de->d_flags & DCACHE_CROSS_REF) {
2879                 struct lustre_id de_id;
2880
2881                 /* name exists and points to remote inode */
2882                 mds_pack_dentry2id(obd, &de_id, de, 1);
2883                 
2884                 CDEBUG(D_OTHER, "%s: %s points to remote inode "DLID4"\n",
2885                        obd->obd_name, rec->ur_tgt, OLID4(&de_id));
2886
2887                 /* checking if we can remove local dentry. */
2888                 rc = mds_check_for_rename(obd, de);
2889                 if (rc)
2890                         GOTO(cleanup, rc);
2891
2892                 /*
2893                  * to be fully POSIX compatible, we should add one more check:
2894                  *
2895                  * if de_new is subdir of dir rec->ur_id1. If so - return
2896                  * -EINVAL.
2897                  *
2898                  * I do not know how to implement it right now, because
2899                  * inodes/dentries for new and old names lie on different MDS,
2900                  * so add this notice here just to make it visible for the rest
2901                  * of developers and do not forget about. And when this check
2902                  * will be added, del_cross_ref should gone, that is local
2903                  * dentry is able to be removed if all checks passed.
2904                  * 
2905                  * Currently -EEXISTS is returned by fsfilt_add_dir_entry() what
2906                  * is not fully correct. --umka
2907                  */
2908
2909                 if (del_cross_ref) {
2910                         handle = fsfilt_start(obd, de_dir->d_inode,
2911                                               FSFILT_OP_RENAME, NULL);
2912                         if (IS_ERR(handle))
2913                                 GOTO(cleanup, rc = PTR_ERR(handle));
2914                         rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
2915                         if (rc)
2916                                 GOTO(cleanup, rc);
2917                 }
2918                 
2919         } else {
2920                 /* name doesn't exist. the simplest case. */
2921                 handle = fsfilt_start(obd, de_dir->d_inode,
2922                                       FSFILT_OP_LINK, NULL);
2923                 if (IS_ERR(handle))
2924                         GOTO(cleanup, rc = PTR_ERR(handle));
2925         }
2926
2927         rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
2928                                   rec->ur_tgtlen - 1, id_ino(id),
2929                                   id_gen(id), id_group(id), id_fid(id));
2930         if (rc) {
2931                 CERROR("add_dir_entry() returned error %d\n", rc);
2932                 GOTO(cleanup, rc);
2933         }
2934
2935 cleanup:
2936         EXIT;
2937         rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
2938                                 handle, req, rc, 0);
2939
2940         RETURN(rc);
2941 }
2942
2943 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
2944                                 struct ptlrpc_request *req, struct dentry *de_dir,
2945                                 struct dentry *de)
2946 {
2947         struct obd_device *obd = req->rq_export->exp_obd;
2948         struct mds_obd *mds = mds_req2mds(req);
2949         void *handle = NULL;
2950         int rc = 0;
2951         ENTRY;
2952
2953         handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
2954         if (IS_ERR(handle))
2955                 GOTO(cleanup, rc = PTR_ERR(handle));
2956         rc = fsfilt_del_dir_entry(obd, de);
2957         d_drop(de);
2958
2959 cleanup:
2960         EXIT;
2961         rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
2962                                 handle, req, rc, 0);
2963         RETURN(0);
2964 }
2965
2966 static int mds_reint_rename_create_name(struct mds_update_record *rec,
2967                                         int offset, struct ptlrpc_request *req)
2968 {
2969         struct lustre_handle parent_lockh[2] = {{0}, {0}};
2970         struct obd_device *obd = req->rq_export->exp_obd;
2971         struct mds_obd *mds = mds_req2mds(req);
2972         struct lustre_handle child_lockh = {0};
2973         struct dentry *de_tgtdir = NULL;
2974         struct dentry *de_new = NULL;
2975         int cleanup_phase = 0;
2976         int update_mode, rc = 0;
2977         ENTRY;
2978
2979         /*
2980          * another MDS executing rename operation has asked us to create target
2981          * name. such a creation should destroy existing target name.
2982          */
2983         CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
2984                obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
2985
2986         /* first, lookup the target */
2987         child_lockh.cookie = 0;
2988         
2989         rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
2990                                          &de_tgtdir, LCK_PW, MDS_INODELOCK_UPDATE,
2991                                          &update_mode, rec->ur_tgt, rec->ur_tgtlen,
2992                                          &child_lockh, &de_new, LCK_EX,
2993                                          MDS_INODELOCK_LOOKUP);
2994         if (rc)
2995                 GOTO(cleanup, rc);
2996
2997         cleanup_phase = 1;
2998
2999         LASSERT(de_tgtdir);
3000         LASSERT(de_tgtdir->d_inode);
3001         LASSERT(de_new);
3002
3003         rc = mds_add_local_dentry(rec, offset, req, rec->ur_id1,
3004                                   de_tgtdir, de_new, 0);
3005
3006         EXIT;
3007 cleanup:
3008         
3009         if (cleanup_phase == 1) {
3010 #ifdef S_PDIROPS
3011                 if (parent_lockh[1].cookie != 0)
3012                         ldlm_lock_decref(&parent_lockh[1], update_mode);
3013 #endif
3014                 ldlm_lock_decref(&parent_lockh[0], LCK_PW);
3015                 if (child_lockh.cookie != 0)
3016                         ldlm_lock_decref(&child_lockh, LCK_EX);
3017                 l_dput(de_new);
3018                 l_dput(de_tgtdir);
3019         }
3020
3021         req->rq_status = rc;
3022         return 0;
3023 }
3024
3025 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
3026                                       struct ptlrpc_request *req)
3027 {
3028         struct obd_device *obd = req->rq_export->exp_obd;
3029         struct ptlrpc_request *req2 = NULL;
3030         struct dentry *de_srcdir = NULL;
3031         struct dentry *de_old = NULL;
3032         struct mds_obd *mds = mds_req2mds(req);
3033         struct lustre_handle parent_lockh[2] = {{0}, {0}};
3034         struct lustre_handle child_lockh = {0};
3035         struct mdc_op_data opdata;
3036         int update_mode, rc = 0;
3037         ENTRY;
3038
3039         CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
3040                obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
3041         memset(&opdata, 0, sizeof(opdata));
3042
3043         child_lockh.cookie = 0;
3044         rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
3045                                          &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
3046                                          &update_mode, rec->ur_name, 
3047                                          rec->ur_namelen, &child_lockh, &de_old,
3048                                          LCK_EX, MDS_INODELOCK_LOOKUP);
3049         LASSERT(rc == 0);
3050         LASSERT(de_srcdir);
3051         LASSERT(de_srcdir->d_inode);
3052         LASSERT(de_old);
3053        
3054         /*
3055          * we already know the target should be created on another MDS so, we
3056          * have to request that MDS to do it.
3057          */
3058
3059         /* prepare source id */
3060         if (de_old->d_flags & DCACHE_CROSS_REF) {
3061                 LASSERT(de_old->d_inode == NULL);
3062                 CDEBUG(D_OTHER, "request to move remote name\n");
3063                 mds_pack_dentry2id(obd, &opdata.id1, de_old, 1);
3064         } else if (de_old->d_inode == NULL) {
3065                 /* oh, source doesn't exist */
3066                 GOTO(cleanup, rc = -ENOENT);
3067         } else {
3068                 struct lustre_id sid;
3069                 struct inode *inode = de_old->d_inode;
3070                 
3071                 LASSERT(inode != NULL);
3072                 CDEBUG(D_OTHER, "request to move local name\n");
3073                 id_ino(&opdata.id1) = inode->i_ino;
3074                 id_group(&opdata.id1) = mds->mds_num;
3075                 id_gen(&opdata.id1) = inode->i_generation;
3076
3077                 down(&inode->i_sem);
3078                 rc = mds_read_inode_sid(obd, inode, &sid);
3079                 up(&inode->i_sem);
3080                 if (rc) {
3081                         CERROR("Can't read inode self id, "
3082                                "inode %lu, rc = %d\n",
3083                                inode->i_ino, rc);
3084                         GOTO(cleanup, rc);
3085                 }
3086
3087                 id_fid(&opdata.id1) = id_fid(&sid);
3088         }
3089
3090         opdata.id2 = *rec->ur_id2;
3091         rc = md_rename(mds->mds_lmv_exp, &opdata, NULL, 0,
3092                        rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3093        
3094         if (rc)
3095                 GOTO(cleanup, rc);
3096         
3097         rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3098                                   de_old);
3099
3100         EXIT;
3101 cleanup:
3102         if (req2)
3103                 ptlrpc_req_finished(req2);
3104
3105 #ifdef S_PDIROPS
3106         if (parent_lockh[1].cookie != 0)
3107                 ldlm_lock_decref(&parent_lockh[1], update_mode);
3108 #endif
3109         ldlm_lock_decref(&parent_lockh[0], LCK_PW);
3110         if (child_lockh.cookie != 0)
3111                 ldlm_lock_decref(&child_lockh, LCK_EX);
3112
3113         l_dput(de_old);
3114         l_dput(de_srcdir);
3115
3116         req->rq_status = rc;
3117         return 0;
3118 }
3119
3120 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3121                             struct ptlrpc_request *req, struct lustre_handle *lockh)
3122 {
3123         struct obd_device *obd = req->rq_export->exp_obd;
3124         struct dentry *de_srcdir = NULL;
3125         struct dentry *de_tgtdir = NULL;
3126         struct dentry *de_old = NULL;
3127         struct dentry *de_new = NULL;
3128         struct inode *old_inode = NULL, *new_inode = NULL;
3129         struct mds_obd *mds = mds_req2mds(req);
3130         struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3131         struct mds_body *body = NULL;
3132         struct llog_create_locks *lcl = NULL;
3133         struct lov_mds_md *lmm = NULL;
3134         int rc = 0, cleanup_phase = 0;
3135         void *handle = NULL;
3136         ENTRY;
3137
3138         LASSERT(offset == 1);
3139
3140         DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3141                   OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3142                   rec->ur_tgt);
3143
3144         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3145
3146         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3147                 DEBUG_REQ(D_HA, req, "rename replay\n");
3148                 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3149                        lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3150                        req->rq_repmsg->buflens[2]);
3151         }
3152
3153         MD_COUNTER_INCREMENT(obd, rename);
3154
3155         if (rec->ur_namelen == 1) {
3156                 rc = mds_reint_rename_create_name(rec, offset, req);
3157                 RETURN(rc);
3158         }
3159
3160         /* check if new name should be located on remote target. */
3161         if (id_group(rec->ur_id2) != mds->mds_num) {
3162                 rc = mds_reint_rename_to_remote(rec, offset, req);
3163                 RETURN(rc);
3164         }
3165         
3166         rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3167                                              rec->ur_id2, &de_tgtdir, LCK_PW,
3168                                              rec->ur_name, rec->ur_namelen,
3169                                              &de_old, rec->ur_tgt,
3170                                              rec->ur_tgtlen, &de_new,
3171                                              dlm_handles, LCK_EX);
3172         if (rc)
3173                 GOTO(cleanup, rc);
3174
3175         cleanup_phase = 1; /* parent(s), children, locks */
3176         old_inode = de_old->d_inode;
3177         new_inode = de_new->d_inode;
3178
3179         /* sanity check for src inode */
3180         if (de_old->d_flags & DCACHE_CROSS_REF) {
3181                 LASSERT(de_old->d_inode == NULL);
3182
3183                 /*
3184                  * in the case of cross-ref dir, we can perform this check only
3185                  * if child and parent lie on the same mds. This is because
3186                  * otherwise they can have the same inode numbers.
3187                  */
3188                 if (de_old->d_mdsnum == mds->mds_num) {
3189                         if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3190                             de_old->d_inum == de_tgtdir->d_inode->i_ino)
3191                                 GOTO(cleanup, rc = -EINVAL);
3192                 }
3193         } else {
3194                 LASSERT(de_old->d_inode != NULL);
3195                 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3196                     de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3197                         GOTO(cleanup, rc = -EINVAL);
3198         }
3199
3200         /* sanity check for dest inode */
3201         if (de_new->d_flags & DCACHE_CROSS_REF) {
3202                 LASSERT(new_inode == NULL);
3203
3204                 /* the same check about target dentry. */
3205                 if (de_new->d_mdsnum == mds->mds_num) {
3206                         if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3207                             de_new->d_inum == de_tgtdir->d_inode->i_ino)
3208                                 GOTO(cleanup, rc = -EINVAL);
3209                 }
3210                 
3211                 /*
3212                  * regular files usualy do not have ->rename() implemented. But
3213                  * we handle only this case when @de_new is cross-ref entry,
3214                  * because in other cases it will be handled by vfs_rename().
3215                  */
3216                 if (de_old->d_inode && (!de_old->d_inode->i_op || 
3217                     !de_old->d_inode->i_op->rename))
3218                         GOTO(cleanup, rc = -EPERM);
3219         } else {
3220                 if (new_inode &&
3221                     (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3222                      new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3223                         GOTO(cleanup, rc = -EINVAL);
3224
3225         }
3226         
3227 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3228         /*
3229          * check if we are moving old entry into its child. 2.6 does not check
3230          * for this in vfs_rename() anymore.
3231          */
3232         if (is_subdir(de_new, de_old))
3233                 GOTO(cleanup, rc = -EINVAL);
3234 #endif
3235         
3236         /* check if inodes point to each other. */
3237         if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3238             !(de_new->d_flags & DCACHE_CROSS_REF) &&
3239             old_inode == new_inode)
3240                 GOTO(cleanup, rc = 0);
3241
3242         /*
3243          * if we are about to remove the target at first, pass the EA of that
3244          * inode to client to perform and cleanup on OST.
3245          */
3246         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3247         LASSERT(body != NULL);
3248
3249         /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3250         if (new_inode) 
3251                 DOWN_READ_I_ALLOC_SEM(new_inode);
3252         
3253         cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3254
3255         if (new_inode && ((S_ISDIR(new_inode->i_mode) && 
3256             new_inode->i_nlink == 2) ||
3257             new_inode->i_nlink == 1)) {
3258                 if (mds_orphan_open_count(new_inode) > 0) {
3259                         /* need to lock pending_dir before transaction */
3260                         down(&mds->mds_pending_dir->d_inode->i_sem);
3261                         cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3262                 } else if (S_ISREG(new_inode->i_mode)) {
3263                         mds_pack_inode2body(obd, body, new_inode, 0);
3264                         mds_pack_md(obd, req->rq_repmsg, 1, body, 
3265                                     new_inode, MDS_PACK_MD_LOCK);
3266                  }
3267         }
3268
3269         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3270                        de_srcdir->d_inode->i_sb);
3271
3272         if (de_old->d_flags & DCACHE_CROSS_REF) {
3273                 struct lustre_id old_id;
3274
3275                 mds_pack_dentry2id(obd, &old_id, de_old, 1);
3276                 
3277                 rc = mds_add_local_dentry(rec, offset, req, &old_id,
3278                                           de_tgtdir, de_new, 1);
3279                 if (rc)
3280                         GOTO(cleanup, rc);
3281
3282                 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3283                                           de_old);
3284                 GOTO(cleanup, rc);
3285         }
3286
3287         lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3288         handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3289                                   NULL, le32_to_cpu(lmm->lmm_stripe_count));
3290
3291         if (IS_ERR(handle))
3292                 GOTO(cleanup, rc = PTR_ERR(handle));
3293
3294         lock_kernel();
3295         de_old->d_fsdata = req;
3296         de_new->d_fsdata = req;
3297         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3298         unlock_kernel();
3299
3300         if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3301                 if (mds_orphan_open_count(new_inode) > 0)
3302                         rc = mds_orphan_add_link(rec, obd, de_new);
3303
3304                 if (rc == 1)
3305                         GOTO(cleanup, rc = 0);
3306
3307                 if (!S_ISREG(new_inode->i_mode))
3308                         GOTO(cleanup, rc);
3309
3310                 if (!(body->valid & OBD_MD_FLEASIZE)) {
3311                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3312                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
3313                 } else if (mds_log_op_unlink(obd, new_inode,
3314                                              lustre_msg_buf(req->rq_repmsg,1,0),
3315                                              req->rq_repmsg->buflens[1],
3316                                              lustre_msg_buf(req->rq_repmsg,2,0),
3317                                              req->rq_repmsg->buflens[2], 
3318                                              &lcl) > 0) {
3319                         body->valid |= OBD_MD_FLCOOKIE;
3320                 }
3321         }
3322
3323         EXIT;
3324 cleanup:
3325         rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3326                                 handle, req, rc, 0);
3327
3328         switch (cleanup_phase) {
3329         case 3:
3330                 up(&mds->mds_pending_dir->d_inode->i_sem);
3331         case 2:
3332                 if (new_inode)
3333                         UP_READ_I_ALLOC_SEM(new_inode);
3334         case 1:
3335 #ifdef S_PDIROPS
3336                 if (dlm_handles[5].cookie != 0)
3337                         ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3338                 if (dlm_handles[6].cookie != 0)
3339                         ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3340 #endif
3341                 if (lcl != NULL)
3342                         ptlrpc_save_llog_lock(req, lcl);
3343
3344                 if (rc) {
3345                         if (dlm_handles[3].cookie != 0)
3346                                 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3347                         ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3348                         ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3349                         ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3350                 } else {
3351                         if (dlm_handles[3].cookie != 0)
3352                                 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3353                         ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3354                         ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3355                         ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3356                 }
3357                 l_dput(de_new);
3358                 l_dput(de_old);
3359                 l_dput(de_tgtdir);
3360                 l_dput(de_srcdir);
3361         case 0:
3362                 break;
3363         default:
3364                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3365                 LBUG();
3366         }
3367         req->rq_status = rc;
3368         return 0;
3369 }
3370
3371 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3372                            struct ptlrpc_request *, struct lustre_handle *);
3373
3374 static mds_reinter reinters[REINT_MAX + 1] = {
3375         [REINT_SETATTR] mds_reint_setattr,
3376         [REINT_CREATE] mds_reint_create,
3377         [REINT_LINK] mds_reint_link,
3378         [REINT_UNLINK] mds_reint_unlink,
3379         [REINT_RENAME] mds_reint_rename,
3380         [REINT_OPEN] mds_open
3381 };
3382
3383 int mds_reint_rec(struct mds_update_record *rec, int offset,
3384                   struct ptlrpc_request *req, struct lustre_handle *lockh)
3385 {
3386         struct obd_device *obd = req->rq_export->exp_obd;
3387         struct lvfs_run_ctxt saved;
3388         int rc;
3389
3390         /* checked by unpacker */
3391         LASSERT(rec->ur_opcode <= REINT_MAX &&
3392                 reinters[rec->ur_opcode] != NULL);
3393
3394         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3395         rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3396         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3397
3398         return rc;
3399 }