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