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