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