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