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