Whamcloud - gitweb
d3615d416f1cc9d2ef0122f085a97a7430b338fe
[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/obd_support.h>
35 #include <linux/obd_class.h>
36 #include <linux/obd.h>
37 #include <linux/lustre_lib.h>
38 #include <linux/lustre_idl.h>
39 #include <linux/lustre_mds.h>
40 #include <linux/lustre_dlm.h>
41 #include <linux/lustre_fsfilt.h>
42
43 #include "mds_internal.h"
44
45 void mds_commit_cb(struct obd_device *obd, __u64 transno, void *data,
46                    int error)
47 {
48         obd_transno_commit_cb(obd, transno, error);
49 }
50
51 struct mds_logcancel_data {
52         struct lov_mds_md      *mlcd_lmm;
53         int                     mlcd_size;
54         int                     mlcd_cookielen;
55         int                     mlcd_eadatalen;
56         struct llog_cookie      mlcd_cookies[0];
57 };
58
59
60 static void mds_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
61                                   void *cb_data, int error)
62 {
63         struct mds_logcancel_data *mlcd = cb_data;
64         struct lov_stripe_md *lsm = NULL;
65         struct llog_ctxt *ctxt;
66         int rc;
67
68         obd_transno_commit_cb(obd, transno, error);
69
70         CDEBUG(D_HA, "cancelling %d cookies\n",
71                (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
72
73         rc = obd_unpackmd(obd->u.mds.mds_osc_exp, &lsm, mlcd->mlcd_lmm,
74                           mlcd->mlcd_eadatalen);
75         if (rc < 0) {
76                 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
77                        (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
78                        rc);
79         } else {
80                 ///* XXX 0 normally, SENDNOW for debug */);
81                 ctxt = llog_get_context(&obd->obd_llogs,
82                                         mlcd->mlcd_cookies[0].lgc_subsys + 1);
83                 rc = llog_cancel(ctxt, lsm, mlcd->mlcd_cookielen /
84                                                 sizeof(*mlcd->mlcd_cookies),
85                                  mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW);
86                 if (rc)
87                         CERROR("error cancelling %d log cookies: rc %d\n",
88                                (int)(mlcd->mlcd_cookielen /
89                                      sizeof(*mlcd->mlcd_cookies)), rc);
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 mds_client_data *mcd = med->med_mcd;
101         struct obd_device *obd = req->rq_export->exp_obd;
102         int err;
103         __u64 transno;
104         loff_t off;
105         int log_pri = D_HA;
106         ENTRY;
107
108         /* if the export has already been failed, we have no last_rcvd slot */
109         if (req->rq_export->exp_failed) {
110                 CERROR("committing transaction for disconnected client\n");
111                 if (handle)
112                         GOTO(commit, rc);
113                 RETURN(rc);
114         }
115
116         if (IS_ERR(handle))
117                 RETURN(rc);
118
119         if (handle == NULL) {
120                 /* if we're starting our own xaction, use our own inode */
121                 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
122                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
123                 if (IS_ERR(handle)) {
124                         CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
125                         RETURN(PTR_ERR(handle));
126                 }
127         }
128
129         off = med->med_off;
130
131         transno = req->rq_reqmsg->transno;
132         if (rc != 0) {
133                 LASSERT(transno == 0);
134         } else if (transno == 0) {
135                 spin_lock(&mds->mds_transno_lock);
136                 transno = ++mds->mds_last_transno;
137                 spin_unlock(&mds->mds_transno_lock);
138         } else {
139                 spin_lock(&mds->mds_transno_lock);
140                 if (transno > mds->mds_last_transno)
141                         mds->mds_last_transno = transno;
142                 spin_unlock(&mds->mds_transno_lock);
143         }
144         req->rq_repmsg->transno = req->rq_transno = transno;
145         mcd->mcd_last_transno = cpu_to_le64(transno);
146         mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
147         mcd->mcd_last_result = cpu_to_le32(rc);
148         mcd->mcd_last_data = cpu_to_le32(op_data);
149
150         fsfilt_add_journal_cb(req->rq_export->exp_obd, transno, handle,
151                               mds_commit_cb, NULL);
152         err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd, sizeof(*mcd),
153                                   &off, 0);
154
155         if (err) {
156                 log_pri = D_ERROR;
157                 if (rc == 0)
158                         rc = err;
159         }
160
161         DEBUG_REQ(log_pri, req,
162                   "wrote trans #"LPU64" client %s at idx %u: err = %d",
163                   transno, mcd->mcd_uuid, med->med_idx, err);
164
165         err = mds_lov_write_objids(obd);
166         if (err) {
167                 log_pri = D_ERROR;
168                 if (rc == 0)
169                         rc = err;
170         }
171         CDEBUG(log_pri, "wrote objids: err = %d\n", err);
172
173 commit:
174         err = fsfilt_commit(obd, inode, handle, 0);
175         if (err) {
176                 CERROR("error committing transaction: %d\n", err);
177                 if (!rc)
178                         rc = err;
179         }
180
181         RETURN(rc);
182 }
183
184 /* this gives the same functionality as the code between
185  * sys_chmod and inode_setattr
186  * chown_common and inode_setattr
187  * utimes and inode_setattr
188  */
189 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
190 {
191         time_t now = LTIME_S(CURRENT_TIME);
192         struct iattr *attr = &rec->ur_iattr;
193         unsigned int ia_valid = attr->ia_valid;
194         int error;
195         ENTRY;
196
197         /* only fix up attrs if the client VFS didn't already */
198         if (!(ia_valid & ATTR_RAW))
199                 RETURN(0);
200
201         if (!(ia_valid & ATTR_CTIME_SET))
202                 LTIME_S(attr->ia_ctime) = now;
203         if (!(ia_valid & ATTR_ATIME_SET))
204                 LTIME_S(attr->ia_atime) = now;
205         if (!(ia_valid & ATTR_MTIME_SET))
206                 LTIME_S(attr->ia_mtime) = now;
207
208         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
209                 RETURN(-EPERM);
210
211         /* times */
212         if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
213                 if (rec->ur_fsuid != inode->i_uid &&
214                     (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
215                         RETURN(error);
216         }
217
218         if (ia_valid & ATTR_SIZE) {
219                 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
220                         RETURN(error);
221         }
222
223         if (ia_valid & ATTR_UID) {
224                 /* chown */
225                 error = -EPERM;
226                 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
227                         RETURN(-EPERM);
228                 if (attr->ia_uid == (uid_t) -1)
229                         attr->ia_uid = inode->i_uid;
230                 if (attr->ia_gid == (gid_t) -1)
231                         attr->ia_gid = inode->i_gid;
232                 attr->ia_mode = inode->i_mode;
233                 /*
234                  * If the user or group of a non-directory has been
235                  * changed by a non-root user, remove the setuid bit.
236                  * 19981026 David C Niemi <niemi@tux.org>
237                  *
238                  * Changed this to apply to all users, including root,
239                  * to avoid some races. This is the behavior we had in
240                  * 2.0. The check for non-root was definitely wrong
241                  * for 2.2 anyway, as it should have been using
242                  * CAP_FSETID rather than fsuid -- 19990830 SD.
243                  */
244                 if ((inode->i_mode & S_ISUID) == S_ISUID &&
245                     !S_ISDIR(inode->i_mode)) {
246                         attr->ia_mode &= ~S_ISUID;
247                         attr->ia_valid |= ATTR_MODE;
248                 }
249                 /*
250                  * Likewise, if the user or group of a non-directory
251                  * has been changed by a non-root user, remove the
252                  * setgid bit UNLESS there is no group execute bit
253                  * (this would be a file marked for mandatory
254                  * locking).  19981026 David C Niemi <niemi@tux.org>
255                  *
256                  * Removed the fsuid check (see the comment above) --
257                  * 19990830 SD.
258                  */
259                 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
260                      (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
261                         attr->ia_mode &= ~S_ISGID;
262                         attr->ia_valid |= ATTR_MODE;
263                 }
264         } else if (ia_valid & ATTR_MODE) {
265                 int mode = attr->ia_mode;
266                 /* chmod */
267                 if (attr->ia_mode == (mode_t) -1)
268                         attr->ia_mode = inode->i_mode;
269                 attr->ia_mode =
270                         (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
271         }
272         RETURN(0);
273 }
274
275 void mds_steal_ack_locks(struct ptlrpc_request *req)
276 {
277         struct obd_export         *exp = req->rq_export;
278         struct list_head          *tmp;
279         struct ptlrpc_reply_state *oldrep;
280         struct ptlrpc_service     *svc;
281         unsigned long              flags;
282         int                        i;
283
284         /* CAVEAT EMPTOR: spinlock order */
285         spin_lock_irqsave (&exp->exp_lock, flags);
286         list_for_each (tmp, &exp->exp_outstanding_replies) {
287                 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
288
289                 if (oldrep->rs_xid != req->rq_xid)
290                         continue;
291
292                 if (oldrep->rs_msg.opc != req->rq_reqmsg->opc)
293                         CERROR ("Resent req xid "LPX64" has mismatched opc: "
294                                 "new %d old %d\n", req->rq_xid,
295                                 req->rq_reqmsg->opc, oldrep->rs_msg.opc);
296
297                 svc = oldrep->rs_srv_ni->sni_service;
298                 spin_lock (&svc->srv_lock);
299
300                 list_del_init (&oldrep->rs_exp_list);
301
302                 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
303                       " o%d NID"LPX64"\n",
304                       oldrep->rs_nlocks, oldrep, 
305                       oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg.opc,
306                       exp->exp_connection->c_peer.peer_nid);
307
308                 for (i = 0; i < oldrep->rs_nlocks; i++)
309                         ptlrpc_save_lock(req, 
310                                          &oldrep->rs_locks[i],
311                                          oldrep->rs_modes[i]);
312                 oldrep->rs_nlocks = 0;
313
314                 DEBUG_REQ(D_HA, req, "stole locks for");
315                 ptlrpc_schedule_difficult_reply (oldrep);
316
317                 spin_unlock (&svc->srv_lock);
318                 spin_unlock_irqrestore (&exp->exp_lock, flags);
319                 return;
320         }
321         spin_unlock_irqrestore (&exp->exp_lock, flags);
322 }
323
324 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
325 {
326         DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
327                   mcd->mcd_last_transno, mcd->mcd_last_result);
328         req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
329         req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
330
331         mds_steal_ack_locks(req);
332 }
333
334 static void reconstruct_reint_setattr(struct mds_update_record *rec,
335                                       int offset, struct ptlrpc_request *req)
336 {
337         struct mds_export_data *med = &req->rq_export->exp_mds_data;
338         struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
339         struct dentry *de;
340         struct mds_body *body;
341
342         mds_req_from_mcd(req, med->med_mcd);
343
344         de = mds_fid2dentry(obd, rec->ur_fid1, NULL);
345         if (IS_ERR(de)) {
346                 LASSERT(PTR_ERR(de) == req->rq_status);
347                 return;
348         }
349
350         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
351         mds_pack_inode2fid(req2obd(req), &body->fid1, de->d_inode);
352         mds_pack_inode2body(req2obd(req), body, de->d_inode);
353
354         /* Don't return OST-specific attributes if we didn't just set them */
355         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
356                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
357         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
358                 body->valid |= OBD_MD_FLMTIME;
359         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
360                 body->valid |= OBD_MD_FLATIME;
361
362         l_dput(de);
363 }
364
365 /* In the raw-setattr case, we lock the child inode.
366  * In the write-back case or if being called from open, the client holds a lock
367  * already.
368  *
369  * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
370 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
371                              struct ptlrpc_request *req,
372                              struct lustre_handle *lh)
373 {
374         struct mds_obd *mds = mds_req2mds(req);
375         struct obd_device *obd = req->rq_export->exp_obd;
376         struct mds_body *body;
377         struct dentry *de;
378         struct inode *inode = NULL;
379         struct lustre_handle lockh[2];
380         void *handle = NULL;
381         struct mds_logcancel_data *mlcd = NULL;
382         int rc = 0, cleanup_phase = 0, err, locked = 0;
383         ENTRY;
384
385         LASSERT(offset == 0);
386
387         DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x", rec->ur_fid1->id,
388                   rec->ur_fid1->generation, rec->ur_iattr.ia_valid);
389
390         MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
391
392         if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
393                 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
394                 if (IS_ERR(de))
395                         GOTO(cleanup, rc = PTR_ERR(de));
396         } else {
397                 __u64 lockpart = MDS_INODELOCK_UPDATE;
398                 if (rec->ur_iattr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID) )
399                         lockpart |= MDS_INODELOCK_LOOKUP;
400                 de = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW,
401                                            lockh, NULL, 0, lockpart);
402                 if (IS_ERR(de))
403                         GOTO(cleanup, rc = PTR_ERR(de));
404                 locked = 1;
405         }
406
407         cleanup_phase = 1;
408
409         inode = de->d_inode;
410         LASSERT(inode);
411         if (S_ISREG(inode->i_mode) && rec->ur_eadata != NULL)
412                 down(&inode->i_sem);
413
414         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
415
416         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
417         if (IS_ERR(handle))
418                 GOTO(cleanup, rc = PTR_ERR(handle));
419
420         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
421                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
422                        LTIME_S(rec->ur_iattr.ia_mtime),
423                        LTIME_S(rec->ur_iattr.ia_ctime));
424         rc = mds_fix_attr(inode, rec);
425         if (rc)
426                 GOTO(cleanup, rc);
427
428         if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG)    /* ioctl */
429                 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
430                                       (long)&rec->ur_iattr.ia_attr_flags);
431         else                                            /* setattr */
432                 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
433
434         if (rc == 0 && S_ISREG(inode->i_mode) && rec->ur_eadata != NULL) {
435                 rc = fsfilt_set_md(obd, inode, handle,
436                                    rec->ur_eadata, rec->ur_eadatalen);
437         }
438
439         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
440         mds_pack_inode2fid(obd, &body->fid1, inode);
441         mds_pack_inode2body(obd, body, inode);
442
443         /* Don't return OST-specific attributes if we didn't just set them */
444         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
445                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
446         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
447                 body->valid |= OBD_MD_FLMTIME;
448         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
449                 body->valid |= OBD_MD_FLATIME;
450
451         if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_osc_obd)) {
452                 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
453                           rec->ur_eadatalen);
454                 if (mlcd) {
455                         mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
456                                 rec->ur_eadatalen;
457                         mlcd->mlcd_eadatalen = rec->ur_eadatalen;
458                         mlcd->mlcd_cookielen = rec->ur_cookielen;
459                         mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
460                                 mlcd->mlcd_cookielen;
461                         memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
462                                mlcd->mlcd_cookielen);
463                         memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
464                                mlcd->mlcd_eadatalen);
465                 } else {
466                         CERROR("unable to allocate log cancel data\n");
467                 }
468         }
469         EXIT;
470  cleanup:
471         if (mlcd != NULL)
472                 fsfilt_add_journal_cb(req->rq_export->exp_obd, 0, handle,
473                                       mds_cancel_cookies_cb, mlcd);
474         err = mds_finish_transno(mds, inode, handle, req, rc, 0);
475         switch (cleanup_phase) {
476         case 1:
477                 if (S_ISREG(inode->i_mode) && rec->ur_eadata != NULL)
478                         up(&inode->i_sem);
479                 l_dput(de);
480                 if (locked) {
481 #ifdef S_PDIROPS
482                         if (lockh[1].cookie != 0)
483                                 ldlm_lock_decref(lockh + 1, LCK_CW);
484 #endif
485                         if (rc) {
486                                 ldlm_lock_decref(lockh, LCK_PW);
487                         } else {
488                                 ptlrpc_save_lock (req, lockh, LCK_PW);
489                         }
490                 }
491         case 0:
492                 break;
493         default:
494                 LBUG();
495         }
496         if (err && !rc)
497                 rc = err;
498
499         req->rq_status = rc;
500         return 0;
501 }
502
503 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
504                                      struct ptlrpc_request *req)
505 {
506         struct mds_export_data *med = &req->rq_export->exp_mds_data;
507         struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
508         struct dentry *parent, *child;
509         struct mds_body *body;
510
511         mds_req_from_mcd(req, med->med_mcd);
512
513         if (req->rq_status)
514                 return;
515
516         parent = mds_fid2dentry(obd, rec->ur_fid1, NULL);
517         LASSERT(!IS_ERR(parent));
518         child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
519         LASSERT(!IS_ERR(child));
520         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
521         mds_pack_inode2fid(req2obd(req), &body->fid1, child->d_inode);
522         mds_pack_inode2body(req2obd(req), body, child->d_inode);
523         l_dput(parent);
524         l_dput(child);
525 }
526
527 static int mds_reint_create(struct mds_update_record *rec, int offset,
528                             struct ptlrpc_request *req,
529                             struct lustre_handle *lh)
530 {
531         struct dentry *dparent = NULL;
532         struct mds_obd *mds = mds_req2mds(req);
533         struct obd_device *obd = req->rq_export->exp_obd;
534         struct dentry *dchild = NULL;
535         struct inode *dir = NULL;
536         void *handle = NULL;
537         struct lustre_handle lockh[2];
538         int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
539         int created = 0;
540         struct dentry_params dp;
541         struct mea *mea = NULL;
542         int mea_size;
543         ENTRY;
544
545         LASSERT(offset == 0);
546         LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
547
548         DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
549                   rec->ur_fid1->id, rec->ur_fid1->generation,
550                   rec->ur_name, rec->ur_mode);
551
552         MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
553
554         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
555                 GOTO(cleanup, rc = -ESTALE);
556
557         dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW, lockh,
558                                         rec->ur_name, rec->ur_namelen - 1,
559                                         MDS_INODELOCK_UPDATE);
560         if (IS_ERR(dparent)) {
561                 rc = PTR_ERR(dparent);
562                 CERROR("parent lookup error %d\n", rc);
563                 GOTO(cleanup, rc);
564         }
565         cleanup_phase = 1; /* locked parent dentry */
566         dir = dparent->d_inode;
567         LASSERT(dir);
568
569         ldlm_lock_dump_handle(D_OTHER, lockh);
570
571         /* try to retrieve MEA data for this dir */
572         rc = mds_get_lmv_attr(obd, dparent->d_inode, &mea, &mea_size);
573         if (mea != NULL) {
574                 /* dir is already splitted, check is requested filename
575                  * should live at this MDS or at another one */
576                 int i;
577                 i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
578                 if (mea->mea_master != i) {
579                         CERROR("inapropriate MDS(%d) for %s. should be %d\n",
580                                 mea->mea_master, rec->ur_name, i);
581                         GOTO(cleanup, rc = -ESTALE);
582                 }
583         }
584
585         dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
586         if (IS_ERR(dchild)) {
587                 rc = PTR_ERR(dchild);
588                 CERROR("child lookup error %d\n", rc);
589                 GOTO(cleanup, rc);
590         }
591
592         cleanup_phase = 2; /* child dentry */
593
594         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
595
596         if (type == S_IFREG || type == S_IFDIR) {
597                 if ((rc = mds_try_to_split_dir(obd, dparent, &mea, 0))) {
598                         if (rc > 0) {
599                                 /* dir got splitted */
600                                 GOTO(cleanup, rc = -ESTALE);
601                         } else {
602                                 /* error happened during spitting */
603                                 GOTO(cleanup, rc);
604                         }
605                 }
606         }
607
608         if (dir->i_mode & S_ISGID) {
609                 if (S_ISDIR(rec->ur_mode))
610                         rec->ur_mode |= S_ISGID;
611         }
612
613         dchild->d_fsdata = (void *)&dp;
614         dp.p_inum = (unsigned long)rec->ur_fid2->id;
615         dp.p_ptr = req;
616
617         switch (type) {
618         case S_IFREG:{
619                 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
620                 if (IS_ERR(handle))
621                         GOTO(cleanup, rc = PTR_ERR(handle));
622                 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
623                 EXIT;
624                 break;
625         }
626         case S_IFDIR:{
627                 int nstripes = 0;
628                 int i;
629                 
630                 /* as Peter asked, mkdir() should distribute new directories
631                  * over the whole cluster in order to distribute namespace
632                  * processing load. first, we calculate which MDS to use to
633                  * put new directory's inode in */
634                 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1);
635                 if (i == mds->mds_num) {
636                         /* inode will be created locally */
637
638                         handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
639                         if (IS_ERR(handle))
640                                 GOTO(cleanup, rc = PTR_ERR(handle));
641
642                         rc = vfs_mkdir(dir, dchild, rec->ur_mode);
643
644                         if (rec->ur_eadata)
645                                 nstripes = *(u16 *)rec->ur_eadata;
646
647 #if 1
648                         /* this is for current testing yet. after the testing
649                          * directory will split if size reaches some limite -bzzz */
650                         if (rc == 0) {
651 #else
652                         if (rc == 0 && nstripes) {
653 #endif
654                                 /* FIXME: error handling here */
655                                 mds_try_to_split_dir(obd, dchild, NULL, nstripes);
656                         }
657                 } else if (!DENTRY_VALID(dchild)) {
658                         /* inode will be created on another MDS */
659                         struct obdo *oa = NULL;
660                         struct mds_body *body;
661                         
662                         /* first, create that inode */
663                         oa = obdo_alloc();
664                         LASSERT(oa != NULL);
665                         oa->o_mds = i;
666                         obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
667                                         OBD_MD_FLMTIME | OBD_MD_FLCTIME |
668                                         OBD_MD_FLUID | OBD_MD_FLGID);
669                         oa->o_mode = dir->i_mode;
670                         CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
671                                         obd->obd_name, i);
672                         rc = obd_create(mds->mds_lmv_exp, oa, NULL, NULL);
673                         LASSERT(rc == 0);
674                         
675                         /* now, add new dir entry for it */
676                         handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
677                         if (IS_ERR(handle))
678                                 GOTO(cleanup, rc = PTR_ERR(handle));
679                         rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
680                                                   rec->ur_namelen - 1,
681                                                   oa->o_id, oa->o_generation,
682                                                   i);
683                         LASSERT(rc == 0);
684
685                         /* fill reply */
686                         body = lustre_msg_buf(req->rq_repmsg,
687                                               offset, sizeof (*body));
688                         body->valid |= OBD_MD_FLID | OBD_MD_MDS;
689                         body->fid1.id = oa->o_id;
690                         body->fid1.mds = i;
691                         body->fid1.generation = oa->o_generation;
692                         obdo_free(oa);
693                 } else {
694                         /* requested name exists in the directory */
695                         rc = -EEXIST;
696                 }
697                 EXIT;
698                 break;
699         }
700         case S_IFLNK:{
701                 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
702                 if (IS_ERR(handle))
703                         GOTO(cleanup, rc = PTR_ERR(handle));
704                 if (rec->ur_tgt == NULL)        /* no target supplied */
705                         rc = -EINVAL;           /* -EPROTO? */
706                 else
707                         rc = vfs_symlink(dir, dchild, rec->ur_tgt);
708                 EXIT;
709                 break;
710         }
711         case S_IFCHR:
712         case S_IFBLK:
713         case S_IFIFO:
714         case S_IFSOCK:{
715                 int rdev = rec->ur_rdev;
716                 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
717                 if (IS_ERR(handle))
718                         GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
719                 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
720                 EXIT;
721                 break;
722         }
723         default:
724                 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
725                 dchild->d_fsdata = NULL;
726                 GOTO(cleanup, rc = -EINVAL);
727         }
728
729         /* In case we stored the desired inum in here, we want to clean up. */
730         if (dchild->d_fsdata == (void *)(unsigned long)rec->ur_fid2->id)
731                 dchild->d_fsdata = NULL;
732
733         if (rc) {
734                 CDEBUG(D_INODE, "error during create: %d\n", rc);
735                 GOTO(cleanup, rc);
736         } else if (dchild->d_inode) {
737                 struct iattr iattr;
738                 struct inode *inode = dchild->d_inode;
739                 struct mds_body *body;
740
741                 created = 1;
742                 LTIME_S(iattr.ia_atime) = rec->ur_time;
743                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
744                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
745                 iattr.ia_uid = rec->ur_fsuid;
746                 if (dir->i_mode & S_ISGID)
747                         iattr.ia_gid = dir->i_gid;
748                 else
749                         iattr.ia_gid = rec->ur_fsgid;
750                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
751                         ATTR_MTIME | ATTR_CTIME;
752
753                 if (rec->ur_fid2->id) {
754                         LASSERT(rec->ur_fid2->id == inode->i_ino);
755                         inode->i_generation = rec->ur_fid2->generation;
756                         /* Dirtied and committed by the upcoming setattr. */
757                         CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
758                                inode->i_ino, inode->i_generation);
759                 } else {
760                         struct lustre_handle child_ino_lockh;
761
762                         CDEBUG(D_INODE, "created ino %lu with gen %x\n",
763                                inode->i_ino, inode->i_generation);
764
765                         /* The inode we were allocated may have just been freed
766                          * by an unlink operation.  We take this lock to
767                          * synchronize against the matching reply-ack-lock taken
768                          * in unlink, to avoid replay problems if this reply
769                          * makes it out to the client but the unlink's does not.
770                          * See bug 2029 for more detail.*/
771                         rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
772                         if (rc != ELDLM_OK) {
773                                 CERROR("error locking for unlink/create sync: "
774                                        "%d\n", rc);
775                         } else {
776                                 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
777                         }
778                 }
779
780                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
781                 if (rc)
782                         CERROR("error on child setattr: rc = %d\n", rc);
783
784                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
785                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
786                 if (rc)
787                         CERROR("error on parent setattr: rc = %d\n", rc);
788
789                 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
790                 mds_pack_inode2fid(obd, &body->fid1, inode);
791                 mds_pack_inode2body(obd, body, inode);
792         }
793         EXIT;
794
795 cleanup:
796         err = mds_finish_transno(mds, dir, handle, req, rc, 0);
797
798         if (rc && created) {
799                 /* Destroy the file we just created.  This should not need
800                  * extra journal credits, as we have already modified all of
801                  * the blocks needed in order to create the file in the first
802                  * place.
803                  */
804                 switch (type) {
805                 case S_IFDIR:
806                         err = vfs_rmdir(dir, dchild);
807                         if (err)
808                                 CERROR("rmdir in error path: %d\n", err);
809                         break;
810                 default:
811                         err = vfs_unlink(dir, dchild);
812                         if (err)
813                                 CERROR("unlink in error path: %d\n", err);
814                         break;
815                 }
816         } else {
817                 rc = err;
818         }
819         switch (cleanup_phase) {
820         case 2: /* child dentry */
821                 l_dput(dchild);
822         case 1: /* locked parent dentry */
823 #ifdef S_PDIROPS
824                 if (lockh[1].cookie != 0)
825                         ldlm_lock_decref(lockh + 1, LCK_CW);
826 #endif
827                 if (rc) {
828                         ldlm_lock_decref(lockh, LCK_PW);
829                 } else {
830                         ptlrpc_save_lock (req, lockh, LCK_PW);
831                 }
832                 l_dput(dparent);
833         case 0:
834                 break;
835         default:
836                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
837                 LBUG();
838         }
839         if (mea)
840                 OBD_FREE(mea, mea_size);
841         req->rq_status = rc;
842         return 0;
843 }
844
845 static int res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
846            ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
847 {
848         int i;
849
850         for (i = 0; i < RES_NAME_SIZE; i++) {
851                 /* return 1 here, because enqueue_ordered will skip resources
852                  * of all zeroes if they're sorted to the end of the list. */
853                 if (res1->name[i] == 0 && res2->name[i] != 0)
854                         return 1;
855                 if (res2->name[i] == 0 && res1->name[i] != 0)
856                         return 0;
857
858                 if (res1->name[i] > res2->name[i])
859                         return 1;
860                 if (res1->name[i] < res2->name[i])
861                         return 0;
862         }
863
864         if (!p1 || !p2)
865                 return 0;
866
867         if (memcmp(p1, p2, sizeof(*p1)) < 0)
868                 return 1;
869
870         return 0;
871 }
872
873 /* This function doesn't use ldlm_match_or_enqueue because we're always called
874  * with EX or PW locks, and the MDS is no longer allowed to match write locks,
875  * because they take the place of local semaphores.
876  *
877  * One or two locks are taken in numerical order.  A res_id->name[0] of 0 means
878  * no lock is taken for that res_id.  Must be at least one non-zero res_id. */
879 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
880                           struct lustre_handle *p1_lockh, int p1_lock_mode,
881                           ldlm_policy_data_t *p1_policy,
882                           struct ldlm_res_id *p2_res_id,
883                           struct lustre_handle *p2_lockh, int p2_lock_mode,
884                           ldlm_policy_data_t *p2_policy)
885 {
886         struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
887         struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
888         int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
889         ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
890         int rc, flags;
891         ENTRY;
892
893         LASSERT(p1_res_id != NULL && p2_res_id != NULL);
894
895         CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n", res_id[0]->name[0],
896                res_id[1]->name[0]);
897
898         if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
899                 handles[1] = p1_lockh;
900                 handles[0] = p2_lockh;
901                 res_id[1] = p1_res_id;
902                 res_id[0] = p2_res_id;
903                 lock_modes[1] = p1_lock_mode;
904                 lock_modes[0] = p2_lock_mode;
905                 policies[1] = p1_policy;
906                 policies[0] = p2_policy;
907         }
908
909         CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
910                res_id[0]->name[0], res_id[1]->name[0]);
911
912         flags = LDLM_FL_LOCAL_ONLY;
913         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
914                               LDLM_IBITS, policies[0], lock_modes[0], &flags,
915                               mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
916                               NULL, 0, NULL, handles[0]);
917         if (rc != ELDLM_OK)
918                 RETURN(-EIO);
919         ldlm_lock_dump_handle(D_OTHER, handles[0]);
920
921         if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
922             (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
923                 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
924                 ldlm_lock_addref(handles[1], lock_modes[1]);
925         } else if (res_id[1]->name[0] != 0) {
926                 flags = LDLM_FL_LOCAL_ONLY;
927                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
928                                       *res_id[1], LDLM_IBITS, policies[1],
929                                       lock_modes[1], &flags, mds_blocking_ast,
930                                       ldlm_completion_ast, NULL, NULL, NULL, 0,
931                                       NULL, handles[1]);
932                 if (rc != ELDLM_OK) {
933                         ldlm_lock_decref(handles[0], lock_modes[0]);
934                         RETURN(-EIO);
935                 }
936                 ldlm_lock_dump_handle(D_OTHER, handles[1]);
937         }
938
939         RETURN(0);
940 }
941
942 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
943                            struct lustre_handle *p1_lockh, int p1_lock_mode,
944                            ldlm_policy_data_t *p1_policy,
945                            struct ldlm_res_id *p2_res_id,
946                            struct lustre_handle *p2_lockh, int p2_lock_mode,
947                            ldlm_policy_data_t *p2_policy,
948                            struct ldlm_res_id *c1_res_id,
949                            struct lustre_handle *c1_lockh, int c1_lock_mode,
950                            ldlm_policy_data_t *c1_policy,
951                            struct ldlm_res_id *c2_res_id,
952                            struct lustre_handle *c2_lockh, int c2_lock_mode,
953                            ldlm_policy_data_t *c2_policy)
954 {
955         struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
956                                           c1_res_id, c2_res_id };
957         struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
958                                                  c1_lockh, c2_lockh };
959         int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
960                               c1_lock_mode, c2_lock_mode };
961         ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
962                                             c1_policy, c2_policy};
963         int rc, i, j, sorted, flags;
964         ENTRY;
965
966         CDEBUG(D_DLMTRACE,
967                "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
968                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
969                res_id[3]->name[0]);
970
971         /* simple insertion sort - we have at most 4 elements */
972         for (i = 1; i < 4; i++) {
973                 j = i - 1;
974                 dlm_handles[4] = dlm_handles[i];
975                 res_id[4] = res_id[i];
976                 lock_modes[4] = lock_modes[i];
977                 policies[4] = policies[i];
978
979                 sorted = 0;
980                 do {
981                         if (res_gt(res_id[j], res_id[4], policies[j],
982                                    policies[4])) {
983                                 dlm_handles[j + 1] = dlm_handles[j];
984                                 res_id[j + 1] = res_id[j];
985                                 lock_modes[j + 1] = lock_modes[j];
986                                 policies[j + 1] = policies[j];
987                                 j--;
988                         } else {
989                                 sorted = 1;
990                         }
991                 } while (j >= 0 && !sorted);
992
993                 dlm_handles[j + 1] = dlm_handles[4];
994                 res_id[j + 1] = res_id[4];
995                 lock_modes[j + 1] = lock_modes[4];
996                 policies[j + 1] = policies[4];
997         }
998
999         CDEBUG(D_DLMTRACE,
1000                "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1001                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1002                res_id[3]->name[0]);
1003
1004         /* XXX we could send ASTs on all these locks first before blocking? */
1005         for (i = 0; i < 4; i++) {
1006                 flags = 0;
1007                 if (res_id[i]->name[0] == 0)
1008                         break;
1009                 if (i != 0 &&
1010                     !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1011                     (policies[i]->l_inodebits.bits &
1012                      policies[i-1]->l_inodebits.bits) ) {
1013                         memcpy(dlm_handles[i], dlm_handles[i-1],
1014                                sizeof(*(dlm_handles[i])));
1015                         ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1016                 } else {
1017                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1018                                               *res_id[i], LDLM_IBITS,
1019                                               policies[i],
1020                                               lock_modes[i], &flags,
1021                                               mds_blocking_ast,
1022                                               ldlm_completion_ast, NULL, NULL,
1023                                               NULL, 0, NULL, dlm_handles[i]);
1024                         if (rc != ELDLM_OK)
1025                                 GOTO(out_err, rc = -EIO);
1026                         ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1027                 }
1028         }
1029
1030         RETURN(0);
1031 out_err:
1032         while (i-- > 0)
1033                 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1034
1035         return rc;
1036 }
1037
1038 /* In the unlikely case that the child changed while we were waiting
1039  * on the lock, we need to drop the lock on the old child and either:
1040  * - if the child has a lower resource name, then we have to also
1041  *   drop the parent lock and regain the locks in the right order
1042  * - in the rename case, if the child has a lower resource name than one of
1043  *   the other parent/child resources (maxres) we also need to reget the locks
1044  * - if the child has a higher resource name (this is the common case)
1045  *   we can just get the lock on the new child (still in lock order)
1046  *
1047  * Returns 0 if the child did not change or if it changed but could be locked.
1048  * Returns 1 if the child changed and we need to re-lock (no locks held).
1049  * Returns -ve error with a valid dchild (no locks held). */
1050 static int mds_verify_child(struct obd_device *obd,
1051                             struct ldlm_res_id *parent_res_id,
1052                             struct lustre_handle *parent_lockh,
1053                             struct dentry *dparent, int parent_mode,
1054                             struct ldlm_res_id *child_res_id,
1055                             struct lustre_handle *child_lockh,
1056                             struct dentry **dchildp, int child_mode,
1057                             ldlm_policy_data_t *child_policy,
1058                             const char *name, int namelen,
1059                             struct ldlm_res_id *maxres)
1060 {
1061         struct dentry *vchild, *dchild = *dchildp;
1062         int rc = 0, cleanup_phase = 2; /* parent, child locks */
1063         ENTRY;
1064
1065         vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1066         if (IS_ERR(vchild))
1067                 GOTO(cleanup, rc = PTR_ERR(vchild));
1068
1069         if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1070                 if  (child_res_id->name[0] == vchild->d_inum &&
1071                                 child_res_id->name[1] == vchild->d_generation) {
1072                         if (dchild != NULL)
1073                                 l_dput(dchild);
1074                         *dchildp = vchild;
1075                         RETURN(0);
1076                 }
1077                 goto changed;
1078         }
1079
1080         if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1081                    (vchild->d_inode != NULL &&
1082                     child_res_id->name[0] == vchild->d_inode->i_ino &&
1083                     child_res_id->name[1] == vchild->d_inode->i_generation))) {
1084                 if (dchild != NULL)
1085                         l_dput(dchild);
1086                 *dchildp = vchild;
1087
1088                 RETURN(0);
1089         }
1090
1091 changed:
1092         CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1093                vchild->d_inode, dchild ? dchild->d_inode : 0,
1094                vchild->d_inode ? vchild->d_inode->i_ino : 0,
1095                child_res_id->name[0]);
1096         if (child_res_id->name[0] != 0)
1097                 ldlm_lock_decref(child_lockh, child_mode);
1098         if (dchild)
1099                 l_dput(dchild);
1100
1101         cleanup_phase = 1; /* parent lock only */
1102         *dchildp = dchild = vchild;
1103
1104         if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1105                 int flags = 0;
1106                 if (dchild->d_inode) {
1107                         child_res_id->name[0] = dchild->d_inode->i_ino;
1108                         child_res_id->name[1] = dchild->d_inode->i_generation;
1109                 } else {
1110                         child_res_id->name[0] = dchild->d_inum;
1111                         child_res_id->name[1] = dchild->d_generation;
1112                 }
1113
1114                 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1115                     res_gt(maxres, child_res_id, NULL, NULL)) {
1116                         CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1117                                child_res_id->name[0], parent_res_id->name[0],
1118                                maxres->name[0]);
1119                         GOTO(cleanup, rc = 1);
1120                 }
1121
1122                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1123                                       *child_res_id, LDLM_IBITS, child_policy,
1124                                       child_mode, &flags, mds_blocking_ast,
1125                                       ldlm_completion_ast, NULL, NULL, NULL, 0,
1126                                       NULL, child_lockh);
1127                 if (rc != ELDLM_OK)
1128                         GOTO(cleanup, rc = -EIO);
1129
1130         } else {
1131                 memset(child_res_id, 0, sizeof(*child_res_id));
1132         }
1133
1134         EXIT;
1135 cleanup:
1136         if (rc) {
1137                 switch(cleanup_phase) {
1138                 case 2:
1139                         if (child_res_id->name[0] != 0)
1140                                 ldlm_lock_decref(child_lockh, child_mode);
1141                 case 1:
1142                         ldlm_lock_decref(parent_lockh, parent_mode);
1143                 }
1144         }
1145         return rc;
1146 }
1147
1148 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1149                                 struct ll_fid *fid,
1150                                 struct lustre_handle *parent_lockh,
1151                                 struct dentry **dparentp, int parent_mode,
1152                                 __u64 parent_lockpart,
1153                                 char *name, int namelen,
1154                                 struct lustre_handle *child_lockh,
1155                                 struct dentry **dchildp, int child_mode,
1156                                 __u64 child_lockpart)
1157 {
1158         struct ldlm_res_id child_res_id = { .name = {0} };
1159         struct ldlm_res_id parent_res_id = { .name = {0} };
1160         ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1161         ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1162         struct inode *inode;
1163         int rc = 0, cleanup_phase = 0;
1164         ENTRY;
1165
1166         /* Step 1: Lookup parent */
1167         *dparentp = mds_fid2dentry(mds, fid, NULL);
1168         if (IS_ERR(*dparentp))
1169                 RETURN(rc = PTR_ERR(*dparentp));
1170         LASSERT((*dparentp)->d_inode);
1171
1172         CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1173                (*dparentp)->d_inode->i_ino, name);
1174
1175         parent_res_id.name[0] = (*dparentp)->d_inode->i_ino;
1176         parent_res_id.name[1] = (*dparentp)->d_inode->i_generation;
1177 #ifdef S_PDIROPS
1178         parent_lockh[1].cookie = 0;
1179         if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1180                 /* lock just dir { ino, generation } to flush client cache */
1181                 if (parent_mode == LCK_PW) {
1182                         struct ldlm_res_id res_id = { .name = {0} };
1183                         ldlm_policy_data_t policy;
1184                         int flags = 0;
1185                         res_id.name[0] = (*dparentp)->d_inode->i_ino;
1186                         res_id.name[1] = (*dparentp)->d_inode->i_generation;
1187                         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1188                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1189                                               res_id, LDLM_IBITS,
1190                                               &policy, LCK_CW, &flags,
1191                                               mds_blocking_ast,
1192                                               ldlm_completion_ast, NULL, NULL,
1193                                               NULL, 0, NULL, parent_lockh+1);
1194                         if (rc != ELDLM_OK)
1195                                 RETURN(-ENOLCK);
1196                 }
1197
1198                 parent_res_id.name[2] = full_name_hash(name, namelen - 1);
1199                 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
1200                        (*dparentp)->d_inode->i_ino, 
1201                        (*dparentp)->d_inode->i_generation,
1202                        parent_res_id.name[2]);
1203         }
1204 #endif
1205
1206         cleanup_phase = 1; /* parent dentry */
1207
1208         /* Step 2: Lookup child (without DLM lock, to get resource name) */
1209         *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1210         if (IS_ERR(*dchildp)) {
1211                 rc = PTR_ERR(*dchildp);
1212                 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1213                 GOTO(cleanup, rc);
1214         }
1215
1216         if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1217                 /* inode lives on another MDS: return * mds/ino/gen
1218                  * and LOOKUP lock. drop possible UPDATE lock! */
1219                 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1220                 child_res_id.name[0] = (*dchildp)->d_inum;
1221                 child_res_id.name[1] = (*dchildp)->d_generation;
1222                 goto retry_locks;
1223         }
1224
1225         inode = (*dchildp)->d_inode;
1226         if (inode != NULL)
1227                 inode = igrab(inode);
1228         if (inode == NULL)
1229                 goto retry_locks;
1230
1231         child_res_id.name[0] = inode->i_ino;
1232         child_res_id.name[1] = inode->i_generation;
1233
1234         iput(inode);
1235
1236 retry_locks:
1237         cleanup_phase = 2; /* child dentry */
1238
1239         /* Step 3: Lock parent and child in resource order.  If child doesn't
1240          *         exist, we still have to lock the parent and re-lookup. */
1241         rc = enqueue_ordered_locks(obd,&parent_res_id,parent_lockh,parent_mode,
1242                                    &parent_policy, &child_res_id, child_lockh,
1243                                    child_mode, &child_policy);
1244         if (rc)
1245                 GOTO(cleanup, rc);
1246
1247         if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1248                 cleanup_phase = 4; /* child lock */
1249         else
1250                 cleanup_phase = 3; /* parent lock */
1251
1252         /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1253         rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1254                               parent_mode, &child_res_id, child_lockh, 
1255                               dchildp, child_mode, &child_policy,
1256                               name, namelen, &parent_res_id);
1257         if (rc > 0)
1258                 goto retry_locks;
1259         if (rc < 0) {
1260                 cleanup_phase = 3;
1261                 GOTO(cleanup, rc);
1262         }
1263
1264 cleanup:
1265         if (rc) {
1266                 switch (cleanup_phase) {
1267                 case 4:
1268                         ldlm_lock_decref(child_lockh, child_mode);
1269                 case 3:
1270                         ldlm_lock_decref(parent_lockh, parent_mode);
1271                 case 2:
1272                         l_dput(*dchildp);
1273                 case 1:
1274 #ifdef S_PDIROPS
1275                         if (parent_lockh[1].cookie)
1276                                 ldlm_lock_decref(parent_lockh + 1, LCK_CW);
1277 #endif
1278                         l_dput(*dparentp);
1279                 default: ;
1280                 }
1281         }
1282         return rc;
1283 }
1284
1285 void mds_reconstruct_generic(struct ptlrpc_request *req)
1286 {
1287         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1288
1289         mds_req_from_mcd(req, med->med_mcd);
1290 }
1291
1292 int mds_create_local_dentry(struct mds_update_record *rec,
1293                            struct obd_device *obd)
1294 {
1295         struct mds_obd *mds = &obd->u.mds;
1296         struct inode *fids_dir = mds->mds_fids_dir->d_inode;
1297         int fidlen = 0, rc, cleanup_phase = 0;
1298         struct dentry *new_child = NULL;
1299         char *fidname = rec->ur_name;
1300         struct dentry *child = NULL;
1301         struct lustre_handle lockh;
1302         void *handle;
1303         ENTRY;
1304
1305         down(&fids_dir->i_sem);
1306         fidlen = ll_fid2str(fidname, rec->ur_fid1->id, rec->ur_fid1->generation);
1307         CDEBUG(D_OTHER, "look for local dentry '%s' for %u/%u\n",
1308                         fidname, (unsigned) rec->ur_fid1->id,
1309                         (unsigned) rec->ur_fid1->generation);
1310
1311         new_child = lookup_one_len(fidname, mds->mds_fids_dir, fidlen);
1312         up(&fids_dir->i_sem);
1313         if (IS_ERR(new_child)) {
1314                 CERROR("can't lookup %s: %d\n", fidname,
1315                                 (int) PTR_ERR(new_child));
1316                 GOTO(cleanup, rc = PTR_ERR(new_child));
1317         }
1318         cleanup_phase = 1;
1319
1320         if (new_child->d_inode != NULL) {
1321                 /* nice. we've already have local dentry! */
1322                 CERROR("found dentry in FIDS/: %u/%u\n", 
1323                        (unsigned) new_child->d_inode->i_ino,
1324                        (unsigned) new_child->d_inode->i_generation);
1325                 rec->ur_fid1->id = fids_dir->i_ino;
1326                 rec->ur_fid1->generation = fids_dir->i_generation;
1327                 rec->ur_namelen = fidlen + 1;
1328                 GOTO(cleanup, rc = 0);
1329         }
1330
1331         /* new, local dentry will be added soon. we need no aliases here */
1332         d_drop(new_child);
1333
1334         child = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_EX,
1335                                       &lockh, NULL, 0, MDS_INODELOCK_UPDATE);
1336         if (IS_ERR(child)) {
1337                 CERROR("can't get victim\n");
1338                 GOTO(cleanup, rc = PTR_ERR(child));
1339         }
1340         cleanup_phase = 2;
1341
1342         handle = fsfilt_start(obd, fids_dir, FSFILT_OP_LINK, NULL);
1343         if (IS_ERR(handle))
1344                 GOTO(cleanup, rc = PTR_ERR(handle));
1345
1346         rc = fsfilt_add_dir_entry(obd, mds->mds_fids_dir, fidname, fidlen,
1347                                   rec->ur_fid1->id, rec->ur_fid1->generation,
1348                                   mds->mds_num);
1349         if (rc)
1350                 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1351                        (unsigned long) child->d_inode->i_ino,
1352                        (unsigned long) child->d_inode->i_generation, rc);
1353         else {
1354                 if (S_ISDIR(child->d_inode->i_mode)) {
1355                         fids_dir->i_nlink++;
1356                         mark_inode_dirty(fids_dir);
1357                 }
1358                 mark_inode_dirty(child->d_inode);
1359         }
1360         fsfilt_commit(obd, fids_dir, handle, 0);
1361
1362         rec->ur_fid1->id = fids_dir->i_ino;
1363         rec->ur_fid1->generation = fids_dir->i_generation;
1364         rec->ur_namelen = fidlen + 1;
1365
1366 cleanup:
1367         switch(cleanup_phase) {
1368                 case 2:
1369                         ldlm_lock_decref(&lockh, LCK_EX);
1370                         dput(child);
1371                 case 1:
1372                         dput(new_child);
1373                 case 0:
1374                        break; 
1375         }
1376         RETURN(rc);
1377 }
1378
1379 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1380                                         struct ptlrpc_request *slave)
1381 {
1382         void *cookie, *cookie2;
1383         struct mds_body *body2;
1384         struct mds_body *body;
1385         void *ea, *ea2;
1386         ENTRY;
1387
1388         body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1389         LASSERT(body != NULL);
1390
1391         body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1392         LASSERT(body2 != NULL);
1393
1394         if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER))) {
1395                 RETURN(0);
1396         }
1397
1398         memcpy(body2, body, sizeof(*body));
1399         body2->valid &= ~OBD_MD_FLCOOKIE;
1400
1401         if (!(body->valid & OBD_MD_FLEASIZE))
1402                 RETURN(0);
1403
1404         if (body->eadatasize == 0) {
1405                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1406                 RETURN(0);
1407         }
1408
1409         LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1410         
1411         ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1412         LASSERT(ea != NULL);
1413         
1414         ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1415         LASSERT(ea2 != NULL);
1416
1417         memcpy(ea2, ea, body->eadatasize);
1418
1419         if (body->valid & OBD_MD_FLCOOKIE) {
1420                 LASSERT(master->rq_repmsg->buflens[2] >=
1421                                 slave->rq_repmsg->buflens[2]);
1422                 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1423                                 slave->rq_repmsg->buflens[2]);
1424                 LASSERT(cookie != NULL);
1425
1426                 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1427                                 master->rq_repmsg->buflens[2]);
1428                 LASSERT(cookie2 != NULL);
1429                 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1430                 body2->valid |= OBD_MD_FLCOOKIE;
1431         }
1432         RETURN(0);
1433 }
1434
1435 static int mds_reint_unlink_remote(struct mds_update_record *rec, int offset,
1436                                    struct ptlrpc_request *req,
1437                                    struct lustre_handle *parent_lockh,
1438                                    struct dentry *dparent,
1439                                    struct lustre_handle *child_lockh,
1440                                    struct dentry *dchild)
1441 {
1442         struct mds_obd *mds = mds_req2mds(req);
1443         struct mdc_op_data op_data;
1444         int rc = 0, cleanup_phase = 0;
1445         struct ptlrpc_request *request = NULL;
1446         ENTRY;
1447
1448         LASSERT(offset == 0 || offset == 2);
1449
1450         DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode %u/%u/%u)\n",
1451                   rec->ur_namelen - 1, rec->ur_name, (unsigned)dchild->d_mdsnum,
1452                   (unsigned) dchild->d_inum, (unsigned) dchild->d_generation);
1453
1454         /* time to drop i_nlink on remote MDS */ 
1455         op_data.fid1.mds = dchild->d_mdsnum;
1456         op_data.fid1.id = dchild->d_inum;
1457         op_data.fid1.generation = dchild->d_generation;
1458         op_data.create_mode = rec->ur_mode;
1459         op_data.namelen = 0;
1460         op_data.name = NULL;
1461         rc = md_unlink(mds->mds_lmv_exp, &op_data, &request);
1462         cleanup_phase = 2;
1463         if (request) {
1464                 mds_copy_unlink_reply(req, request);
1465                 ptlrpc_req_finished(request);
1466         }
1467         if (rc == 0)
1468                 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
1469         req->rq_status = rc;
1470
1471 #ifdef S_PDIROPS
1472         if (parent_lockh[1].cookie != 0)
1473                 ldlm_lock_decref(parent_lockh + 1, LCK_CW);
1474 #endif
1475         ldlm_lock_decref(child_lockh, LCK_EX);
1476         if (rc)
1477                 ldlm_lock_decref(parent_lockh, LCK_PW);
1478         else
1479                 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1480         l_dput(dchild);
1481         l_dput(dparent);
1482
1483         return 0;
1484 }
1485
1486 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1487                             struct ptlrpc_request *req,
1488                             struct lustre_handle *lh)
1489 {
1490         struct dentry *dparent, *dchild;
1491         struct mds_obd *mds = mds_req2mds(req);
1492         struct obd_device *obd = req->rq_export->exp_obd;
1493         struct mds_body *body = NULL;
1494         struct inode *child_inode;
1495         struct lustre_handle parent_lockh[2], child_lockh, child_reuse_lockh;
1496         char fidname[LL_FID_NAMELEN];
1497         void *handle = NULL;
1498         int rc = 0, log_unlink = 0, cleanup_phase = 0;
1499         int unlink_by_fid = 0;
1500         ENTRY;
1501
1502         LASSERT(offset == 0 || offset == 2);
1503
1504         DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1505                   rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name);
1506
1507         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1508
1509         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1510                 GOTO(cleanup, rc = -ENOENT);
1511
1512         if (rec->ur_namelen == 1) {
1513                 /* this is request to drop i_nlink on local inode */
1514                 unlink_by_fid = 1;
1515                 rec->ur_name = fidname;
1516                 rc = mds_create_local_dentry(rec, obd);
1517                 LASSERT(rc == 0);
1518         }
1519         rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1,
1520                                          parent_lockh, &dparent, LCK_PW,
1521                                          MDS_INODELOCK_UPDATE,
1522                                          rec->ur_name, rec->ur_namelen,
1523                                          &child_lockh, &dchild, LCK_EX,
1524                                          MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE);
1525         if (rc)
1526                 GOTO(cleanup, rc);
1527
1528         if (dchild->d_flags & DCACHE_CROSS_REF) {
1529                 /* we should have parent lock only here */
1530                 LASSERT(unlink_by_fid == 0);
1531                 LASSERT(dchild->d_mdsnum != mds->mds_num);
1532                 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
1533                                              dparent, &child_lockh, dchild);
1534                 RETURN(0);
1535         }
1536
1537         cleanup_phase = 1; /* dchild, dparent, locks */
1538
1539         dget(dchild);
1540         child_inode = dchild->d_inode;
1541         if (child_inode == NULL) {
1542                 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1543                        dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
1544                 GOTO(cleanup, rc = -ENOENT);
1545         }
1546
1547         cleanup_phase = 2; /* dchild has a lock */
1548
1549         /* Step 4: Get a lock on the ino to sync with creation WRT inode
1550          * reuse (see bug 2029). */
1551         rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
1552         if (rc != ELDLM_OK)
1553                 GOTO(cleanup, rc);
1554
1555         cleanup_phase = 3; /* child inum lock */
1556
1557         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
1558
1559         /* ldlm_reply in buf[0] if called via intent */
1560         if (offset)
1561                 offset = 1;
1562
1563         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
1564         LASSERT(body != NULL);
1565
1566         /* If this is the last reference to this inode, get the OBD EA
1567          * data first so the client can destroy OST objects.
1568          * we only do the object removal if no open files remain.
1569          * Nobody can get at this name anymore because of the locks so
1570          * we make decisions here as to whether to remove the inode */
1571         if (S_ISREG(child_inode->i_mode) && child_inode->i_nlink == 1 &&
1572             mds_open_orphan_count(child_inode) == 0) {
1573                 mds_pack_inode2fid(obd, &body->fid1, child_inode);
1574                 mds_pack_inode2body(obd, body, child_inode);
1575                 mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
1576                             child_inode, 1);
1577                 if (!(body->valid & OBD_MD_FLEASIZE)) {
1578                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1579                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
1580                 } else {
1581                         log_unlink = 1;
1582                 }
1583         }
1584
1585         /* We have to do these checks ourselves, in case we are making an
1586          * orphan.  The client tells us whether rmdir() or unlink() was called,
1587          * so we need to return appropriate errors (bug 72).
1588          *
1589          * We don't have to check permissions, because vfs_rename (called from
1590          * mds_open_unlink_rename) also calls may_delete. */
1591         if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1592                 if (!S_ISDIR(child_inode->i_mode))
1593                         GOTO(cleanup, rc = -ENOTDIR);
1594         } else {
1595                 if (S_ISDIR(child_inode->i_mode))
1596                         GOTO(cleanup, rc = -EISDIR);
1597         }
1598
1599         if (child_inode->i_nlink == (S_ISDIR(child_inode->i_mode) ? 2 : 1) &&
1600             mds_open_orphan_count(child_inode) > 0) {
1601                 rc = mds_open_unlink_rename(rec, obd, dparent, dchild, &handle);
1602                 cleanup_phase = 4; /* transaction */
1603                 GOTO(cleanup, rc);
1604         }
1605
1606         /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
1607         switch (child_inode->i_mode & S_IFMT) {
1608         case S_IFDIR:
1609                 /* Drop any lingering child directories before we start our
1610                  * transaction, to avoid doing multiple inode dirty/delete
1611                  * in our compound transaction (bug 1321). */
1612                 shrink_dcache_parent(dchild);
1613                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1614                                       NULL);
1615                 if (IS_ERR(handle))
1616                         GOTO(cleanup, rc = PTR_ERR(handle));
1617                 cleanup_phase = 4; /* transaction */
1618                 rc = vfs_rmdir(dparent->d_inode, dchild);
1619                 break;
1620         case S_IFREG: {
1621 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
1622                 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
1623                                                         offset + 1, 0);
1624                 handle = fsfilt_start_log(obd, dparent->d_inode,
1625                                           FSFILT_OP_UNLINK, NULL,
1626                                           le32_to_cpu(lmm->lmm_stripe_count));
1627                 if (IS_ERR(handle))
1628                         GOTO(cleanup, rc = PTR_ERR(handle));
1629
1630                 cleanup_phase = 4; /* transaction */
1631                 rc = vfs_unlink(dparent->d_inode, dchild);
1632
1633                 if (!rc && log_unlink)
1634                         if (mds_log_op_unlink(obd, child_inode,
1635                                 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
1636                                 req->rq_repmsg->buflens[offset + 1],
1637                                 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
1638                                 req->rq_repmsg->buflens[offset + 2]) > 0)
1639                                 body->valid |= OBD_MD_FLCOOKIE;
1640                 break;
1641         }
1642         case S_IFLNK:
1643         case S_IFCHR:
1644         case S_IFBLK:
1645         case S_IFIFO:
1646         case S_IFSOCK:
1647                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
1648                                       NULL);
1649                 if (IS_ERR(handle))
1650                         GOTO(cleanup, rc = PTR_ERR(handle));
1651                 cleanup_phase = 4; /* transaction */
1652                 rc = vfs_unlink(dparent->d_inode, dchild);
1653                 break;
1654         default:
1655                 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
1656                        rec->ur_name);
1657                 LBUG();
1658                 GOTO(cleanup, rc = -EINVAL);
1659         }
1660
1661  cleanup:
1662         if (rc == 0) {
1663                 struct iattr iattr;
1664                 int err;
1665
1666                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1667                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1668                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1669
1670                 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1671                 if (err)
1672                         CERROR("error on parent setattr: rc = %d\n", err);
1673         }
1674
1675         switch(cleanup_phase) {
1676         case 4:
1677                 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1678                                         rc, 0);
1679                 if (!rc)
1680                         (void)obd_set_info(mds->mds_osc_exp, strlen("unlinked"),
1681                                            "unlinked", 0, NULL);
1682         case 3: /* child ino-reuse lock */
1683                 if (rc && body != NULL) {
1684                         // Don't unlink the OST objects if the MDS unlink failed
1685                         body->valid = 0;
1686                 }
1687                 if (rc)
1688                         ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
1689                 else
1690                         ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
1691         case 2: /* child lock */
1692                 ldlm_lock_decref(&child_lockh, LCK_EX);
1693         case 1: /* child and parent dentry, parent lock */
1694 #ifdef S_PDIROPS
1695                 if (parent_lockh[1].cookie != 0)
1696                         ldlm_lock_decref(parent_lockh + 1, LCK_CW);
1697 #endif
1698                 if (rc)
1699                         ldlm_lock_decref(parent_lockh, LCK_PW);
1700                 else
1701                         ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1702                 l_dput(dchild);
1703                 l_dput(dchild);
1704                 l_dput(dparent);
1705         case 0:
1706                 break;
1707         default:
1708                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1709                 LBUG();
1710         }
1711         req->rq_status = rc;
1712         return 0;
1713 }
1714
1715 /*
1716  * to service requests from remote MDS to increment i_nlink
1717  */
1718 static int mds_reint_link_acquire(struct mds_update_record *rec,
1719                                  int offset, struct ptlrpc_request *req,
1720                                  struct lustre_handle *lh)
1721 {
1722         struct obd_device *obd = req->rq_export->exp_obd;
1723         struct ldlm_res_id src_res_id = { .name = {0} };
1724         struct lustre_handle *handle = NULL, src_lockh;
1725         struct mds_obd *mds = mds_req2mds(req);
1726         int rc = 0, cleanup_phase = 0;
1727         struct dentry *de_src = NULL;
1728         ldlm_policy_data_t policy;
1729         int flags = 0;
1730         ENTRY;
1731
1732         DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks %u/%u/%u\n",
1733                   obd->obd_name, (unsigned) rec->ur_fid1->mds,
1734                   (unsigned) rec->ur_fid1->id,
1735                   (unsigned) rec->ur_fid1->generation);
1736
1737         /* Step 1: Lookup the source inode and target directory by FID */
1738         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1739         if (IS_ERR(de_src))
1740                 GOTO(cleanup, rc = PTR_ERR(de_src));
1741         cleanup_phase = 1; /* source dentry */
1742
1743         src_res_id.name[0] = de_src->d_inode->i_ino;
1744         src_res_id.name[1] = de_src->d_inode->i_generation;
1745         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1746
1747         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1748                         src_res_id, LDLM_IBITS, &policy,
1749                         LCK_EX, &flags, mds_blocking_ast,
1750                         ldlm_completion_ast, NULL, NULL,
1751                         NULL, 0, NULL, &src_lockh);
1752         if (rc != ELDLM_OK)
1753                 GOTO(cleanup, rc = -ENOLCK);
1754         cleanup_phase = 2; /* lock */
1755
1756         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1757
1758         handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
1759         if (IS_ERR(handle)) {
1760                 rc = PTR_ERR(handle);
1761                 GOTO(cleanup, rc);
1762         }
1763         de_src->d_inode->i_nlink++;
1764         mark_inode_dirty(de_src->d_inode);
1765
1766 cleanup:
1767         rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
1768                                         handle, req, rc, 0);
1769         EXIT;
1770         switch (cleanup_phase) {
1771                 case 2:
1772                         if (rc)
1773                                 ldlm_lock_decref(&src_lockh, LCK_EX);
1774                         else
1775                                 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
1776                 case 1:
1777                         l_dput(de_src);
1778                 case 0:
1779                         break;
1780                 default:
1781                         CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1782                         LBUG();
1783         }
1784         req->rq_status = rc;
1785         return 0;
1786 }
1787
1788 /*
1789  * request to link to foreign inode:
1790  *  - acquire i_nlinks on this inode
1791  *  - add dentry
1792  */
1793 static int mds_reint_link_to_remote(struct mds_update_record *rec,
1794                                     int offset, struct ptlrpc_request *req,
1795                                     struct lustre_handle *lh)
1796 {
1797         struct lustre_handle *handle = NULL, tgt_dir_lockh[2];
1798         struct obd_device *obd = req->rq_export->exp_obd;
1799         struct dentry *de_tgt_dir = NULL;
1800         struct mds_obd *mds = mds_req2mds(req);
1801         int rc = 0, cleanup_phase = 0;
1802         struct mdc_op_data op_data;
1803         struct ptlrpc_request *request = NULL;
1804         ENTRY;
1805
1806 #define fmt     "%s: request to link %u/%u/%u:%*s to foreign inode %u/%u/%u\n"
1807         DEBUG_REQ(D_INODE, req, fmt, obd->obd_name,
1808                   (unsigned) rec->ur_fid2->mds,
1809                   (unsigned) rec->ur_fid2->id,
1810                   (unsigned) rec->ur_fid2->generation,
1811                   rec->ur_namelen - 1, rec->ur_name,
1812                   (unsigned) rec->ur_fid1->mds,
1813                   (unsigned) rec->ur_fid1->id,
1814                   (unsigned)rec->ur_fid1->generation);
1815
1816         de_tgt_dir = mds_fid2locked_dentry(obd, rec->ur_fid2, NULL, LCK_EX,
1817                                            tgt_dir_lockh, rec->ur_name,
1818                                            rec->ur_namelen - 1,
1819                                            MDS_INODELOCK_UPDATE);
1820         if (IS_ERR(de_tgt_dir))
1821                 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
1822         cleanup_phase = 1;
1823
1824         op_data.fid1 = *(rec->ur_fid1);
1825         op_data.namelen = 0;
1826         op_data.name = NULL;
1827         rc = md_link(mds->mds_lmv_exp, &op_data, &request);
1828         LASSERT(rc == 0);
1829         cleanup_phase = 2;
1830         if (request)
1831                 ptlrpc_req_finished(request);
1832
1833         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
1834
1835         handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
1836         if (IS_ERR(handle)) {
1837                 rc = PTR_ERR(handle);
1838                 GOTO(cleanup, rc);
1839         }
1840         
1841         rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
1842                                   rec->ur_namelen - 1, rec->ur_fid1->id,
1843                                   rec->ur_fid1->generation, rec->ur_fid1->mds);
1844         cleanup_phase = 3;
1845
1846 cleanup:
1847         rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
1848                                 handle, req, rc, 0);
1849         EXIT;
1850
1851         switch (cleanup_phase) {
1852                 case 3:
1853                         if (rc) {
1854                                 /* FIXME: drop i_nlink on remote inode here */
1855                                 CERROR("MUST drop drop i_nlink here\n");
1856                         }
1857                 case 2:
1858                 case 1:
1859                         if (rc) {
1860                                 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
1861 #ifdef S_PDIROPS
1862                                 ldlm_lock_decref(tgt_dir_lockh + 1, LCK_CW);
1863 #endif
1864                         } else {
1865                                 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
1866 #ifdef S_PDIROPS
1867                                 ptlrpc_save_lock(req, tgt_dir_lockh + 1, LCK_CW);
1868 #endif
1869                         }
1870                         l_dput(de_tgt_dir);
1871                         break;
1872                 default:
1873                         CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1874                         LBUG();
1875         }
1876         req->rq_status = rc;
1877         return 0;
1878 }
1879
1880 static int mds_reint_link(struct mds_update_record *rec, int offset,
1881                           struct ptlrpc_request *req,
1882                           struct lustre_handle *lh)
1883 {
1884         struct obd_device *obd = req->rq_export->exp_obd;
1885         struct dentry *de_src = NULL;
1886         struct dentry *de_tgt_dir = NULL;
1887         struct dentry *dchild = NULL;
1888         struct mds_obd *mds = mds_req2mds(req);
1889         struct lustre_handle *handle = NULL, tgt_dir_lockh[2], src_lockh;
1890         struct ldlm_res_id src_res_id = { .name = {0} };
1891         struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
1892         ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
1893         ldlm_policy_data_t tgt_dir_policy =
1894                                        {.l_inodebits = {MDS_INODELOCK_UPDATE}};
1895
1896         int rc = 0, cleanup_phase = 0;
1897         ENTRY;
1898
1899         LASSERT(offset == 0);
1900
1901         DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
1902                   rec->ur_fid1->id, rec->ur_fid1->generation,
1903                   rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_name);
1904
1905         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1906
1907         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1908                 GOTO(cleanup, rc = -ENOENT);
1909
1910         if (rec->ur_fid1->mds != mds->mds_num) {
1911                 rc = mds_reint_link_to_remote(rec, offset, req, lh);
1912                 RETURN(rc);
1913         }
1914         
1915         if (rec->ur_namelen == 1) {
1916                 rc = mds_reint_link_acquire(rec, offset, req, lh);
1917                 RETURN(rc);
1918         }
1919
1920         /* Step 1: Lookup the source inode and target directory by FID */
1921         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1922         if (IS_ERR(de_src))
1923                 GOTO(cleanup, rc = PTR_ERR(de_src));
1924
1925         cleanup_phase = 1; /* source dentry */
1926
1927         de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
1928         if (IS_ERR(de_tgt_dir))
1929                 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
1930
1931         cleanup_phase = 2; /* target directory dentry */
1932
1933         CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
1934                de_tgt_dir->d_name.len, de_tgt_dir->d_name.name, rec->ur_name,
1935                de_src->d_inode->i_ino);
1936
1937         /* Step 2: Take the two locks */
1938         src_res_id.name[0] = de_src->d_inode->i_ino;
1939         src_res_id.name[1] = de_src->d_inode->i_generation;
1940         tgt_dir_res_id.name[0] = de_tgt_dir->d_inode->i_ino;
1941         tgt_dir_res_id.name[1] = de_tgt_dir->d_inode->i_generation;
1942 #ifdef S_PDIROPS
1943         if (IS_PDIROPS(de_tgt_dir->d_inode)) {
1944                 int flags = 0;
1945                 /* Get a temp lock on just ino, gen to flush client cache */
1946                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1947                                       tgt_dir_res_id, LDLM_IBITS, &src_policy,
1948                                       LCK_CW, &flags, mds_blocking_ast,
1949                                       ldlm_completion_ast, NULL, NULL,
1950                                       NULL, 0, NULL, tgt_dir_lockh + 1);
1951                 if (rc != ELDLM_OK)
1952                         GOTO(cleanup, rc = -ENOLCK);
1953
1954                 tgt_dir_res_id.name[2] = full_name_hash(rec->ur_name,
1955                                                         rec->ur_namelen - 1);
1956                 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
1957                        de_tgt_dir->d_inode->i_ino,
1958                        de_tgt_dir->d_inode->i_generation,
1959                        tgt_dir_res_id.name[2]);
1960         }
1961 #endif
1962         rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
1963                                    &src_policy,
1964                                    &tgt_dir_res_id, tgt_dir_lockh, LCK_EX,
1965                                    &tgt_dir_policy);
1966         if (rc)
1967                 GOTO(cleanup, rc);
1968
1969         cleanup_phase = 3; /* locks */
1970
1971         /* Step 3: Lookup the child */
1972         dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen-1);
1973         if (IS_ERR(dchild)) {
1974                 rc = PTR_ERR(dchild);
1975                 if (rc != -EPERM && rc != -EACCES)
1976                         CERROR("child lookup error %d\n", rc);
1977                 GOTO(cleanup, rc);
1978         }
1979
1980         cleanup_phase = 4; /* child dentry */
1981
1982         if (dchild->d_inode) {
1983                 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
1984                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
1985                 rc = -EEXIST;
1986                 GOTO(cleanup, rc);
1987         }
1988
1989         /* Step 4: Do it. */
1990         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1991
1992         handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
1993         if (IS_ERR(handle)) {
1994                 rc = PTR_ERR(handle);
1995                 GOTO(cleanup, rc);
1996         }
1997
1998         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
1999         if (rc && rc != -EPERM && rc != -EACCES)
2000                 CERROR("vfs_link error %d\n", rc);
2001 cleanup:
2002         rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2003                                 handle, req, rc, 0);
2004         EXIT;
2005
2006         switch (cleanup_phase) {
2007         case 4: /* child dentry */
2008                 l_dput(dchild);
2009         case 3: /* locks */
2010                 if (rc) {
2011                         ldlm_lock_decref(&src_lockh, LCK_EX);
2012                         ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2013                 } else {
2014                         ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2015                         ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2016                 }
2017         case 2: /* target dentry */
2018 #ifdef S_PDIROPS
2019                 if (tgt_dir_lockh[1].cookie)
2020                         ldlm_lock_decref(tgt_dir_lockh + 1, LCK_CW);
2021 #endif
2022                 if (de_tgt_dir)
2023                         l_dput(de_tgt_dir);
2024         case 1: /* source dentry */
2025                 l_dput(de_src);
2026         case 0:
2027                 break;
2028         default:
2029                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2030                 LBUG();
2031         }
2032         req->rq_status = rc;
2033         return 0;
2034 }
2035
2036 /*
2037  * add a hard link in the PENDING directory, only used by rename()
2038  */
2039 static int mds_add_link_orphan(struct mds_update_record *rec,
2040                                struct obd_device *obd,
2041                                struct dentry *dentry)
2042 {
2043         struct mds_obd *mds = &obd->u.mds;
2044         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
2045         struct dentry *pending_child;
2046         char fidname[LL_FID_NAMELEN];
2047         int fidlen = 0, rc;
2048         ENTRY;
2049
2050         LASSERT(dentry->d_inode);
2051         LASSERT(!mds_inode_is_orphan(dentry->d_inode));
2052
2053         down(&pending_dir->i_sem);
2054         fidlen = ll_fid2str(fidname, dentry->d_inode->i_ino,
2055                             dentry->d_inode->i_generation);
2056
2057         CDEBUG(D_ERROR, "pending destroy of %dx open file %s = %s\n",
2058                mds_open_orphan_count(dentry->d_inode),
2059                rec->ur_name, fidname);
2060
2061         pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
2062         if (IS_ERR(pending_child))
2063                 GOTO(out_lock, rc = PTR_ERR(pending_child));
2064
2065         if (pending_child->d_inode != NULL) {
2066                 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
2067                 LASSERT(pending_child->d_inode == dentry->d_inode);
2068                 GOTO(out_dput, rc = 0);
2069         }
2070
2071         lock_kernel();
2072         rc = vfs_link(dentry, pending_dir, pending_child);
2073         unlock_kernel();
2074         if (rc)
2075                 CERROR("error addlink orphan %s to PENDING: rc = %d\n",
2076                        rec->ur_name, rc);
2077         else
2078                 mds_inode_set_orphan(dentry->d_inode);
2079 out_dput:
2080         l_dput(pending_child);
2081 out_lock:
2082         up(&pending_dir->i_sem);
2083         RETURN(rc);
2084 }
2085
2086 /* The idea here is that we need to get four locks in the end:
2087  * one on each parent directory, one on each child.  We need to take
2088  * these locks in some kind of order (to avoid deadlocks), and the order
2089  * I selected is "increasing resource number" order.  We need to look up
2090  * the children, however, before we know what the resource number(s) are.
2091  * Thus the following plan:
2092  *
2093  * 1,2. Look up the parents
2094  * 3,4. Look up the children
2095  * 5. Take locks on the parents and children, in order
2096  * 6. Verify that the children haven't changed since they were looked up
2097  *
2098  * If there was a race and the children changed since they were first looked
2099  * up, it is possible that mds_verify_child() will be able to just grab the
2100  * lock on the new child resource (if it has a higher resource than any other)
2101  * but we need to compare against not only its parent, but also against the
2102  * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2103  *
2104  * We need the fancy igrab() on the child inodes because we aren't holding a
2105  * lock on the parent after the lookup is done, so dentry->d_inode may change
2106  * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2107  */
2108 static int mds_get_parents_children_locked(struct obd_device *obd,
2109                                            struct mds_obd *mds,
2110                                            struct ll_fid *p1_fid,
2111                                            struct dentry **de_srcdirp,
2112                                            struct ll_fid *p2_fid,
2113                                            struct dentry **de_tgtdirp,
2114                                            int parent_mode,
2115                                            const char *old_name, int old_len,
2116                                            struct dentry **de_oldp,
2117                                            const char *new_name, int new_len,
2118                                            struct dentry **de_newp,
2119                                            struct lustre_handle *dlm_handles,
2120                                            int child_mode)
2121 {
2122         struct ldlm_res_id p1_res_id = { .name = {0} };
2123         struct ldlm_res_id p2_res_id = { .name = {0} };
2124         struct ldlm_res_id c1_res_id = { .name = {0} };
2125         struct ldlm_res_id c2_res_id = { .name = {0} };
2126         ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2127         /* Only dentry should change, but the inode itself would be
2128            intact otherwise */
2129         ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2130         /* If something is going to be replaced, both dentry and inode locks are
2131            needed */
2132         ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP|
2133                                                         MDS_INODELOCK_UPDATE}};
2134         struct ldlm_res_id *maxres_src, *maxres_tgt;
2135         struct inode *inode;
2136         int rc = 0, cleanup_phase = 0;
2137         ENTRY;
2138
2139         /* Step 1: Lookup the source directory */
2140         *de_srcdirp = mds_fid2dentry(mds, p1_fid, NULL);
2141         if (IS_ERR(*de_srcdirp))
2142                 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2143
2144         cleanup_phase = 1; /* source directory dentry */
2145
2146         p1_res_id.name[0] = (*de_srcdirp)->d_inode->i_ino;
2147         p1_res_id.name[1] = (*de_srcdirp)->d_inode->i_generation;
2148
2149         /* Step 2: Lookup the target directory */
2150         if (memcmp(p1_fid, p2_fid, sizeof(*p1_fid)) == 0) {
2151                 *de_tgtdirp = dget(*de_srcdirp);
2152         } else {
2153                 *de_tgtdirp = mds_fid2dentry(mds, p2_fid, NULL);
2154                 if (IS_ERR(*de_tgtdirp))
2155                         GOTO(cleanup, rc = PTR_ERR(*de_tgtdirp));
2156         }
2157
2158         cleanup_phase = 2; /* target directory dentry */
2159
2160         p2_res_id.name[0] = (*de_tgtdirp)->d_inode->i_ino;
2161         p2_res_id.name[1] = (*de_tgtdirp)->d_inode->i_generation;
2162
2163 #ifdef S_PDIROPS
2164         dlm_handles[5].cookie = 0;
2165         dlm_handles[6].cookie = 0;
2166         if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2167                 /* Get a temp lock on just ino, gen to flush client cache */
2168                 rc = enqueue_ordered_locks(obd, &p1_res_id, &(dlm_handles[5]),
2169                                            LCK_CW, &p_policy, &p2_res_id,
2170                                            &(dlm_handles[6]),LCK_CW,&p_policy);
2171                 if (rc != ELDLM_OK)
2172                         GOTO(cleanup, rc);
2173
2174                 p1_res_id.name[2] = full_name_hash(old_name, old_len - 1);
2175                 p2_res_id.name[2] = full_name_hash(new_name, new_len - 1);
2176                 CDEBUG(D_INFO, "take locks on %lu:%u:"LPX64", %lu:%u:"LPX64"\n",
2177                        (*de_srcdirp)->d_inode->i_ino,
2178                        (*de_srcdirp)->d_inode->i_generation, p1_res_id.name[2],
2179                        (*de_tgtdirp)->d_inode->i_ino,
2180                        (*de_tgtdirp)->d_inode->i_generation, p2_res_id.name[2]);
2181         }
2182 #endif
2183
2184         /* Step 3: Lookup the source child entry */
2185         *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp, old_len - 1);
2186         if (IS_ERR(*de_oldp)) {
2187                 rc = PTR_ERR(*de_oldp);
2188                 CERROR("old child lookup error (%*s): %d\n",
2189                        old_len - 1, old_name, rc);
2190                 GOTO(cleanup, rc);
2191         }
2192
2193         cleanup_phase = 3; /* original name dentry */
2194
2195         inode = (*de_oldp)->d_inode;
2196         if (inode != NULL) {
2197                 inode = igrab(inode);
2198                 if (inode == NULL)
2199                         GOTO(cleanup, rc = -ENOENT);
2200
2201                 c1_res_id.name[0] = inode->i_ino;
2202                 c1_res_id.name[1] = inode->i_generation;
2203                 iput(inode);
2204         } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2205                 c1_res_id.name[0] = (*de_oldp)->d_inum;
2206                 c1_res_id.name[1] = (*de_oldp)->d_generation;
2207         }
2208
2209         /* Step 4: Lookup the target child entry */
2210         *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp, new_len - 1);
2211         if (IS_ERR(*de_newp)) {
2212                 rc = PTR_ERR(*de_newp);
2213                 CERROR("new child lookup error (%*s): %d\n",
2214                        old_len - 1, old_name, rc);
2215                 GOTO(cleanup, rc);
2216         }
2217
2218         cleanup_phase = 4; /* target dentry */
2219
2220         inode = (*de_newp)->d_inode;
2221         if (inode != NULL) {
2222                 inode = igrab(inode);
2223                 if (inode == NULL)
2224                         goto retry_locks;
2225
2226                 c2_res_id.name[0] = inode->i_ino;
2227                 c2_res_id.name[1] = inode->i_generation;
2228                 iput(inode);
2229         } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2230                 c2_res_id.name[0] = (*de_newp)->d_inum;
2231                 c2_res_id.name[1] = (*de_newp)->d_generation;
2232         }
2233
2234 retry_locks:
2235         /* Step 5: Take locks on the parents and child(ren) */
2236         maxres_src = &p1_res_id;
2237         maxres_tgt = &p2_res_id;
2238         cleanup_phase = 4; /* target dentry */
2239
2240         if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL,NULL))
2241                 maxres_src = &c1_res_id;
2242         if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL,NULL))
2243                 maxres_tgt = &c2_res_id;
2244
2245         rc = enqueue_4ordered_locks(obd, &p1_res_id,&dlm_handles[0],parent_mode,
2246                                     &p_policy,
2247                                     &p2_res_id, &dlm_handles[1], parent_mode,
2248                                     &p_policy,
2249                                     &c1_res_id, &dlm_handles[2], child_mode,
2250                                     &c1_policy,
2251                                     &c2_res_id, &dlm_handles[3], child_mode,
2252                                     &c2_policy);
2253         if (rc)
2254                 GOTO(cleanup, rc);
2255
2256         cleanup_phase = 6; /* parent and child(ren) locks */
2257
2258         /* Step 6a: Re-lookup source child to verify it hasn't changed */
2259         rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2260                               parent_mode, &c1_res_id, &dlm_handles[2],
2261                               de_oldp, child_mode, &c1_policy, old_name,old_len,
2262                               maxres_tgt);
2263         if (rc) {
2264                 if (c2_res_id.name[0] != 0)
2265                         ldlm_lock_decref(&dlm_handles[3], child_mode);
2266                 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2267                 cleanup_phase = 4;
2268                 if (rc > 0)
2269                         goto retry_locks;
2270                 GOTO(cleanup, rc);
2271         }
2272
2273         if (!DENTRY_VALID(*de_oldp))
2274                 GOTO(cleanup, rc = -ENOENT);
2275
2276         /* Step 6b: Re-lookup target child to verify it hasn't changed */
2277         rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2278                               parent_mode, &c2_res_id, &dlm_handles[3],
2279                               de_newp, child_mode, &c2_policy, new_name,
2280                               new_len, maxres_src);
2281         if (rc) {
2282                 ldlm_lock_decref(&dlm_handles[2], child_mode);
2283                 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2284                 cleanup_phase = 4;
2285                 if (rc > 0)
2286                         goto retry_locks;
2287                 GOTO(cleanup, rc);
2288         }
2289
2290         EXIT;
2291 cleanup:
2292         if (rc) {
2293                 switch (cleanup_phase) {
2294                 case 6: /* child lock(s) */
2295                         if (c2_res_id.name[0] != 0)
2296                                 ldlm_lock_decref(&dlm_handles[3], child_mode);
2297                         if (c1_res_id.name[0] != 0)
2298                                 ldlm_lock_decref(&dlm_handles[2], child_mode);
2299                 case 5: /* parent locks */
2300                         ldlm_lock_decref(&dlm_handles[1], parent_mode);
2301                         ldlm_lock_decref(&dlm_handles[0], parent_mode);
2302                 case 4: /* target dentry */
2303                         l_dput(*de_newp);
2304                 case 3: /* source dentry */
2305                         l_dput(*de_oldp);
2306                 case 2: /* target directory dentry */
2307                         l_dput(*de_tgtdirp);
2308                 case 1: /* source directry dentry */
2309                         l_dput(*de_srcdirp);
2310                 }
2311         }
2312
2313         return rc;
2314 }
2315
2316 static int mds_reint_rename_create_name(struct mds_update_record *rec,
2317                                         int offset, struct ptlrpc_request *req)
2318 {
2319         struct obd_device *obd = req->rq_export->exp_obd;
2320         struct dentry *de_srcdir = NULL;
2321         struct dentry *de_new = NULL;
2322         struct mds_obd *mds = mds_req2mds(req);
2323         struct lustre_handle parent_lockh[2];
2324         struct lustre_handle child_lockh;
2325         int cleanup_phase = 0;
2326         void *handle = NULL;
2327         int rc = 0;
2328         ENTRY;
2329
2330         /* another MDS executing rename operation has asked us
2331          * to create target name. such a creation should destroy
2332          * existing target name */
2333
2334         CDEBUG(D_OTHER, "%s: request to create name %s for %lu/%lu/%lu\n",
2335                         obd->obd_name, rec->ur_tgt,
2336                         (unsigned long) rec->ur_fid1->mds,
2337                         (unsigned long) rec->ur_fid1->id,
2338                         (unsigned long) rec->ur_fid1->generation);
2339
2340         /* first, lookup the target */
2341         child_lockh.cookie = 0;
2342         rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid2, parent_lockh,
2343                                          &de_srcdir,LCK_PW,MDS_INODELOCK_UPDATE,
2344                                          rec->ur_tgt, rec->ur_tgtlen,
2345                                          &child_lockh, &de_new, LCK_EX,
2346                                          MDS_INODELOCK_LOOKUP);
2347         if (rc)
2348                 GOTO(cleanup, rc);
2349
2350         cleanup_phase = 1;
2351
2352         LASSERT(de_srcdir);
2353         LASSERT(de_srcdir->d_inode);
2354         LASSERT(de_new);
2355
2356         if (de_new->d_inode) {
2357                 /* name exists and points to local inode
2358                  * try to unlink this name and create new one */
2359                 CERROR("%s: %s points to local inode %lu/%lu\n",
2360                        obd->obd_name, rec->ur_tgt,
2361                        (unsigned long) de_new->d_inode->i_ino,
2362                        (unsigned long) de_new->d_inode->i_generation);
2363                 handle = fsfilt_start(obd, de_srcdir->d_inode,
2364                                       FSFILT_OP_RENAME, NULL);
2365                 if (IS_ERR(handle))
2366                         GOTO(cleanup, rc = PTR_ERR(handle));
2367                 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de_new);
2368                 if (rc)
2369                         GOTO(cleanup, rc);
2370         } else if (de_new->d_flags & DCACHE_CROSS_REF) {
2371                 /* name exists adn points to remove inode */
2372                 CERROR("%s: %s points to remote inode %lu/%lu/%lu\n",
2373                        obd->obd_name, rec->ur_tgt,
2374                        (unsigned long) de_new->d_mdsnum,
2375                        (unsigned long) de_new->d_inum,
2376                        (unsigned long) de_new->d_generation);
2377         } else {
2378                 /* name doesn't exist. the simplest case */
2379                 handle = fsfilt_start(obd, de_srcdir->d_inode,
2380                                       FSFILT_OP_LINK, NULL);
2381                 if (IS_ERR(handle))
2382                         GOTO(cleanup, rc = PTR_ERR(handle));
2383         }
2384        
2385         cleanup_phase = 2;
2386         rc = fsfilt_add_dir_entry(obd, de_srcdir, rec->ur_tgt,
2387                         rec->ur_tgtlen - 1, rec->ur_fid1->id,
2388                         rec->ur_fid1->generation, rec->ur_fid1->mds);
2389         if (rc)
2390                 CERROR("add_dir_entry() returned error %d\n", rc);
2391 cleanup:
2392         EXIT;
2393         rc = mds_finish_transno(mds, de_srcdir ? de_srcdir->d_inode : NULL,
2394                                 handle, req, rc, 0);
2395         switch(cleanup_phase) {
2396                 case 2:
2397                 case 1:
2398 #ifdef S_PDIROPS
2399                         if (parent_lockh[1].cookie != 0)
2400                                 ldlm_lock_decref(&parent_lockh[1], LCK_CW);
2401 #endif
2402                         ldlm_lock_decref(&parent_lockh[0], LCK_PW);
2403                         if (child_lockh.cookie != 0)
2404                                 ldlm_lock_decref(&child_lockh, LCK_EX);
2405                         l_dput(de_new);
2406                         l_dput(de_srcdir);
2407                         break;
2408                 default:
2409                         LBUG();
2410         }
2411
2412         req->rq_status = rc;
2413
2414         RETURN(0);
2415 }
2416
2417 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
2418                                       struct ptlrpc_request *req)
2419 {
2420         struct obd_device *obd = req->rq_export->exp_obd;
2421         struct ptlrpc_request *req2 = NULL;
2422         struct dentry *de_srcdir = NULL;
2423         struct dentry *de_old = NULL;
2424         struct mds_obd *mds = mds_req2mds(req);
2425         struct lustre_handle parent_lockh[2];
2426         struct lustre_handle child_lockh;
2427         struct mdc_op_data opdata;
2428         int cleanup_phase = 0;
2429         void *handle = NULL;
2430         int rc = 0;
2431         ENTRY;
2432
2433         CDEBUG(D_OTHER, "%s: move name %s onto another mds%u\n",
2434                obd->obd_name, rec->ur_name, rec->ur_fid2->mds + 1);
2435         memset(&opdata, 0, sizeof(opdata));
2436
2437         child_lockh.cookie = 0;
2438         rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1, parent_lockh,
2439                                          &de_srcdir,LCK_PW,MDS_INODELOCK_UPDATE,
2440                                          rec->ur_name, rec->ur_namelen,
2441                                          &child_lockh, &de_old, LCK_EX,
2442                                          MDS_INODELOCK_LOOKUP);
2443         LASSERT(rc == 0);
2444         LASSERT(de_srcdir);
2445         LASSERT(de_srcdir->d_inode);
2446         LASSERT(de_old);
2447        
2448         /* we already know the target should be created on another MDS
2449          * so, we have to request that MDS to do it */
2450
2451         /* prepare source fid */
2452         if (de_old->d_flags & DCACHE_CROSS_REF) {
2453                 LASSERT(de_old->d_inode == NULL);
2454                 CDEBUG(D_OTHER, "request to move remote name\n");
2455                 opdata.fid1.mds = de_old->d_mdsnum;
2456                 opdata.fid1.id = de_old->d_inum;
2457                 opdata.fid1.generation = de_old->d_generation;
2458         } else if (de_old->d_inode == NULL) {
2459                 /* oh, source doesn't exist */
2460                 GOTO(cleanup, rc = -ENOENT);
2461         } else {
2462                 LASSERT(de_old->d_inode != NULL);
2463                 CDEBUG(D_OTHER, "request to move local name\n");
2464                 opdata.fid1.mds = mds->mds_num;
2465                 opdata.fid1.id = de_old->d_inode->i_ino;
2466                 opdata.fid1.generation = de_old->d_inode->i_generation;
2467         }
2468
2469         opdata.fid2 = *(rec->ur_fid2);
2470         rc = md_rename(mds->mds_lmv_exp, &opdata, NULL, 0, rec->ur_tgt,
2471                        rec->ur_tgtlen - 1, &req2);
2472        
2473         if (rc)
2474                 GOTO(cleanup, rc);
2475
2476         handle = fsfilt_start(obd, de_srcdir->d_inode, FSFILT_OP_UNLINK, NULL);
2477         if (IS_ERR(handle))
2478                 GOTO(cleanup, rc = PTR_ERR(handle));
2479         rc = fsfilt_del_dir_entry(obd, de_old);
2480         d_drop(de_old);
2481
2482 cleanup:
2483         EXIT;
2484         rc = mds_finish_transno(mds, de_srcdir ? de_srcdir->d_inode : NULL,
2485                                 handle, req, rc, 0);
2486         if (req2)
2487                 ptlrpc_req_finished(req2);
2488
2489 #ifdef S_PDIROPS
2490         if (parent_lockh[1].cookie != 0)
2491                 ldlm_lock_decref(&parent_lockh[1], LCK_CW);
2492 #endif
2493         ldlm_lock_decref(&parent_lockh[0], LCK_PW);
2494         if (child_lockh.cookie != 0)
2495                 ldlm_lock_decref(&child_lockh, LCK_EX);
2496
2497         l_dput(de_old);
2498         l_dput(de_srcdir);
2499
2500         req->rq_status = rc;
2501         RETURN(0);
2502
2503 }
2504
2505 static int mds_reint_rename(struct mds_update_record *rec, int offset,
2506                             struct ptlrpc_request *req,
2507                             struct lustre_handle *lockh)
2508 {
2509         struct obd_device *obd = req->rq_export->exp_obd;
2510         struct dentry *de_srcdir = NULL;
2511         struct dentry *de_tgtdir = NULL;
2512         struct dentry *de_old = NULL;
2513         struct dentry *de_new = NULL;
2514         struct mds_obd *mds = mds_req2mds(req);
2515         struct lustre_handle dlm_handles[7];
2516         struct mds_body *body = NULL;
2517         int rc = 0, lock_count = 3;
2518         int cleanup_phase = 0;
2519         void *handle = NULL;
2520         ENTRY;
2521
2522         LASSERT(offset == 0);
2523
2524         DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u %s to "LPU64"/%u %s",
2525                   rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name,
2526                   rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_tgt);
2527
2528         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2529
2530         if (rec->ur_namelen == 1) {
2531                 rc = mds_reint_rename_create_name(rec, offset, req);
2532                 RETURN(rc);
2533         }
2534
2535         if (rec->ur_fid2->mds != mds->mds_num) {
2536                 rc = mds_reint_rename_to_remote(rec, offset, req);
2537                 RETURN(rc);
2538         }
2539         
2540         rc = mds_get_parents_children_locked(obd, mds, rec->ur_fid1, &de_srcdir,
2541                                              rec->ur_fid2, &de_tgtdir, LCK_PW,
2542                                              rec->ur_name, rec->ur_namelen,
2543                                              &de_old, rec->ur_tgt,
2544                                              rec->ur_tgtlen, &de_new,
2545                                              dlm_handles, LCK_EX);
2546         if (rc)
2547                 GOTO(cleanup, rc);
2548
2549         cleanup_phase = 1; /* parent(s), children, locks */
2550
2551         if (de_new->d_inode)
2552                 lock_count = 4;
2553
2554         /* sanity check for src inode */
2555         if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
2556             de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
2557                 GOTO(cleanup, rc = -EINVAL);
2558
2559         /* sanity check for dest inode */
2560         if (de_new->d_inode &&
2561             (de_new->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
2562              de_new->d_inode->i_ino == de_tgtdir->d_inode->i_ino))
2563                 GOTO(cleanup, rc = -EINVAL);
2564
2565         if (de_old->d_inode == de_new->d_inode) {
2566                 GOTO(cleanup, rc = 0);
2567         }
2568
2569         /* if we are about to remove the target at first, pass the EA of
2570          * that inode to client to perform and cleanup on OST */
2571         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
2572         LASSERT(body != NULL);
2573
2574         if (de_new->d_inode &&
2575             S_ISREG(de_new->d_inode->i_mode) &&
2576             de_new->d_inode->i_nlink == 1 &&
2577             mds_open_orphan_count(de_new->d_inode) == 0) {
2578                 mds_pack_inode2fid(obd, &body->fid1, de_new->d_inode);
2579                 mds_pack_inode2body(obd, body, de_new->d_inode);
2580                 mds_pack_md(obd, req->rq_repmsg, 1, body, de_new->d_inode, 1);
2581                 if (!(body->valid & OBD_MD_FLEASIZE)) {
2582                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2583                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
2584                 } else {
2585                         /* XXX need log unlink? */
2586                 }
2587         }
2588
2589         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
2590                        de_srcdir->d_inode->i_sb);
2591
2592         handle = fsfilt_start(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME, NULL);
2593         if (IS_ERR(handle))
2594                 GOTO(cleanup, rc = PTR_ERR(handle));
2595
2596         /* FIXME need adjust the journal block count? */
2597         /* if the target should be moved to PENDING, we at first increase the
2598          * link and later vfs_rename() will decrease the link count again */
2599         if (de_new->d_inode &&
2600             S_ISREG(de_new->d_inode->i_mode) &&
2601             de_new->d_inode->i_nlink == 1 &&
2602             mds_open_orphan_count(de_new->d_inode) > 0) {
2603                 rc = mds_add_link_orphan(rec, obd, de_new);
2604                 if (rc)
2605                         GOTO(cleanup, rc);
2606         }
2607
2608         lock_kernel();
2609         de_old->d_fsdata = req;
2610         de_new->d_fsdata = req;
2611         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
2612         unlock_kernel();
2613
2614         GOTO(cleanup, rc);
2615 cleanup:
2616         rc = mds_finish_transno(mds, de_tgtdir ? de_tgtdir->d_inode : NULL,
2617                                 handle, req, rc, 0);
2618         switch (cleanup_phase) {
2619         case 1:
2620 #ifdef S_PDIROPS
2621                 if (dlm_handles[5].cookie != 0)
2622                         ldlm_lock_decref(&(dlm_handles[5]), LCK_CW);
2623                 if (dlm_handles[6].cookie != 0)
2624                         ldlm_lock_decref(&(dlm_handles[6]), LCK_CW);
2625 #endif
2626                 if (rc) {
2627                         if (lock_count == 4)
2628                                 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
2629                         ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
2630                         ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
2631                         ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
2632                 } else {
2633                         if (lock_count == 4)
2634                                 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
2635                         ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
2636                         ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
2637                         ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
2638                 }
2639                 l_dput(de_new);
2640                 l_dput(de_old);
2641                 l_dput(de_tgtdir);
2642                 l_dput(de_srcdir);
2643         case 0:
2644                 break;
2645         default:
2646                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2647                 LBUG();
2648         }
2649         req->rq_status = rc;
2650         return 0;
2651 }
2652
2653 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
2654                            struct ptlrpc_request *, struct lustre_handle *);
2655
2656 static mds_reinter reinters[REINT_MAX + 1] = {
2657         [REINT_SETATTR] mds_reint_setattr,
2658         [REINT_CREATE] mds_reint_create,
2659         [REINT_LINK] mds_reint_link,
2660         [REINT_UNLINK] mds_reint_unlink,
2661         [REINT_RENAME] mds_reint_rename,
2662         [REINT_OPEN] mds_open
2663 };
2664
2665 int mds_reint_rec(struct mds_update_record *rec, int offset,
2666                   struct ptlrpc_request *req, struct lustre_handle *lockh)
2667 {
2668         struct obd_device *obd = req->rq_export->exp_obd;
2669         struct obd_run_ctxt saved;
2670         int rc;
2671
2672         /* checked by unpacker */
2673         LASSERT(rec->ur_opcode <= REINT_MAX &&
2674                 reinters[rec->ur_opcode] != NULL);
2675
2676         push_ctxt(&saved, &obd->obd_ctxt, &rec->ur_uc);
2677         rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
2678         pop_ctxt(&saved, &obd->obd_ctxt, &rec->ur_uc);
2679
2680         return rc;
2681 }