Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[fs/lustre-release.git] / lustre / mds / mds_reint.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/mds/mds_reint.c
5  *  Lustre Metadata Server (mds) reintegration routines
6  *
7  *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8  *   Author: Peter Braam <braam@clusterfs.com>
9  *   Author: Andreas Dilger <adilger@clusterfs.com>
10  *   Author: Phil Schwan <phil@clusterfs.com>
11  *
12  *   This file is part of Lustre, http://www.lustre.org.
13  *
14  *   Lustre is free software; you can redistribute it and/or
15  *   modify it under the terms of version 2 of the GNU General Public
16  *   License as published by the Free Software Foundation.
17  *
18  *   Lustre is distributed in the hope that it will be useful,
19  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *   GNU General Public License for more details.
22  *
23  *   You should have received a copy of the GNU General Public License
24  *   along with Lustre; if not, write to the Free Software
25  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #ifndef EXPORT_SYMTAB
29 # define EXPORT_SYMTAB
30 #endif
31 #define DEBUG_SUBSYSTEM S_MDS
32
33 #include <linux/fs.h>
34 #include <linux/obd_support.h>
35 #include <linux/obd_class.h>
36 #include <linux/obd.h>
37 #include <linux/lustre_lib.h>
38 #include <linux/lustre_idl.h>
39 #include <linux/lustre_mds.h>
40 #include <linux/lustre_dlm.h>
41 #include <linux/lustre_fsfilt.h>
42
43 #include "mds_internal.h"
44
45 void mds_commit_cb(struct obd_device *obd, __u64 transno, void *data,
46                    int error)
47 {
48         obd_transno_commit_cb(obd, transno, error);
49 }
50
51 struct mds_logcancel_data {
52         struct lov_mds_md      *mlcd_lmm;
53         int                     mlcd_size;
54         int                     mlcd_cookielen;
55         int                     mlcd_eadatalen;
56         struct llog_cookie      mlcd_cookies[0];
57 };
58
59
60 static void mds_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
61                                   void *cb_data, int error)
62 {
63         struct mds_logcancel_data *mlcd = cb_data;
64         struct lov_stripe_md *lsm = NULL;
65 #ifdef ENABLE_ORPHANS
66         struct llog_ctxt *ctxt;
67 #endif
68         int rc;
69
70         obd_transno_commit_cb(obd, transno, error);
71
72         CDEBUG(D_HA, "cancelling %d cookies\n",
73                (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
74
75         rc = obd_unpackmd(obd->u.mds.mds_osc_exp, &lsm, mlcd->mlcd_lmm,
76                           mlcd->mlcd_eadatalen);
77         if (rc < 0) {
78                 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
79                        (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
80                        rc);
81         } else {
82 #ifdef ENABLE_ORPHANS
83                 ///* XXX 0 normally, SENDNOW for debug */);
84                 ctxt = llog_get_context(obd, mlcd->mlcd_cookies[0].lgc_subsys + 1);
85                 rc = llog_cancel(ctxt, lsm,
86                                          mlcd->mlcd_cookielen /
87                                          sizeof(*mlcd->mlcd_cookies),
88                                          mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW);
89                 if (rc)
90                         CERROR("error cancelling %d log cookies: rc %d\n",
91                                (int)(mlcd->mlcd_cookielen /
92                                      sizeof(*mlcd->mlcd_cookies)), rc);
93 #endif
94         }
95
96         OBD_FREE(mlcd, mlcd->mlcd_size);
97 }
98
99 /* Assumes caller has already pushed us into the kernel context. */
100 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
101                        struct ptlrpc_request *req, int rc, __u32 op_data)
102 {
103         struct mds_export_data *med = &req->rq_export->exp_mds_data;
104         struct mds_client_data *mcd = med->med_mcd;
105         struct obd_device *obd = req->rq_export->exp_obd;
106         int err;
107         __u64 transno;
108         loff_t off;
109         int log_pri = D_HA;
110         ENTRY;
111
112         /* if the export has already been failed, we have no last_rcvd slot */
113         if (req->rq_export->exp_failed) {
114                 CERROR("committing transaction for disconnected client\n");
115                 if (handle)
116                         GOTO(commit, rc);
117                 RETURN(rc);
118         }
119
120         if (IS_ERR(handle))
121                 RETURN(rc);
122
123         if (handle == NULL) {
124                 /* if we're starting our own xaction, use our own inode */
125                 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
126                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
127                 if (IS_ERR(handle)) {
128                         CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
129                         RETURN(PTR_ERR(handle));
130                 }
131         }
132
133         off = med->med_off;
134
135         transno = req->rq_reqmsg->transno;
136         if (transno == 0) {
137                 spin_lock(&mds->mds_transno_lock);
138                 transno = ++mds->mds_last_transno;
139                 spin_unlock(&mds->mds_transno_lock);
140         } else {
141                 spin_lock(&mds->mds_transno_lock);
142                 if (transno > mds->mds_last_transno)
143                         mds->mds_last_transno = transno;
144                 spin_unlock(&mds->mds_transno_lock);
145         }
146         req->rq_repmsg->transno = req->rq_transno = transno;
147         mcd->mcd_last_transno = cpu_to_le64(transno);
148         mcd->mcd_mount_count = cpu_to_le64(mds->mds_mount_count);
149         mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
150         mcd->mcd_last_result = cpu_to_le32(rc);
151         mcd->mcd_last_data = cpu_to_le32(op_data);
152
153         fsfilt_add_journal_cb(req->rq_export->exp_obd, transno, handle,
154                               mds_commit_cb, NULL);
155         err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd, sizeof(*mcd),
156                                   &off, 0);
157
158         if (err) {
159                 log_pri = D_ERROR;
160                 if (rc == 0)
161                         rc = err;
162         }
163
164         DEBUG_REQ(log_pri, req,
165                   "wrote trans #"LPU64" client %s at idx %u: err = %d\n",
166                   transno, mcd->mcd_uuid, med->med_idx, err);
167
168         err = mds_lov_write_objids(obd);
169         if (err) {
170                 log_pri = D_ERROR;
171                 if (rc == 0)
172                         rc = err;
173         }
174         CDEBUG(log_pri, "wrote objids: err = %d\n", err);
175
176 commit:
177         err = fsfilt_commit(obd, inode, handle, 0);
178         if (err) {
179                 CERROR("error committing transaction: %d\n", err);
180                 if (!rc)
181                         rc = err;
182         }
183
184         RETURN(rc);
185 }
186
187 /* this gives the same functionality as the code between
188  * sys_chmod and inode_setattr
189  * chown_common and inode_setattr
190  * utimes and inode_setattr
191  */
192 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
193 {
194         time_t now = LTIME_S(CURRENT_TIME);
195         struct iattr *attr = &rec->ur_iattr;
196         unsigned int ia_valid = attr->ia_valid;
197         int error;
198         ENTRY;
199
200         /* only fix up attrs if the client VFS didn't already */
201         if (!(ia_valid & ATTR_RAW))
202                 RETURN(0);
203
204         if (!(ia_valid & ATTR_CTIME_SET))
205                 LTIME_S(attr->ia_ctime) = now;
206         if (!(ia_valid & ATTR_ATIME_SET))
207                 LTIME_S(attr->ia_atime) = now;
208         if (!(ia_valid & ATTR_MTIME_SET))
209                 LTIME_S(attr->ia_mtime) = now;
210
211         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
212                 RETURN(-EPERM);
213
214         /* times */
215         if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
216                 if (rec->ur_fsuid != inode->i_uid &&
217                     (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
218                         RETURN(error);
219         }
220
221         if (ia_valid & ATTR_SIZE) {
222                 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
223                         RETURN(error);
224         }
225
226         if (ia_valid & ATTR_UID) {
227                 /* chown */
228                 error = -EPERM;
229                 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
230                         RETURN(-EPERM);
231                 if (attr->ia_uid == (uid_t) -1)
232                         attr->ia_uid = inode->i_uid;
233                 if (attr->ia_gid == (gid_t) -1)
234                         attr->ia_gid = inode->i_gid;
235                 attr->ia_mode = inode->i_mode;
236                 /*
237                  * If the user or group of a non-directory has been
238                  * changed by a non-root user, remove the setuid bit.
239                  * 19981026 David C Niemi <niemi@tux.org>
240                  *
241                  * Changed this to apply to all users, including root,
242                  * to avoid some races. This is the behavior we had in
243                  * 2.0. The check for non-root was definitely wrong
244                  * for 2.2 anyway, as it should have been using
245                  * CAP_FSETID rather than fsuid -- 19990830 SD.
246                  */
247                 if ((inode->i_mode & S_ISUID) == S_ISUID &&
248                     !S_ISDIR(inode->i_mode)) {
249                         attr->ia_mode &= ~S_ISUID;
250                         attr->ia_valid |= ATTR_MODE;
251                 }
252                 /*
253                  * Likewise, if the user or group of a non-directory
254                  * has been changed by a non-root user, remove the
255                  * setgid bit UNLESS there is no group execute bit
256                  * (this would be a file marked for mandatory
257                  * locking).  19981026 David C Niemi <niemi@tux.org>
258                  *
259                  * Removed the fsuid check (see the comment above) --
260                  * 19990830 SD.
261                  */
262                 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
263                      (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
264                         attr->ia_mode &= ~S_ISGID;
265                         attr->ia_valid |= ATTR_MODE;
266                 }
267         } else if (ia_valid & ATTR_MODE) {
268                 int mode = attr->ia_mode;
269                 /* chmod */
270                 if (attr->ia_mode == (mode_t) -1)
271                         attr->ia_mode = inode->i_mode;
272                 attr->ia_mode =
273                         (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
274         }
275         RETURN(0);
276 }
277
278 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
279 {
280         DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
281                   mcd->mcd_last_transno, mcd->mcd_last_result);
282         req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
283         req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
284
285         if (req->rq_export->exp_outstanding_reply)
286                 mds_steal_ack_locks(req->rq_export, req);
287 }
288
289 static void reconstruct_reint_setattr(struct mds_update_record *rec,
290                                       int offset, struct ptlrpc_request *req)
291 {
292         struct mds_export_data *med = &req->rq_export->exp_mds_data;
293         struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
294         struct dentry *de;
295         struct mds_body *body;
296
297         mds_req_from_mcd(req, med->med_mcd);
298
299         if (req->rq_export->exp_outstanding_reply)
300                 mds_steal_ack_locks(req->rq_export, req);
301
302         de = mds_fid2dentry(obd, rec->ur_fid1, NULL);
303         if (IS_ERR(de)) {
304                 LASSERT(PTR_ERR(de) == req->rq_status);
305                 return;
306         }
307
308         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
309         mds_pack_inode2fid(&body->fid1, de->d_inode);
310         mds_pack_inode2body(body, de->d_inode);
311
312         /* Don't return OST-specific attributes if we didn't just set them */
313         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
314                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
315         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
316                 body->valid |= OBD_MD_FLMTIME;
317         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
318                 body->valid |= OBD_MD_FLATIME;
319
320         l_dput(de);
321 }
322
323 /* In the raw-setattr case, we lock the child inode.
324  * In the write-back case or if being called from open, the client holds a lock
325  * already.
326  *
327  * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
328 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
329                              struct ptlrpc_request *req,
330                              struct lustre_handle *lh)
331 {
332         struct mds_obd *mds = mds_req2mds(req);
333         struct obd_device *obd = req->rq_export->exp_obd;
334         struct mds_body *body;
335         struct dentry *de;
336         struct inode *inode = NULL;
337         struct lustre_handle lockh;
338         void *handle = NULL;
339         struct mds_logcancel_data *mlcd = NULL;
340         int rc = 0, cleanup_phase = 0, err, locked = 0;
341         ENTRY;
342
343         LASSERT(offset == 0);
344
345         MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
346
347         if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
348                 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
349                 if (IS_ERR(de))
350                         GOTO(cleanup, rc = PTR_ERR(de));
351         } else {
352                 de = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW,
353                                            &lockh, NULL, 0);
354                 if (IS_ERR(de))
355                         GOTO(cleanup, rc = PTR_ERR(de));
356                 locked = 1;
357         }
358
359         cleanup_phase = 1;
360         inode = de->d_inode;
361         LASSERT(inode);
362         if (S_ISREG(inode->i_mode) && rec->ur_eadata != NULL)
363                 down(&inode->i_sem);
364
365         CDEBUG(D_INODE, "ino %lu\n", inode->i_ino);
366         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
367
368         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
369         if (IS_ERR(handle))
370                 GOTO(cleanup, rc = PTR_ERR(handle));
371
372         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
373                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
374                        LTIME_S(rec->ur_iattr.ia_mtime),
375                        LTIME_S(rec->ur_iattr.ia_ctime));
376         rc = mds_fix_attr(inode, rec);
377         if (rc)
378                 GOTO(cleanup, rc);
379
380         if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG)    /* ioctl */
381                 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
382                                       (long)&rec->ur_iattr.ia_attr_flags);
383         else                                            /* setattr */
384                 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
385
386         if (rc == 0 && S_ISREG(inode->i_mode) && rec->ur_eadata != NULL) {
387                 rc = fsfilt_set_md(obd, inode, handle,
388                                    rec->ur_eadata, rec->ur_eadatalen);
389         }
390
391         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
392         mds_pack_inode2fid(&body->fid1, inode);
393         mds_pack_inode2body(body, inode);
394
395         /* Don't return OST-specific attributes if we didn't just set them */
396         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
397                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
398         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
399                 body->valid |= OBD_MD_FLMTIME;
400         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
401                 body->valid |= OBD_MD_FLATIME;
402
403         if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_osc_obd)) {
404                 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
405                           rec->ur_eadatalen);
406                 if (mlcd) {
407                         mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
408                                 rec->ur_eadatalen;
409                         mlcd->mlcd_eadatalen = rec->ur_eadatalen;
410                         mlcd->mlcd_cookielen = rec->ur_cookielen;
411                         mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
412                                 mlcd->mlcd_cookielen;
413                         memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
414                                mlcd->mlcd_cookielen);
415                         memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
416                                mlcd->mlcd_eadatalen);
417                 } else {
418                         CERROR("unable to allocate log cancel data\n");
419                 }
420         }
421         EXIT;
422  cleanup:
423         if (mlcd != NULL)
424                 fsfilt_add_journal_cb(req->rq_export->exp_obd, 0, handle,
425                                       mds_cancel_cookies_cb, mlcd);
426         err = mds_finish_transno(mds, inode, handle, req, rc, 0);
427         switch (cleanup_phase) {
428         case 1:
429                 if (S_ISREG(inode->i_mode) && rec->ur_eadata != NULL)
430                         down(&inode->i_sem);
431                 l_dput(de);
432                 if (locked) {
433                         if (rc) {
434                                 ldlm_lock_decref(&lockh, LCK_PW);
435                         } else {
436                                 ldlm_put_lock_into_req(req, &lockh, LCK_PW);
437                         }
438                 }
439         case 0:
440                 break;
441         default:
442                 LBUG();
443         }
444         if (err && !rc)
445                 rc = err;
446
447         req->rq_status = rc;
448         return 0;
449 }
450
451 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
452                                      struct ptlrpc_request *req)
453 {
454         struct mds_export_data *med = &req->rq_export->exp_mds_data;
455         struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
456         struct dentry *parent, *child;
457         struct mds_body *body;
458
459         mds_req_from_mcd(req, med->med_mcd);
460
461         if (req->rq_status)
462                 return;
463
464         parent = mds_fid2dentry(obd, rec->ur_fid1, NULL);
465         LASSERT(!IS_ERR(parent));
466         child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
467         LASSERT(!IS_ERR(child));
468         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
469         mds_pack_inode2fid(&body->fid1, child->d_inode);
470         mds_pack_inode2body(body, child->d_inode);
471         l_dput(parent);
472         l_dput(child);
473 }
474
475 static int mds_reint_create(struct mds_update_record *rec, int offset,
476                             struct ptlrpc_request *req,
477                             struct lustre_handle *lh)
478 {
479         struct dentry *dparent = NULL;
480         struct mds_obd *mds = mds_req2mds(req);
481         struct obd_device *obd = req->rq_export->exp_obd;
482         struct dentry *dchild = NULL;
483         struct inode *dir = NULL;
484         void *handle = NULL;
485         struct lustre_handle lockh;
486         int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
487         int created = 0;
488         struct dentry_params dp;
489         ENTRY;
490
491         LASSERT(offset == 0);
492         LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
493
494         MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
495
496         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
497                 GOTO(cleanup, rc = -ESTALE);
498
499         dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW, &lockh,
500                                         rec->ur_name, rec->ur_namelen - 1);
501         if (IS_ERR(dparent)) {
502                 rc = PTR_ERR(dparent);
503                 CERROR("parent lookup error %d\n", rc);
504                 GOTO(cleanup, rc);
505         }
506         cleanup_phase = 1; /* locked parent dentry */
507         dir = dparent->d_inode;
508         LASSERT(dir);
509         CDEBUG(D_INODE, "parent ino %lu creating name %s mode %o\n",
510                dir->i_ino, rec->ur_name, rec->ur_mode);
511
512         ldlm_lock_dump_handle(D_OTHER, &lockh);
513
514         dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
515         if (IS_ERR(dchild)) {
516                 rc = PTR_ERR(dchild);
517                 CERROR("child lookup error %d\n", rc);
518                 GOTO(cleanup, rc);
519         }
520
521         cleanup_phase = 2; /* child dentry */
522
523         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
524
525         if (dir->i_mode & S_ISGID) {
526                 if (S_ISDIR(rec->ur_mode))
527                         rec->ur_mode |= S_ISGID;
528         }
529
530         dchild->d_fsdata = (void *)&dp;
531         dp.p_inum = (unsigned long)rec->ur_fid2->id;
532         dp.p_ptr = req;
533
534         switch (type) {
535         case S_IFREG:{
536                 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
537                 if (IS_ERR(handle))
538                         GOTO(cleanup, rc = PTR_ERR(handle));
539                 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
540                 EXIT;
541                 break;
542         }
543         case S_IFDIR:{
544                 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
545                 if (IS_ERR(handle))
546                         GOTO(cleanup, rc = PTR_ERR(handle));
547                 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
548                 EXIT;
549                 break;
550         }
551         case S_IFLNK:{
552                 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
553                 if (IS_ERR(handle))
554                         GOTO(cleanup, rc = PTR_ERR(handle));
555                 if (rec->ur_tgt == NULL)        /* no target supplied */
556                         rc = -EINVAL;           /* -EPROTO? */
557                 else
558                         rc = vfs_symlink(dir, dchild, rec->ur_tgt);
559                 EXIT;
560                 break;
561         }
562         case S_IFCHR:
563         case S_IFBLK:
564         case S_IFIFO:
565         case S_IFSOCK:{
566                 int rdev = rec->ur_rdev;
567                 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
568                 if (IS_ERR(handle))
569                         GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
570                 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
571                 EXIT;
572                 break;
573         }
574         default:
575                 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
576                 dchild->d_fsdata = NULL;
577                 GOTO(cleanup, rc = -EINVAL);
578         }
579
580         /* In case we stored the desired inum in here, we want to clean up. */
581         if (dchild->d_fsdata == (void *)(unsigned long)rec->ur_fid2->id)
582                 dchild->d_fsdata = NULL;
583
584         if (rc) {
585                 CDEBUG(D_INODE, "error during create: %d\n", rc);
586                 GOTO(cleanup, rc);
587         } else {
588                 struct iattr iattr;
589                 struct inode *inode = dchild->d_inode;
590                 struct mds_body *body;
591
592                 created = 1;
593                 LTIME_S(iattr.ia_atime) = rec->ur_time;
594                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
595                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
596                 iattr.ia_uid = rec->ur_fsuid;
597                 if (dir->i_mode & S_ISGID)
598                         iattr.ia_gid = dir->i_gid;
599                 else
600                         iattr.ia_gid = rec->ur_fsgid;
601                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
602                         ATTR_MTIME | ATTR_CTIME;
603
604                 if (rec->ur_fid2->id) {
605                         LASSERT(rec->ur_fid2->id == inode->i_ino);
606                         inode->i_generation = rec->ur_fid2->generation;
607                         /* Dirtied and committed by the upcoming setattr. */
608                         CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
609                                inode->i_ino, inode->i_generation);
610                 } else {
611                         struct lustre_handle child_ino_lockh;
612                         struct ldlm_res_id child_res_id =
613                              { .name = { inode->i_ino, 0 } };
614                         int lock_flags = 0;
615
616                         CDEBUG(D_INODE, "created ino %lu with gen %x\n",
617                                inode->i_ino, inode->i_generation);
618
619                         /* The inode we were allocated may have just been freed
620                          * by an unlink operation.  We take this lock to
621                          * synchronize against the matching reply-ack-lock taken
622                          * in unlink, to avoid replay problems if this reply
623                          * makes it out to the client but the unlink's does not.
624                          * See bug 2029 for more detail.*/
625                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
626                                               NULL, child_res_id, LDLM_PLAIN,
627                                               NULL, 0, LCK_EX, &lock_flags,
628                                               ldlm_completion_ast,
629                                               mds_blocking_ast, NULL,
630                                               &child_ino_lockh);
631                         if (rc != ELDLM_OK) {
632                                 CERROR("error locking for unlink/create sync: "
633                                        "%d\n", rc);
634                         } else {
635                                 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
636                         }
637                 }
638
639                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
640                 if (rc)
641                         CERROR("error on child setattr: rc = %d\n", rc);
642
643                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
644                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
645                 if (rc)
646                         CERROR("error on parent setattr: rc = %d\n", rc);
647
648                 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
649                 mds_pack_inode2fid(&body->fid1, inode);
650                 mds_pack_inode2body(body, inode);
651         }
652         EXIT;
653
654 cleanup:
655         err = mds_finish_transno(mds, dir, handle, req, rc, 0);
656
657         if (rc && created) {
658                 /* Destroy the file we just created.  This should not need
659                  * extra journal credits, as we have already modified all of
660                  * the blocks needed in order to create the file in the first
661                  * place.
662                  */
663                 switch (type) {
664                 case S_IFDIR:
665                         err = vfs_rmdir(dir, dchild);
666                         if (err)
667                                 CERROR("rmdir in error path: %d\n", err);
668                         break;
669                 default:
670                         err = vfs_unlink(dir, dchild);
671                         if (err)
672                                 CERROR("unlink in error path: %d\n", err);
673                         break;
674                 }
675         } else {
676                 rc = err;
677         }
678         switch (cleanup_phase) {
679         case 2: /* child dentry */
680                 l_dput(dchild);
681         case 1: /* locked parent dentry */
682                 if (rc) {
683                         ldlm_lock_decref(&lockh, LCK_PW);
684                 } else {
685                         ldlm_put_lock_into_req(req, &lockh, LCK_PW);
686                 }
687                 l_dput(dparent);
688         case 0:
689                 break;
690         default:
691                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
692                 LBUG();
693         }
694         req->rq_status = rc;
695         return 0;
696 }
697
698 /* This function doesn't use ldlm_match_or_enqueue because we're always called
699  * with EX or PW locks, and the MDS is no longer allowed to match write locks,
700  * because they take the place of local semaphores.
701  *
702  * Two locks are taken in numerical order */
703 int enqueue_ordered_locks(int lock_mode, struct obd_device *obd,
704                           struct ldlm_res_id *p1_res_id,
705                           struct ldlm_res_id *p2_res_id,
706                           struct lustre_handle *p1_lockh,
707                           struct lustre_handle *p2_lockh)
708 {
709         struct ldlm_res_id res_id[2];
710         struct lustre_handle *handles[2] = {p1_lockh, p2_lockh};
711         int rc, flags;
712         ENTRY;
713
714         LASSERT(p1_res_id != NULL && p2_res_id != NULL);
715
716         CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
717                p1_res_id[0].name[0], p2_res_id[0].name[0]);
718
719         if (p1_res_id->name[0] < p2_res_id->name[0]) {
720                 handles[0] = p1_lockh;
721                 handles[1] = p2_lockh;
722                 res_id[0] = *p1_res_id;
723                 res_id[1] = *p2_res_id;
724         } else {
725                 handles[1] = p1_lockh;
726                 handles[0] = p2_lockh;
727                 res_id[1] = *p1_res_id;
728                 res_id[0] = *p2_res_id;
729         }
730
731         CDEBUG(D_INFO, "lock order: "LPU64"/"LPU64"\n",
732                p1_res_id[0].name[0], p2_res_id[0].name[0]);
733
734         flags = LDLM_FL_LOCAL_ONLY;
735         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL, res_id[0],
736                               LDLM_PLAIN, NULL, 0, lock_mode, &flags,
737                               ldlm_completion_ast, mds_blocking_ast, NULL,
738                               handles[0]);
739         if (rc != ELDLM_OK)
740                 RETURN(-EIO);
741         ldlm_lock_dump_handle(D_OTHER, handles[0]);
742
743         if (memcmp(&res_id[0], &res_id[1], sizeof(res_id[0])) == 0) {
744                 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
745                 ldlm_lock_addref(handles[1], lock_mode);
746         } else {
747                 flags = LDLM_FL_LOCAL_ONLY;
748                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
749                                       res_id[1], LDLM_PLAIN, NULL, 0, lock_mode,
750                                       &flags, ldlm_completion_ast,
751                                       mds_blocking_ast, NULL, handles[1]);
752                 if (rc != ELDLM_OK) {
753                         ldlm_lock_decref(handles[0], lock_mode);
754                         RETURN(-EIO);
755                 }
756         }
757         ldlm_lock_dump_handle(D_OTHER, handles[1]);
758
759         RETURN(0);
760 }
761
762 void mds_reconstruct_generic(struct ptlrpc_request *req)
763 {
764         struct mds_export_data *med = &req->rq_export->exp_mds_data;
765
766         mds_req_from_mcd(req, med->med_mcd);
767 }
768
769 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
770                             struct ptlrpc_request *req,
771                             struct lustre_handle *lh)
772 {
773         struct dentry *dparent = NULL;
774         struct dentry *dchild = NULL;
775         struct mds_obd *mds = mds_req2mds(req);
776         struct obd_device *obd = req->rq_export->exp_obd;
777         struct mds_body *body = NULL;
778         struct inode *child_inode;
779         struct lustre_handle parent_lockh, child_lockh, child_reuse_lockh;
780         void *handle = NULL;
781         struct ldlm_res_id child_res_id = { .name = {0} };
782         int rc = 0, flags = 0, log_unlink = 0, cleanup_phase = 0;
783         ENTRY;
784
785         LASSERT(offset == 0 || offset == 2);
786
787         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
788
789         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
790                 GOTO(cleanup, rc = -ENOENT);
791
792         /* Step 1: Lookup the parent by FID */
793         dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW,
794                                         &parent_lockh, rec->ur_name,
795                                         rec->ur_namelen - 1);
796         if (IS_ERR(dparent))
797                 GOTO(cleanup, rc = PTR_ERR(dparent));
798         LASSERT(dparent->d_inode);
799
800         cleanup_phase = 1; /* Have parent dentry lock */
801
802         /* Step 2: Lookup the child */
803         dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
804         if (IS_ERR(dchild))
805                 GOTO(cleanup, rc = PTR_ERR(dchild));
806
807         cleanup_phase = 2; /* child dentry */
808
809         child_inode = dchild->d_inode;
810         if (child_inode == NULL) {
811                 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
812                        dparent->d_inode->i_ino, rec->ur_name);
813                 GOTO(cleanup, rc = -ENOENT);
814         }
815
816         DEBUG_REQ(D_INODE, req, "parent ino %lu, child ino %lu",
817                   dparent->d_inode->i_ino, child_inode->i_ino);
818
819         /* Step 3: Get a lock on the child */
820         child_res_id.name[0] = child_inode->i_ino;
821         child_res_id.name[1] = child_inode->i_generation;
822
823         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
824                               child_res_id, LDLM_PLAIN, NULL, 0, LCK_EX,
825                               &flags, ldlm_completion_ast, mds_blocking_ast,
826                               NULL, &child_lockh);
827         if (rc != ELDLM_OK)
828                 GOTO(cleanup, rc);
829
830         cleanup_phase = 3; /* child lock */
831
832         /* Step 4: Get a lock on the ino to sync with creation WRT inode
833          * reuse (see bug 2029). */
834         child_res_id.name[1] = 0;
835         
836         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
837                               child_res_id, LDLM_PLAIN, NULL, 0, LCK_EX,
838                               &flags, ldlm_completion_ast, mds_blocking_ast,
839                               NULL, &child_reuse_lockh);
840         if (rc != ELDLM_OK)
841                 GOTO(cleanup, rc);
842
843         cleanup_phase = 4; /* child lock */
844         
845         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
846
847         /* ldlm_reply in buf[0] if called via intent */
848         if (offset)
849                 offset = 1;
850
851         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
852         LASSERT(body != NULL);
853
854         /* If this is the last reference to this inode, get the OBD EA
855          * data first so the client can destroy OST objects. 
856          * we only do the object removal if no open files remain.
857          * Nobody can get at this name anymore because of the locks so 
858          * we make decisions here as to whether to remove the inode */
859         if (S_ISREG(child_inode->i_mode) && child_inode->i_nlink == 1 && 
860             mds_open_orphan_count(child_inode) == 0) {
861                 mds_pack_inode2fid(&body->fid1, child_inode);
862                 mds_pack_inode2body(body, child_inode);
863                 mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
864                             child_inode, 1);
865                 if (!(body->valid & OBD_MD_FLEASIZE)) {
866                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
867                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
868                 } else {
869                         log_unlink = 1;
870                 }
871         }
872
873         /* We have to do these checks ourselves, in case we are making an
874          * orphan.  The client tells us whether rmdir() or unlink() was called,
875          * so we need to return appropriate errors (bug 72).
876          *
877          * We don't have to check permissions, because vfs_rename (called from
878          * mds_open_unlink_rename) also calls may_delete. */
879         if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
880                 if (!S_ISDIR(child_inode->i_mode))
881                         GOTO(cleanup, rc = -ENOTDIR);
882         } else {
883                 if (S_ISDIR(child_inode->i_mode))
884                         GOTO(cleanup, rc = -EISDIR);
885         }
886
887         if (child_inode->i_nlink == (S_ISDIR(child_inode->i_mode) ? 2 : 1) &&
888             mds_open_orphan_count(child_inode) > 0) {
889                 rc = mds_open_unlink_rename(rec, obd, dparent, dchild, &handle);
890                 cleanup_phase = 5; /* transaction */
891                 GOTO(cleanup, rc);
892         }
893
894         /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
895         switch (child_inode->i_mode & S_IFMT) {
896         case S_IFDIR:
897                 /* Drop any lingering child directories before we start our
898                  * transaction, to avoid doing multiple inode dirty/delete
899                  * in our compound transaction (bug 1321). */
900                 shrink_dcache_parent(dchild);
901                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
902                                       NULL);
903                 if (IS_ERR(handle))
904                         GOTO(cleanup, rc = PTR_ERR(handle));
905                 cleanup_phase = 5; /* transaction */
906                 rc = vfs_rmdir(dparent->d_inode, dchild);
907                 break;
908         case S_IFREG: {
909                 handle = fsfilt_start(obd, dparent->d_inode,
910                                       FSFILT_OP_UNLINK_LOG, NULL);
911                 if (IS_ERR(handle))
912                         GOTO(cleanup, rc = PTR_ERR(handle));
913
914                 cleanup_phase = 5; /* transaction */
915                 rc = vfs_unlink(dparent->d_inode, dchild);
916
917 #ifdef ENABLE_ORPHANS
918
919                 if (!rc && log_unlink)
920                         if (mds_log_op_unlink(obd, child_inode, req->rq_repmsg,
921                                               offset + 1) > 0)
922                                 body->valid |= OBD_MD_FLCOOKIE;
923 #endif
924                 break;
925         }
926         case S_IFLNK:
927         case S_IFCHR:
928         case S_IFBLK:
929         case S_IFIFO:
930         case S_IFSOCK:
931                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
932                                       NULL);
933                 if (IS_ERR(handle))
934                         GOTO(cleanup, rc = PTR_ERR(handle));
935                 cleanup_phase = 5;
936                 rc = vfs_unlink(dparent->d_inode, dchild);
937                 break;
938         default:
939                 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
940                        rec->ur_name);
941                 LBUG();
942                 GOTO(cleanup, rc = -EINVAL);
943         }
944
945  cleanup:
946         if (rc == 0) {
947                 struct iattr iattr;
948                 int err;
949
950                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
951                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
952                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
953
954                 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
955                 if (err)
956                         CERROR("error on parent setattr: rc = %d\n", err);
957         }
958
959         switch(cleanup_phase) {
960         case 5:
961                 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
962                                         rc, 0);
963                 if (!rc)
964                         (void)obd_set_info(mds->mds_osc_exp, strlen("unlinked"),
965                                            "unlinked", 0, NULL);
966         case 4: /* child ino-reuse lock */
967                 if (rc && body != NULL) {
968                         // Don't unlink the OST objects if the MDS unlink failed
969                         body->valid = 0;
970                 }
971                 if (rc)
972                         ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
973                 else
974                         ldlm_put_lock_into_req(req, &child_reuse_lockh, LCK_EX);
975         case 3: /* child lock */
976                 ldlm_lock_decref(&child_lockh, LCK_EX);
977         case 2: /* child dentry */
978                 l_dput(dchild);
979         case 1: /* parent dentry and lock */
980                 if (rc) {
981                         ldlm_lock_decref(&parent_lockh, LCK_PW);
982                 } else {
983                         ldlm_put_lock_into_req(req, &parent_lockh, LCK_PW);
984                 }
985                 l_dput(dparent);
986         case 0:
987                 break;
988         default:
989                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
990                 LBUG();
991         }
992         req->rq_status = rc;
993         return 0;
994 }
995
996 static int mds_reint_link(struct mds_update_record *rec, int offset,
997                           struct ptlrpc_request *req,
998                           struct lustre_handle *lh)
999 {
1000         struct obd_device *obd = req->rq_export->exp_obd;
1001         struct dentry *de_src = NULL;
1002         struct dentry *de_tgt_dir = NULL;
1003         struct dentry *dchild = NULL;
1004         struct mds_obd *mds = mds_req2mds(req);
1005         struct lustre_handle *handle = NULL, tgt_dir_lockh, src_lockh;
1006         struct ldlm_res_id src_res_id = { .name = {0} };
1007         struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
1008         int lock_mode = 0, rc = 0, cleanup_phase = 0;
1009         ENTRY;
1010
1011         LASSERT(offset == 0);
1012
1013         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1014
1015         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1016                 GOTO(cleanup, rc = -ENOENT);
1017
1018         /* Step 1: Lookup the source inode and target directory by FID */
1019         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1020         if (IS_ERR(de_src))
1021                 GOTO(cleanup, rc = PTR_ERR(de_src));
1022
1023         cleanup_phase = 1; /* source dentry */
1024
1025         de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
1026         if (IS_ERR(de_tgt_dir))
1027                 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
1028
1029         cleanup_phase = 2; /* target directory dentry */
1030
1031         CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
1032                de_tgt_dir->d_name.len, de_tgt_dir->d_name.name, rec->ur_name,
1033                de_src->d_inode->i_ino);
1034
1035         /* Step 2: Take the two locks */
1036         lock_mode = LCK_EX;
1037         src_res_id.name[0] = de_src->d_inode->i_ino;
1038         src_res_id.name[1] = de_src->d_inode->i_generation;
1039         tgt_dir_res_id.name[0] = de_tgt_dir->d_inode->i_ino;
1040         tgt_dir_res_id.name[1] = de_tgt_dir->d_inode->i_generation;
1041
1042         rc = enqueue_ordered_locks(LCK_EX, obd, &src_res_id, &tgt_dir_res_id,
1043                                    &src_lockh, &tgt_dir_lockh);
1044         if (rc != ELDLM_OK)
1045                 GOTO(cleanup, rc = -EIO);
1046
1047         cleanup_phase = 3; /* locks */
1048
1049         /* Step 3: Lookup the child */
1050         dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen-1);
1051         if (IS_ERR(dchild)) {
1052                 rc = PTR_ERR(dchild);
1053                 if (rc != -EPERM && rc != -EACCES)
1054                         CERROR("child lookup error %d\n", rc);
1055                 GOTO(cleanup, rc);
1056         }
1057
1058         cleanup_phase = 4; /* child dentry */
1059
1060         if (dchild->d_inode) {
1061                 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
1062                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
1063                 rc = -EEXIST;
1064                 GOTO(cleanup, rc);
1065         }
1066
1067         /* Step 4: Do it. */
1068         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1069
1070         handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
1071         if (IS_ERR(handle)) {
1072                 rc = PTR_ERR(handle);
1073                 GOTO(cleanup, rc);
1074         }
1075
1076         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
1077         if (rc && rc != -EPERM && rc != -EACCES)
1078                 CERROR("vfs_link error %d\n", rc);
1079 cleanup:
1080         rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
1081                                 handle, req, rc, 0);
1082         EXIT;
1083
1084         switch (cleanup_phase) {
1085         case 4: /* child dentry */
1086                 l_dput(dchild);
1087         case 3: /* locks */
1088                 if (rc) {
1089                         ldlm_lock_decref(&src_lockh, lock_mode);
1090                         ldlm_lock_decref(&tgt_dir_lockh, lock_mode);
1091                 } else {
1092                         ldlm_put_lock_into_req(req, &src_lockh, lock_mode);
1093                         ldlm_put_lock_into_req(req, &tgt_dir_lockh, lock_mode);
1094                 }
1095         case 2: /* target dentry */
1096                 l_dput(de_tgt_dir);
1097         case 1: /* source dentry */
1098                 l_dput(de_src);
1099         case 0:
1100                 break;
1101         default:
1102                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1103                 LBUG();
1104         }
1105         req->rq_status = rc;
1106         return 0;
1107 }
1108
1109 /*
1110  * add a hard link in the PENDING directory, only used by rename()
1111  */
1112 static int mds_add_link_orphan(struct mds_update_record *rec,
1113                                struct obd_device *obd,
1114                                struct dentry *dentry)
1115 {
1116         struct mds_obd *mds = &obd->u.mds;
1117         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1118         struct dentry *pending_child;
1119         char fidname[LL_FID_NAMELEN];
1120         int fidlen = 0, rc;
1121         ENTRY;
1122
1123         LASSERT(dentry->d_inode);
1124         LASSERT(!mds_inode_is_orphan(dentry->d_inode));
1125
1126         down(&pending_dir->i_sem);
1127         fidlen = ll_fid2str(fidname, dentry->d_inode->i_ino,
1128                             dentry->d_inode->i_generation);
1129
1130         CDEBUG(D_ERROR, "pending destroy of %dx open file %s = %s\n",
1131                mds_open_orphan_count(dentry->d_inode),
1132                rec->ur_name, fidname);
1133
1134         pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
1135         if (IS_ERR(pending_child))
1136                 GOTO(out_lock, rc = PTR_ERR(pending_child));
1137
1138         if (pending_child->d_inode != NULL) {
1139                 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1140                 LASSERT(pending_child->d_inode == dentry->d_inode);
1141                 GOTO(out_dput, rc = 0);
1142         }
1143
1144         lock_kernel();
1145         rc = vfs_link(dentry, pending_dir, pending_child);
1146         unlock_kernel();
1147         if (rc)
1148                 CERROR("error addlink orphan %s to PENDING: rc = %d\n",
1149                        rec->ur_name, rc);
1150         else
1151                 mds_inode_set_orphan(dentry->d_inode);
1152 out_dput:
1153         l_dput(pending_child);
1154 out_lock:
1155         up(&pending_dir->i_sem);
1156         RETURN(rc);
1157 }
1158
1159 static int mds_reint_rename(struct mds_update_record *rec, int offset,
1160                             struct ptlrpc_request *req,
1161                             struct lustre_handle *lockh)
1162 {
1163         struct obd_device *obd = req->rq_export->exp_obd;
1164         struct dentry *de_srcdir = NULL;
1165         struct dentry *de_tgtdir = NULL;
1166         struct dentry *de_old = NULL;
1167         struct dentry *de_new = NULL;
1168         struct mds_obd *mds = mds_req2mds(req);
1169         struct lustre_handle dlm_handles[4];
1170         struct ldlm_res_id p1_res_id = { .name = {0} };
1171         struct ldlm_res_id p2_res_id = { .name = {0} };
1172         struct ldlm_res_id c1_res_id = { .name = {0} };
1173         struct ldlm_res_id c2_res_id = { .name = {0} };
1174         struct mds_body *body = NULL;
1175         int rc = 0, lock_count = 3, flags = LDLM_FL_LOCAL_ONLY;
1176         int cleanup_phase = 0;
1177         void *handle = NULL;
1178         ENTRY;
1179
1180         LASSERT(offset == 0);
1181
1182         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1183
1184         de_srcdir = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1185         if (IS_ERR(de_srcdir))
1186                 GOTO(cleanup, rc = PTR_ERR(de_srcdir));
1187
1188         cleanup_phase = 1; /* source directory dentry */
1189
1190         de_tgtdir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
1191         if (IS_ERR(de_tgtdir))
1192                 GOTO(cleanup, rc = PTR_ERR(de_tgtdir));
1193
1194         cleanup_phase = 2; /* target directory dentry */
1195
1196         /* The idea here is that we need to get four locks in the end:
1197          * one on each parent directory, one on each child.  We need to take
1198          * these locks in some kind of order (to avoid deadlocks), and the order
1199          * I selected is "increasing resource number" order.  We need to take
1200          * the locks on the parent directories, however, before we can lookup
1201          * the children.  Thus the following plan:
1202          *
1203          * 1. Take locks on the parent(s), in order
1204          * 2. Lookup the children
1205          * 3. Take locks on the children, in order
1206          * 4. Execute the rename
1207          */
1208
1209         /* Step 1: Take locks on the parent(s), in order */
1210         p1_res_id.name[0] = de_srcdir->d_inode->i_ino;
1211         p1_res_id.name[1] = de_srcdir->d_inode->i_generation;
1212
1213         p2_res_id.name[0] = de_tgtdir->d_inode->i_ino;
1214         p2_res_id.name[1] = de_tgtdir->d_inode->i_generation;
1215
1216         rc = enqueue_ordered_locks(LCK_EX, obd, &p1_res_id, &p2_res_id,
1217                                    &(dlm_handles[0]), &(dlm_handles[1]));
1218         if (rc != ELDLM_OK)
1219                 GOTO(cleanup, rc);
1220
1221         cleanup_phase = 3; /* parent locks */
1222
1223         /* Step 2: Lookup the children */
1224         de_old = ll_lookup_one_len(rec->ur_name, de_srcdir, rec->ur_namelen-1);
1225         if (IS_ERR(de_old)) {
1226                 CERROR("old child lookup error (%*s): %ld\n",
1227                        rec->ur_namelen - 1, rec->ur_name, PTR_ERR(de_old));
1228                 GOTO(cleanup, rc = PTR_ERR(de_old));
1229         }
1230
1231         cleanup_phase = 4; /* original name dentry */
1232
1233         if (de_old->d_inode == NULL)
1234                 GOTO(cleanup, rc = -ENOENT);
1235
1236         /* sanity check for src inode */
1237         if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
1238             de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
1239                 GOTO(cleanup, rc = -EINVAL);
1240
1241         de_new = ll_lookup_one_len(rec->ur_tgt, de_tgtdir, rec->ur_tgtlen - 1);
1242         if (IS_ERR(de_new)) {
1243                 CERROR("new child lookup error (%*s): %ld\n",
1244                        rec->ur_tgtlen - 1, rec->ur_tgt, PTR_ERR(de_new));
1245                 GOTO(cleanup, rc = PTR_ERR(de_new));
1246         }
1247
1248         cleanup_phase = 5; /* target dentry */
1249
1250         /* sanity check for dest inode */
1251         if (de_new->d_inode &&
1252             (de_new->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
1253             de_new->d_inode->i_ino == de_tgtdir->d_inode->i_ino))
1254                 GOTO(cleanup, rc = -EINVAL);
1255
1256         /* Step 3: Take locks on the children */
1257         c1_res_id.name[0] = de_old->d_inode->i_ino;
1258         c1_res_id.name[1] = de_old->d_inode->i_generation;
1259         if (de_new->d_inode == NULL) {
1260                 flags = LDLM_FL_LOCAL_ONLY;
1261                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
1262                                       c1_res_id, LDLM_PLAIN, NULL, 0, LCK_EX,
1263                                       &flags, ldlm_completion_ast,
1264                                       mds_blocking_ast, NULL,
1265                                       &(dlm_handles[2]));
1266                 lock_count = 3;
1267         } else {
1268                 c2_res_id.name[0] = de_new->d_inode->i_ino;
1269                 c2_res_id.name[1] = de_new->d_inode->i_generation;
1270                 rc = enqueue_ordered_locks(LCK_EX, obd, &c1_res_id, &c2_res_id,
1271                                            &(dlm_handles[2]),
1272                                            &(dlm_handles[3]));
1273                 lock_count = 4;
1274         }
1275         if (rc != ELDLM_OK)
1276                 GOTO(cleanup, rc);
1277
1278         cleanup_phase = 6; /* child locks */
1279
1280         /* if we are about to remove the target at first, pass the EA of
1281          * that inode to client to perform and cleanup on OST */
1282         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
1283         LASSERT(body != NULL);
1284
1285         if (de_new->d_inode &&
1286             S_ISREG(de_new->d_inode->i_mode) &&
1287             de_new->d_inode->i_nlink == 1 &&
1288             mds_open_orphan_count(de_new->d_inode) == 0) {
1289                 mds_pack_inode2fid(&body->fid1, de_new->d_inode);
1290                 mds_pack_inode2body(body, de_new->d_inode);
1291                 mds_pack_md(obd, req->rq_repmsg, 1, body, de_new->d_inode, 1);
1292                 if (!(body->valid & OBD_MD_FLEASIZE)) {
1293                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1294                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
1295                 } else {
1296                         /* XXX need log unlink? */
1297                 }
1298         }
1299
1300         /* Step 4: Execute the rename */
1301         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,de_srcdir->d_inode->i_sb);
1302
1303         handle = fsfilt_start(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME, NULL);
1304         if (IS_ERR(handle))
1305                 GOTO(cleanup, rc = PTR_ERR(handle));
1306
1307         /* FIXME need adjust the journal block count? */
1308         /* if the target should be moved to PENDING, we at first increase the
1309          * link and later vfs_rename() will decrease the link count again */
1310         if (de_new->d_inode &&
1311             S_ISREG(de_new->d_inode->i_mode) &&
1312             de_new->d_inode->i_nlink == 1 &&
1313             mds_open_orphan_count(de_new->d_inode) > 0) {
1314                 rc = mds_add_link_orphan(rec, obd, de_new);
1315                 if (rc)
1316                         GOTO(cleanup, rc);
1317         }
1318
1319         lock_kernel();
1320         de_old->d_fsdata = req;
1321         de_new->d_fsdata = req;
1322         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
1323         unlock_kernel();
1324
1325         EXIT;
1326 cleanup:
1327         rc = mds_finish_transno(mds, de_tgtdir ? de_tgtdir->d_inode : NULL,
1328                                 handle, req, rc, 0);
1329         switch (cleanup_phase) {
1330         case 6: /* child locks */
1331                 if (rc) {
1332                         ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
1333                         if (lock_count == 4)
1334                                 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
1335                 } else {
1336                         ldlm_put_lock_into_req(req, &(dlm_handles[2]), LCK_EX);
1337                         if (lock_count == 4)
1338                                 ldlm_put_lock_into_req(req,
1339                                                 &(dlm_handles[3]), LCK_EX);
1340                 }
1341         case 5: /* target dentry */
1342                 l_dput(de_new);
1343         case 4: /* source dentry */
1344                 l_dput(de_old);
1345         case 3: /* parent locks */
1346                 if (rc) {
1347                         ldlm_lock_decref(&(dlm_handles[0]), LCK_EX);
1348                         ldlm_lock_decref(&(dlm_handles[1]), LCK_EX);
1349                 } else {
1350                         ldlm_put_lock_into_req(req, &(dlm_handles[0]), LCK_EX);
1351                         ldlm_put_lock_into_req(req, &(dlm_handles[1]), LCK_EX);
1352                 }
1353         case 2: /* target directory dentry */
1354                 l_dput(de_tgtdir);
1355         case 1: /* source directry dentry */
1356                 l_dput(de_srcdir);
1357         case 0:
1358                 break;
1359         default:
1360                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1361                 LBUG();
1362         }
1363         req->rq_status = rc;
1364         return 0;
1365 }
1366
1367 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
1368                            struct ptlrpc_request *, struct lustre_handle *);
1369
1370 static mds_reinter reinters[REINT_MAX + 1] = {
1371         [REINT_SETATTR] mds_reint_setattr,
1372         [REINT_CREATE] mds_reint_create,
1373         [REINT_UNLINK] mds_reint_unlink,
1374         [REINT_LINK] mds_reint_link,
1375         [REINT_RENAME] mds_reint_rename,
1376         [REINT_OPEN] mds_open
1377 };
1378
1379 int mds_reint_rec(struct mds_update_record *rec, int offset,
1380                   struct ptlrpc_request *req, struct lustre_handle *lockh)
1381 {
1382         struct obd_device *obd = req->rq_export->exp_obd;
1383         struct obd_run_ctxt saved;
1384         int rc;
1385         ENTRY;
1386
1387         /* checked by unpacker */
1388         LASSERT(rec->ur_opcode <= REINT_MAX &&
1389                 reinters[rec->ur_opcode] != NULL);
1390
1391         push_ctxt(&saved, &obd->obd_ctxt, &rec->ur_uc);
1392         rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
1393         pop_ctxt(&saved, &obd->obd_ctxt, &rec->ur_uc);
1394
1395         RETURN(rc);
1396 }