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