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