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