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