Whamcloud - gitweb
landing smfs.
[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_log.h>
42 #include <linux/lustre_fsfilt.h>
43
44 #include "mds_internal.h"
45
46 void mds_commit_cb(struct obd_device *obd, __u64 transno, void *data,
47                    int error)
48 {
49         obd_transno_commit_cb(obd, transno, error);
50 }
51
52 struct mds_logcancel_data {
53         struct lov_mds_md      *mlcd_lmm;
54         int                     mlcd_size;
55         int                     mlcd_cookielen;
56         int                     mlcd_eadatalen;
57         struct llog_cookie      mlcd_cookies[0];
58 };
59
60
61 static void mds_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
62                                   void *cb_data, int error)
63 {
64         struct mds_logcancel_data *mlcd = cb_data;
65         struct lov_stripe_md *lsm = NULL;
66         struct llog_ctxt *ctxt;
67         int rc;
68
69         obd_transno_commit_cb(obd, transno, error);
70
71         CDEBUG(D_HA, "cancelling %d cookies\n",
72                (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
73
74         rc = obd_unpackmd(obd->u.mds.mds_osc_exp, &lsm, mlcd->mlcd_lmm,
75                           mlcd->mlcd_eadatalen);
76         if (rc < 0) {
77                 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
78                        (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
79                        rc);
80         } else {
81                 ///* XXX 0 normally, SENDNOW for debug */);
82                 ctxt = llog_get_context(obd,mlcd->mlcd_cookies[0].lgc_subsys+1);
83                 rc = llog_cancel(ctxt, mlcd->mlcd_cookielen /
84                                  sizeof(*mlcd->mlcd_cookies),
85                                  mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW, lsm);
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, mds->mds_sb,
151                               transno, handle, 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         char                       str[PTL_NALFMT_SIZE];
283         int                        i;
284
285         /* CAVEAT EMPTOR: spinlock order */
286         spin_lock_irqsave (&exp->exp_lock, flags);
287         list_for_each (tmp, &exp->exp_outstanding_replies) {
288                 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
289
290                 if (oldrep->rs_xid != req->rq_xid)
291                         continue;
292
293                 if (oldrep->rs_msg.opc != req->rq_reqmsg->opc)
294                         CERROR ("Resent req xid "LPX64" has mismatched opc: "
295                                 "new %d old %d\n", req->rq_xid,
296                                 req->rq_reqmsg->opc, oldrep->rs_msg.opc);
297
298                 svc = oldrep->rs_srv_ni->sni_service;
299                 spin_lock (&svc->srv_lock);
300
301                 list_del_init (&oldrep->rs_exp_list);
302
303                 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
304                       " o%d NID %s\n",
305                       oldrep->rs_nlocks, oldrep,
306                       oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg.opc,
307                       ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
308
309                 for (i = 0; i < oldrep->rs_nlocks; i++)
310                         ptlrpc_save_lock(req,
311                                          &oldrep->rs_locks[i],
312                                          oldrep->rs_modes[i]);
313                 oldrep->rs_nlocks = 0;
314
315                 DEBUG_REQ(D_HA, req, "stole locks for");
316                 ptlrpc_schedule_difficult_reply (oldrep);
317
318                 spin_unlock (&svc->srv_lock);
319                 spin_unlock_irqrestore (&exp->exp_lock, flags);
320                 return;
321         }
322         spin_unlock_irqrestore (&exp->exp_lock, flags);
323 }
324
325 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
326 {
327         DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
328                   mcd->mcd_last_transno, mcd->mcd_last_result);
329         req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
330         req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
331
332         mds_steal_ack_locks(req);
333 }
334
335 static void reconstruct_reint_setattr(struct mds_update_record *rec,
336                                       int offset, struct ptlrpc_request *req)
337 {
338         struct mds_export_data *med = &req->rq_export->exp_mds_data;
339         struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
340         struct dentry *de;
341         struct mds_body *body;
342
343         mds_req_from_mcd(req, med->med_mcd);
344
345         de = mds_fid2dentry(obd, rec->ur_fid1, NULL);
346         if (IS_ERR(de)) {
347                 LASSERT(PTR_ERR(de) == req->rq_status);
348                 return;
349         }
350
351         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
352         mds_pack_inode2fid(&body->fid1, de->d_inode);
353         mds_pack_inode2body(body, de->d_inode);
354
355         /* Don't return OST-specific attributes if we didn't just set them */
356         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
357                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
358         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
359                 body->valid |= OBD_MD_FLMTIME;
360         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
361                 body->valid |= OBD_MD_FLATIME;
362
363         l_dput(de);
364 }
365
366 /* In the raw-setattr case, we lock the child inode.
367  * In the write-back case or if being called from open, the client holds a lock
368  * already.
369  *
370  * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
371 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
372                              struct ptlrpc_request *req,
373                              struct lustre_handle *lh)
374 {
375         struct mds_obd *mds = mds_req2mds(req);
376         struct obd_device *obd = req->rq_export->exp_obd;
377         struct mds_body *body;
378         struct dentry *de;
379         struct inode *inode = NULL;
380         struct lustre_handle lockh;
381         void *handle = NULL;
382         struct mds_logcancel_data *mlcd = NULL;
383         int rc = 0, cleanup_phase = 0, err, locked = 0;
384         ENTRY;
385
386         LASSERT(offset == 0);
387
388         DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x", rec->ur_fid1->id,
389                   rec->ur_fid1->generation, rec->ur_iattr.ia_valid);
390
391         MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
392
393         if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
394                 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
395                 if (IS_ERR(de))
396                         GOTO(cleanup, rc = PTR_ERR(de));
397         } else {
398                 __u64 lockpart = MDS_INODELOCK_UPDATE;
399                 if (rec->ur_iattr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID) )
400                         lockpart |= MDS_INODELOCK_LOOKUP;
401                 de = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW,
402                                            &lockh, NULL, 0, lockpart);
403                 if (IS_ERR(de))
404                         GOTO(cleanup, rc = PTR_ERR(de));
405                 locked = 1;
406         }
407
408         cleanup_phase = 1;
409
410         inode = de->d_inode;
411         LASSERT(inode);
412         if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
413             rec->ur_eadata != NULL)
414                 down(&inode->i_sem);
415
416         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
417
418         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
419         if (IS_ERR(handle))
420                 GOTO(cleanup, rc = PTR_ERR(handle));
421
422         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
423                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
424                        LTIME_S(rec->ur_iattr.ia_mtime),
425                        LTIME_S(rec->ur_iattr.ia_ctime));
426         rc = mds_fix_attr(inode, rec);
427         if (rc)
428                 GOTO(cleanup, rc);
429
430         if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG)    /* ioctl */
431                 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
432                                       (long)&rec->ur_iattr.ia_attr_flags);
433         else                                            /* setattr */
434                 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
435
436         if (rc == 0 && (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
437             rec->ur_eadata != NULL) {
438                 struct lov_stripe_md *lsm = NULL;
439
440                 rc = ll_permission(inode, MAY_WRITE, NULL);
441                 if (rc < 0)
442                         GOTO(cleanup, rc);
443
444                 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
445                                    mds->mds_osc_exp, 0, &lsm, rec->ur_eadata);
446                 if (rc)
447                         GOTO(cleanup, rc);
448
449                 obd_free_memmd(mds->mds_osc_exp, &lsm);
450
451                 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
452                                    rec->ur_eadatalen);
453                 if (rc)
454                         GOTO(cleanup, rc);
455         }
456
457         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
458         mds_pack_inode2fid(&body->fid1, inode);
459         mds_pack_inode2body(body, inode);
460
461         /* Don't return OST-specific attributes if we didn't just set them */
462         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
463                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
464         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
465                 body->valid |= OBD_MD_FLMTIME;
466         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
467                 body->valid |= OBD_MD_FLATIME;
468
469         if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_osc_obd)) {
470                 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
471                           rec->ur_eadatalen);
472                 if (mlcd) {
473                         mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
474                                 rec->ur_eadatalen;
475                         mlcd->mlcd_eadatalen = rec->ur_eadatalen;
476                         mlcd->mlcd_cookielen = rec->ur_cookielen;
477                         mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
478                                 mlcd->mlcd_cookielen;
479                         memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
480                                mlcd->mlcd_cookielen);
481                         memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
482                                mlcd->mlcd_eadatalen);
483                 } else {
484                         CERROR("unable to allocate log cancel data\n");
485                 }
486         }
487         EXIT;
488  cleanup:
489         if (mlcd != NULL)
490                 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
491                                       handle, mds_cancel_cookies_cb, mlcd);
492         err = mds_finish_transno(mds, inode, handle, req, rc, 0);
493         switch (cleanup_phase) {
494         case 1:
495                 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
496                     rec->ur_eadata != NULL)
497                         up(&inode->i_sem);
498                 l_dput(de);
499                 if (locked) {
500                         if (rc) {
501                                 ldlm_lock_decref(&lockh, LCK_PW);
502                         } else {
503                                 ptlrpc_save_lock (req, &lockh, LCK_PW);
504                         }
505                 }
506         case 0:
507                 break;
508         default:
509                 LBUG();
510         }
511         if (err && !rc)
512                 rc = err;
513
514         req->rq_status = rc;
515         return 0;
516 }
517
518 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
519                                      struct ptlrpc_request *req)
520 {
521         struct mds_export_data *med = &req->rq_export->exp_mds_data;
522         struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
523         struct dentry *parent, *child;
524         struct mds_body *body;
525
526         mds_req_from_mcd(req, med->med_mcd);
527
528         if (req->rq_status)
529                 return;
530
531         parent = mds_fid2dentry(obd, rec->ur_fid1, NULL);
532         LASSERT(!IS_ERR(parent));
533         child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
534         LASSERT(!IS_ERR(child));
535         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
536         mds_pack_inode2fid(&body->fid1, child->d_inode);
537         mds_pack_inode2body(body, child->d_inode);
538         l_dput(parent);
539         l_dput(child);
540 }
541
542 static int mds_reint_create(struct mds_update_record *rec, int offset,
543                             struct ptlrpc_request *req,
544                             struct lustre_handle *lh)
545 {
546         struct dentry *dparent = NULL;
547         struct mds_obd *mds = mds_req2mds(req);
548         struct obd_device *obd = req->rq_export->exp_obd;
549         struct dentry *dchild = NULL;
550         struct inode *dir = NULL;
551         void *handle = NULL;
552         struct lustre_handle lockh;
553         int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
554         int created = 0;
555         struct dentry_params dp;
556         ENTRY;
557
558         LASSERT(offset == 0);
559         LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
560
561         DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
562                   rec->ur_fid1->id, rec->ur_fid1->generation,
563                   rec->ur_name, rec->ur_mode);
564
565         MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
566
567         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
568                 GOTO(cleanup, rc = -ESTALE);
569
570         dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW, &lockh,
571                                         rec->ur_name, rec->ur_namelen - 1,
572                                         MDS_INODELOCK_UPDATE);
573         if (IS_ERR(dparent)) {
574                 rc = PTR_ERR(dparent);
575                 CERROR("parent lookup error %d\n", rc);
576                 GOTO(cleanup, rc);
577         }
578         cleanup_phase = 1; /* locked parent dentry */
579         dir = dparent->d_inode;
580         LASSERT(dir);
581
582         ldlm_lock_dump_handle(D_OTHER, &lockh);
583
584         dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
585         if (IS_ERR(dchild)) {
586                 rc = PTR_ERR(dchild);
587                 CERROR("child lookup error %d\n", rc);
588                 GOTO(cleanup, rc);
589         }
590
591         cleanup_phase = 2; /* child dentry */
592
593         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
594
595         if (dir->i_mode & S_ISGID) {
596                 if (S_ISDIR(rec->ur_mode))
597                         rec->ur_mode |= S_ISGID;
598         }
599
600         dchild->d_fsdata = (void *)&dp;
601         dp.p_inum = (unsigned long)rec->ur_fid2->id;
602         dp.p_ptr = req;
603
604         switch (type) {
605         case S_IFREG:{
606                 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
607                 if (IS_ERR(handle))
608                         GOTO(cleanup, rc = PTR_ERR(handle));
609                 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
610                 EXIT;
611                 break;
612         }
613         case S_IFDIR:{
614                 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
615                 if (IS_ERR(handle))
616                         GOTO(cleanup, rc = PTR_ERR(handle));
617                 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
618                 EXIT;
619                 break;
620         }
621         case S_IFLNK:{
622                 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
623                 if (IS_ERR(handle))
624                         GOTO(cleanup, rc = PTR_ERR(handle));
625                 if (rec->ur_tgt == NULL)        /* no target supplied */
626                         rc = -EINVAL;           /* -EPROTO? */
627                 else
628                         rc = vfs_symlink(dir, dchild, rec->ur_tgt);
629                 EXIT;
630                 break;
631         }
632         case S_IFCHR:
633         case S_IFBLK:
634         case S_IFIFO:
635         case S_IFSOCK:{
636                 int rdev = rec->ur_rdev;
637                 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
638                 if (IS_ERR(handle))
639                         GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
640                 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
641                 EXIT;
642                 break;
643         }
644         default:
645                 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
646                 dchild->d_fsdata = NULL;
647                 GOTO(cleanup, rc = -EINVAL);
648         }
649
650         /* In case we stored the desired inum in here, we want to clean up. */
651         if (dchild->d_fsdata == (void *)(unsigned long)rec->ur_fid2->id)
652                 dchild->d_fsdata = NULL;
653
654         if (rc) {
655                 CDEBUG(D_INODE, "error during create: %d\n", rc);
656                 GOTO(cleanup, rc);
657         } else {
658                 struct iattr iattr;
659                 struct inode *inode = dchild->d_inode;
660                 struct mds_body *body;
661
662                 created = 1;
663                 LTIME_S(iattr.ia_atime) = rec->ur_time;
664                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
665                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
666                 iattr.ia_uid = rec->ur_fsuid;
667                 if (dir->i_mode & S_ISGID)
668                         iattr.ia_gid = dir->i_gid;
669                 else
670                         iattr.ia_gid = rec->ur_fsgid;
671                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
672                         ATTR_MTIME | ATTR_CTIME;
673
674                 if (rec->ur_fid2->id) {
675                         LASSERT(rec->ur_fid2->id == inode->i_ino);
676                         inode->i_generation = rec->ur_fid2->generation;
677                         /* Dirtied and committed by the upcoming setattr. */
678                         CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
679                                inode->i_ino, inode->i_generation);
680                 } else {
681                         struct lustre_handle child_ino_lockh;
682
683                         CDEBUG(D_INODE, "created ino %lu with gen %x\n",
684                                inode->i_ino, inode->i_generation);
685
686                         /* The inode we were allocated may have just been freed
687                          * by an unlink operation.  We take this lock to
688                          * synchronize against the matching reply-ack-lock taken
689                          * in unlink, to avoid replay problems if this reply
690                          * makes it out to the client but the unlink's does not.
691                          * See bug 2029 for more detail.*/
692                         rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
693                         if (rc != ELDLM_OK) {
694                                 CERROR("error locking for unlink/create sync: "
695                                        "%d\n", rc);
696                         } else {
697                                 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
698                         }
699                 }
700
701                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
702                 if (rc)
703                         CERROR("error on child setattr: rc = %d\n", rc);
704
705                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
706                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
707                 if (rc)
708                         CERROR("error on parent setattr: rc = %d\n", rc);
709
710                 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
711                 mds_pack_inode2fid(&body->fid1, inode);
712                 mds_pack_inode2body(body, inode);
713         }
714         EXIT;
715
716 cleanup:
717         err = mds_finish_transno(mds, dir, handle, req, rc, 0);
718
719         if (rc && created) {
720                 /* Destroy the file we just created.  This should not need
721                  * extra journal credits, as we have already modified all of
722                  * the blocks needed in order to create the file in the first
723                  * place.
724                  */
725                 switch (type) {
726                 case S_IFDIR:
727                         err = vfs_rmdir(dir, dchild);
728                         if (err)
729                                 CERROR("rmdir in error path: %d\n", err);
730                         break;
731                 default:
732                         err = vfs_unlink(dir, dchild);
733                         if (err)
734                                 CERROR("unlink in error path: %d\n", err);
735                         break;
736                 }
737         } else {
738                 rc = err;
739         }
740         switch (cleanup_phase) {
741         case 2: /* child dentry */
742                 l_dput(dchild);
743         case 1: /* locked parent dentry */
744                 if (rc) {
745                         ldlm_lock_decref(&lockh, LCK_PW);
746                 } else {
747                         ptlrpc_save_lock (req, &lockh, LCK_PW);
748                 }
749                 l_dput(dparent);
750         case 0:
751                 break;
752         default:
753                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
754                 LBUG();
755         }
756         req->rq_status = rc;
757         return 0;
758 }
759
760 static int res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
761            ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
762 {
763         int i;
764
765         for (i = 0; i < RES_NAME_SIZE; i++) {
766                 /* return 1 here, because enqueue_ordered will skip resources
767                  * of all zeroes if they're sorted to the end of the list. */
768                 if (res1->name[i] == 0 && res2->name[i] != 0)
769                         return 1;
770                 if (res2->name[i] == 0 && res1->name[i] != 0)
771                         return 0;
772
773                 if (res1->name[i] > res2->name[i])
774                         return 1;
775                 if (res1->name[i] < res2->name[i])
776                         return 0;
777         }
778
779         if (!p1 || !p2)
780                 return 0;
781
782         if (memcmp(p1, p2, sizeof(*p1)) < 0)
783                 return 1;
784
785         return 0;
786 }
787
788 /* This function doesn't use ldlm_match_or_enqueue because we're always called
789  * with EX or PW locks, and the MDS is no longer allowed to match write locks,
790  * because they take the place of local semaphores.
791  *
792  * One or two locks are taken in numerical order.  A res_id->name[0] of 0 means
793  * no lock is taken for that res_id.  Must be at least one non-zero res_id. */
794 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
795                           struct lustre_handle *p1_lockh, int p1_lock_mode,
796                           ldlm_policy_data_t *p1_policy,
797                           struct ldlm_res_id *p2_res_id,
798                           struct lustre_handle *p2_lockh, int p2_lock_mode,
799                           ldlm_policy_data_t *p2_policy)
800 {
801         struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
802         struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
803         int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
804         ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
805         int rc, flags;
806         ENTRY;
807
808         LASSERT(p1_res_id != NULL && p2_res_id != NULL);
809
810         CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n", res_id[0]->name[0],
811                res_id[1]->name[0]);
812
813         if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
814                 handles[1] = p1_lockh;
815                 handles[0] = p2_lockh;
816                 res_id[1] = p1_res_id;
817                 res_id[0] = p2_res_id;
818                 lock_modes[1] = p1_lock_mode;
819                 lock_modes[0] = p2_lock_mode;
820                 policies[1] = p1_policy;
821                 policies[0] = p2_policy;
822         }
823
824         CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
825                res_id[0]->name[0], res_id[1]->name[0]);
826
827         flags = LDLM_FL_LOCAL_ONLY;
828         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
829                               LDLM_IBITS, policies[0], lock_modes[0], &flags,
830                               mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
831                               NULL, 0, NULL, handles[0]);
832         if (rc != ELDLM_OK)
833                 RETURN(-EIO);
834         ldlm_lock_dump_handle(D_OTHER, handles[0]);
835
836         if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
837             (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
838                 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
839                 ldlm_lock_addref(handles[1], lock_modes[1]);
840         } else if (res_id[1]->name[0] != 0) {
841                 flags = LDLM_FL_LOCAL_ONLY;
842                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
843                                       *res_id[1], LDLM_IBITS, policies[1],
844                                       lock_modes[1], &flags, mds_blocking_ast,
845                                       ldlm_completion_ast, NULL, NULL, NULL, 0,
846                                       NULL, handles[1]);
847                 if (rc != ELDLM_OK) {
848                         ldlm_lock_decref(handles[0], lock_modes[0]);
849                         RETURN(-EIO);
850                 }
851                 ldlm_lock_dump_handle(D_OTHER, handles[1]);
852         }
853
854         RETURN(0);
855 }
856
857 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
858                            struct lustre_handle *p1_lockh, int p1_lock_mode,
859                            ldlm_policy_data_t *p1_policy,
860                            struct ldlm_res_id *p2_res_id,
861                            struct lustre_handle *p2_lockh, int p2_lock_mode,
862                            ldlm_policy_data_t *p2_policy,
863                            struct ldlm_res_id *c1_res_id,
864                            struct lustre_handle *c1_lockh, int c1_lock_mode,
865                            ldlm_policy_data_t *c1_policy,
866                            struct ldlm_res_id *c2_res_id,
867                            struct lustre_handle *c2_lockh, int c2_lock_mode,
868                            ldlm_policy_data_t *c2_policy)
869 {
870         struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
871                                           c1_res_id, c2_res_id };
872         struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
873                                                  c1_lockh, c2_lockh };
874         int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
875                               c1_lock_mode, c2_lock_mode };
876         ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
877                                             c1_policy, c2_policy};
878         int rc, i, j, sorted, flags;
879         ENTRY;
880
881         CDEBUG(D_DLMTRACE,
882                "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
883                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
884                res_id[3]->name[0]);
885
886         /* simple insertion sort - we have at most 4 elements */
887         for (i = 1; i < 4; i++) {
888                 j = i - 1;
889                 dlm_handles[4] = dlm_handles[i];
890                 res_id[4] = res_id[i];
891                 lock_modes[4] = lock_modes[i];
892                 policies[4] = policies[i];
893
894                 sorted = 0;
895                 do {
896                         if (res_gt(res_id[j], res_id[4], policies[j],
897                                    policies[4])) {
898                                 dlm_handles[j + 1] = dlm_handles[j];
899                                 res_id[j + 1] = res_id[j];
900                                 lock_modes[j + 1] = lock_modes[j];
901                                 policies[j + 1] = policies[j];
902                                 j--;
903                         } else {
904                                 sorted = 1;
905                         }
906                 } while (j >= 0 && !sorted);
907
908                 dlm_handles[j + 1] = dlm_handles[4];
909                 res_id[j + 1] = res_id[4];
910                 lock_modes[j + 1] = lock_modes[4];
911                 policies[j + 1] = policies[4];
912         }
913
914         CDEBUG(D_DLMTRACE,
915                "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
916                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
917                res_id[3]->name[0]);
918
919         /* XXX we could send ASTs on all these locks first before blocking? */
920         for (i = 0; i < 4; i++) {
921                 flags = 0;
922                 if (res_id[i]->name[0] == 0)
923                         break;
924                 if (i != 0 &&
925                     !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
926                     (policies[i]->l_inodebits.bits &
927                      policies[i-1]->l_inodebits.bits) ) {
928                         memcpy(dlm_handles[i], dlm_handles[i-1],
929                                sizeof(*(dlm_handles[i])));
930                         ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
931                 } else {
932                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
933                                               *res_id[i], LDLM_IBITS,
934                                               policies[i],
935                                               lock_modes[i], &flags,
936                                               mds_blocking_ast,
937                                               ldlm_completion_ast, NULL, NULL,
938                                               NULL, 0, NULL, dlm_handles[i]);
939                         if (rc != ELDLM_OK)
940                                 GOTO(out_err, rc = -EIO);
941                         ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
942                 }
943         }
944
945         RETURN(0);
946 out_err:
947         while (i-- > 0)
948                 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
949
950         return rc;
951 }
952
953 /* In the unlikely case that the child changed while we were waiting
954  * on the lock, we need to drop the lock on the old child and either:
955  * - if the child has a lower resource name, then we have to also
956  *   drop the parent lock and regain the locks in the right order
957  * - in the rename case, if the child has a lower resource name than one of
958  *   the other parent/child resources (maxres) we also need to reget the locks
959  * - if the child has a higher resource name (this is the common case)
960  *   we can just get the lock on the new child (still in lock order)
961  *
962  * Returns 0 if the child did not change or if it changed but could be locked.
963  * Returns 1 if the child changed and we need to re-lock (no locks held).
964  * Returns -ve error with a valid dchild (no locks held). */
965 static int mds_verify_child(struct obd_device *obd,
966                             struct ldlm_res_id *parent_res_id,
967                             struct lustre_handle *parent_lockh,
968                             struct dentry *dparent, int parent_mode,
969                             struct ldlm_res_id *child_res_id,
970                             struct lustre_handle *child_lockh,
971                             struct dentry **dchildp, int child_mode,
972                             ldlm_policy_data_t *child_policy,
973                             const char *name, int namelen,
974                             struct ldlm_res_id *maxres)
975 {
976         struct dentry *vchild, *dchild = *dchildp;
977         int rc = 0, cleanup_phase = 2; /* parent, child locks */
978         ENTRY;
979
980         vchild = ll_lookup_one_len(name, dparent, namelen - 1);
981         if (IS_ERR(vchild))
982                 GOTO(cleanup, rc = PTR_ERR(vchild));
983
984         if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
985                    (vchild->d_inode != NULL &&
986                     child_res_id->name[0] == vchild->d_inode->i_ino &&
987                     child_res_id->name[1] == vchild->d_inode->i_generation))) {
988                 if (dchild != NULL)
989                         l_dput(dchild);
990                 *dchildp = vchild;
991
992                 RETURN(0);
993         }
994
995         CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
996                vchild->d_inode, dchild ? dchild->d_inode : 0,
997                vchild->d_inode ? vchild->d_inode->i_ino : 0,
998                child_res_id->name[0]);
999         if (child_res_id->name[0] != 0)
1000                 ldlm_lock_decref(child_lockh, child_mode);
1001         if (dchild)
1002                 l_dput(dchild);
1003
1004         cleanup_phase = 1; /* parent lock only */
1005         *dchildp = dchild = vchild;
1006
1007         if (dchild->d_inode) {
1008                 int flags = 0;
1009                 child_res_id->name[0] = dchild->d_inode->i_ino;
1010                 child_res_id->name[1] = dchild->d_inode->i_generation;
1011
1012                 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1013                     res_gt(maxres, child_res_id, NULL, NULL)) {
1014                         CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1015                                child_res_id->name[0], parent_res_id->name[0],
1016                                maxres->name[0]);
1017                         GOTO(cleanup, rc = 1);
1018                 }
1019
1020                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1021                                       *child_res_id, LDLM_IBITS, child_policy,
1022                                       child_mode, &flags, mds_blocking_ast,
1023                                       ldlm_completion_ast, NULL, NULL, NULL, 0,
1024                                       NULL, child_lockh);
1025                 if (rc != ELDLM_OK)
1026                         GOTO(cleanup, rc = -EIO);
1027
1028         } else {
1029                 memset(child_res_id, 0, sizeof(*child_res_id));
1030         }
1031
1032         EXIT;
1033 cleanup:
1034         if (rc) {
1035                 switch(cleanup_phase) {
1036                 case 2:
1037                         if (child_res_id->name[0] != 0)
1038                                 ldlm_lock_decref(child_lockh, child_mode);
1039                 case 1:
1040                         ldlm_lock_decref(parent_lockh, parent_mode);
1041                 }
1042         }
1043         return rc;
1044 }
1045
1046 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1047                                 struct ll_fid *fid,
1048                                 struct lustre_handle *parent_lockh,
1049                                 struct dentry **dparentp, int parent_mode,
1050                                 __u64 parent_lockpart,
1051                                 char *name, int namelen,
1052                                 struct lustre_handle *child_lockh,
1053                                 struct dentry **dchildp, int child_mode,
1054                                 __u64 child_lockpart)
1055 {
1056         struct ldlm_res_id child_res_id = { .name = {0} };
1057         struct ldlm_res_id parent_res_id = { .name = {0} };
1058         ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1059         ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1060         struct inode *inode;
1061         int rc = 0, cleanup_phase = 0;
1062         ENTRY;
1063
1064         /* Step 1: Lookup parent */
1065         *dparentp = mds_fid2dentry(mds, fid, NULL);
1066         if (IS_ERR(*dparentp)) {
1067                 rc = PTR_ERR(*dparentp);
1068                 *dparentp = NULL;
1069                 RETURN(rc);
1070         }
1071
1072         CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1073                (*dparentp)->d_inode->i_ino, name);
1074
1075         parent_res_id.name[0] = (*dparentp)->d_inode->i_ino;
1076         parent_res_id.name[1] = (*dparentp)->d_inode->i_generation;
1077
1078         cleanup_phase = 1; /* parent dentry */
1079
1080         /* Step 2: Lookup child (without DLM lock, to get resource name) */
1081         *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1082         if (IS_ERR(*dchildp)) {
1083                 rc = PTR_ERR(*dchildp);
1084                 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1085                 GOTO(cleanup, rc);
1086         }
1087
1088         inode = (*dchildp)->d_inode;
1089         if (inode != NULL)
1090                 inode = igrab(inode);
1091         if (inode == NULL)
1092                 goto retry_locks;
1093
1094         child_res_id.name[0] = inode->i_ino;
1095         child_res_id.name[1] = inode->i_generation;
1096
1097         iput(inode);
1098
1099 retry_locks:
1100         cleanup_phase = 2; /* child dentry */
1101
1102         /* Step 3: Lock parent and child in resource order.  If child doesn't
1103          *         exist, we still have to lock the parent and re-lookup. */
1104         rc = enqueue_ordered_locks(obd,&parent_res_id,parent_lockh,parent_mode,
1105                                    &parent_policy,
1106                                    &child_res_id, child_lockh, child_mode,
1107                                    &child_policy);
1108         if (rc)
1109                 GOTO(cleanup, rc);
1110
1111         if (!(*dchildp)->d_inode)
1112                 cleanup_phase = 3; /* parent lock */
1113         else
1114                 cleanup_phase = 4; /* child lock */
1115
1116         /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1117         rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1118                               parent_mode, &child_res_id, child_lockh, 
1119                               dchildp, child_mode, &child_policy,
1120                               name, namelen, &parent_res_id);
1121         if (rc > 0)
1122                 goto retry_locks;
1123         if (rc < 0) {
1124                 cleanup_phase = 3;
1125                 GOTO(cleanup, rc);
1126         }
1127
1128 cleanup:
1129         if (rc) {
1130                 switch (cleanup_phase) {
1131                 case 4:
1132                         ldlm_lock_decref(child_lockh, child_mode);
1133                 case 3:
1134                         ldlm_lock_decref(parent_lockh, parent_mode);
1135                 case 2:
1136                         l_dput(*dchildp);
1137                 case 1:
1138                         l_dput(*dparentp);
1139                 default: ;
1140                 }
1141         }
1142         return rc;
1143 }
1144
1145 void mds_reconstruct_generic(struct ptlrpc_request *req)
1146 {
1147         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1148
1149         mds_req_from_mcd(req, med->med_mcd);
1150 }
1151
1152 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1153                             struct ptlrpc_request *req,
1154                             struct lustre_handle *lh)
1155 {
1156         struct dentry *dparent, *dchild;
1157         struct mds_obd *mds = mds_req2mds(req);
1158         struct obd_device *obd = req->rq_export->exp_obd;
1159         struct mds_body *body = NULL;
1160         struct inode *child_inode;
1161         struct lustre_handle parent_lockh, child_lockh, child_reuse_lockh;
1162         void *handle = NULL;
1163         int rc = 0, log_unlink = 0, cleanup_phase = 0;
1164         ENTRY;
1165
1166         LASSERT(offset == 0 || offset == 2);
1167
1168         DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1169                   rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name);
1170
1171         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1172
1173         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1174                 GOTO(cleanup, rc = -ENOENT);
1175
1176         rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1,
1177                                          &parent_lockh, &dparent, LCK_PW,
1178                                          MDS_INODELOCK_UPDATE,
1179                                          rec->ur_name, rec->ur_namelen,
1180                                          &child_lockh, &dchild, LCK_EX,
1181                                          MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE);
1182         if (rc)
1183                 GOTO(cleanup, rc);
1184
1185         cleanup_phase = 1; /* dchild, dparent, locks */
1186
1187         dget(dchild);
1188         child_inode = dchild->d_inode;
1189         if (child_inode == NULL) {
1190                 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1191                        dparent->d_inode->i_ino, rec->ur_name);
1192                 GOTO(cleanup, rc = -ENOENT);
1193         }
1194
1195         cleanup_phase = 2; /* dchild has a lock */
1196
1197         /* Step 4: Get a lock on the ino to sync with creation WRT inode
1198          * reuse (see bug 2029). */
1199         rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
1200         if (rc != ELDLM_OK)
1201                 GOTO(cleanup, rc);
1202
1203         cleanup_phase = 3; /* child inum lock */
1204
1205         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
1206
1207         /* ldlm_reply in buf[0] if called via intent */
1208         if (offset)
1209                 offset = 1;
1210
1211         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
1212         LASSERT(body != NULL);
1213
1214         /* If this is the last reference to this inode, get the OBD EA
1215          * data first so the client can destroy OST objects.
1216          * we only do the object removal if no open files remain.
1217          * Nobody can get at this name anymore because of the locks so
1218          * we make decisions here as to whether to remove the inode */
1219         if (S_ISREG(child_inode->i_mode) && child_inode->i_nlink == 1 &&
1220             mds_open_orphan_count(child_inode) == 0) {
1221                 mds_pack_inode2fid(&body->fid1, child_inode);
1222                 mds_pack_inode2body(body, child_inode);
1223                 mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
1224                             child_inode, 1);
1225                 if (!(body->valid & OBD_MD_FLEASIZE)) {
1226                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1227                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
1228                 } else {
1229                         log_unlink = 1;
1230                 }
1231         }
1232
1233         /* We have to do these checks ourselves, in case we are making an
1234          * orphan.  The client tells us whether rmdir() or unlink() was called,
1235          * so we need to return appropriate errors (bug 72).
1236          *
1237          * We don't have to check permissions, because vfs_rename (called from
1238          * mds_open_unlink_rename) also calls may_delete. */
1239         if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1240                 if (!S_ISDIR(child_inode->i_mode))
1241                         GOTO(cleanup, rc = -ENOTDIR);
1242         } else {
1243                 if (S_ISDIR(child_inode->i_mode))
1244                         GOTO(cleanup, rc = -EISDIR);
1245         }
1246
1247         /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
1248         switch (child_inode->i_mode & S_IFMT) {
1249         case S_IFDIR:
1250                 /* Drop any lingering child directories before we start our
1251                  * transaction, to avoid doing multiple inode dirty/delete
1252                  * in our compound transaction (bug 1321). */
1253                 shrink_dcache_parent(dchild);
1254                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1255                                       NULL);
1256                 if (IS_ERR(handle))
1257                         GOTO(cleanup, rc = PTR_ERR(handle));
1258                 cleanup_phase = 4; /* transaction */
1259                 rc = vfs_rmdir(dparent->d_inode, dchild);
1260                 break;
1261         case S_IFREG: {
1262                 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
1263                                                         offset + 1, 0);
1264                 handle = fsfilt_start_log(obd, dparent->d_inode,
1265                                           FSFILT_OP_UNLINK, NULL,
1266                                           le32_to_cpu(lmm->lmm_stripe_count));
1267                 if (IS_ERR(handle))
1268                         GOTO(cleanup, rc = PTR_ERR(handle));
1269
1270                 cleanup_phase = 4; /* transaction */
1271                 rc = vfs_unlink(dparent->d_inode, dchild);
1272
1273                 if (!rc && log_unlink)
1274                         if (mds_log_op_unlink(obd, child_inode,
1275                                 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
1276                                 req->rq_repmsg->buflens[offset + 1],
1277                                 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
1278                                 req->rq_repmsg->buflens[offset + 2]) > 0)
1279                                 body->valid |= OBD_MD_FLCOOKIE;
1280                 break;
1281         }
1282         case S_IFLNK:
1283         case S_IFCHR:
1284         case S_IFBLK:
1285         case S_IFIFO:
1286         case S_IFSOCK:
1287                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
1288                                       NULL);
1289                 if (IS_ERR(handle))
1290                         GOTO(cleanup, rc = PTR_ERR(handle));
1291                 cleanup_phase = 4; /* transaction */
1292                 rc = vfs_unlink(dparent->d_inode, dchild);
1293                 break;
1294         default:
1295                 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
1296                        rec->ur_name);
1297                 LBUG();
1298                 GOTO(cleanup, rc = -EINVAL);
1299         }
1300
1301  cleanup:
1302         if (rc == 0) {
1303                 struct iattr iattr;
1304                 int err;
1305
1306                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1307                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1308                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1309
1310                 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1311                 if (err)
1312                         CERROR("error on parent setattr: rc = %d\n", err);
1313         }
1314
1315         switch(cleanup_phase) {
1316         case 4:
1317                 LASSERT(dchild != NULL && dchild->d_inode != NULL);
1318                 LASSERT(atomic_read(&dchild->d_inode->i_count) > 0);
1319                 if (rc == 0 && dchild->d_inode->i_nlink == 0 &&
1320                                 mds_open_orphan_count(dchild->d_inode) > 0) {
1321                         /* filesystem is really going to destroy an inode
1322                          * we have to delay this till inode is opened -bzzz */
1323                         mds_open_unlink_rename(rec, obd, dparent, dchild, NULL);
1324                 }
1325                 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1326                                         rc, 0);
1327                 if (!rc)
1328                         (void)obd_set_info(mds->mds_osc_exp, strlen("unlinked"),
1329                                            "unlinked", 0, NULL);
1330         case 3: /* child ino-reuse lock */
1331                 if (rc && body != NULL) {
1332                         // Don't unlink the OST objects if the MDS unlink failed
1333                         body->valid = 0;
1334                 }
1335                 if (rc)
1336                         ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
1337                 else
1338                         ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
1339         case 2: /* child lock */
1340                 ldlm_lock_decref(&child_lockh, LCK_EX);
1341         case 1: /* child and parent dentry, parent lock */
1342                 if (rc)
1343                         ldlm_lock_decref(&parent_lockh, LCK_PW);
1344                 else
1345                         ptlrpc_save_lock(req, &parent_lockh, LCK_PW);
1346                 l_dput(dchild);
1347                 l_dput(dchild);
1348                 l_dput(dparent);
1349         case 0:
1350                 break;
1351         default:
1352                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1353                 LBUG();
1354         }
1355         req->rq_status = rc;
1356         return 0;
1357 }
1358
1359 static int mds_reint_link(struct mds_update_record *rec, int offset,
1360                           struct ptlrpc_request *req,
1361                           struct lustre_handle *lh)
1362 {
1363         struct obd_device *obd = req->rq_export->exp_obd;
1364         struct dentry *de_src = NULL;
1365         struct dentry *de_tgt_dir = NULL;
1366         struct dentry *dchild = NULL;
1367         struct mds_obd *mds = mds_req2mds(req);
1368         struct lustre_handle *handle = NULL, tgt_dir_lockh, src_lockh;
1369         struct ldlm_res_id src_res_id = { .name = {0} };
1370         struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
1371         ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
1372         ldlm_policy_data_t tgt_dir_policy =
1373                                        {.l_inodebits = {MDS_INODELOCK_UPDATE}};
1374
1375         int rc = 0, cleanup_phase = 0;
1376         ENTRY;
1377
1378         LASSERT(offset == 0);
1379
1380         DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
1381                   rec->ur_fid1->id, rec->ur_fid1->generation,
1382                   rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_name);
1383
1384         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1385
1386         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1387                 GOTO(cleanup, rc = -ENOENT);
1388
1389         /* Step 1: Lookup the source inode and target directory by FID */
1390         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1391         if (IS_ERR(de_src))
1392                 GOTO(cleanup, rc = PTR_ERR(de_src));
1393
1394         cleanup_phase = 1; /* source dentry */
1395
1396         de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
1397         if (IS_ERR(de_tgt_dir)) {
1398                 rc = PTR_ERR(de_tgt_dir);
1399                 de_tgt_dir = NULL;
1400                 GOTO(cleanup, rc);
1401         }
1402
1403         cleanup_phase = 2; /* target directory dentry */
1404
1405         CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
1406                de_tgt_dir->d_name.len, de_tgt_dir->d_name.name, rec->ur_name,
1407                de_src->d_inode->i_ino);
1408
1409         /* Step 2: Take the two locks */
1410         src_res_id.name[0] = de_src->d_inode->i_ino;
1411         src_res_id.name[1] = de_src->d_inode->i_generation;
1412         tgt_dir_res_id.name[0] = de_tgt_dir->d_inode->i_ino;
1413         tgt_dir_res_id.name[1] = de_tgt_dir->d_inode->i_generation;
1414
1415         rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
1416                                    &src_policy,
1417                                    &tgt_dir_res_id, &tgt_dir_lockh, LCK_EX,
1418                                    &tgt_dir_policy);
1419         if (rc)
1420                 GOTO(cleanup, rc);
1421
1422         cleanup_phase = 3; /* locks */
1423
1424         /* Step 3: Lookup the child */
1425         dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen-1);
1426         if (IS_ERR(dchild)) {
1427                 rc = PTR_ERR(dchild);
1428                 if (rc != -EPERM && rc != -EACCES)
1429                         CERROR("child lookup error %d\n", rc);
1430                 GOTO(cleanup, rc);
1431         }
1432
1433         cleanup_phase = 4; /* child dentry */
1434
1435         if (dchild->d_inode) {
1436                 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
1437                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
1438                 rc = -EEXIST;
1439                 GOTO(cleanup, rc);
1440         }
1441
1442         /* Step 4: Do it. */
1443         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1444
1445         handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
1446         if (IS_ERR(handle)) {
1447                 rc = PTR_ERR(handle);
1448                 GOTO(cleanup, rc);
1449         }
1450
1451         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
1452         if (rc && rc != -EPERM && rc != -EACCES)
1453                 CERROR("vfs_link error %d\n", rc);
1454 cleanup:
1455         rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
1456                                 handle, req, rc, 0);
1457         EXIT;
1458
1459         switch (cleanup_phase) {
1460         case 4: /* child dentry */
1461                 l_dput(dchild);
1462         case 3: /* locks */
1463                 if (rc) {
1464                         ldlm_lock_decref(&src_lockh, LCK_EX);
1465                         ldlm_lock_decref(&tgt_dir_lockh, LCK_EX);
1466                 } else {
1467                         ptlrpc_save_lock(req, &src_lockh, LCK_EX);
1468                         ptlrpc_save_lock(req, &tgt_dir_lockh, LCK_EX);
1469                 }
1470         case 2: /* target dentry */
1471                 l_dput(de_tgt_dir);
1472         case 1: /* source dentry */
1473                 l_dput(de_src);
1474         case 0:
1475                 break;
1476         default:
1477                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1478                 LBUG();
1479         }
1480         req->rq_status = rc;
1481         return 0;
1482 }
1483
1484 /*
1485  * add a hard link in the PENDING directory, only used by rename()
1486  */
1487 static int mds_add_link_orphan(struct mds_update_record *rec,
1488                                struct obd_device *obd,
1489                                struct dentry *dentry)
1490 {
1491         struct mds_obd *mds = &obd->u.mds;
1492         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1493         struct dentry *pending_child;
1494         char fidname[LL_FID_NAMELEN];
1495         int fidlen = 0, rc;
1496         ENTRY;
1497
1498         LASSERT(dentry->d_inode);
1499         LASSERT(!mds_inode_is_orphan(dentry->d_inode));
1500
1501         down(&pending_dir->i_sem);
1502         fidlen = ll_fid2str(fidname, dentry->d_inode->i_ino,
1503                             dentry->d_inode->i_generation);
1504
1505         CDEBUG(D_ERROR, "pending destroy of %dx open file %s = %s\n",
1506                mds_open_orphan_count(dentry->d_inode),
1507                rec->ur_name, fidname);
1508
1509         pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
1510         if (IS_ERR(pending_child))
1511                 GOTO(out_lock, rc = PTR_ERR(pending_child));
1512
1513         if (pending_child->d_inode != NULL) {
1514                 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1515                 LASSERT(pending_child->d_inode == dentry->d_inode);
1516                 GOTO(out_dput, rc = 0);
1517         }
1518
1519         lock_kernel();
1520         rc = vfs_link(dentry, pending_dir, pending_child);
1521         unlock_kernel();
1522         if (rc)
1523                 CERROR("error addlink orphan %s to PENDING: rc = %d\n",
1524                        rec->ur_name, rc);
1525         else
1526                 mds_inode_set_orphan(dentry->d_inode);
1527 out_dput:
1528         l_dput(pending_child);
1529 out_lock:
1530         up(&pending_dir->i_sem);
1531         RETURN(rc);
1532 }
1533
1534 /* The idea here is that we need to get four locks in the end:
1535  * one on each parent directory, one on each child.  We need to take
1536  * these locks in some kind of order (to avoid deadlocks), and the order
1537  * I selected is "increasing resource number" order.  We need to look up
1538  * the children, however, before we know what the resource number(s) are.
1539  * Thus the following plan:
1540  *
1541  * 1,2. Look up the parents
1542  * 3,4. Look up the children
1543  * 5. Take locks on the parents and children, in order
1544  * 6. Verify that the children haven't changed since they were looked up
1545  *
1546  * If there was a race and the children changed since they were first looked
1547  * up, it is possible that mds_verify_child() will be able to just grab the
1548  * lock on the new child resource (if it has a higher resource than any other)
1549  * but we need to compare against not only its parent, but also against the
1550  * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
1551  *
1552  * We need the fancy igrab() on the child inodes because we aren't holding a
1553  * lock on the parent after the lookup is done, so dentry->d_inode may change
1554  * at any time, and igrab() itself doesn't like getting passed a NULL argument.
1555  */
1556 static int mds_get_parents_children_locked(struct obd_device *obd,
1557                                            struct mds_obd *mds,
1558                                            struct ll_fid *p1_fid,
1559                                            struct dentry **de_srcdirp,
1560                                            struct ll_fid *p2_fid,
1561                                            struct dentry **de_tgtdirp,
1562                                            int parent_mode,
1563                                            const char *old_name, int old_len,
1564                                            struct dentry **de_oldp,
1565                                            const char *new_name, int new_len,
1566                                            struct dentry **de_newp,
1567                                            struct lustre_handle *dlm_handles,
1568                                            int child_mode)
1569 {
1570         struct ldlm_res_id p1_res_id = { .name = {0} };
1571         struct ldlm_res_id p2_res_id = { .name = {0} };
1572         struct ldlm_res_id c1_res_id = { .name = {0} };
1573         struct ldlm_res_id c2_res_id = { .name = {0} };
1574         ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
1575         /* Only dentry should change, but the inode itself would be
1576            intact otherwise */
1577         ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
1578         /* If something is going to be replaced, both dentry and inode locks are
1579            needed */
1580         ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP|
1581                                                         MDS_INODELOCK_UPDATE}};
1582         struct ldlm_res_id *maxres_src, *maxres_tgt;
1583         struct inode *inode;
1584         int rc = 0, cleanup_phase = 0;
1585         ENTRY;
1586
1587         /* Step 1: Lookup the source directory */
1588         *de_srcdirp = mds_fid2dentry(mds, p1_fid, NULL);
1589         if (IS_ERR(*de_srcdirp))
1590                 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
1591
1592         cleanup_phase = 1; /* source directory dentry */
1593
1594         p1_res_id.name[0] = (*de_srcdirp)->d_inode->i_ino;
1595         p1_res_id.name[1] = (*de_srcdirp)->d_inode->i_generation;
1596
1597         /* Step 2: Lookup the target directory */
1598         if (memcmp(p1_fid, p2_fid, sizeof(*p1_fid)) == 0) {
1599                 *de_tgtdirp = dget(*de_srcdirp);
1600         } else {
1601                 *de_tgtdirp = mds_fid2dentry(mds, p2_fid, NULL);
1602                 if (IS_ERR(*de_tgtdirp)) {
1603                         rc = PTR_ERR(*de_tgtdirp);
1604                         *de_tgtdirp = NULL;
1605                         GOTO(cleanup, rc);
1606                 }
1607         }
1608
1609         cleanup_phase = 2; /* target directory dentry */
1610
1611         p2_res_id.name[0] = (*de_tgtdirp)->d_inode->i_ino;
1612         p2_res_id.name[1] = (*de_tgtdirp)->d_inode->i_generation;
1613
1614         /* Step 3: Lookup the source child entry */
1615         *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp, old_len - 1);
1616         if (IS_ERR(*de_oldp)) {
1617                 rc = PTR_ERR(*de_oldp);
1618                 CERROR("old child lookup error (%*s): %d\n",
1619                        old_len - 1, old_name, rc);
1620                 GOTO(cleanup, rc);
1621         }
1622
1623         cleanup_phase = 3; /* original name dentry */
1624
1625         inode = (*de_oldp)->d_inode;
1626         if (inode != NULL)
1627                 inode = igrab(inode);
1628         if (inode == NULL)
1629                 GOTO(cleanup, rc = -ENOENT);
1630
1631         c1_res_id.name[0] = inode->i_ino;
1632         c1_res_id.name[1] = inode->i_generation;
1633         iput(inode);
1634
1635         /* Step 4: Lookup the target child entry */
1636         *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp, new_len - 1);
1637         if (IS_ERR(*de_newp)) {
1638                 rc = PTR_ERR(*de_newp);
1639                 CERROR("new child lookup error (%*s): %d\n",
1640                        old_len - 1, old_name, rc);
1641                 GOTO(cleanup, rc);
1642         }
1643
1644         cleanup_phase = 4; /* target dentry */
1645
1646         inode = (*de_newp)->d_inode;
1647         if (inode != NULL)
1648                 inode = igrab(inode);
1649         if (inode == NULL)
1650                 goto retry_locks;
1651
1652         c2_res_id.name[0] = inode->i_ino;
1653         c2_res_id.name[1] = inode->i_generation;
1654
1655         iput(inode);
1656
1657 retry_locks:
1658         /* Step 5: Take locks on the parents and child(ren) */
1659         maxres_src = &p1_res_id;
1660         maxres_tgt = &p2_res_id;
1661         cleanup_phase = 4; /* target dentry */
1662
1663         if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL,NULL))
1664                 maxres_src = &c1_res_id;
1665         if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL,NULL))
1666                 maxres_tgt = &c2_res_id;
1667
1668         rc = enqueue_4ordered_locks(obd, &p1_res_id,&dlm_handles[0],parent_mode,
1669                                     &p_policy,
1670                                     &p2_res_id, &dlm_handles[1], parent_mode,
1671                                     &p_policy,
1672                                     &c1_res_id, &dlm_handles[2], child_mode,
1673                                     &c1_policy,
1674                                     &c2_res_id, &dlm_handles[3], child_mode,
1675                                     &c2_policy);
1676         if (rc)
1677                 GOTO(cleanup, rc);
1678
1679         cleanup_phase = 6; /* parent and child(ren) locks */
1680
1681         /* Step 6a: Re-lookup source child to verify it hasn't changed */
1682         rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
1683                               parent_mode, &c1_res_id, &dlm_handles[2],
1684                               de_oldp, child_mode, &c1_policy, old_name,old_len,
1685                               maxres_tgt);
1686         if (rc) {
1687                 if (c2_res_id.name[0] != 0)
1688                         ldlm_lock_decref(&dlm_handles[3], child_mode);
1689                 ldlm_lock_decref(&dlm_handles[1], parent_mode);
1690                 cleanup_phase = 4;
1691                 if (rc > 0)
1692                         goto retry_locks;
1693                 GOTO(cleanup, rc);
1694         }
1695
1696         if ((*de_oldp)->d_inode == NULL)
1697                 GOTO(cleanup, rc = -ENOENT);
1698
1699         /* Step 6b: Re-lookup target child to verify it hasn't changed */
1700         rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
1701                               parent_mode, &c2_res_id, &dlm_handles[3],
1702                               de_newp, child_mode, &c2_policy, new_name,
1703                               new_len, maxres_src);
1704         if (rc) {
1705                 ldlm_lock_decref(&dlm_handles[2], child_mode);
1706                 ldlm_lock_decref(&dlm_handles[0], parent_mode);
1707                 cleanup_phase = 4;
1708                 if (rc > 0)
1709                         goto retry_locks;
1710                 GOTO(cleanup, rc);
1711         }
1712
1713         EXIT;
1714 cleanup:
1715         if (rc) {
1716                 switch (cleanup_phase) {
1717                 case 6: /* child lock(s) */
1718                         if (c2_res_id.name[0] != 0)
1719                                 ldlm_lock_decref(&dlm_handles[3], child_mode);
1720                         if (c1_res_id.name[0] != 0)
1721                                 ldlm_lock_decref(&dlm_handles[2], child_mode);
1722                 case 5: /* parent locks */
1723                         ldlm_lock_decref(&dlm_handles[1], parent_mode);
1724                         ldlm_lock_decref(&dlm_handles[0], parent_mode);
1725                 case 4: /* target dentry */
1726                         l_dput(*de_newp);
1727                 case 3: /* source dentry */
1728                         l_dput(*de_oldp);
1729                 case 2: /* target directory dentry */
1730                         l_dput(*de_tgtdirp);
1731                 case 1: /* source directry dentry */
1732                         l_dput(*de_srcdirp);
1733                 }
1734         }
1735
1736         return rc;
1737 }
1738
1739 static int mds_reint_rename(struct mds_update_record *rec, int offset,
1740                             struct ptlrpc_request *req,
1741                             struct lustre_handle *lockh)
1742 {
1743         struct obd_device *obd = req->rq_export->exp_obd;
1744         struct dentry *de_srcdir = NULL;
1745         struct dentry *de_tgtdir = NULL;
1746         struct dentry *de_old = NULL;
1747         struct dentry *de_new = NULL;
1748         struct mds_obd *mds = mds_req2mds(req);
1749         struct lustre_handle dlm_handles[4];
1750         struct mds_body *body = NULL;
1751         int rc = 0, lock_count = 3;
1752         int cleanup_phase = 0;
1753         void *handle = NULL;
1754         ENTRY;
1755
1756         LASSERT(offset == 0);
1757
1758         DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u %s to "LPU64"/%u %s",
1759                   rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name,
1760                   rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_tgt);
1761
1762         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1763
1764         rc = mds_get_parents_children_locked(obd, mds, rec->ur_fid1, &de_srcdir,
1765                                              rec->ur_fid2, &de_tgtdir, LCK_PW,
1766                                              rec->ur_name, rec->ur_namelen,
1767                                              &de_old, rec->ur_tgt,
1768                                              rec->ur_tgtlen, &de_new,
1769                                              dlm_handles, LCK_EX);
1770         if (rc)
1771                 GOTO(cleanup, rc);
1772
1773         cleanup_phase = 1; /* parent(s), children, locks */
1774
1775         if (de_new->d_inode)
1776                 lock_count = 4;
1777
1778         /* sanity check for src inode */
1779         if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
1780             de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
1781                 GOTO(cleanup, rc = -EINVAL);
1782
1783         /* sanity check for dest inode */
1784         if (de_new->d_inode &&
1785             (de_new->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
1786              de_new->d_inode->i_ino == de_tgtdir->d_inode->i_ino))
1787                 GOTO(cleanup, rc = -EINVAL);
1788
1789         if (de_old->d_inode == de_new->d_inode) {
1790                 GOTO(cleanup, rc = 0);
1791         }
1792
1793         /* if we are about to remove the target at first, pass the EA of
1794          * that inode to client to perform and cleanup on OST */
1795         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
1796         LASSERT(body != NULL);
1797
1798         if (de_new->d_inode &&
1799             S_ISREG(de_new->d_inode->i_mode) &&
1800             de_new->d_inode->i_nlink == 1 &&
1801             mds_open_orphan_count(de_new->d_inode) == 0) {
1802                 mds_pack_inode2fid(&body->fid1, de_new->d_inode);
1803                 mds_pack_inode2body(body, de_new->d_inode);
1804                 mds_pack_md(obd, req->rq_repmsg, 1, body, de_new->d_inode, 1);
1805                 if (!(body->valid & OBD_MD_FLEASIZE)) {
1806                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1807                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
1808                 } else {
1809                         /* XXX need log unlink? */
1810                 }
1811         }
1812
1813         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
1814                        de_srcdir->d_inode->i_sb);
1815
1816         handle = fsfilt_start(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME, NULL);
1817         if (IS_ERR(handle))
1818                 GOTO(cleanup, rc = PTR_ERR(handle));
1819
1820         /* FIXME need adjust the journal block count? */
1821         /* if the target should be moved to PENDING, we at first increase the
1822          * link and later vfs_rename() will decrease the link count again */
1823         if (de_new->d_inode &&
1824             S_ISREG(de_new->d_inode->i_mode) &&
1825             de_new->d_inode->i_nlink == 1 &&
1826             mds_open_orphan_count(de_new->d_inode) > 0) {
1827                 rc = mds_add_link_orphan(rec, obd, de_new);
1828                 if (rc)
1829                         GOTO(cleanup, rc);
1830         }
1831
1832         lock_kernel();
1833         de_old->d_fsdata = req;
1834         de_new->d_fsdata = req;
1835         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
1836         unlock_kernel();
1837
1838         GOTO(cleanup, rc);
1839 cleanup:
1840         rc = mds_finish_transno(mds, de_tgtdir ? de_tgtdir->d_inode : NULL,
1841                                 handle, req, rc, 0);
1842         switch (cleanup_phase) {
1843         case 1:
1844                 if (rc) {
1845                         if (lock_count == 4)
1846                                 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
1847                         ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
1848                         ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
1849                         ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
1850                 } else {
1851                         if (lock_count == 4)
1852                                 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
1853                         ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
1854                         ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
1855                         ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
1856                 }
1857                 l_dput(de_new);
1858                 l_dput(de_old);
1859                 l_dput(de_tgtdir);
1860                 l_dput(de_srcdir);
1861         case 0:
1862                 break;
1863         default:
1864                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1865                 LBUG();
1866         }
1867         req->rq_status = rc;
1868         return 0;
1869 }
1870
1871 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
1872                            struct ptlrpc_request *, struct lustre_handle *);
1873
1874 static mds_reinter reinters[REINT_MAX + 1] = {
1875         [REINT_SETATTR] mds_reint_setattr,
1876         [REINT_CREATE] mds_reint_create,
1877         [REINT_LINK] mds_reint_link,
1878         [REINT_UNLINK] mds_reint_unlink,
1879         [REINT_RENAME] mds_reint_rename,
1880         [REINT_OPEN] mds_open
1881 };
1882
1883 int mds_reint_rec(struct mds_update_record *rec, int offset,
1884                   struct ptlrpc_request *req, struct lustre_handle *lockh)
1885 {
1886         struct obd_device *obd = req->rq_export->exp_obd;
1887         struct lvfs_run_ctxt saved;
1888         int rc;
1889         ENTRY;
1890
1891         /* checked by unpacker */
1892         LASSERT(rec->ur_opcode <= REINT_MAX &&
1893                 reinters[rec->ur_opcode] != NULL);
1894
1895         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
1896         rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
1897         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
1898
1899         RETURN(rc);
1900 }