Whamcloud - gitweb
- lmv_getattr_name() recognizes cross-node name and repeats getattr to proper
[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 = -ERESTART);
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 = -ERESTART);
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 (rc == 0 && nstripes) {
648                                 /* FIXME: error handling here */
649                                 mds_try_to_split_dir(obd, dchild,
650                                                         NULL, nstripes);
651                         }
652                 } else if (!DENTRY_VALID(dchild)) {
653                         /* inode will be created on another MDS */
654                         struct obdo *oa = NULL;
655                         struct mds_body *body;
656                         
657                         /* first, create that inode */
658                         oa = obdo_alloc();
659                         LASSERT(oa != NULL);
660                         oa->o_mds = i;
661                         obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
662                                         OBD_MD_FLMTIME | OBD_MD_FLCTIME |
663                                         OBD_MD_FLUID | OBD_MD_FLGID);
664                         oa->o_mode = dir->i_mode;
665                         CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
666                                         obd->obd_name, i);
667                         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
668                                 CWARN("%s: replay dir creation %*s -> %u/%u\n",
669                                       obd->obd_name, rec->ur_namelen - 1,
670                                       rec->ur_name, (unsigned) rec->ur_fid2->id,
671                                       (unsigned) rec->ur_fid2->generation);
672                                 oa->o_id = rec->ur_fid2->id;
673                                 oa->o_generation = rec->ur_fid2->generation;
674                                 oa->o_flags |= OBD_FL_RECREATE_OBJS;
675                         }
676
677                         rc = obd_create(mds->mds_lmv_exp, oa, NULL, NULL);
678                         LASSERT(rc == 0);
679                         
680                         /* now, add new dir entry for it */
681                         handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
682                         if (IS_ERR(handle))
683                                 GOTO(cleanup, rc = PTR_ERR(handle));
684                         rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
685                                                   rec->ur_namelen - 1,
686                                                   oa->o_id, oa->o_generation,
687                                                   i);
688                         LASSERT(rc == 0);
689
690                         /* fill reply */
691                         body = lustre_msg_buf(req->rq_repmsg,
692                                               offset, sizeof (*body));
693                         body->valid |= OBD_MD_FLID | OBD_MD_MDS;
694                         body->fid1.id = oa->o_id;
695                         body->fid1.mds = i;
696                         body->fid1.generation = oa->o_generation;
697                         obdo_free(oa);
698                 } else {
699                         /* requested name exists in the directory */
700                         rc = -EEXIST;
701                 }
702                 EXIT;
703                 break;
704         }
705         case S_IFLNK:{
706                 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
707                 if (IS_ERR(handle))
708                         GOTO(cleanup, rc = PTR_ERR(handle));
709                 if (rec->ur_tgt == NULL)        /* no target supplied */
710                         rc = -EINVAL;           /* -EPROTO? */
711                 else
712                         rc = vfs_symlink(dir, dchild, rec->ur_tgt);
713                 EXIT;
714                 break;
715         }
716         case S_IFCHR:
717         case S_IFBLK:
718         case S_IFIFO:
719         case S_IFSOCK:{
720                 int rdev = rec->ur_rdev;
721                 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
722                 if (IS_ERR(handle))
723                         GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
724                 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
725                 EXIT;
726                 break;
727         }
728         default:
729                 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
730                 dchild->d_fsdata = NULL;
731                 GOTO(cleanup, rc = -EINVAL);
732         }
733
734         /* In case we stored the desired inum in here, we want to clean up. */
735         if (dchild->d_fsdata == (void *)(unsigned long)rec->ur_fid2->id)
736                 dchild->d_fsdata = NULL;
737
738         if (rc) {
739                 CDEBUG(D_INODE, "error during create: %d\n", rc);
740                 GOTO(cleanup, rc);
741         } else if (dchild->d_inode) {
742                 struct iattr iattr;
743                 struct inode *inode = dchild->d_inode;
744                 struct mds_body *body;
745
746                 created = 1;
747                 LTIME_S(iattr.ia_atime) = rec->ur_time;
748                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
749                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
750                 iattr.ia_uid = rec->ur_fsuid;
751                 if (dir->i_mode & S_ISGID)
752                         iattr.ia_gid = dir->i_gid;
753                 else
754                         iattr.ia_gid = rec->ur_fsgid;
755                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
756                         ATTR_MTIME | ATTR_CTIME;
757
758                 if (rec->ur_fid2->id) {
759                         LASSERT(rec->ur_fid2->id == inode->i_ino);
760                         inode->i_generation = rec->ur_fid2->generation;
761                         /* Dirtied and committed by the upcoming setattr. */
762                         CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
763                                inode->i_ino, inode->i_generation);
764                 } else {
765                         struct lustre_handle child_ino_lockh;
766
767                         CDEBUG(D_INODE, "created ino %lu with gen %x\n",
768                                inode->i_ino, inode->i_generation);
769
770                         /* The inode we were allocated may have just been freed
771                          * by an unlink operation.  We take this lock to
772                          * synchronize against the matching reply-ack-lock taken
773                          * in unlink, to avoid replay problems if this reply
774                          * makes it out to the client but the unlink's does not.
775                          * See bug 2029 for more detail.*/
776                         rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
777                         if (rc != ELDLM_OK) {
778                                 CERROR("error locking for unlink/create sync: "
779                                        "%d\n", rc);
780                         } else {
781                                 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
782                         }
783                 }
784
785                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
786                 if (rc)
787                         CERROR("error on child setattr: rc = %d\n", rc);
788
789                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
790                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
791                 if (rc)
792                         CERROR("error on parent setattr: rc = %d\n", rc);
793
794                 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
795                 mds_pack_inode2fid(obd, &body->fid1, inode);
796                 mds_pack_inode2body(obd, body, inode);
797         }
798         EXIT;
799
800 cleanup:
801         err = mds_finish_transno(mds, dir, handle, req, rc, 0);
802
803         if (rc && created) {
804                 /* Destroy the file we just created.  This should not need
805                  * extra journal credits, as we have already modified all of
806                  * the blocks needed in order to create the file in the first
807                  * place.
808                  */
809                 switch (type) {
810                 case S_IFDIR:
811                         err = vfs_rmdir(dir, dchild);
812                         if (err)
813                                 CERROR("rmdir in error path: %d\n", err);
814                         break;
815                 default:
816                         err = vfs_unlink(dir, dchild);
817                         if (err)
818                                 CERROR("unlink in error path: %d\n", err);
819                         break;
820                 }
821         } else {
822                 rc = err;
823         }
824         switch (cleanup_phase) {
825         case 2: /* child dentry */
826                 l_dput(dchild);
827         case 1: /* locked parent dentry */
828 #ifdef S_PDIROPS
829                 if (lockh[1].cookie != 0)
830                         ldlm_lock_decref(lockh + 1, LCK_CW);
831 #endif
832                 if (rc) {
833                         ldlm_lock_decref(lockh, LCK_PW);
834                 } else {
835                         ptlrpc_save_lock (req, lockh, LCK_PW);
836                 }
837                 l_dput(dparent);
838         case 0:
839                 break;
840         default:
841                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
842                 LBUG();
843         }
844         if (mea)
845                 OBD_FREE(mea, mea_size);
846         req->rq_status = rc;
847         return 0;
848 }
849
850 static int res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
851            ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
852 {
853         int i;
854
855         for (i = 0; i < RES_NAME_SIZE; i++) {
856                 /* return 1 here, because enqueue_ordered will skip resources
857                  * of all zeroes if they're sorted to the end of the list. */
858                 if (res1->name[i] == 0 && res2->name[i] != 0)
859                         return 1;
860                 if (res2->name[i] == 0 && res1->name[i] != 0)
861                         return 0;
862
863                 if (res1->name[i] > res2->name[i])
864                         return 1;
865                 if (res1->name[i] < res2->name[i])
866                         return 0;
867         }
868
869         if (!p1 || !p2)
870                 return 0;
871
872         if (memcmp(p1, p2, sizeof(*p1)) < 0)
873                 return 1;
874
875         return 0;
876 }
877
878 /* This function doesn't use ldlm_match_or_enqueue because we're always called
879  * with EX or PW locks, and the MDS is no longer allowed to match write locks,
880  * because they take the place of local semaphores.
881  *
882  * One or two locks are taken in numerical order.  A res_id->name[0] of 0 means
883  * no lock is taken for that res_id.  Must be at least one non-zero res_id. */
884 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
885                           struct lustre_handle *p1_lockh, int p1_lock_mode,
886                           ldlm_policy_data_t *p1_policy,
887                           struct ldlm_res_id *p2_res_id,
888                           struct lustre_handle *p2_lockh, int p2_lock_mode,
889                           ldlm_policy_data_t *p2_policy)
890 {
891         struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
892         struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
893         int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
894         ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
895         int rc, flags;
896         ENTRY;
897
898         LASSERT(p1_res_id != NULL && p2_res_id != NULL);
899
900         CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n", res_id[0]->name[0],
901                res_id[1]->name[0]);
902
903         if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
904                 handles[1] = p1_lockh;
905                 handles[0] = p2_lockh;
906                 res_id[1] = p1_res_id;
907                 res_id[0] = p2_res_id;
908                 lock_modes[1] = p1_lock_mode;
909                 lock_modes[0] = p2_lock_mode;
910                 policies[1] = p1_policy;
911                 policies[0] = p2_policy;
912         }
913
914         CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
915                res_id[0]->name[0], res_id[1]->name[0]);
916
917         flags = LDLM_FL_LOCAL_ONLY;
918         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
919                               LDLM_IBITS, policies[0], lock_modes[0], &flags,
920                               mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
921                               NULL, 0, NULL, handles[0]);
922         if (rc != ELDLM_OK)
923                 RETURN(-EIO);
924         ldlm_lock_dump_handle(D_OTHER, handles[0]);
925
926         if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
927             (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
928                 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
929                 ldlm_lock_addref(handles[1], lock_modes[1]);
930         } else if (res_id[1]->name[0] != 0) {
931                 flags = LDLM_FL_LOCAL_ONLY;
932                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
933                                       *res_id[1], LDLM_IBITS, policies[1],
934                                       lock_modes[1], &flags, mds_blocking_ast,
935                                       ldlm_completion_ast, NULL, NULL, NULL, 0,
936                                       NULL, handles[1]);
937                 if (rc != ELDLM_OK) {
938                         ldlm_lock_decref(handles[0], lock_modes[0]);
939                         RETURN(-EIO);
940                 }
941                 ldlm_lock_dump_handle(D_OTHER, handles[1]);
942         }
943
944         RETURN(0);
945 }
946
947 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
948                            struct lustre_handle *p1_lockh, int p1_lock_mode,
949                            ldlm_policy_data_t *p1_policy,
950                            struct ldlm_res_id *p2_res_id,
951                            struct lustre_handle *p2_lockh, int p2_lock_mode,
952                            ldlm_policy_data_t *p2_policy,
953                            struct ldlm_res_id *c1_res_id,
954                            struct lustre_handle *c1_lockh, int c1_lock_mode,
955                            ldlm_policy_data_t *c1_policy,
956                            struct ldlm_res_id *c2_res_id,
957                            struct lustre_handle *c2_lockh, int c2_lock_mode,
958                            ldlm_policy_data_t *c2_policy)
959 {
960         struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
961                                           c1_res_id, c2_res_id };
962         struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
963                                                  c1_lockh, c2_lockh };
964         int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
965                               c1_lock_mode, c2_lock_mode };
966         ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
967                                             c1_policy, c2_policy};
968         int rc, i, j, sorted, flags;
969         ENTRY;
970
971         CDEBUG(D_DLMTRACE,
972                "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
973                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
974                res_id[3]->name[0]);
975
976         /* simple insertion sort - we have at most 4 elements */
977         for (i = 1; i < 4; i++) {
978                 j = i - 1;
979                 dlm_handles[4] = dlm_handles[i];
980                 res_id[4] = res_id[i];
981                 lock_modes[4] = lock_modes[i];
982                 policies[4] = policies[i];
983
984                 sorted = 0;
985                 do {
986                         if (res_gt(res_id[j], res_id[4], policies[j],
987                                    policies[4])) {
988                                 dlm_handles[j + 1] = dlm_handles[j];
989                                 res_id[j + 1] = res_id[j];
990                                 lock_modes[j + 1] = lock_modes[j];
991                                 policies[j + 1] = policies[j];
992                                 j--;
993                         } else {
994                                 sorted = 1;
995                         }
996                 } while (j >= 0 && !sorted);
997
998                 dlm_handles[j + 1] = dlm_handles[4];
999                 res_id[j + 1] = res_id[4];
1000                 lock_modes[j + 1] = lock_modes[4];
1001                 policies[j + 1] = policies[4];
1002         }
1003
1004         CDEBUG(D_DLMTRACE,
1005                "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1006                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1007                res_id[3]->name[0]);
1008
1009         /* XXX we could send ASTs on all these locks first before blocking? */
1010         for (i = 0; i < 4; i++) {
1011                 flags = 0;
1012                 if (res_id[i]->name[0] == 0)
1013                         break;
1014                 if (i != 0 &&
1015                     !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1016                     (policies[i]->l_inodebits.bits &
1017                      policies[i-1]->l_inodebits.bits) ) {
1018                         memcpy(dlm_handles[i], dlm_handles[i-1],
1019                                sizeof(*(dlm_handles[i])));
1020                         ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1021                 } else {
1022                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1023                                               *res_id[i], LDLM_IBITS,
1024                                               policies[i],
1025                                               lock_modes[i], &flags,
1026                                               mds_blocking_ast,
1027                                               ldlm_completion_ast, NULL, NULL,
1028                                               NULL, 0, NULL, dlm_handles[i]);
1029                         if (rc != ELDLM_OK)
1030                                 GOTO(out_err, rc = -EIO);
1031                         ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1032                 }
1033         }
1034
1035         RETURN(0);
1036 out_err:
1037         while (i-- > 0)
1038                 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1039
1040         return rc;
1041 }
1042
1043 /* In the unlikely case that the child changed while we were waiting
1044  * on the lock, we need to drop the lock on the old child and either:
1045  * - if the child has a lower resource name, then we have to also
1046  *   drop the parent lock and regain the locks in the right order
1047  * - in the rename case, if the child has a lower resource name than one of
1048  *   the other parent/child resources (maxres) we also need to reget the locks
1049  * - if the child has a higher resource name (this is the common case)
1050  *   we can just get the lock on the new child (still in lock order)
1051  *
1052  * Returns 0 if the child did not change or if it changed but could be locked.
1053  * Returns 1 if the child changed and we need to re-lock (no locks held).
1054  * Returns -ve error with a valid dchild (no locks held). */
1055 static int mds_verify_child(struct obd_device *obd,
1056                             struct ldlm_res_id *parent_res_id,
1057                             struct lustre_handle *parent_lockh,
1058                             struct dentry *dparent, int parent_mode,
1059                             struct ldlm_res_id *child_res_id,
1060                             struct lustre_handle *child_lockh,
1061                             struct dentry **dchildp, int child_mode,
1062                             ldlm_policy_data_t *child_policy,
1063                             const char *name, int namelen,
1064                             struct ldlm_res_id *maxres)
1065 {
1066         struct dentry *vchild, *dchild = *dchildp;
1067         int rc = 0, cleanup_phase = 2; /* parent, child locks */
1068         ENTRY;
1069
1070         vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1071         if (IS_ERR(vchild))
1072                 GOTO(cleanup, rc = PTR_ERR(vchild));
1073
1074         if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1075                 if  (child_res_id->name[0] == vchild->d_inum &&
1076                                 child_res_id->name[1] == vchild->d_generation) {
1077                         if (dchild != NULL)
1078                                 l_dput(dchild);
1079                         *dchildp = vchild;
1080                         RETURN(0);
1081                 }
1082                 goto changed;
1083         }
1084
1085         if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1086                    (vchild->d_inode != NULL &&
1087                     child_res_id->name[0] == vchild->d_inode->i_ino &&
1088                     child_res_id->name[1] == vchild->d_inode->i_generation))) {
1089                 if (dchild != NULL)
1090                         l_dput(dchild);
1091                 *dchildp = vchild;
1092
1093                 RETURN(0);
1094         }
1095
1096 changed:
1097         CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1098                vchild->d_inode, dchild ? dchild->d_inode : 0,
1099                vchild->d_inode ? vchild->d_inode->i_ino : 0,
1100                child_res_id->name[0]);
1101         if (child_res_id->name[0] != 0)
1102                 ldlm_lock_decref(child_lockh, child_mode);
1103         if (dchild)
1104                 l_dput(dchild);
1105
1106         cleanup_phase = 1; /* parent lock only */
1107         *dchildp = dchild = vchild;
1108
1109         if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1110                 int flags = 0;
1111                 if (dchild->d_inode) {
1112                         child_res_id->name[0] = dchild->d_inode->i_ino;
1113                         child_res_id->name[1] = dchild->d_inode->i_generation;
1114                 } else {
1115                         child_res_id->name[0] = dchild->d_inum;
1116                         child_res_id->name[1] = dchild->d_generation;
1117                 }
1118
1119                 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1120                     res_gt(maxres, child_res_id, NULL, NULL)) {
1121                         CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1122                                child_res_id->name[0], parent_res_id->name[0],
1123                                maxres->name[0]);
1124                         GOTO(cleanup, rc = 1);
1125                 }
1126
1127                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1128                                       *child_res_id, LDLM_IBITS, child_policy,
1129                                       child_mode, &flags, mds_blocking_ast,
1130                                       ldlm_completion_ast, NULL, NULL, NULL, 0,
1131                                       NULL, child_lockh);
1132                 if (rc != ELDLM_OK)
1133                         GOTO(cleanup, rc = -EIO);
1134
1135         } else {
1136                 memset(child_res_id, 0, sizeof(*child_res_id));
1137         }
1138
1139         EXIT;
1140 cleanup:
1141         if (rc) {
1142                 switch(cleanup_phase) {
1143                 case 2:
1144                         if (child_res_id->name[0] != 0)
1145                                 ldlm_lock_decref(child_lockh, child_mode);
1146                 case 1:
1147                         ldlm_lock_decref(parent_lockh, parent_mode);
1148                 }
1149         }
1150         return rc;
1151 }
1152
1153 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1154                                 struct ll_fid *fid,
1155                                 struct lustre_handle *parent_lockh,
1156                                 struct dentry **dparentp, int parent_mode,
1157                                 __u64 parent_lockpart,
1158                                 char *name, int namelen,
1159                                 struct lustre_handle *child_lockh,
1160                                 struct dentry **dchildp, int child_mode,
1161                                 __u64 child_lockpart)
1162 {
1163         struct ldlm_res_id child_res_id = { .name = {0} };
1164         struct ldlm_res_id parent_res_id = { .name = {0} };
1165         ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1166         ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1167         struct inode *inode;
1168         int rc = 0, cleanup_phase = 0;
1169         ENTRY;
1170
1171         /* Step 1: Lookup parent */
1172         *dparentp = mds_fid2dentry(mds, fid, NULL);
1173         if (IS_ERR(*dparentp))
1174                 RETURN(rc = PTR_ERR(*dparentp));
1175         LASSERT((*dparentp)->d_inode);
1176
1177         CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1178                (*dparentp)->d_inode->i_ino, name);
1179
1180         parent_res_id.name[0] = (*dparentp)->d_inode->i_ino;
1181         parent_res_id.name[1] = (*dparentp)->d_inode->i_generation;
1182 #ifdef S_PDIROPS
1183         parent_lockh[1].cookie = 0;
1184         if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1185                 /* lock just dir { ino, generation } to flush client cache */
1186                 if (parent_mode == LCK_PW) {
1187                         struct ldlm_res_id res_id = { .name = {0} };
1188                         ldlm_policy_data_t policy;
1189                         int flags = 0;
1190                         res_id.name[0] = (*dparentp)->d_inode->i_ino;
1191                         res_id.name[1] = (*dparentp)->d_inode->i_generation;
1192                         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1193                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1194                                               res_id, LDLM_IBITS,
1195                                               &policy, LCK_CW, &flags,
1196                                               mds_blocking_ast,
1197                                               ldlm_completion_ast, NULL, NULL,
1198                                               NULL, 0, NULL, parent_lockh+1);
1199                         if (rc != ELDLM_OK)
1200                                 RETURN(-ENOLCK);
1201                 }
1202
1203                 parent_res_id.name[2] = full_name_hash(name, namelen - 1);
1204                 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
1205                        (*dparentp)->d_inode->i_ino, 
1206                        (*dparentp)->d_inode->i_generation,
1207                        parent_res_id.name[2]);
1208         }
1209 #endif
1210
1211         cleanup_phase = 1; /* parent dentry */
1212
1213         /* Step 2: Lookup child (without DLM lock, to get resource name) */
1214         *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1215         if (IS_ERR(*dchildp)) {
1216                 rc = PTR_ERR(*dchildp);
1217                 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1218                 GOTO(cleanup, rc);
1219         }
1220
1221         if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1222                 /* inode lives on another MDS: return * mds/ino/gen
1223                  * and LOOKUP lock. drop possible UPDATE lock! */
1224                 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1225                 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1226                 child_res_id.name[0] = (*dchildp)->d_inum;
1227                 child_res_id.name[1] = (*dchildp)->d_generation;
1228                 goto retry_locks;
1229         }
1230
1231         inode = (*dchildp)->d_inode;
1232         if (inode != NULL)
1233                 inode = igrab(inode);
1234         if (inode == NULL)
1235                 goto retry_locks;
1236
1237         child_res_id.name[0] = inode->i_ino;
1238         child_res_id.name[1] = inode->i_generation;
1239
1240         iput(inode);
1241
1242 retry_locks:
1243         cleanup_phase = 2; /* child dentry */
1244
1245         /* Step 3: Lock parent and child in resource order.  If child doesn't
1246          *         exist, we still have to lock the parent and re-lookup. */
1247         rc = enqueue_ordered_locks(obd,&parent_res_id,parent_lockh,parent_mode,
1248                                    &parent_policy, &child_res_id, child_lockh,
1249                                    child_mode, &child_policy);
1250         if (rc)
1251                 GOTO(cleanup, rc);
1252
1253         if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1254                 cleanup_phase = 4; /* child lock */
1255         else
1256                 cleanup_phase = 3; /* parent lock */
1257
1258         /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1259         rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1260                               parent_mode, &child_res_id, child_lockh, 
1261                               dchildp, child_mode, &child_policy,
1262                               name, namelen, &parent_res_id);
1263         if (rc > 0)
1264                 goto retry_locks;
1265         if (rc < 0) {
1266                 cleanup_phase = 3;
1267                 GOTO(cleanup, rc);
1268         }
1269
1270 cleanup:
1271         if (rc) {
1272                 switch (cleanup_phase) {
1273                 case 4:
1274                         ldlm_lock_decref(child_lockh, child_mode);
1275                 case 3:
1276                         ldlm_lock_decref(parent_lockh, parent_mode);
1277                 case 2:
1278                         l_dput(*dchildp);
1279                 case 1:
1280 #ifdef S_PDIROPS
1281                         if (parent_lockh[1].cookie)
1282                                 ldlm_lock_decref(parent_lockh + 1, LCK_CW);
1283 #endif
1284                         l_dput(*dparentp);
1285                 default: ;
1286                 }
1287         }
1288         return rc;
1289 }
1290
1291 void mds_reconstruct_generic(struct ptlrpc_request *req)
1292 {
1293         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1294
1295         mds_req_from_mcd(req, med->med_mcd);
1296 }
1297
1298 int mds_create_local_dentry(struct mds_update_record *rec,
1299                            struct obd_device *obd)
1300 {
1301         struct mds_obd *mds = &obd->u.mds;
1302         struct inode *fids_dir = mds->mds_fids_dir->d_inode;
1303         int fidlen = 0, rc, cleanup_phase = 0;
1304         struct dentry *new_child = NULL;
1305         char *fidname = rec->ur_name;
1306         struct dentry *child = NULL;
1307         struct lustre_handle lockh;
1308         void *handle;
1309         ENTRY;
1310
1311         down(&fids_dir->i_sem);
1312         fidlen = ll_fid2str(fidname, rec->ur_fid1->id, rec->ur_fid1->generation);
1313         CDEBUG(D_OTHER, "look for local dentry '%s' for %u/%u\n",
1314                         fidname, (unsigned) rec->ur_fid1->id,
1315                         (unsigned) rec->ur_fid1->generation);
1316
1317         new_child = lookup_one_len(fidname, mds->mds_fids_dir, fidlen);
1318         up(&fids_dir->i_sem);
1319         if (IS_ERR(new_child)) {
1320                 CERROR("can't lookup %s: %d\n", fidname,
1321                                 (int) PTR_ERR(new_child));
1322                 GOTO(cleanup, rc = PTR_ERR(new_child));
1323         }
1324         cleanup_phase = 1;
1325
1326         if (new_child->d_inode != NULL) {
1327                 /* nice. we've already have local dentry! */
1328                 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n", 
1329                        (unsigned) new_child->d_inode->i_ino,
1330                        (unsigned) new_child->d_inode->i_generation);
1331                 rec->ur_fid1->id = fids_dir->i_ino;
1332                 rec->ur_fid1->generation = fids_dir->i_generation;
1333                 rec->ur_namelen = fidlen + 1;
1334                 GOTO(cleanup, rc = 0);
1335         }
1336
1337         /* new, local dentry will be added soon. we need no aliases here */
1338         d_drop(new_child);
1339
1340         child = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_EX,
1341                                       &lockh, NULL, 0, MDS_INODELOCK_UPDATE);
1342         if (IS_ERR(child)) {
1343                 CERROR("can't get victim\n");
1344                 GOTO(cleanup, rc = PTR_ERR(child));
1345         }
1346         cleanup_phase = 2;
1347
1348         handle = fsfilt_start(obd, fids_dir, FSFILT_OP_LINK, NULL);
1349         if (IS_ERR(handle))
1350                 GOTO(cleanup, rc = PTR_ERR(handle));
1351
1352         rc = fsfilt_add_dir_entry(obd, mds->mds_fids_dir, fidname, fidlen,
1353                                   rec->ur_fid1->id, rec->ur_fid1->generation,
1354                                   mds->mds_num);
1355         if (rc)
1356                 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1357                        (unsigned long) child->d_inode->i_ino,
1358                        (unsigned long) child->d_inode->i_generation, rc);
1359         else {
1360                 if (S_ISDIR(child->d_inode->i_mode)) {
1361                         fids_dir->i_nlink++;
1362                         mark_inode_dirty(fids_dir);
1363                 }
1364                 mark_inode_dirty(child->d_inode);
1365         }
1366         fsfilt_commit(obd, fids_dir, handle, 0);
1367
1368         rec->ur_fid1->id = fids_dir->i_ino;
1369         rec->ur_fid1->generation = fids_dir->i_generation;
1370         rec->ur_namelen = fidlen + 1;
1371
1372 cleanup:
1373         switch(cleanup_phase) {
1374                 case 2:
1375                         ldlm_lock_decref(&lockh, LCK_EX);
1376                         dput(child);
1377                 case 1:
1378                         dput(new_child);
1379                 case 0:
1380                        break; 
1381         }
1382         RETURN(rc);
1383 }
1384
1385 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1386                                         struct ptlrpc_request *slave)
1387 {
1388         void *cookie, *cookie2;
1389         struct mds_body *body2;
1390         struct mds_body *body;
1391         void *ea, *ea2;
1392         ENTRY;
1393
1394         body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1395         LASSERT(body != NULL);
1396
1397         body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1398         LASSERT(body2 != NULL);
1399
1400         if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER))) {
1401                 RETURN(0);
1402         }
1403
1404         memcpy(body2, body, sizeof(*body));
1405         body2->valid &= ~OBD_MD_FLCOOKIE;
1406
1407         if (!(body->valid & OBD_MD_FLEASIZE))
1408                 RETURN(0);
1409
1410         if (body->eadatasize == 0) {
1411                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1412                 RETURN(0);
1413         }
1414
1415         LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1416         
1417         ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1418         LASSERT(ea != NULL);
1419         
1420         ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1421         LASSERT(ea2 != NULL);
1422
1423         memcpy(ea2, ea, body->eadatasize);
1424
1425         if (body->valid & OBD_MD_FLCOOKIE) {
1426                 LASSERT(master->rq_repmsg->buflens[2] >=
1427                                 slave->rq_repmsg->buflens[2]);
1428                 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1429                                 slave->rq_repmsg->buflens[2]);
1430                 LASSERT(cookie != NULL);
1431
1432                 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1433                                 master->rq_repmsg->buflens[2]);
1434                 LASSERT(cookie2 != NULL);
1435                 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1436                 body2->valid |= OBD_MD_FLCOOKIE;
1437         }
1438         RETURN(0);
1439 }
1440
1441 static int mds_reint_unlink_remote(struct mds_update_record *rec, int offset,
1442                                    struct ptlrpc_request *req,
1443                                    struct lustre_handle *parent_lockh,
1444                                    struct dentry *dparent,
1445                                    struct lustre_handle *child_lockh,
1446                                    struct dentry *dchild)
1447 {
1448         struct mds_obd *mds = mds_req2mds(req);
1449         struct mdc_op_data op_data;
1450         int rc = 0, cleanup_phase = 0;
1451         struct ptlrpc_request *request = NULL;
1452         ENTRY;
1453
1454         LASSERT(offset == 0 || offset == 2);
1455
1456         DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode %u/%u/%u)",
1457                   rec->ur_namelen - 1, rec->ur_name, (unsigned)dchild->d_mdsnum,
1458                   (unsigned) dchild->d_inum, (unsigned) dchild->d_generation);
1459
1460         /* time to drop i_nlink on remote MDS */ 
1461         op_data.fid1.mds = dchild->d_mdsnum;
1462         op_data.fid1.id = dchild->d_inum;
1463         op_data.fid1.generation = dchild->d_generation;
1464         op_data.create_mode = rec->ur_mode;
1465         op_data.namelen = 0;
1466         op_data.name = NULL;
1467         rc = md_unlink(mds->mds_lmv_exp, &op_data, &request);
1468         cleanup_phase = 2;
1469         if (request) {
1470                 mds_copy_unlink_reply(req, request);
1471                 ptlrpc_req_finished(request);
1472         }
1473         if (rc == 0)
1474                 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
1475         req->rq_status = rc;
1476
1477 #ifdef S_PDIROPS
1478         if (parent_lockh[1].cookie != 0)
1479                 ldlm_lock_decref(parent_lockh + 1, LCK_CW);
1480 #endif
1481         ldlm_lock_decref(child_lockh, LCK_EX);
1482         if (rc)
1483                 ldlm_lock_decref(parent_lockh, LCK_PW);
1484         else
1485                 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1486         l_dput(dchild);
1487         l_dput(dparent);
1488
1489         return 0;
1490 }
1491
1492 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1493                             struct ptlrpc_request *req,
1494                             struct lustre_handle *lh)
1495 {
1496         struct dentry *dparent, *dchild;
1497         struct mds_obd *mds = mds_req2mds(req);
1498         struct obd_device *obd = req->rq_export->exp_obd;
1499         struct mds_body *body = NULL;
1500         struct inode *child_inode;
1501         struct lustre_handle parent_lockh[2], child_lockh, child_reuse_lockh;
1502         char fidname[LL_FID_NAMELEN];
1503         void *handle = NULL;
1504         int rc = 0, log_unlink = 0, cleanup_phase = 0;
1505         int unlink_by_fid = 0;
1506         ENTRY;
1507
1508         LASSERT(offset == 0 || offset == 2);
1509
1510         DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1511                   rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name);
1512
1513         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1514
1515         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1516                 GOTO(cleanup, rc = -ENOENT);
1517
1518         if (rec->ur_namelen == 1) {
1519                 /* this is request to drop i_nlink on local inode */
1520                 unlink_by_fid = 1;
1521                 rec->ur_name = fidname;
1522                 rc = mds_create_local_dentry(rec, obd);
1523                 LASSERT(rc == 0);
1524         }
1525         rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1,
1526                                          parent_lockh, &dparent, LCK_PW,
1527                                          MDS_INODELOCK_UPDATE,
1528                                          rec->ur_name, rec->ur_namelen,
1529                                          &child_lockh, &dchild, LCK_EX,
1530                                          MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE);
1531         if (rc)
1532                 GOTO(cleanup, rc);
1533
1534         if (dchild->d_flags & DCACHE_CROSS_REF) {
1535                 /* we should have parent lock only here */
1536                 LASSERT(unlink_by_fid == 0);
1537                 LASSERT(dchild->d_mdsnum != mds->mds_num);
1538                 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
1539                                              dparent, &child_lockh, dchild);
1540                 RETURN(0);
1541         }
1542
1543         cleanup_phase = 1; /* dchild, dparent, locks */
1544
1545         dget(dchild);
1546         child_inode = dchild->d_inode;
1547         if (child_inode == NULL) {
1548                 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1549                        dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
1550                 GOTO(cleanup, rc = -ENOENT);
1551         }
1552
1553         cleanup_phase = 2; /* dchild has a lock */
1554
1555         /* Step 4: Get a lock on the ino to sync with creation WRT inode
1556          * reuse (see bug 2029). */
1557         rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
1558         if (rc != ELDLM_OK)
1559                 GOTO(cleanup, rc);
1560
1561         cleanup_phase = 3; /* child inum lock */
1562
1563         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
1564
1565         /* ldlm_reply in buf[0] if called via intent */
1566         if (offset)
1567                 offset = 1;
1568
1569         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
1570         LASSERT(body != NULL);
1571
1572         /* If this is the last reference to this inode, get the OBD EA
1573          * data first so the client can destroy OST objects.
1574          * we only do the object removal if no open files remain.
1575          * Nobody can get at this name anymore because of the locks so
1576          * we make decisions here as to whether to remove the inode */
1577         if (S_ISREG(child_inode->i_mode) && child_inode->i_nlink == 1 &&
1578             mds_open_orphan_count(child_inode) == 0) {
1579                 mds_pack_inode2fid(obd, &body->fid1, child_inode);
1580                 mds_pack_inode2body(obd, body, child_inode);
1581                 mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
1582                             child_inode, 1);
1583                 if (!(body->valid & OBD_MD_FLEASIZE)) {
1584                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1585                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
1586                 } else {
1587                         log_unlink = 1;
1588                 }
1589         }
1590
1591         /* We have to do these checks ourselves, in case we are making an
1592          * orphan.  The client tells us whether rmdir() or unlink() was called,
1593          * so we need to return appropriate errors (bug 72).
1594          *
1595          * We don't have to check permissions, because vfs_rename (called from
1596          * mds_open_unlink_rename) also calls may_delete. */
1597         if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1598                 if (!S_ISDIR(child_inode->i_mode))
1599                         GOTO(cleanup, rc = -ENOTDIR);
1600         } else {
1601                 if (S_ISDIR(child_inode->i_mode))
1602                         GOTO(cleanup, rc = -EISDIR);
1603         }
1604
1605         if (child_inode->i_nlink == (S_ISDIR(child_inode->i_mode) ? 2 : 1) &&
1606             mds_open_orphan_count(child_inode) > 0) {
1607                 rc = mds_open_unlink_rename(rec, obd, dparent, dchild, &handle);
1608                 cleanup_phase = 4; /* transaction */
1609                 GOTO(cleanup, rc);
1610         }
1611
1612         /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
1613         switch (child_inode->i_mode & S_IFMT) {
1614         case S_IFDIR:
1615                 /* Drop any lingering child directories before we start our
1616                  * transaction, to avoid doing multiple inode dirty/delete
1617                  * in our compound transaction (bug 1321). */
1618                 shrink_dcache_parent(dchild);
1619                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1620                                       NULL);
1621                 if (IS_ERR(handle))
1622                         GOTO(cleanup, rc = PTR_ERR(handle));
1623                 cleanup_phase = 4; /* transaction */
1624                 rc = vfs_rmdir(dparent->d_inode, dchild);
1625                 break;
1626         case S_IFREG: {
1627 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
1628                 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
1629                                                         offset + 1, 0);
1630                 handle = fsfilt_start_log(obd, dparent->d_inode,
1631                                           FSFILT_OP_UNLINK, NULL,
1632                                           le32_to_cpu(lmm->lmm_stripe_count));
1633                 if (IS_ERR(handle))
1634                         GOTO(cleanup, rc = PTR_ERR(handle));
1635
1636                 cleanup_phase = 4; /* transaction */
1637                 rc = vfs_unlink(dparent->d_inode, dchild);
1638
1639                 if (!rc && log_unlink)
1640                         if (mds_log_op_unlink(obd, child_inode,
1641                                 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
1642                                 req->rq_repmsg->buflens[offset + 1],
1643                                 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
1644                                 req->rq_repmsg->buflens[offset + 2]) > 0)
1645                                 body->valid |= OBD_MD_FLCOOKIE;
1646                 break;
1647         }
1648         case S_IFLNK:
1649         case S_IFCHR:
1650         case S_IFBLK:
1651         case S_IFIFO:
1652         case S_IFSOCK:
1653                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
1654                                       NULL);
1655                 if (IS_ERR(handle))
1656                         GOTO(cleanup, rc = PTR_ERR(handle));
1657                 cleanup_phase = 4; /* transaction */
1658                 rc = vfs_unlink(dparent->d_inode, dchild);
1659                 break;
1660         default:
1661                 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
1662                        rec->ur_name);
1663                 LBUG();
1664                 GOTO(cleanup, rc = -EINVAL);
1665         }
1666
1667  cleanup:
1668         if (rc == 0) {
1669                 struct iattr iattr;
1670                 int err;
1671
1672                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1673                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1674                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1675
1676                 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1677                 if (err)
1678                         CERROR("error on parent setattr: rc = %d\n", err);
1679         }
1680
1681         switch(cleanup_phase) {
1682         case 4:
1683                 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1684                                         rc, 0);
1685                 if (!rc)
1686                         (void)obd_set_info(mds->mds_osc_exp, strlen("unlinked"),
1687                                            "unlinked", 0, NULL);
1688         case 3: /* child ino-reuse lock */
1689                 if (rc && body != NULL) {
1690                         // Don't unlink the OST objects if the MDS unlink failed
1691                         body->valid = 0;
1692                 }
1693                 if (rc)
1694                         ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
1695                 else
1696                         ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
1697         case 2: /* child lock */
1698                 ldlm_lock_decref(&child_lockh, LCK_EX);
1699         case 1: /* child and parent dentry, parent lock */
1700 #ifdef S_PDIROPS
1701                 if (parent_lockh[1].cookie != 0)
1702                         ldlm_lock_decref(parent_lockh + 1, LCK_CW);
1703 #endif
1704                 if (rc)
1705                         ldlm_lock_decref(parent_lockh, LCK_PW);
1706                 else
1707                         ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1708                 l_dput(dchild);
1709                 l_dput(dchild);
1710                 l_dput(dparent);
1711         case 0:
1712                 break;
1713         default:
1714                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1715                 LBUG();
1716         }
1717         req->rq_status = rc;
1718         return 0;
1719 }
1720
1721 /*
1722  * to service requests from remote MDS to increment i_nlink
1723  */
1724 static int mds_reint_link_acquire(struct mds_update_record *rec,
1725                                  int offset, struct ptlrpc_request *req,
1726                                  struct lustre_handle *lh)
1727 {
1728         struct obd_device *obd = req->rq_export->exp_obd;
1729         struct ldlm_res_id src_res_id = { .name = {0} };
1730         struct lustre_handle *handle = NULL, src_lockh;
1731         struct mds_obd *mds = mds_req2mds(req);
1732         int rc = 0, cleanup_phase = 0;
1733         struct dentry *de_src = NULL;
1734         ldlm_policy_data_t policy;
1735         int flags = 0;
1736         ENTRY;
1737
1738         DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks %u/%u/%u\n",
1739                   obd->obd_name, (unsigned) rec->ur_fid1->mds,
1740                   (unsigned) rec->ur_fid1->id,
1741                   (unsigned) rec->ur_fid1->generation);
1742
1743         /* Step 1: Lookup the source inode and target directory by FID */
1744         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1745         if (IS_ERR(de_src))
1746                 GOTO(cleanup, rc = PTR_ERR(de_src));
1747         cleanup_phase = 1; /* source dentry */
1748
1749         src_res_id.name[0] = de_src->d_inode->i_ino;
1750         src_res_id.name[1] = de_src->d_inode->i_generation;
1751         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1752
1753         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1754                         src_res_id, LDLM_IBITS, &policy,
1755                         LCK_EX, &flags, mds_blocking_ast,
1756                         ldlm_completion_ast, NULL, NULL,
1757                         NULL, 0, NULL, &src_lockh);
1758         if (rc != ELDLM_OK)
1759                 GOTO(cleanup, rc = -ENOLCK);
1760         cleanup_phase = 2; /* lock */
1761
1762         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1763
1764         handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
1765         if (IS_ERR(handle)) {
1766                 rc = PTR_ERR(handle);
1767                 GOTO(cleanup, rc);
1768         }
1769         de_src->d_inode->i_nlink++;
1770         mark_inode_dirty(de_src->d_inode);
1771
1772 cleanup:
1773         rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
1774                                         handle, req, rc, 0);
1775         EXIT;
1776         switch (cleanup_phase) {
1777                 case 2:
1778                         if (rc)
1779                                 ldlm_lock_decref(&src_lockh, LCK_EX);
1780                         else
1781                                 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
1782                 case 1:
1783                         l_dput(de_src);
1784                 case 0:
1785                         break;
1786                 default:
1787                         CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1788                         LBUG();
1789         }
1790         req->rq_status = rc;
1791         return 0;
1792 }
1793
1794 /*
1795  * request to link to foreign inode:
1796  *  - acquire i_nlinks on this inode
1797  *  - add dentry
1798  */
1799 static int mds_reint_link_to_remote(struct mds_update_record *rec,
1800                                     int offset, struct ptlrpc_request *req,
1801                                     struct lustre_handle *lh)
1802 {
1803         struct lustre_handle *handle = NULL, tgt_dir_lockh[2];
1804         struct obd_device *obd = req->rq_export->exp_obd;
1805         struct dentry *de_tgt_dir = NULL;
1806         struct mds_obd *mds = mds_req2mds(req);
1807         int rc = 0, cleanup_phase = 0;
1808         struct mdc_op_data op_data;
1809         struct ptlrpc_request *request = NULL;
1810         ENTRY;
1811
1812 #define fmt     "%s: request to link %u/%u/%u:%*s to foreign inode %u/%u/%u\n"
1813         DEBUG_REQ(D_INODE, req, fmt, obd->obd_name,
1814                   (unsigned) rec->ur_fid2->mds,
1815                   (unsigned) rec->ur_fid2->id,
1816                   (unsigned) rec->ur_fid2->generation,
1817                   rec->ur_namelen - 1, rec->ur_name,
1818                   (unsigned) rec->ur_fid1->mds,
1819                   (unsigned) rec->ur_fid1->id,
1820                   (unsigned)rec->ur_fid1->generation);
1821
1822         de_tgt_dir = mds_fid2locked_dentry(obd, rec->ur_fid2, NULL, LCK_EX,
1823                                            tgt_dir_lockh, rec->ur_name,
1824                                            rec->ur_namelen - 1,
1825                                            MDS_INODELOCK_UPDATE);
1826         if (IS_ERR(de_tgt_dir))
1827                 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
1828         cleanup_phase = 1;
1829
1830         op_data.fid1 = *(rec->ur_fid1);
1831         op_data.namelen = 0;
1832         op_data.name = NULL;
1833         rc = md_link(mds->mds_lmv_exp, &op_data, &request);
1834         LASSERT(rc == 0);
1835         cleanup_phase = 2;
1836         if (request)
1837                 ptlrpc_req_finished(request);
1838
1839         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
1840
1841         handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
1842         if (IS_ERR(handle)) {
1843                 rc = PTR_ERR(handle);
1844                 GOTO(cleanup, rc);
1845         }
1846         
1847         rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
1848                                   rec->ur_namelen - 1, rec->ur_fid1->id,
1849                                   rec->ur_fid1->generation, rec->ur_fid1->mds);
1850         cleanup_phase = 3;
1851
1852 cleanup:
1853         rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
1854                                 handle, req, rc, 0);
1855         EXIT;
1856
1857         switch (cleanup_phase) {
1858                 case 3:
1859                         if (rc) {
1860                                 /* FIXME: drop i_nlink on remote inode here */
1861                                 CERROR("MUST drop drop i_nlink here\n");
1862                         }
1863                 case 2:
1864                 case 1:
1865                         if (rc) {
1866                                 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
1867 #ifdef S_PDIROPS
1868                                 ldlm_lock_decref(tgt_dir_lockh + 1, LCK_CW);
1869 #endif
1870                         } else {
1871                                 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
1872 #ifdef S_PDIROPS
1873                                 ptlrpc_save_lock(req, tgt_dir_lockh + 1, LCK_CW);
1874 #endif
1875                         }
1876                         l_dput(de_tgt_dir);
1877                         break;
1878                 default:
1879                         CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1880                         LBUG();
1881         }
1882         req->rq_status = rc;
1883         return 0;
1884 }
1885
1886 static int mds_reint_link(struct mds_update_record *rec, int offset,
1887                           struct ptlrpc_request *req,
1888                           struct lustre_handle *lh)
1889 {
1890         struct obd_device *obd = req->rq_export->exp_obd;
1891         struct dentry *de_src = NULL;
1892         struct dentry *de_tgt_dir = NULL;
1893         struct dentry *dchild = NULL;
1894         struct mds_obd *mds = mds_req2mds(req);
1895         struct lustre_handle *handle = NULL, tgt_dir_lockh[2], src_lockh;
1896         struct ldlm_res_id src_res_id = { .name = {0} };
1897         struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
1898         ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
1899         ldlm_policy_data_t tgt_dir_policy =
1900                                        {.l_inodebits = {MDS_INODELOCK_UPDATE}};
1901
1902         int rc = 0, cleanup_phase = 0;
1903         ENTRY;
1904
1905         LASSERT(offset == 0);
1906
1907         DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
1908                   rec->ur_fid1->id, rec->ur_fid1->generation,
1909                   rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_name);
1910
1911         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1912
1913         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1914                 GOTO(cleanup, rc = -ENOENT);
1915
1916         if (rec->ur_fid1->mds != mds->mds_num) {
1917                 rc = mds_reint_link_to_remote(rec, offset, req, lh);
1918                 RETURN(rc);
1919         }
1920         
1921         if (rec->ur_namelen == 1) {
1922                 rc = mds_reint_link_acquire(rec, offset, req, lh);
1923                 RETURN(rc);
1924         }
1925
1926         /* Step 1: Lookup the source inode and target directory by FID */
1927         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1928         if (IS_ERR(de_src))
1929                 GOTO(cleanup, rc = PTR_ERR(de_src));
1930
1931         cleanup_phase = 1; /* source dentry */
1932
1933         de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
1934         if (IS_ERR(de_tgt_dir))
1935                 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
1936
1937         cleanup_phase = 2; /* target directory dentry */
1938
1939         CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
1940                de_tgt_dir->d_name.len, de_tgt_dir->d_name.name, rec->ur_name,
1941                de_src->d_inode->i_ino);
1942
1943         /* Step 2: Take the two locks */
1944         src_res_id.name[0] = de_src->d_inode->i_ino;
1945         src_res_id.name[1] = de_src->d_inode->i_generation;
1946         tgt_dir_res_id.name[0] = de_tgt_dir->d_inode->i_ino;
1947         tgt_dir_res_id.name[1] = de_tgt_dir->d_inode->i_generation;
1948 #ifdef S_PDIROPS
1949         if (IS_PDIROPS(de_tgt_dir->d_inode)) {
1950                 int flags = 0;
1951                 /* Get a temp lock on just ino, gen to flush client cache */
1952                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1953                                       tgt_dir_res_id, LDLM_IBITS, &src_policy,
1954                                       LCK_CW, &flags, mds_blocking_ast,
1955                                       ldlm_completion_ast, NULL, NULL,
1956                                       NULL, 0, NULL, tgt_dir_lockh + 1);
1957                 if (rc != ELDLM_OK)
1958                         GOTO(cleanup, rc = -ENOLCK);
1959
1960                 tgt_dir_res_id.name[2] = full_name_hash(rec->ur_name,
1961                                                         rec->ur_namelen - 1);
1962                 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
1963                        de_tgt_dir->d_inode->i_ino,
1964                        de_tgt_dir->d_inode->i_generation,
1965                        tgt_dir_res_id.name[2]);
1966         }
1967 #endif
1968         rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
1969                                    &src_policy,
1970                                    &tgt_dir_res_id, tgt_dir_lockh, LCK_EX,
1971                                    &tgt_dir_policy);
1972         if (rc)
1973                 GOTO(cleanup, rc);
1974
1975         cleanup_phase = 3; /* locks */
1976
1977         /* Step 3: Lookup the child */
1978         dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen-1);
1979         if (IS_ERR(dchild)) {
1980                 rc = PTR_ERR(dchild);
1981                 if (rc != -EPERM && rc != -EACCES)
1982                         CERROR("child lookup error %d\n", rc);
1983                 GOTO(cleanup, rc);
1984         }
1985
1986         cleanup_phase = 4; /* child dentry */
1987
1988         if (dchild->d_inode) {
1989                 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
1990                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
1991                 rc = -EEXIST;
1992                 GOTO(cleanup, rc);
1993         }
1994
1995         /* Step 4: Do it. */
1996         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1997
1998         handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
1999         if (IS_ERR(handle)) {
2000                 rc = PTR_ERR(handle);
2001                 GOTO(cleanup, rc);
2002         }
2003
2004         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2005         if (rc && rc != -EPERM && rc != -EACCES)
2006                 CERROR("vfs_link error %d\n", rc);
2007 cleanup:
2008         rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2009                                 handle, req, rc, 0);
2010         EXIT;
2011
2012         switch (cleanup_phase) {
2013         case 4: /* child dentry */
2014                 l_dput(dchild);
2015         case 3: /* locks */
2016                 if (rc) {
2017                         ldlm_lock_decref(&src_lockh, LCK_EX);
2018                         ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2019                 } else {
2020                         ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2021                         ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2022                 }
2023         case 2: /* target dentry */
2024 #ifdef S_PDIROPS
2025                 if (tgt_dir_lockh[1].cookie)
2026                         ldlm_lock_decref(tgt_dir_lockh + 1, LCK_CW);
2027 #endif
2028                 if (de_tgt_dir)
2029                         l_dput(de_tgt_dir);
2030         case 1: /* source dentry */
2031                 l_dput(de_src);
2032         case 0:
2033                 break;
2034         default:
2035                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2036                 LBUG();
2037         }
2038         req->rq_status = rc;
2039         return 0;
2040 }
2041
2042 /*
2043  * add a hard link in the PENDING directory, only used by rename()
2044  */
2045 static int mds_add_link_orphan(struct mds_update_record *rec,
2046                                struct obd_device *obd,
2047                                struct dentry *dentry)
2048 {
2049         struct mds_obd *mds = &obd->u.mds;
2050         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
2051         struct dentry *pending_child;
2052         char fidname[LL_FID_NAMELEN];
2053         int fidlen = 0, rc;
2054         ENTRY;
2055
2056         LASSERT(dentry->d_inode);
2057         LASSERT(!mds_inode_is_orphan(dentry->d_inode));
2058
2059         down(&pending_dir->i_sem);
2060         fidlen = ll_fid2str(fidname, dentry->d_inode->i_ino,
2061                             dentry->d_inode->i_generation);
2062
2063         CDEBUG(D_ERROR, "pending destroy of %dx open file %s = %s\n",
2064                mds_open_orphan_count(dentry->d_inode),
2065                rec->ur_name, fidname);
2066
2067         pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
2068         if (IS_ERR(pending_child))
2069                 GOTO(out_lock, rc = PTR_ERR(pending_child));
2070
2071         if (pending_child->d_inode != NULL) {
2072                 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
2073                 LASSERT(pending_child->d_inode == dentry->d_inode);
2074                 GOTO(out_dput, rc = 0);
2075         }
2076
2077         lock_kernel();
2078         rc = vfs_link(dentry, pending_dir, pending_child);
2079         unlock_kernel();
2080         if (rc)
2081                 CERROR("error addlink orphan %s to PENDING: rc = %d\n",
2082                        rec->ur_name, rc);
2083         else
2084                 mds_inode_set_orphan(dentry->d_inode);
2085 out_dput:
2086         l_dput(pending_child);
2087 out_lock:
2088         up(&pending_dir->i_sem);
2089         RETURN(rc);
2090 }
2091
2092 /* The idea here is that we need to get four locks in the end:
2093  * one on each parent directory, one on each child.  We need to take
2094  * these locks in some kind of order (to avoid deadlocks), and the order
2095  * I selected is "increasing resource number" order.  We need to look up
2096  * the children, however, before we know what the resource number(s) are.
2097  * Thus the following plan:
2098  *
2099  * 1,2. Look up the parents
2100  * 3,4. Look up the children
2101  * 5. Take locks on the parents and children, in order
2102  * 6. Verify that the children haven't changed since they were looked up
2103  *
2104  * If there was a race and the children changed since they were first looked
2105  * up, it is possible that mds_verify_child() will be able to just grab the
2106  * lock on the new child resource (if it has a higher resource than any other)
2107  * but we need to compare against not only its parent, but also against the
2108  * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2109  *
2110  * We need the fancy igrab() on the child inodes because we aren't holding a
2111  * lock on the parent after the lookup is done, so dentry->d_inode may change
2112  * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2113  */
2114 static int mds_get_parents_children_locked(struct obd_device *obd,
2115                                            struct mds_obd *mds,
2116                                            struct ll_fid *p1_fid,
2117                                            struct dentry **de_srcdirp,
2118                                            struct ll_fid *p2_fid,
2119                                            struct dentry **de_tgtdirp,
2120                                            int parent_mode,
2121                                            const char *old_name, int old_len,
2122                                            struct dentry **de_oldp,
2123                                            const char *new_name, int new_len,
2124                                            struct dentry **de_newp,
2125                                            struct lustre_handle *dlm_handles,
2126                                            int child_mode)
2127 {
2128         struct ldlm_res_id p1_res_id = { .name = {0} };
2129         struct ldlm_res_id p2_res_id = { .name = {0} };
2130         struct ldlm_res_id c1_res_id = { .name = {0} };
2131         struct ldlm_res_id c2_res_id = { .name = {0} };
2132         ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2133         /* Only dentry should change, but the inode itself would be
2134            intact otherwise */
2135         ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2136         /* If something is going to be replaced, both dentry and inode locks are
2137            needed */
2138         ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP|
2139                                                         MDS_INODELOCK_UPDATE}};
2140         struct ldlm_res_id *maxres_src, *maxres_tgt;
2141         struct inode *inode;
2142         int rc = 0, cleanup_phase = 0;
2143         ENTRY;
2144
2145         /* Step 1: Lookup the source directory */
2146         *de_srcdirp = mds_fid2dentry(mds, p1_fid, NULL);
2147         if (IS_ERR(*de_srcdirp))
2148                 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2149
2150         cleanup_phase = 1; /* source directory dentry */
2151
2152         p1_res_id.name[0] = (*de_srcdirp)->d_inode->i_ino;
2153         p1_res_id.name[1] = (*de_srcdirp)->d_inode->i_generation;
2154
2155         /* Step 2: Lookup the target directory */
2156         if (memcmp(p1_fid, p2_fid, sizeof(*p1_fid)) == 0) {
2157                 *de_tgtdirp = dget(*de_srcdirp);
2158         } else {
2159                 *de_tgtdirp = mds_fid2dentry(mds, p2_fid, NULL);
2160                 if (IS_ERR(*de_tgtdirp))
2161                         GOTO(cleanup, rc = PTR_ERR(*de_tgtdirp));
2162         }
2163
2164         cleanup_phase = 2; /* target directory dentry */
2165
2166         p2_res_id.name[0] = (*de_tgtdirp)->d_inode->i_ino;
2167         p2_res_id.name[1] = (*de_tgtdirp)->d_inode->i_generation;
2168
2169 #ifdef S_PDIROPS
2170         dlm_handles[5].cookie = 0;
2171         dlm_handles[6].cookie = 0;
2172         if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2173                 /* Get a temp lock on just ino, gen to flush client cache */
2174                 rc = enqueue_ordered_locks(obd, &p1_res_id, &(dlm_handles[5]),
2175                                            LCK_CW, &p_policy, &p2_res_id,
2176                                            &(dlm_handles[6]),LCK_CW,&p_policy);
2177                 if (rc != ELDLM_OK)
2178                         GOTO(cleanup, rc);
2179
2180                 p1_res_id.name[2] = full_name_hash(old_name, old_len - 1);
2181                 p2_res_id.name[2] = full_name_hash(new_name, new_len - 1);
2182                 CDEBUG(D_INFO, "take locks on %lu:%u:"LPX64", %lu:%u:"LPX64"\n",
2183                        (*de_srcdirp)->d_inode->i_ino,
2184                        (*de_srcdirp)->d_inode->i_generation, p1_res_id.name[2],
2185                        (*de_tgtdirp)->d_inode->i_ino,
2186                        (*de_tgtdirp)->d_inode->i_generation, p2_res_id.name[2]);
2187         }
2188 #endif
2189
2190         /* Step 3: Lookup the source child entry */
2191         *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp, old_len - 1);
2192         if (IS_ERR(*de_oldp)) {
2193                 rc = PTR_ERR(*de_oldp);
2194                 CERROR("old child lookup error (%*s): %d\n",
2195                        old_len - 1, old_name, rc);
2196                 GOTO(cleanup, rc);
2197         }
2198
2199         cleanup_phase = 3; /* original name dentry */
2200
2201         inode = (*de_oldp)->d_inode;
2202         if (inode != NULL) {
2203                 inode = igrab(inode);
2204                 if (inode == NULL)
2205                         GOTO(cleanup, rc = -ENOENT);
2206
2207                 c1_res_id.name[0] = inode->i_ino;
2208                 c1_res_id.name[1] = inode->i_generation;
2209                 iput(inode);
2210         } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2211                 c1_res_id.name[0] = (*de_oldp)->d_inum;
2212                 c1_res_id.name[1] = (*de_oldp)->d_generation;
2213         }
2214
2215         /* Step 4: Lookup the target child entry */
2216         *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp, new_len - 1);
2217         if (IS_ERR(*de_newp)) {
2218                 rc = PTR_ERR(*de_newp);
2219                 CERROR("new child lookup error (%*s): %d\n",
2220                        old_len - 1, old_name, rc);
2221                 GOTO(cleanup, rc);
2222         }
2223
2224         cleanup_phase = 4; /* target dentry */
2225
2226         inode = (*de_newp)->d_inode;
2227         if (inode != NULL) {
2228                 inode = igrab(inode);
2229                 if (inode == NULL)
2230                         goto retry_locks;
2231
2232                 c2_res_id.name[0] = inode->i_ino;
2233                 c2_res_id.name[1] = inode->i_generation;
2234                 iput(inode);
2235         } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2236                 c2_res_id.name[0] = (*de_newp)->d_inum;
2237                 c2_res_id.name[1] = (*de_newp)->d_generation;
2238         }
2239
2240 retry_locks:
2241         /* Step 5: Take locks on the parents and child(ren) */
2242         maxres_src = &p1_res_id;
2243         maxres_tgt = &p2_res_id;
2244         cleanup_phase = 4; /* target dentry */
2245
2246         if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL,NULL))
2247                 maxres_src = &c1_res_id;
2248         if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL,NULL))
2249                 maxres_tgt = &c2_res_id;
2250
2251         rc = enqueue_4ordered_locks(obd, &p1_res_id,&dlm_handles[0],parent_mode,
2252                                     &p_policy,
2253                                     &p2_res_id, &dlm_handles[1], parent_mode,
2254                                     &p_policy,
2255                                     &c1_res_id, &dlm_handles[2], child_mode,
2256                                     &c1_policy,
2257                                     &c2_res_id, &dlm_handles[3], child_mode,
2258                                     &c2_policy);
2259         if (rc)
2260                 GOTO(cleanup, rc);
2261
2262         cleanup_phase = 6; /* parent and child(ren) locks */
2263
2264         /* Step 6a: Re-lookup source child to verify it hasn't changed */
2265         rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2266                               parent_mode, &c1_res_id, &dlm_handles[2],
2267                               de_oldp, child_mode, &c1_policy, old_name,old_len,
2268                               maxres_tgt);
2269         if (rc) {
2270                 if (c2_res_id.name[0] != 0)
2271                         ldlm_lock_decref(&dlm_handles[3], child_mode);
2272                 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2273                 cleanup_phase = 4;
2274                 if (rc > 0)
2275                         goto retry_locks;
2276                 GOTO(cleanup, rc);
2277         }
2278
2279         if (!DENTRY_VALID(*de_oldp))
2280                 GOTO(cleanup, rc = -ENOENT);
2281
2282         /* Step 6b: Re-lookup target child to verify it hasn't changed */
2283         rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2284                               parent_mode, &c2_res_id, &dlm_handles[3],
2285                               de_newp, child_mode, &c2_policy, new_name,
2286                               new_len, maxres_src);
2287         if (rc) {
2288                 ldlm_lock_decref(&dlm_handles[2], child_mode);
2289                 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2290                 cleanup_phase = 4;
2291                 if (rc > 0)
2292                         goto retry_locks;
2293                 GOTO(cleanup, rc);
2294         }
2295
2296         EXIT;
2297 cleanup:
2298         if (rc) {
2299                 switch (cleanup_phase) {
2300                 case 6: /* child lock(s) */
2301                         if (c2_res_id.name[0] != 0)
2302                                 ldlm_lock_decref(&dlm_handles[3], child_mode);
2303                         if (c1_res_id.name[0] != 0)
2304                                 ldlm_lock_decref(&dlm_handles[2], child_mode);
2305                 case 5: /* parent locks */
2306                         ldlm_lock_decref(&dlm_handles[1], parent_mode);
2307                         ldlm_lock_decref(&dlm_handles[0], parent_mode);
2308                 case 4: /* target dentry */
2309                         l_dput(*de_newp);
2310                 case 3: /* source dentry */
2311                         l_dput(*de_oldp);
2312                 case 2: /* target directory dentry */
2313                         l_dput(*de_tgtdirp);
2314                 case 1: /* source directry dentry */
2315                         l_dput(*de_srcdirp);
2316                 }
2317         }
2318
2319         return rc;
2320 }
2321
2322 static int mds_reint_rename_create_name(struct mds_update_record *rec,
2323                                         int offset, struct ptlrpc_request *req)
2324 {
2325         struct obd_device *obd = req->rq_export->exp_obd;
2326         struct dentry *de_srcdir = NULL;
2327         struct dentry *de_new = NULL;
2328         struct mds_obd *mds = mds_req2mds(req);
2329         struct lustre_handle parent_lockh[2];
2330         struct lustre_handle child_lockh;
2331         int cleanup_phase = 0;
2332         void *handle = NULL;
2333         int rc = 0;
2334         ENTRY;
2335
2336         /* another MDS executing rename operation has asked us
2337          * to create target name. such a creation should destroy
2338          * existing target name */
2339
2340         CDEBUG(D_OTHER, "%s: request to create name %s for %lu/%lu/%lu\n",
2341                         obd->obd_name, rec->ur_tgt,
2342                         (unsigned long) rec->ur_fid1->mds,
2343                         (unsigned long) rec->ur_fid1->id,
2344                         (unsigned long) rec->ur_fid1->generation);
2345
2346         /* first, lookup the target */
2347         child_lockh.cookie = 0;
2348         rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid2, parent_lockh,
2349                                          &de_srcdir,LCK_PW,MDS_INODELOCK_UPDATE,
2350                                          rec->ur_tgt, rec->ur_tgtlen,
2351                                          &child_lockh, &de_new, LCK_EX,
2352                                          MDS_INODELOCK_LOOKUP);
2353         if (rc)
2354                 GOTO(cleanup, rc);
2355
2356         cleanup_phase = 1;
2357
2358         LASSERT(de_srcdir);
2359         LASSERT(de_srcdir->d_inode);
2360         LASSERT(de_new);
2361
2362         if (de_new->d_inode) {
2363                 /* name exists and points to local inode
2364                  * try to unlink this name and create new one */
2365                 CERROR("%s: %s points to local inode %lu/%lu\n",
2366                        obd->obd_name, rec->ur_tgt,
2367                        (unsigned long) de_new->d_inode->i_ino,
2368                        (unsigned long) de_new->d_inode->i_generation);
2369                 handle = fsfilt_start(obd, de_srcdir->d_inode,
2370                                       FSFILT_OP_RENAME, NULL);
2371                 if (IS_ERR(handle))
2372                         GOTO(cleanup, rc = PTR_ERR(handle));
2373                 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de_new);
2374                 if (rc)
2375                         GOTO(cleanup, rc);
2376         } else if (de_new->d_flags & DCACHE_CROSS_REF) {
2377                 /* name exists adn points to remove inode */
2378                 CERROR("%s: %s points to remote inode %lu/%lu/%lu\n",
2379                        obd->obd_name, rec->ur_tgt,
2380                        (unsigned long) de_new->d_mdsnum,
2381                        (unsigned long) de_new->d_inum,
2382                        (unsigned long) de_new->d_generation);
2383         } else {
2384                 /* name doesn't exist. the simplest case */
2385                 handle = fsfilt_start(obd, de_srcdir->d_inode,
2386                                       FSFILT_OP_LINK, NULL);
2387                 if (IS_ERR(handle))
2388                         GOTO(cleanup, rc = PTR_ERR(handle));
2389         }
2390        
2391         cleanup_phase = 2;
2392         rc = fsfilt_add_dir_entry(obd, de_srcdir, rec->ur_tgt,
2393                         rec->ur_tgtlen - 1, rec->ur_fid1->id,
2394                         rec->ur_fid1->generation, rec->ur_fid1->mds);
2395         if (rc)
2396                 CERROR("add_dir_entry() returned error %d\n", rc);
2397 cleanup:
2398         EXIT;
2399         rc = mds_finish_transno(mds, de_srcdir ? de_srcdir->d_inode : NULL,
2400                                 handle, req, rc, 0);
2401         switch(cleanup_phase) {
2402                 case 2:
2403                 case 1:
2404 #ifdef S_PDIROPS
2405                         if (parent_lockh[1].cookie != 0)
2406                                 ldlm_lock_decref(&parent_lockh[1], LCK_CW);
2407 #endif
2408                         ldlm_lock_decref(&parent_lockh[0], LCK_PW);
2409                         if (child_lockh.cookie != 0)
2410                                 ldlm_lock_decref(&child_lockh, LCK_EX);
2411                         l_dput(de_new);
2412                         l_dput(de_srcdir);
2413                         break;
2414                 default:
2415                         LBUG();
2416         }
2417
2418         req->rq_status = rc;
2419
2420         RETURN(0);
2421 }
2422
2423 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
2424                                       struct ptlrpc_request *req)
2425 {
2426         struct obd_device *obd = req->rq_export->exp_obd;
2427         struct ptlrpc_request *req2 = NULL;
2428         struct dentry *de_srcdir = NULL;
2429         struct dentry *de_old = NULL;
2430         struct mds_obd *mds = mds_req2mds(req);
2431         struct lustre_handle parent_lockh[2];
2432         struct lustre_handle child_lockh;
2433         struct mdc_op_data opdata;
2434         int cleanup_phase = 0;
2435         void *handle = NULL;
2436         int rc = 0;
2437         ENTRY;
2438
2439         CDEBUG(D_OTHER, "%s: move name %s onto another mds%u\n",
2440                obd->obd_name, rec->ur_name, rec->ur_fid2->mds + 1);
2441         memset(&opdata, 0, sizeof(opdata));
2442
2443         child_lockh.cookie = 0;
2444         rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1, parent_lockh,
2445                                          &de_srcdir,LCK_PW,MDS_INODELOCK_UPDATE,
2446                                          rec->ur_name, rec->ur_namelen,
2447                                          &child_lockh, &de_old, LCK_EX,
2448                                          MDS_INODELOCK_LOOKUP);
2449         LASSERT(rc == 0);
2450         LASSERT(de_srcdir);
2451         LASSERT(de_srcdir->d_inode);
2452         LASSERT(de_old);
2453        
2454         /* we already know the target should be created on another MDS
2455          * so, we have to request that MDS to do it */
2456
2457         /* prepare source fid */
2458         if (de_old->d_flags & DCACHE_CROSS_REF) {
2459                 LASSERT(de_old->d_inode == NULL);
2460                 CDEBUG(D_OTHER, "request to move remote name\n");
2461                 opdata.fid1.mds = de_old->d_mdsnum;
2462                 opdata.fid1.id = de_old->d_inum;
2463                 opdata.fid1.generation = de_old->d_generation;
2464         } else if (de_old->d_inode == NULL) {
2465                 /* oh, source doesn't exist */
2466                 GOTO(cleanup, rc = -ENOENT);
2467         } else {
2468                 LASSERT(de_old->d_inode != NULL);
2469                 CDEBUG(D_OTHER, "request to move local name\n");
2470                 opdata.fid1.mds = mds->mds_num;
2471                 opdata.fid1.id = de_old->d_inode->i_ino;
2472                 opdata.fid1.generation = de_old->d_inode->i_generation;
2473         }
2474
2475         opdata.fid2 = *(rec->ur_fid2);
2476         rc = md_rename(mds->mds_lmv_exp, &opdata, NULL, 0, rec->ur_tgt,
2477                        rec->ur_tgtlen - 1, &req2);
2478        
2479         if (rc)
2480                 GOTO(cleanup, rc);
2481
2482         handle = fsfilt_start(obd, de_srcdir->d_inode, FSFILT_OP_UNLINK, NULL);
2483         if (IS_ERR(handle))
2484                 GOTO(cleanup, rc = PTR_ERR(handle));
2485         rc = fsfilt_del_dir_entry(obd, de_old);
2486         d_drop(de_old);
2487
2488 cleanup:
2489         EXIT;
2490         rc = mds_finish_transno(mds, de_srcdir ? de_srcdir->d_inode : NULL,
2491                                 handle, req, rc, 0);
2492         if (req2)
2493                 ptlrpc_req_finished(req2);
2494
2495 #ifdef S_PDIROPS
2496         if (parent_lockh[1].cookie != 0)
2497                 ldlm_lock_decref(&parent_lockh[1], LCK_CW);
2498 #endif
2499         ldlm_lock_decref(&parent_lockh[0], LCK_PW);
2500         if (child_lockh.cookie != 0)
2501                 ldlm_lock_decref(&child_lockh, LCK_EX);
2502
2503         l_dput(de_old);
2504         l_dput(de_srcdir);
2505
2506         req->rq_status = rc;
2507         RETURN(0);
2508
2509 }
2510
2511 static int mds_reint_rename(struct mds_update_record *rec, int offset,
2512                             struct ptlrpc_request *req,
2513                             struct lustre_handle *lockh)
2514 {
2515         struct obd_device *obd = req->rq_export->exp_obd;
2516         struct dentry *de_srcdir = NULL;
2517         struct dentry *de_tgtdir = NULL;
2518         struct dentry *de_old = NULL;
2519         struct dentry *de_new = NULL;
2520         struct mds_obd *mds = mds_req2mds(req);
2521         struct lustre_handle dlm_handles[7];
2522         struct mds_body *body = NULL;
2523         int rc = 0, lock_count = 3;
2524         int cleanup_phase = 0;
2525         void *handle = NULL;
2526         ENTRY;
2527
2528         LASSERT(offset == 0);
2529
2530         DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u %s to "LPU64"/%u %s",
2531                   rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name,
2532                   rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_tgt);
2533
2534         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2535
2536         if (rec->ur_namelen == 1) {
2537                 rc = mds_reint_rename_create_name(rec, offset, req);
2538                 RETURN(rc);
2539         }
2540
2541         if (rec->ur_fid2->mds != mds->mds_num) {
2542                 rc = mds_reint_rename_to_remote(rec, offset, req);
2543                 RETURN(rc);
2544         }
2545         
2546         rc = mds_get_parents_children_locked(obd, mds, rec->ur_fid1, &de_srcdir,
2547                                              rec->ur_fid2, &de_tgtdir, LCK_PW,
2548                                              rec->ur_name, rec->ur_namelen,
2549                                              &de_old, rec->ur_tgt,
2550                                              rec->ur_tgtlen, &de_new,
2551                                              dlm_handles, LCK_EX);
2552         if (rc)
2553                 GOTO(cleanup, rc);
2554
2555         cleanup_phase = 1; /* parent(s), children, locks */
2556
2557         if (de_new->d_inode)
2558                 lock_count = 4;
2559
2560         /* sanity check for src inode */
2561         if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
2562             de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
2563                 GOTO(cleanup, rc = -EINVAL);
2564
2565         /* sanity check for dest inode */
2566         if (de_new->d_inode &&
2567             (de_new->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
2568              de_new->d_inode->i_ino == de_tgtdir->d_inode->i_ino))
2569                 GOTO(cleanup, rc = -EINVAL);
2570
2571         if (de_old->d_inode == de_new->d_inode) {
2572                 GOTO(cleanup, rc = 0);
2573         }
2574
2575         /* if we are about to remove the target at first, pass the EA of
2576          * that inode to client to perform and cleanup on OST */
2577         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
2578         LASSERT(body != NULL);
2579
2580         if (de_new->d_inode &&
2581             S_ISREG(de_new->d_inode->i_mode) &&
2582             de_new->d_inode->i_nlink == 1 &&
2583             mds_open_orphan_count(de_new->d_inode) == 0) {
2584                 mds_pack_inode2fid(obd, &body->fid1, de_new->d_inode);
2585                 mds_pack_inode2body(obd, body, de_new->d_inode);
2586                 mds_pack_md(obd, req->rq_repmsg, 1, body, de_new->d_inode, 1);
2587                 if (!(body->valid & OBD_MD_FLEASIZE)) {
2588                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2589                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
2590                 } else {
2591                         /* XXX need log unlink? */
2592                 }
2593         }
2594
2595         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
2596                        de_srcdir->d_inode->i_sb);
2597
2598         handle = fsfilt_start(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME, NULL);
2599         if (IS_ERR(handle))
2600                 GOTO(cleanup, rc = PTR_ERR(handle));
2601
2602         /* FIXME need adjust the journal block count? */
2603         /* if the target should be moved to PENDING, we at first increase the
2604          * link and later vfs_rename() will decrease the link count again */
2605         if (de_new->d_inode &&
2606             S_ISREG(de_new->d_inode->i_mode) &&
2607             de_new->d_inode->i_nlink == 1 &&
2608             mds_open_orphan_count(de_new->d_inode) > 0) {
2609                 rc = mds_add_link_orphan(rec, obd, de_new);
2610                 if (rc)
2611                         GOTO(cleanup, rc);
2612         }
2613
2614         lock_kernel();
2615         de_old->d_fsdata = req;
2616         de_new->d_fsdata = req;
2617         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
2618         unlock_kernel();
2619
2620         GOTO(cleanup, rc);
2621 cleanup:
2622         rc = mds_finish_transno(mds, de_tgtdir ? de_tgtdir->d_inode : NULL,
2623                                 handle, req, rc, 0);
2624         switch (cleanup_phase) {
2625         case 1:
2626 #ifdef S_PDIROPS
2627                 if (dlm_handles[5].cookie != 0)
2628                         ldlm_lock_decref(&(dlm_handles[5]), LCK_CW);
2629                 if (dlm_handles[6].cookie != 0)
2630                         ldlm_lock_decref(&(dlm_handles[6]), LCK_CW);
2631 #endif
2632                 if (rc) {
2633                         if (lock_count == 4)
2634                                 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
2635                         ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
2636                         ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
2637                         ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
2638                 } else {
2639                         if (lock_count == 4)
2640                                 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
2641                         ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
2642                         ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
2643                         ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
2644                 }
2645                 l_dput(de_new);
2646                 l_dput(de_old);
2647                 l_dput(de_tgtdir);
2648                 l_dput(de_srcdir);
2649         case 0:
2650                 break;
2651         default:
2652                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2653                 LBUG();
2654         }
2655         req->rq_status = rc;
2656         return 0;
2657 }
2658
2659 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
2660                            struct ptlrpc_request *, struct lustre_handle *);
2661
2662 static mds_reinter reinters[REINT_MAX + 1] = {
2663         [REINT_SETATTR] mds_reint_setattr,
2664         [REINT_CREATE] mds_reint_create,
2665         [REINT_LINK] mds_reint_link,
2666         [REINT_UNLINK] mds_reint_unlink,
2667         [REINT_RENAME] mds_reint_rename,
2668         [REINT_OPEN] mds_open
2669 };
2670
2671 int mds_reint_rec(struct mds_update_record *rec, int offset,
2672                   struct ptlrpc_request *req, struct lustre_handle *lockh)
2673 {
2674         struct obd_device *obd = req->rq_export->exp_obd;
2675         struct obd_run_ctxt saved;
2676         int rc;
2677
2678         /* checked by unpacker */
2679         LASSERT(rec->ur_opcode <= REINT_MAX &&
2680                 reinters[rec->ur_opcode] != NULL);
2681
2682         push_ctxt(&saved, &obd->obd_ctxt, &rec->ur_uc);
2683         rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
2684         pop_ctxt(&saved, &obd->obd_ctxt, &rec->ur_uc);
2685
2686         return rc;
2687 }