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