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