Whamcloud - gitweb
Branch HEAD
[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-2005 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 the Lustre file system, http://www.lustre.org
13  *   Lustre is a trademark of Cluster File Systems, Inc.
14  *
15  *   You may have signed or agreed to another license before downloading
16  *   this software.  If so, you are bound by the terms and conditions
17  *   of that agreement, and the following does not apply to you.  See the
18  *   LICENSE file included with this distribution for more information.
19  *
20  *   If you did not agree to a different license, then this copy of Lustre
21  *   is open source software; you can redistribute it and/or modify it
22  *   under the terms of version 2 of the GNU General Public License as
23  *   published by the Free Software Foundation.
24  *
25  *   In either case, Lustre is distributed in the hope that it will be
26  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
27  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  *   license text for more details.
29  */
30
31 #ifndef EXPORT_SYMTAB
32 # define EXPORT_SYMTAB
33 #endif
34 #define DEBUG_SUBSYSTEM S_MDS
35
36 #include <linux/fs.h>
37 #include <obd_support.h>
38 #include <obd_class.h>
39 #include <obd.h>
40 #include <lustre_lib.h>
41 #include <lustre/lustre_idl.h>
42 #include <lustre_mds.h>
43 #include <lustre_dlm.h>
44 #include <lustre_fsfilt.h>
45 #include <lustre_ucache.h>
46
47 #include "mds_internal.h"
48
49 void mds_commit_cb(struct obd_device *obd, __u64 transno, void *data,
50                    int error)
51 {
52         obd_transno_commit_cb(obd, transno, error);
53 }
54
55 struct mds_logcancel_data {
56         struct lov_mds_md      *mlcd_lmm;
57         int                     mlcd_size;
58         int                     mlcd_cookielen;
59         int                     mlcd_eadatalen;
60         struct llog_cookie      mlcd_cookies[0];
61 };
62
63
64 static void mds_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
65                                   void *cb_data, int error)
66 {
67         struct mds_logcancel_data *mlcd = cb_data;
68         struct lov_stripe_md *lsm = NULL;
69         struct llog_ctxt *ctxt;
70         int rc;
71
72         obd_transno_commit_cb(obd, transno, error);
73
74         CDEBUG(D_RPCTRACE, "cancelling %d cookies\n",
75                (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
76
77         rc = obd_unpackmd(obd->u.mds.mds_osc_exp, &lsm, mlcd->mlcd_lmm,
78                           mlcd->mlcd_eadatalen);
79         if (rc < 0) {
80                 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
81                        (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
82                        rc);
83         } else {
84                 ///* XXX 0 normally, SENDNOW for debug */);
85                 rc = obd_checkmd(obd->u.mds.mds_osc_exp, obd->obd_self_export,
86                                  lsm);
87                 if (rc)
88                         CERROR("Can not revalidate lsm %p \n", lsm);
89
90                 ctxt = llog_get_context(obd,mlcd->mlcd_cookies[0].lgc_subsys+1);
91                 rc = llog_cancel(ctxt, lsm, mlcd->mlcd_cookielen /
92                                                 sizeof(*mlcd->mlcd_cookies),
93                                  mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW);
94                 if (rc)
95                         CERROR("error cancelling %d log cookies: rc %d\n",
96                                (int)(mlcd->mlcd_cookielen /
97                                      sizeof(*mlcd->mlcd_cookies)), rc);
98         }
99
100         OBD_FREE(mlcd, mlcd->mlcd_size);
101 }
102
103 /* Assumes caller has already pushed us into the kernel context. */
104 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
105                        struct ptlrpc_request *req, int rc, __u32 op_data,
106                        int force_sync)
107 {
108         struct mds_export_data *med = &req->rq_export->exp_mds_data;
109         struct mds_client_data *mcd = med->med_mcd;
110         struct obd_device *obd = req->rq_export->exp_obd;
111         int err;
112         __u64 transno, prev_transno;
113         loff_t off;
114         int log_pri = D_RPCTRACE;
115         ENTRY;
116
117         if (IS_ERR(handle)) {
118                 LASSERT(rc != 0);
119                 RETURN(rc);
120         }
121
122         /* if the export has already been failed, we have no last_rcvd slot */
123         if (req->rq_export->exp_failed || obd->obd_fail) {
124                 CWARN("commit transaction for disconnected client %s: rc %d\n",
125                       req->rq_export->exp_client_uuid.uuid, rc);
126                 if (rc == 0)
127                         rc = -ENOTCONN;
128                 if (handle)
129                         GOTO(commit, rc);
130                 RETURN(rc);
131         }
132
133         if (handle == NULL) {
134                 /* if we're starting our own xaction, use our own inode */
135                 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
136                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
137                 if (IS_ERR(handle)) {
138                         CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
139                         RETURN(PTR_ERR(handle));
140                 }
141         }
142
143         off = med->med_lr_off;
144
145         transno = lustre_msg_get_transno(req->rq_reqmsg);
146         if (rc != 0) {
147                 if (transno != 0) {
148                         CERROR("%s: replay %s transno "LPU64" failed: rc %d\n",
149                                obd->obd_name,
150                                libcfs_nid2str(req->rq_export->exp_connection->c_peer.nid),
151                                transno, rc);
152                         transno = 0;
153                 }
154         } else if (transno == 0) {
155                 spin_lock(&mds->mds_transno_lock);
156                 transno = ++mds->mds_last_transno;
157                 spin_unlock(&mds->mds_transno_lock);
158         } else {
159                 spin_lock(&mds->mds_transno_lock);
160                 if (transno > mds->mds_last_transno)
161                         mds->mds_last_transno = transno;
162                 spin_unlock(&mds->mds_transno_lock);
163         }
164
165         req->rq_transno = transno;
166         lustre_msg_set_transno(req->rq_repmsg, transno);
167         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
168                 prev_transno = le64_to_cpu(mcd->mcd_last_close_transno);
169                 mcd->mcd_last_close_transno = cpu_to_le64(transno);
170                 mcd->mcd_last_close_xid = cpu_to_le64(req->rq_xid);
171                 mcd->mcd_last_close_result = cpu_to_le32(rc);
172                 mcd->mcd_last_close_data = cpu_to_le32(op_data);
173         } else {
174                 prev_transno = le64_to_cpu(mcd->mcd_last_transno);
175                 if (((lustre_msg_get_flags(req->rq_reqmsg) &
176                       (MSG_RESENT | MSG_REPLAY)) == 0) ||
177                     (transno > prev_transno)) {
178                         mcd->mcd_last_transno = cpu_to_le64(transno);
179                         mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
180                         mcd->mcd_last_result = cpu_to_le32(rc);
181                         mcd->mcd_last_data = cpu_to_le32(op_data);
182                 }
183         }
184         /* update the server data to not lose the greatest transno. Bug 11125 */
185         if ((transno == 0) && (prev_transno == mds->mds_last_transno))
186                 mds_update_server_data(obd, 0);
187
188         if (off <= 0) {
189                 CERROR("client idx %d has offset %lld\n", med->med_lr_idx, off);
190                 err = -EINVAL;
191         } else {
192                 struct obd_export *exp = req->rq_export;
193
194                 if (!force_sync)
195                         force_sync = fsfilt_add_journal_cb(exp->exp_obd,transno,
196                                                           handle, mds_commit_cb,
197                                                           NULL);
198
199                 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd,
200                                           sizeof(*mcd), &off,
201                                           force_sync | exp->exp_need_sync);
202                 if (force_sync)
203                         mds_commit_cb(obd, transno, NULL, err);
204         }
205
206         if (err) {
207                 log_pri = D_ERROR;
208                 if (rc == 0)
209                         rc = err;
210         }
211
212         DEBUG_REQ(log_pri, req,
213                   "wrote trans #"LPU64" rc %d client %s at idx %u: err = %d",
214                    transno, rc, mcd->mcd_uuid, med->med_lr_idx, err);
215
216         err = mds_lov_write_objids(obd);
217         if (err) {
218                 log_pri = D_ERROR;
219                 if (rc == 0)
220                         rc = err;
221         }
222         CDEBUG(log_pri, "wrote objids: err = %d\n", err);
223
224 commit:
225         err = fsfilt_commit(obd, inode, handle, 0);
226         if (err) {
227                 CERROR("error committing transaction: %d\n", err);
228                 if (!rc)
229                         rc = err;
230         }
231
232         RETURN(rc);
233 }
234
235 /* this gives the same functionality as the code between
236  * sys_chmod and inode_setattr
237  * chown_common and inode_setattr
238  * utimes and inode_setattr
239  */
240 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
241 {
242         time_t now = CURRENT_SECONDS;
243         struct iattr *attr = &rec->ur_iattr;
244         unsigned int ia_valid = attr->ia_valid;
245         int error;
246         ENTRY;
247
248         if (ia_valid & ATTR_RAW)
249                 attr->ia_valid &= ~ATTR_RAW;
250
251         if (!(ia_valid & ATTR_CTIME_SET))
252                 LTIME_S(attr->ia_ctime) = now;
253         else
254                 attr->ia_valid &= ~ATTR_CTIME_SET;
255         if (!(ia_valid & ATTR_ATIME_SET))
256                 LTIME_S(attr->ia_atime) = now;
257         if (!(ia_valid & ATTR_MTIME_SET))
258                 LTIME_S(attr->ia_mtime) = now;
259
260         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
261                 RETURN((attr->ia_valid & ~ATTR_ATTR_FLAG) ? -EPERM : 0);
262
263         /* times */
264         if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
265                 if (current->fsuid != inode->i_uid &&
266                     (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
267                         RETURN(error);
268         }
269
270         if (ia_valid & ATTR_SIZE &&
271             /* NFSD hack for open(O_CREAT|O_TRUNC)=mknod+truncate (bug 5781) */
272             !(rec->ur_uc.luc_fsuid == inode->i_uid &&
273               ia_valid & MDS_OPEN_OWNEROVERRIDE)) {
274                 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
275                         RETURN(error);
276         }
277
278         if (ia_valid & (ATTR_UID | ATTR_GID)) {
279                 /* chown */
280                 error = -EPERM;
281                 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
282                         RETURN(-EPERM);
283                 if (attr->ia_uid == (uid_t) -1)
284                         attr->ia_uid = inode->i_uid;
285                 if (attr->ia_gid == (gid_t) -1)
286                         attr->ia_gid = inode->i_gid;
287                 if (!(ia_valid & ATTR_MODE))
288                         attr->ia_mode = inode->i_mode;
289                 /*
290                  * If the user or group of a non-directory has been
291                  * changed by a non-root user, remove the setuid bit.
292                  * 19981026 David C Niemi <niemi@tux.org>
293                  *
294                  * Changed this to apply to all users, including root,
295                  * to avoid some races. This is the behavior we had in
296                  * 2.0. The check for non-root was definitely wrong
297                  * for 2.2 anyway, as it should have been using
298                  * CAP_FSETID rather than fsuid -- 19990830 SD.
299                  */
300                 if ((inode->i_mode & S_ISUID) == S_ISUID &&
301                     !S_ISDIR(inode->i_mode)) {
302                         attr->ia_mode &= ~S_ISUID;
303                         attr->ia_valid |= ATTR_MODE;
304                 }
305                 /*
306                  * Likewise, if the user or group of a non-directory
307                  * has been changed by a non-root user, remove the
308                  * setgid bit UNLESS there is no group execute bit
309                  * (this would be a file marked for mandatory
310                  * locking).  19981026 David C Niemi <niemi@tux.org>
311                  *
312                  * Removed the fsuid check (see the comment above) --
313                  * 19990830 SD.
314                  */
315                 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
316                      (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
317                         attr->ia_mode &= ~S_ISGID;
318                         attr->ia_valid |= ATTR_MODE;
319                 }
320         } else if (ia_valid & ATTR_MODE) {
321                 int mode = attr->ia_mode;
322                 /* chmod */
323                 if (attr->ia_mode == (umode_t)-1)
324                         mode = inode->i_mode;
325                 attr->ia_mode =
326                         (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
327         }
328         RETURN(0);
329 }
330
331 void mds_steal_ack_locks(struct ptlrpc_request *req)
332 {
333         struct obd_export         *exp = req->rq_export;
334         struct list_head          *tmp;
335         struct ptlrpc_reply_state *oldrep;
336         struct ptlrpc_service     *svc;
337         int                        i;
338
339         /* CAVEAT EMPTOR: spinlock order */
340         spin_lock(&exp->exp_lock);
341         list_for_each (tmp, &exp->exp_outstanding_replies) {
342                 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
343
344                 if (oldrep->rs_xid != req->rq_xid)
345                         continue;
346
347                 if (lustre_msg_get_opc(oldrep->rs_msg) !=
348                     lustre_msg_get_opc(req->rq_reqmsg))
349                         CERROR ("Resent req xid "LPX64" has mismatched opc: "
350                                 "new %d old %d\n", req->rq_xid,
351                                 lustre_msg_get_opc(req->rq_reqmsg),
352                                 lustre_msg_get_opc(oldrep->rs_msg));
353
354                 svc = oldrep->rs_service;
355                 spin_lock (&svc->srv_lock);
356
357                 list_del_init (&oldrep->rs_exp_list);
358
359                 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
360                       " o%d NID %s\n",
361                       oldrep->rs_nlocks, oldrep,
362                       oldrep->rs_xid, oldrep->rs_transno,
363                       lustre_msg_get_opc(oldrep->rs_msg),
364                       libcfs_nid2str(exp->exp_connection->c_peer.nid));
365
366                 for (i = 0; i < oldrep->rs_nlocks; i++)
367                         ptlrpc_save_lock(req,
368                                          &oldrep->rs_locks[i],
369                                          oldrep->rs_modes[i]);
370                 oldrep->rs_nlocks = 0;
371
372                 DEBUG_REQ(D_HA, req, "stole locks for");
373                 ptlrpc_schedule_difficult_reply (oldrep);
374
375                 spin_unlock (&svc->srv_lock);
376                 break;
377         }
378         spin_unlock(&exp->exp_lock);
379 }
380 EXPORT_SYMBOL(mds_steal_ack_locks);
381 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
382 {
383         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
384                 req->rq_transno = le64_to_cpu(mcd->mcd_last_close_transno);
385                 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
386                 req->rq_status = le32_to_cpu(mcd->mcd_last_close_result);
387                 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
388         } else {
389                 req->rq_transno = le64_to_cpu(mcd->mcd_last_transno);
390                 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
391                 req->rq_status = le32_to_cpu(mcd->mcd_last_result);
392                 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
393         }
394         DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
395                   req->rq_transno, req->rq_status);
396
397         mds_steal_ack_locks(req);
398 }
399
400 static void reconstruct_reint_setattr(struct mds_update_record *rec,
401                                       int offset, struct ptlrpc_request *req)
402 {
403         struct mds_export_data *med = &req->rq_export->exp_mds_data;
404         struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
405         struct dentry *de;
406         struct mds_body *body;
407
408         mds_req_from_mcd(req, med->med_mcd);
409
410         de = mds_fid2dentry(obd, rec->ur_fid1, NULL);
411         if (IS_ERR(de)) {
412                 LASSERT(PTR_ERR(de) == req->rq_status);
413                 return;
414         }
415
416         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
417         mds_pack_inode2fid(&body->fid1, de->d_inode);
418         mds_pack_inode2body(body, de->d_inode);
419
420         /* Don't return OST-specific attributes if we didn't just set them */
421         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
422                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
423         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
424                 body->valid |= OBD_MD_FLMTIME;
425         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
426                 body->valid |= OBD_MD_FLATIME;
427
428         l_dput(de);
429 }
430
431 int mds_osc_setattr_async(struct obd_device *obd, __u32 uid, __u32 gid,
432                           struct lov_mds_md *lmm, int lmm_size,
433                           struct llog_cookie *logcookies, __u64 id, __u32 gen,
434                           struct obd_capa *oc)
435 {
436         struct mds_obd *mds = &obd->u.mds;
437         struct obd_trans_info oti = { 0 };
438         struct obd_info oinfo = { { { 0 } } };
439         int rc;
440         ENTRY;
441
442         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OST_SETATTR))
443                 RETURN(0);
444
445         /* first get memory EA */
446         OBDO_ALLOC(oinfo.oi_oa);
447         if (!oinfo.oi_oa)
448                 RETURN(-ENOMEM);
449
450         LASSERT(lmm);
451
452         rc = obd_unpackmd(mds->mds_osc_exp, &oinfo.oi_md, lmm, lmm_size);
453         if (rc < 0) {
454                 CERROR("Error unpack md %p for inode "LPU64"\n", lmm, id);
455                 GOTO(out, rc);
456         }
457
458         rc = obd_checkmd(mds->mds_osc_exp, obd->obd_self_export, oinfo.oi_md);
459         if (rc) {
460                 CERROR("Error revalidate lsm %p \n", oinfo.oi_md);
461                 GOTO(out, rc);
462         }
463
464         /* then fill oa */
465         oinfo.oi_oa->o_uid = uid;
466         oinfo.oi_oa->o_gid = gid;
467         oinfo.oi_oa->o_id = oinfo.oi_md->lsm_object_id;
468         oinfo.oi_oa->o_gr = oinfo.oi_md->lsm_object_gr;
469         oinfo.oi_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP |
470                                 OBD_MD_FLUID | OBD_MD_FLGID;
471         if (logcookies) {
472                 oinfo.oi_oa->o_valid |= OBD_MD_FLCOOKIE;
473                 oti.oti_logcookies = logcookies;
474         }
475
476         oinfo.oi_oa->o_fid = id;
477         oinfo.oi_oa->o_generation = gen;
478         oinfo.oi_oa->o_valid |= OBD_MD_FLFID | OBD_MD_FLGENER;
479         oinfo.oi_capa = oc;
480
481         /* do async setattr from mds to ost not waiting for responses. */
482         rc = obd_setattr_async(mds->mds_osc_exp, &oinfo, &oti, NULL);
483         if (rc)
484                 CDEBUG(D_INODE, "mds to ost setattr objid 0x"LPX64
485                        " on ost error %d\n", oinfo.oi_md->lsm_object_id, rc);
486 out:
487         if (oinfo.oi_md)
488                 obd_free_memmd(mds->mds_osc_exp, &oinfo.oi_md);
489         OBDO_FREE(oinfo.oi_oa);
490         RETURN(rc);
491 }
492 EXPORT_SYMBOL(mds_osc_setattr_async);
493
494 /* In the raw-setattr case, we lock the child inode.
495  * In the write-back case or if being called from open, the client holds a lock
496  * already.
497  *
498  * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
499 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
500                              struct ptlrpc_request *req,
501                              struct lustre_handle *lh)
502 {
503         unsigned int ia_valid = rec->ur_iattr.ia_valid;
504         struct mds_obd *mds = mds_req2mds(req);
505         struct obd_device *obd = req->rq_export->exp_obd;
506         struct mds_body *body;
507         struct dentry *de;
508         struct inode *inode = NULL;
509         struct lustre_handle lockh;
510         void *handle = NULL;
511         struct mds_logcancel_data *mlcd = NULL;
512         struct lov_mds_md *lmm = NULL;
513         struct llog_cookie *logcookies = NULL;
514         int lmm_size = 0, need_lock = 1, cookie_size = 0;
515         int rc = 0, cleanup_phase = 0, err, locked = 0, sync = 0;
516         unsigned int qcids[MAXQUOTAS] = { 0, 0 };
517         unsigned int qpids[MAXQUOTAS] = { rec->ur_iattr.ia_uid,
518                                           rec->ur_iattr.ia_gid };
519         ENTRY;
520
521         LASSERT(offset == REQ_REC_OFF);
522         offset = REPLY_REC_OFF;
523
524         DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x", rec->ur_fid1->id,
525                   rec->ur_fid1->generation, rec->ur_iattr.ia_valid);
526         OBD_COUNTER_INCREMENT(obd, setattr);
527
528         MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
529
530         if (rec->ur_dlm)
531                 ldlm_request_cancel(req, rec->ur_dlm, 0);
532
533         if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN ||
534             (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)) {
535                 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
536                 if (IS_ERR(de))
537                         GOTO(cleanup, rc = PTR_ERR(de));
538                 if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
539                         GOTO(cleanup, rc = -EROFS);
540         } else {
541                 __u64 lockpart = MDS_INODELOCK_UPDATE;
542                 if (rec->ur_iattr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
543                         lockpart |= MDS_INODELOCK_LOOKUP;
544
545                 de = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_EX,
546                                            &lockh, lockpart);
547                 if (IS_ERR(de))
548                         GOTO(cleanup, rc = PTR_ERR(de));
549                 locked = 1;
550         }
551
552         cleanup_phase = 1;
553         inode = de->d_inode;
554         LASSERT(inode);
555
556         if ((rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) ||
557             (rec->ur_iattr.ia_valid & ATTR_SIZE)) {
558                 /* Check write access for the O_TRUNC case */
559                 if (mds_query_write_access(inode) < 0)
560                         GOTO(cleanup, rc = -ETXTBSY);
561         }
562
563         /* save uid/gid for quota acq/rel */
564         qcids[USRQUOTA] = inode->i_uid;
565         qcids[GRPQUOTA] = inode->i_gid;
566
567         if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
568             rec->ur_eadata != NULL) {
569                 LOCK_INODE_MUTEX(inode);
570                 need_lock = 0;
571         }
572
573         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
574
575         /* start a log jounal handle if needed */
576         if (S_ISREG(inode->i_mode) &&
577             rec->ur_iattr.ia_valid & (ATTR_UID | ATTR_GID)) {
578                 lmm_size = mds->mds_max_mdsize;
579                 OBD_ALLOC(lmm, lmm_size);
580                 if (lmm == NULL)
581                         GOTO(cleanup, rc = -ENOMEM);
582
583                 cleanup_phase = 2;
584                 rc = mds_get_md(obd, inode, lmm, &lmm_size, need_lock);
585                 if (rc < 0)
586                         GOTO(cleanup, rc);
587                 rc = 0;
588
589                 handle = fsfilt_start_log(obd, inode, FSFILT_OP_SETATTR, NULL,
590                                           le32_to_cpu(lmm->lmm_stripe_count));
591         } else {
592                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
593         }
594         if (IS_ERR(handle))
595                 GOTO(cleanup, rc = PTR_ERR(handle));
596
597         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
598                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
599                        LTIME_S(rec->ur_iattr.ia_mtime),
600                        LTIME_S(rec->ur_iattr.ia_ctime));
601         rc = mds_fix_attr(inode, rec);
602         if (rc)
603                 GOTO(cleanup, rc);
604
605         if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) {  /* ioctl */
606                 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
607                                       (long)&rec->ur_flags);
608         } else if (rec->ur_iattr.ia_valid) {            /* setattr */
609                 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
610                 /* journal chown/chgrp in llog, just like unlink */
611                 if (rc == 0 && lmm_size){
612                         cookie_size = mds_get_cookie_size(obd, lmm);
613                         OBD_ALLOC(logcookies, cookie_size);
614                         if (logcookies == NULL)
615                                 GOTO(cleanup, rc = -ENOMEM);
616
617                         if (mds_log_op_setattr(obd, inode->i_uid, inode->i_gid,
618                                                lmm, lmm_size,
619                                                logcookies, cookie_size) <= 0) {
620                                 OBD_FREE(logcookies, cookie_size);
621                                 logcookies = NULL;
622                         }
623                 }
624         }
625
626         if (rc == 0 && (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
627             rec->ur_eadata != NULL) {
628                 struct lov_stripe_md *lsm = NULL;
629                 struct lov_user_md *lum = NULL;
630
631                 rc = ll_permission(inode, MAY_WRITE, NULL);
632                 if (rc < 0)
633                         GOTO(cleanup, rc);
634
635                 lum = rec->ur_eadata;
636                 /* if { size, offset, count } = { 0, -1, 0 } (i.e. all default
637                  * values specified) then delete default striping from dir. */
638                 if (S_ISDIR(inode->i_mode) &&
639                     ((lum->lmm_stripe_size == 0 &&
640                       lum->lmm_stripe_offset ==
641                       (typeof(lum->lmm_stripe_offset))(-1) &&
642                       lum->lmm_stripe_count == 0) ||
643                     /* lmm_stripe_size == -1 is deprecated in 1.4.6 */
644                     lum->lmm_stripe_size ==
645                     (typeof(lum->lmm_stripe_size))(-1))){
646                         rc = fsfilt_set_md(obd, inode, handle, NULL, 0, "lov");
647                         if (rc)
648                                 GOTO(cleanup, rc);
649                 } else {
650                         rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
651                                            mds->mds_osc_exp, 0,
652                                            &lsm, rec->ur_eadata);
653                         if (rc)
654                                 GOTO(cleanup, rc);
655
656                         obd_free_memmd(mds->mds_osc_exp, &lsm);
657
658                         rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
659                                            rec->ur_eadatalen, "lov");
660                         if (rc)
661                                 GOTO(cleanup, rc);
662                 }
663         }
664
665         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
666         mds_pack_inode2fid(&body->fid1, inode);
667         mds_pack_inode2body(body, inode);
668
669         /* don't return OST-specific attributes if we didn't just set them. */
670         if (ia_valid & ATTR_SIZE)
671                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
672         if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
673                 body->valid |= OBD_MD_FLMTIME;
674         if (ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
675                 body->valid |= OBD_MD_FLATIME;
676
677         if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_osc_obd)) {
678                 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
679                           rec->ur_eadatalen);
680                 if (mlcd) {
681                         mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
682                                 rec->ur_eadatalen;
683                         mlcd->mlcd_eadatalen = rec->ur_eadatalen;
684                         mlcd->mlcd_cookielen = rec->ur_cookielen;
685                         mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
686                                 mlcd->mlcd_cookielen;
687                         memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
688                                mlcd->mlcd_cookielen);
689                         memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
690                                mlcd->mlcd_eadatalen);
691                 } else {
692                         CERROR("unable to allocate log cancel data\n");
693                 }
694         }
695         EXIT;
696  cleanup:
697         if (mlcd != NULL)
698                 sync = fsfilt_add_journal_cb(req->rq_export->exp_obd, 0, handle,
699                                              mds_cancel_cookies_cb, mlcd);
700         err = mds_finish_transno(mds, inode, handle, req, rc, 0, sync);
701         /* do mds to ost setattr if needed */
702         if (!rc && !err && lmm_size)
703                 mds_osc_setattr_async(obd, inode->i_ino, inode->i_generation, lmm,
704                                       lmm_size, logcookies, rec->ur_fid1->id,
705                                       rec->ur_fid1->generation, NULL);
706
707         switch (cleanup_phase) {
708         case 2:
709                 OBD_FREE(lmm, mds->mds_max_mdsize);
710                 if (logcookies)
711                         OBD_FREE(logcookies, cookie_size);
712         case 1:
713                 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
714                     rec->ur_eadata != NULL)
715                         UNLOCK_INODE_MUTEX(inode);
716                 l_dput(de);
717                 if (locked) {
718                         if (rc) {
719                                 ldlm_lock_decref(&lockh, LCK_EX);
720                         } else {
721                                 ptlrpc_save_lock (req, &lockh, LCK_EX);
722                         }
723                 }
724         case 0:
725                 break;
726         default:
727                 LBUG();
728         }
729         if (err && !rc)
730                 rc = err;
731
732         req->rq_status = rc;
733
734         /* trigger dqrel/dqacq for original owner and new owner */
735         if (ia_valid & (ATTR_UID | ATTR_GID))
736                 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
737                               FSFILT_OP_SETATTR);
738
739         return 0;
740 }
741
742 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
743                                      struct ptlrpc_request *req)
744 {
745         struct mds_export_data *med = &req->rq_export->exp_mds_data;
746         struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
747         struct dentry *parent, *child;
748         struct mds_body *body;
749
750         mds_req_from_mcd(req, med->med_mcd);
751
752         if (req->rq_status)
753                 return;
754
755         parent = mds_fid2dentry(obd, rec->ur_fid1, NULL);
756         LASSERT(!IS_ERR(parent));
757         child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
758         LASSERT(!IS_ERR(child));
759
760         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
761         mds_pack_inode2fid(&body->fid1, child->d_inode);
762         mds_pack_inode2body(body, child->d_inode);
763
764         l_dput(parent);
765         l_dput(child);
766 }
767
768 static int mds_reint_create(struct mds_update_record *rec, int offset,
769                             struct ptlrpc_request *req,
770                             struct lustre_handle *lh)
771 {
772         struct dentry *dparent = NULL;
773         struct mds_obd *mds = mds_req2mds(req);
774         struct obd_device *obd = req->rq_export->exp_obd;
775         struct dentry *dchild = NULL;
776         struct inode *dir = NULL;
777         void *handle = NULL;
778         struct lustre_handle lockh;
779         int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
780         int created = 0;
781         unsigned int qcids[MAXQUOTAS] = { current->fsuid, current->fsgid };
782         unsigned int qpids[MAXQUOTAS] = { 0, 0 };
783         struct lvfs_dentry_params dp = LVFS_DENTRY_PARAMS_INIT;
784         ENTRY;
785
786         LASSERT(offset == REQ_REC_OFF);
787         offset = REPLY_REC_OFF;
788
789         LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
790                         LUSTRE_MDS_NAME));
791
792         DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
793                   rec->ur_fid1->id, rec->ur_fid1->generation,
794                   rec->ur_name, rec->ur_mode);
795
796         MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
797
798         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
799                 GOTO(cleanup, rc = -ESTALE);
800
801         if (rec->ur_dlm)
802                 ldlm_request_cancel(req, rec->ur_dlm, 0);
803
804         dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_EX, &lockh,
805                                         MDS_INODELOCK_UPDATE);
806         if (IS_ERR(dparent)) {
807                 rc = PTR_ERR(dparent);
808                 if (rc != -ENOENT)
809                         CERROR("parent "LPU64"/%u lookup error %d\n",
810                                rec->ur_fid1->id, rec->ur_fid1->generation, rc);
811                 GOTO(cleanup, rc);
812         }
813         cleanup_phase = 1; /* locked parent dentry */
814         dir = dparent->d_inode;
815         LASSERT(dir);
816
817         ldlm_lock_dump_handle(D_OTHER, &lockh);
818
819         dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
820         if (IS_ERR(dchild)) {
821                 rc = PTR_ERR(dchild);
822                 if (rc != -ENAMETOOLONG)
823                 CERROR("child lookup error %d\n", rc);
824                 GOTO(cleanup, rc);
825         }
826
827         cleanup_phase = 2; /* child dentry */
828
829         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
830
831         if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY) {
832                 if (dchild->d_inode)
833                         GOTO(cleanup, rc = -EEXIST);
834                 GOTO(cleanup, rc = -EROFS);
835         }
836
837         if (dir->i_mode & S_ISGID && S_ISDIR(rec->ur_mode))
838                 rec->ur_mode |= S_ISGID;
839
840         dchild->d_fsdata = (void *)&dp;
841         dp.ldp_inum = (unsigned long)rec->ur_fid2->id;
842         dp.ldp_ptr = req;
843
844         switch (type) {
845         case S_IFREG:{
846                 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
847                 if (IS_ERR(handle))
848                         GOTO(cleanup, rc = PTR_ERR(handle));
849                 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
850                 mds_counter_incr(req->rq_export, LPROC_MDS_MKNOD);
851                 EXIT;
852                 break;
853         }
854         case S_IFDIR:{
855                 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
856                 if (IS_ERR(handle))
857                         GOTO(cleanup, rc = PTR_ERR(handle));
858                 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
859                 mds_counter_incr(req->rq_export, LPROC_MDS_MKDIR);
860                 EXIT;
861                 break;
862         }
863         case S_IFLNK:{
864                 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
865                 if (IS_ERR(handle))
866                         GOTO(cleanup, rc = PTR_ERR(handle));
867                 if (rec->ur_tgt == NULL)        /* no target supplied */
868                         rc = -EINVAL;           /* -EPROTO? */
869                 else
870                         rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
871                 mds_counter_incr(req->rq_export, LPROC_MDS_MKNOD);
872                 EXIT;
873                 break;
874         }
875         case S_IFCHR:
876         case S_IFBLK:
877         case S_IFIFO:
878         case S_IFSOCK:{
879                 int rdev = rec->ur_rdev;
880                 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
881                 if (IS_ERR(handle))
882                         GOTO(cleanup, rc = PTR_ERR(handle));
883                 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
884                 mds_counter_incr(req->rq_export, LPROC_MDS_MKNOD);
885                 EXIT;
886                 break;
887         }
888         default:
889                 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
890                 dchild->d_fsdata = NULL;
891                 GOTO(cleanup, rc = -EINVAL);
892         }
893
894         /* In case we stored the desired inum in here, we want to clean up. */
895         if (dchild->d_fsdata == (void *)(unsigned long)rec->ur_fid2->id)
896                 dchild->d_fsdata = NULL;
897
898         if (rc) {
899                 CDEBUG(D_INODE, "error during create: %d\n", rc);
900                 GOTO(cleanup, rc);
901         } else {
902                 struct iattr iattr;
903                 struct inode *inode = dchild->d_inode;
904                 struct mds_body *body;
905
906                 created = 1;
907                 LTIME_S(iattr.ia_atime) = rec->ur_time;
908                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
909                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
910                 iattr.ia_uid = current->fsuid;  /* set by push_ctxt already */
911                 if (dir->i_mode & S_ISGID)
912                         iattr.ia_gid = dir->i_gid;
913                 else
914                         iattr.ia_gid = current->fsgid;
915                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
916                         ATTR_MTIME | ATTR_CTIME;
917
918                 if (rec->ur_fid2->id) {
919                         LASSERT(rec->ur_fid2->id == inode->i_ino);
920                         inode->i_generation = rec->ur_fid2->generation;
921                         /* Dirtied and committed by the upcoming setattr. */
922                         CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
923                                inode->i_ino, inode->i_generation);
924                 } else {
925                         CDEBUG(D_INODE, "created ino %lu with gen %x\n",
926                                inode->i_ino, inode->i_generation);
927                 }
928
929                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
930                 if (rc)
931                         CERROR("error on child setattr: rc = %d\n", rc);
932
933                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
934                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
935                 if (rc)
936                         CERROR("error on parent setattr: rc = %d\n", rc);
937
938                 if (S_ISDIR(inode->i_mode)) {
939                         struct lov_mds_md lmm;
940                         int lmm_size = sizeof(lmm);
941                         rc = mds_get_md(obd, dir, &lmm, &lmm_size, 1);
942                         if (rc > 0) {
943                                 LOCK_INODE_MUTEX(inode);
944                                 rc = fsfilt_set_md(obd, inode, handle,
945                                                    &lmm, lmm_size, "lov");
946                                 UNLOCK_INODE_MUTEX(inode);
947                         }
948                         if (rc)
949                                 CERROR("error on copy stripe info: rc = %d\n",
950                                         rc);
951                 }
952
953                 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
954                 mds_pack_inode2fid(&body->fid1, inode);
955                 mds_pack_inode2body(body, inode);
956         }
957         EXIT;
958
959 cleanup:
960         err = mds_finish_transno(mds, dir, handle, req, rc, 0, 0);
961
962         if (rc && created) {
963                 /* Destroy the file we just created.  This should not need
964                  * extra journal credits, as we have already modified all of
965                  * the blocks needed in order to create the file in the first
966                  * place.
967                  */
968                 switch (type) {
969                 case S_IFDIR:
970                         err = vfs_rmdir(dir, dchild);
971                         if (err)
972                                 CERROR("rmdir in error path: %d\n", err);
973                         break;
974                 default:
975                         err = vfs_unlink(dir, dchild);
976                         if (err)
977                                 CERROR("unlink in error path: %d\n", err);
978                         break;
979                 }
980         } else if (created) {
981                 /* The inode we were allocated may have just been freed
982                  * by an unlink operation.  We take this lock to
983                  * synchronize against the matching reply-ack-lock taken
984                  * in unlink, to avoid replay problems if this reply
985                  * makes it out to the client but the unlink's does not.
986                  * See bug 2029 for more detail.*/
987                 mds_lock_new_child(obd, dchild->d_inode, NULL);
988                 /* save uid/gid of create inode and parent */
989                 qpids[USRQUOTA] = dir->i_uid;
990                 qpids[GRPQUOTA] = dir->i_gid;
991         } else {
992                 rc = err;
993         }
994
995         switch (cleanup_phase) {
996         case 2: /* child dentry */
997                 l_dput(dchild);
998         case 1: /* locked parent dentry */
999                 if (rc) {
1000                         ldlm_lock_decref(&lockh, LCK_EX);
1001                 } else {
1002                         ptlrpc_save_lock (req, &lockh, LCK_EX);
1003                 }
1004                 l_dput(dparent);
1005         case 0:
1006                 break;
1007         default:
1008                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1009                 LBUG();
1010         }
1011         req->rq_status = rc;
1012
1013         /* trigger dqacq on the owner of child and parent */
1014         lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1015                       FSFILT_OP_CREATE);
1016         return 0;
1017 }
1018
1019 int res_gt(const struct ldlm_res_id *res1, const struct ldlm_res_id *res2,
1020            ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1021 {
1022         int i;
1023
1024         for (i = 0; i < RES_NAME_SIZE; i++) {
1025                 /* return 1 here, because enqueue_ordered will skip resources
1026                  * of all zeroes if they're sorted to the end of the list. */
1027                 if (res1->name[i] == 0 && res2->name[i] != 0)
1028                         return 1;
1029                 if (res2->name[i] == 0 && res1->name[i] != 0)
1030                         return 0;
1031
1032                 if (res1->name[i] > res2->name[i])
1033                         return 1;
1034                 if (res1->name[i] < res2->name[i])
1035                         return 0;
1036         }
1037         if (!p1 || !p2)
1038                 return 0;
1039         if (memcmp(p1, p2, sizeof(*p1)) < 0)
1040                 return 1;
1041         return 0;
1042 }
1043
1044 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1045  * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1046  * because they take the place of local semaphores.
1047  *
1048  * One or two locks are taken in numerical order.  A res_id->name[0] of 0 means
1049  * no lock is taken for that res_id.  Must be at least one non-zero res_id. */
1050 int enqueue_ordered_locks(struct obd_device *obd,
1051                           const struct ldlm_res_id *p1_res_id,
1052                           struct lustre_handle *p1_lockh, int p1_lock_mode,
1053                           ldlm_policy_data_t *p1_policy,
1054                           const struct ldlm_res_id *p2_res_id,
1055                           struct lustre_handle *p2_lockh, int p2_lock_mode,
1056                           ldlm_policy_data_t *p2_policy)
1057 {
1058         const struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1059         struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1060         int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1061         ldlm_policy_data_t *policies[2] = {p1_policy, p2_policy};
1062         int rc, flags;
1063         ENTRY;
1064
1065         LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1066
1067         CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1068                res_id[0]->name[0], res_id[1]->name[0]);
1069
1070         if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1071                 handles[1] = p1_lockh;
1072                 handles[0] = p2_lockh;
1073                 res_id[1] = p1_res_id;
1074                 res_id[0] = p2_res_id;
1075                 lock_modes[1] = p1_lock_mode;
1076                 lock_modes[0] = p2_lock_mode;
1077                 policies[1] = p1_policy;
1078                 policies[0] = p2_policy;
1079         }
1080
1081         CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1082                res_id[0]->name[0], res_id[1]->name[0]);
1083
1084         flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1085         rc = ldlm_cli_enqueue_local(obd->obd_namespace, res_id[0],
1086                                     LDLM_IBITS, policies[0], lock_modes[0],
1087                                     &flags, ldlm_blocking_ast,
1088                                     ldlm_completion_ast, NULL, NULL, 0,
1089                                     NULL, handles[0]);
1090         if (rc != ELDLM_OK)
1091                 RETURN(-EIO);
1092         ldlm_lock_dump_handle(D_OTHER, handles[0]);
1093
1094         if (memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) == 0 &&
1095             (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1096                 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1097                 ldlm_lock_addref(handles[1], lock_modes[1]);
1098         } else if (res_id[1]->name[0] != 0) {
1099                 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1100                 rc = ldlm_cli_enqueue_local(obd->obd_namespace, res_id[1],
1101                                             LDLM_IBITS, policies[1],
1102                                             lock_modes[1], &flags,
1103                                             ldlm_blocking_ast,
1104                                             ldlm_completion_ast, NULL, NULL,
1105                                             0, NULL, handles[1]);
1106                 if (rc != ELDLM_OK) {
1107                         ldlm_lock_decref(handles[0], lock_modes[0]);
1108                         RETURN(-EIO);
1109                 }
1110                 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1111         }
1112
1113         RETURN(0);
1114 }
1115
1116 static inline int res_eq(const struct ldlm_res_id *res1,
1117                          const struct ldlm_res_id *res2)
1118 {
1119         return !memcmp(res1, res2, sizeof(*res1));
1120 }
1121
1122 static inline void
1123 try_to_aggregate_locks(const struct ldlm_res_id *res1, ldlm_policy_data_t *p1,
1124                        const struct ldlm_res_id *res2, ldlm_policy_data_t *p2)
1125 {
1126         if (!res_eq(res1, res2))
1127                 return;
1128         /* XXX: any additional inodebits (to current LOOKUP and UPDATE)
1129          * should be taken with great care here */
1130         p1->l_inodebits.bits |= p2->l_inodebits.bits;
1131 }
1132
1133 int enqueue_4ordered_locks(struct obd_device *obd,
1134                            const struct ldlm_res_id *p1_res_id,
1135                            struct lustre_handle *p1_lockh, int p1_lock_mode,
1136                            ldlm_policy_data_t *p1_policy,
1137                            const struct ldlm_res_id *p2_res_id,
1138                            struct lustre_handle *p2_lockh, int p2_lock_mode,
1139                            ldlm_policy_data_t *p2_policy,
1140                            const struct ldlm_res_id *c1_res_id,
1141                            struct lustre_handle *c1_lockh, int c1_lock_mode,
1142                            ldlm_policy_data_t *c1_policy,
1143                            const struct ldlm_res_id *c2_res_id,
1144                            struct lustre_handle *c2_lockh, int c2_lock_mode,
1145                            ldlm_policy_data_t *c2_policy)
1146 {
1147         const struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1148                                                 c1_res_id, c2_res_id };
1149         struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1150                                                  c1_lockh, c2_lockh };
1151         int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1152                               c1_lock_mode, c2_lock_mode };
1153         ldlm_policy_data_t *policies[5] = {p1_policy, p2_policy,
1154                                            c1_policy, c2_policy};
1155         int rc, i, j, sorted, flags;
1156         ENTRY;
1157
1158         CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1159                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1160                res_id[3]->name[0]);
1161
1162         /* simple insertion sort - we have at most 4 elements */
1163         for (i = 1; i < 4; i++) {
1164                 j = i - 1;
1165                 dlm_handles[4] = dlm_handles[i];
1166                 res_id[4] = res_id[i];
1167                 lock_modes[4] = lock_modes[i];
1168                 policies[4] = policies[i];
1169
1170                 sorted = 0;
1171                 do {
1172                         if (res_gt(res_id[j], res_id[4], policies[j],
1173                                    policies[4])) {
1174                                 dlm_handles[j + 1] = dlm_handles[j];
1175                                 res_id[j + 1] = res_id[j];
1176                                 lock_modes[j + 1] = lock_modes[j];
1177                                 policies[j + 1] = policies[j];
1178                                 j--;
1179                         } else {
1180                                 sorted = 1;
1181                         }
1182                 } while (j >= 0 && !sorted);
1183
1184                 dlm_handles[j + 1] = dlm_handles[4];
1185                 res_id[j + 1] = res_id[4];
1186                 lock_modes[j + 1] = lock_modes[4];
1187                 policies[j + 1] = policies[4];
1188         }
1189
1190         CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1191                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1192                res_id[3]->name[0]);
1193
1194         /* XXX we could send ASTs on all these locks first before blocking? */
1195         for (i = 0; i < 4; i++) {
1196                 flags = LDLM_FL_ATOMIC_CB;
1197                 if (res_id[i]->name[0] == 0)
1198                         break;
1199                 if (i && res_eq(res_id[i], res_id[i-1])) {
1200                         memcpy(dlm_handles[i], dlm_handles[i-1],
1201                                sizeof(*(dlm_handles[i])));
1202                         ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1203                 } else {
1204                         /* we need to enqueue locks with different inodebits
1205                          * at once, because otherwise concurrent thread can
1206                          * hit the windown between these two locks and we'll
1207                          * get to deadlock. see bug 10360. note also, that it
1208                          * is impossible to have >2 equal res. */
1209                         if (i < 3)
1210                                 try_to_aggregate_locks(res_id[i], policies[i],
1211                                                        res_id[i+1], policies[i+1]);
1212                         rc = ldlm_cli_enqueue_local(obd->obd_namespace,
1213                                                     res_id[i], LDLM_IBITS,
1214                                                     policies[i], lock_modes[i],
1215                                                     &flags, ldlm_blocking_ast,
1216                                                     ldlm_completion_ast, NULL,
1217                                                     NULL, 0, NULL,
1218                                                     dlm_handles[i]);
1219                         if (rc != ELDLM_OK)
1220                                 GOTO(out_err, rc = -EIO);
1221                         ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1222                 }
1223         }
1224
1225         RETURN(0);
1226 out_err:
1227         while (i-- > 0)
1228                 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1229
1230         return rc;
1231 }
1232
1233 /* In the unlikely case that the child changed while we were waiting
1234  * on the lock, we need to drop the lock on the old child and either:
1235  * - if the child has a lower resource name, then we have to also
1236  *   drop the parent lock and regain the locks in the right order
1237  * - in the rename case, if the child has a lower resource name than one of
1238  *   the other parent/child resources (maxres) we also need to reget the locks
1239  * - if the child has a higher resource name (this is the common case)
1240  *   we can just get the lock on the new child (still in lock order)
1241  *
1242  * Returns 0 if the child did not change or if it changed but could be locked.
1243  * Returns 1 if the child changed and we need to re-lock (no locks held).
1244  * Returns -ve error with a valid dchild (no locks held). */
1245 static int mds_verify_child(struct obd_device *obd,
1246                             const struct ldlm_res_id *parent_res_id,
1247                             struct lustre_handle *parent_lockh,
1248                             struct dentry *dparent, int parent_mode,
1249                             struct ldlm_res_id *child_res_id,
1250                             struct lustre_handle *child_lockh,
1251                             struct dentry **dchildp, int child_mode,
1252                             ldlm_policy_data_t *child_policy,
1253                             const char *name, int namelen,
1254                             const struct ldlm_res_id *maxres)
1255 {
1256         struct dentry *vchild, *dchild = *dchildp;
1257         int rc = 0, cleanup_phase = 2; /* parent, child locks */
1258         ENTRY;
1259
1260         vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1261         if (IS_ERR(vchild))
1262                 GOTO(cleanup, rc = PTR_ERR(vchild));
1263
1264         if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1265                    (vchild->d_inode != NULL &&
1266                     child_res_id->name[0] == vchild->d_inode->i_ino &&
1267                     child_res_id->name[1] == vchild->d_inode->i_generation))) {
1268                 if (dchild != NULL)
1269                         l_dput(dchild);
1270                 *dchildp = vchild;
1271
1272                 RETURN(0);
1273         }
1274
1275         CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1276                vchild->d_inode, dchild ? dchild->d_inode : 0,
1277                vchild->d_inode ? vchild->d_inode->i_ino : 0,
1278                child_res_id->name[0]);
1279         if (child_res_id->name[0] != 0)
1280                 ldlm_lock_decref(child_lockh, child_mode);
1281         if (dchild)
1282                 l_dput(dchild);
1283
1284         cleanup_phase = 1; /* parent lock only */
1285         *dchildp = dchild = vchild;
1286
1287         if (dchild->d_inode) {
1288                 int flags = LDLM_FL_ATOMIC_CB;
1289                 child_res_id->name[0] = dchild->d_inode->i_ino;
1290                 child_res_id->name[1] = dchild->d_inode->i_generation;
1291
1292                 /* Make sure that we don't try to re-enqueue a lock on the
1293                  * same resource if it happens that the source is renamed to
1294                  * the target by another thread (bug 9974, thanks racer :-) */
1295                 if (!res_gt(child_res_id, parent_res_id, NULL, NULL) ||
1296                     !res_gt(child_res_id, maxres, NULL, NULL)) {
1297                         CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1298                                child_res_id->name[0], parent_res_id->name[0],
1299                                maxres->name[0]);
1300                         GOTO(cleanup, rc = 1);
1301                 }
1302
1303                 rc = ldlm_cli_enqueue_local(obd->obd_namespace, child_res_id,
1304                                             LDLM_IBITS, child_policy,
1305                                             child_mode, &flags,
1306                                             ldlm_blocking_ast,
1307                                             ldlm_completion_ast, NULL,
1308                                             NULL, 0, NULL, child_lockh);
1309                 if (rc != ELDLM_OK)
1310                         GOTO(cleanup, rc = -EIO);
1311         } else {
1312                 memset(child_res_id, 0, sizeof(*child_res_id));
1313         }
1314
1315         EXIT;
1316 cleanup:
1317         if (rc) {
1318                 switch(cleanup_phase) {
1319                 case 2:
1320                         if (child_res_id->name[0] != 0)
1321                                 ldlm_lock_decref(child_lockh, child_mode);
1322                 case 1:
1323                         ldlm_lock_decref(parent_lockh, parent_mode);
1324                 }
1325         }
1326         return rc;
1327 }
1328
1329 #define INODE_CTIME_AGE (10)
1330 #define INODE_CTIME_OLD(inode) (LTIME_S(inode->i_ctime) +               \
1331                                 INODE_CTIME_AGE < CURRENT_SECONDS)
1332
1333 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1334                                 struct ll_fid *fid,
1335                                 struct lustre_handle *parent_lockh,
1336                                 struct dentry **dparentp, int parent_mode,
1337                                 __u64 parent_lockpart,
1338                                 char *name, int namelen,
1339                                 struct lustre_handle *child_lockh,
1340                                 struct dentry **dchildp, int child_mode,
1341                                 __u64 child_lockpart)
1342 {
1343         struct ldlm_res_id child_res_id = { .name = {0} };
1344         struct ldlm_res_id parent_res_id = { .name = {0} };
1345         ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1346         ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1347         struct inode *inode;
1348         int rc = 0, cleanup_phase = 0;
1349         ENTRY;
1350
1351         /* Step 1: Lookup parent */
1352         *dparentp = mds_fid2dentry(mds, fid, NULL);
1353         if (IS_ERR(*dparentp)) {
1354                 rc = PTR_ERR(*dparentp);
1355                 *dparentp = NULL;
1356                 RETURN(rc);
1357         }
1358
1359         CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1360                (*dparentp)->d_inode->i_ino, name);
1361
1362         parent_res_id.name[0] = (*dparentp)->d_inode->i_ino;
1363         parent_res_id.name[1] = (*dparentp)->d_inode->i_generation;
1364
1365         cleanup_phase = 1; /* parent dentry */
1366
1367         /* Step 2: Lookup child (without DLM lock, to get resource name) */
1368         *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1369         if (IS_ERR(*dchildp)) {
1370                 rc = PTR_ERR(*dchildp);
1371                 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1372                 GOTO(cleanup, rc);
1373         }
1374
1375         cleanup_phase = 2; /* child dentry */
1376         inode = (*dchildp)->d_inode;
1377         if (inode != NULL) {
1378                 if (is_bad_inode(inode)) {
1379                         CERROR("bad inode returned %lu/%u\n",
1380                                inode->i_ino, inode->i_generation);
1381                         GOTO(cleanup, rc = -ENOENT);
1382                 }
1383                 inode = igrab(inode);
1384         }
1385         if (inode == NULL)
1386                 goto retry_locks;
1387
1388         child_res_id.name[0] = inode->i_ino;
1389         child_res_id.name[1] = inode->i_generation;
1390
1391         /* If we want a LCK_CR for a directory, and this directory has not been
1392            changed for some time, we return not only a LOOKUP lock, but also an
1393            UPDATE lock to have negative dentry starts working for this dir.
1394            Also we apply same logic to non-directories. If the file is rarely
1395            changed - we return both locks and this might save us RPC on
1396            later STAT. */
1397         if ((child_mode & (LCK_CR|LCK_PR|LCK_CW)) && INODE_CTIME_OLD(inode))
1398                 child_policy.l_inodebits.bits |= MDS_INODELOCK_UPDATE;
1399
1400         iput(inode);
1401
1402 retry_locks:
1403         cleanup_phase = 2; /* child dentry */
1404
1405         /* Step 3: Lock parent and child in resource order.  If child doesn't
1406          *         exist, we still have to lock the parent and re-lookup. */
1407         rc = enqueue_ordered_locks(obd,&parent_res_id,parent_lockh,parent_mode,
1408                                    &parent_policy,
1409                                    &child_res_id, child_lockh, child_mode,
1410                                    &child_policy);
1411         if (rc)
1412                 GOTO(cleanup, rc);
1413
1414         if (!(*dchildp)->d_inode)
1415                 cleanup_phase = 3; /* parent lock */
1416         else
1417                 cleanup_phase = 4; /* child lock */
1418
1419         /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1420         rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1421                               parent_mode, &child_res_id, child_lockh, dchildp,
1422                               child_mode,&child_policy, name, namelen, &parent_res_id);
1423         if (rc > 0)
1424                 goto retry_locks;
1425         if (rc < 0) {
1426                 cleanup_phase = 2;
1427                 GOTO(cleanup, rc);
1428         }
1429
1430 cleanup:
1431         if (rc) {
1432                 switch (cleanup_phase) {
1433                 case 4:
1434                         ldlm_lock_decref(child_lockh, child_mode);
1435                 case 3:
1436                         ldlm_lock_decref(parent_lockh, parent_mode);
1437                 case 2:
1438                         l_dput(*dchildp);
1439                 case 1:
1440                         l_dput(*dparentp);
1441                 default: ;
1442                 }
1443         }
1444         return rc;
1445 }
1446
1447 void mds_reconstruct_generic(struct ptlrpc_request *req)
1448 {
1449         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1450
1451         mds_req_from_mcd(req, med->med_mcd);
1452 }
1453
1454 /* If we are unlinking an open file/dir (i.e. creating an orphan) then
1455  * we instead link the inode into the PENDING directory until it is
1456  * finally released.  We can't simply call mds_reint_rename() or some
1457  * part thereof, because we don't have the inode to check for link
1458  * count/open status until after it is locked.
1459  *
1460  * For lock ordering, caller must get child->i_mutex first, then
1461  * pending->i_mutex before starting journal transaction.
1462  *
1463  * returns 1 on success
1464  * returns 0 if we lost a race and didn't make a new link
1465  * returns negative on error
1466  */
1467 static int mds_orphan_add_link(struct mds_update_record *rec,
1468                                struct obd_device *obd, struct dentry *dentry)
1469 {
1470         struct mds_obd *mds = &obd->u.mds;
1471         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1472         struct inode *inode = dentry->d_inode;
1473         struct dentry *pending_child;
1474         char fidname[LL_FID_NAMELEN];
1475         int fidlen = 0, rc, mode;
1476         ENTRY;
1477
1478         LASSERT(inode != NULL);
1479         LASSERT(!mds_inode_is_orphan(inode));
1480 #ifndef HAVE_I_ALLOC_SEM
1481         LASSERT(TRYLOCK_INODE_MUTEX(inode) == 0);
1482 #endif
1483         LASSERT(TRYLOCK_INODE_MUTEX(pending_dir) == 0);
1484
1485         fidlen = ll_fid2str(fidname, inode->i_ino, inode->i_generation);
1486
1487         CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1488                mds_orphan_open_count(inode), inode->i_nlink,
1489                S_ISDIR(inode->i_mode) ? "dir" :
1490                 S_ISREG(inode->i_mode) ? "file" : "other",rec->ur_name,fidname);
1491
1492         if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1493                 RETURN(0);
1494
1495         pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
1496         if (IS_ERR(pending_child))
1497                 RETURN(PTR_ERR(pending_child));
1498
1499         if (pending_child->d_inode != NULL) {
1500                 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1501                 LASSERT(pending_child->d_inode == inode);
1502                 GOTO(out_dput, rc = 0);
1503         }
1504
1505         /* link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG
1506          * for linking and return real mode back then -bzzz */
1507         mode = inode->i_mode;
1508         inode->i_mode = S_IFREG;
1509         rc = vfs_link(dentry, pending_dir, pending_child);
1510         if (rc)
1511                 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1512                        rec->ur_name, rc);
1513         else
1514                 mds_inode_set_orphan(inode);
1515
1516         /* return mode and correct i_nlink if inode is directory */
1517         inode->i_mode = mode;
1518         LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1519                  S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1520                  inode->i_nlink);
1521         if (S_ISDIR(mode)) {
1522                 inode->i_nlink++;
1523                 pending_dir->i_nlink++;
1524                 mark_inode_dirty(inode);
1525                 mark_inode_dirty(pending_dir);
1526         }
1527
1528         GOTO(out_dput, rc = 1);
1529 out_dput:
1530         l_dput(pending_child);
1531         RETURN(rc);
1532 }
1533
1534 int mds_get_cookie_size(struct obd_device *obd, struct lov_mds_md *lmm)
1535 {
1536         int count = le32_to_cpu(lmm->lmm_stripe_count);
1537         int real_csize = count * sizeof(struct llog_cookie);
1538         return real_csize;
1539 }
1540
1541 void mds_shrink_reply(struct obd_device *obd, struct ptlrpc_request *req,
1542                       struct mds_body *body, int md_off)
1543 {
1544         int cookie_size = 0, md_size = 0;
1545
1546         if (body && body->valid & OBD_MD_FLEASIZE) {
1547                 md_size = body->eadatasize;
1548         }
1549         if (body && body->valid & OBD_MD_FLCOOKIE) {
1550                 LASSERT(body->valid & OBD_MD_FLEASIZE);
1551                 cookie_size = mds_get_cookie_size(obd, lustre_msg_buf(
1552                                                   req->rq_repmsg, md_off, 0));
1553         }
1554
1555         CDEBUG(D_INFO, "Shrink to md_size %d cookie_size %d \n", md_size,
1556                cookie_size);
1557
1558         lustre_shrink_reply(req, md_off, md_size, 1);
1559
1560         lustre_shrink_reply(req, md_off + (md_size > 0), cookie_size, 0);
1561 }
1562
1563 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1564                             struct ptlrpc_request *req,
1565                             struct lustre_handle *lh)
1566 {
1567         struct dentry *dparent = NULL, *dchild;
1568         struct mds_obd *mds = mds_req2mds(req);
1569         struct obd_device *obd = req->rq_export->exp_obd;
1570         struct mds_body *body = NULL;
1571         struct inode *child_inode = NULL;
1572         struct lustre_handle parent_lockh, child_lockh, child_reuse_lockh;
1573         void *handle = NULL;
1574         int rc = 0, cleanup_phase = 0;
1575         unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1576         unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1577         ENTRY;
1578
1579         LASSERT(offset == REQ_REC_OFF); /*  || offset == DLM_INTENT_REC_OFF); */
1580         offset = REPLY_REC_OFF;
1581
1582         DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1583                   rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name);
1584
1585         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1586
1587         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1588                 GOTO(cleanup, rc = -ENOENT);
1589
1590         if (rec->ur_dlm)
1591                 ldlm_request_cancel(req, rec->ur_dlm, 0);
1592
1593         rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1,
1594                                          &parent_lockh, &dparent, LCK_EX,
1595                                          MDS_INODELOCK_UPDATE,
1596                                          rec->ur_name, rec->ur_namelen,
1597                                          &child_lockh, &dchild, LCK_EX,
1598                                          MDS_INODELOCK_FULL);
1599         if (rc)
1600                 GOTO(cleanup, rc);
1601
1602         cleanup_phase = 1; /* dchild, dparent, locks */
1603
1604         dget(dchild);
1605         child_inode = dchild->d_inode;
1606         if (child_inode == NULL) {
1607                 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1608                        dparent->d_inode->i_ino, rec->ur_name);
1609                 GOTO(cleanup, rc = -ENOENT);
1610         }
1611
1612         /* save uid/gid for quota acquire/release */
1613         qcids[USRQUOTA] = child_inode->i_uid;
1614         qcids[GRPQUOTA] = child_inode->i_gid;
1615         qpids[USRQUOTA] = dparent->d_inode->i_uid;
1616         qpids[GRPQUOTA] = dparent->d_inode->i_gid;
1617
1618         cleanup_phase = 2; /* dchild has a lock */
1619
1620         /* We have to do these checks ourselves, in case we are making an
1621          * orphan.  The client tells us whether rmdir() or unlink() was called,
1622          * so we need to return appropriate errors (bug 72). */
1623         if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1624                 if (!S_ISDIR(child_inode->i_mode))
1625                         GOTO(cleanup, rc = -ENOTDIR);
1626         } else {
1627                 if (S_ISDIR(child_inode->i_mode))
1628                         GOTO(cleanup, rc = -EISDIR);
1629         }
1630
1631         /* Check for EROFS after we check ENODENT, ENOTDIR, and EISDIR */
1632         if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
1633                 GOTO(cleanup, rc = -EROFS);
1634
1635         /* Step 3: Get a lock on the ino to sync with creation WRT inode
1636          * reuse (see bug 2029). */
1637         rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
1638         if (rc != ELDLM_OK)
1639                 GOTO(cleanup, rc);
1640
1641         cleanup_phase = 3; /* child inum lock */
1642
1643         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
1644
1645         /* ldlm_reply in buf[0] if called via intent */
1646         if (offset == DLM_INTENT_REC_OFF)
1647                 offset = DLM_REPLY_REC_OFF;
1648
1649         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
1650         LASSERT(body != NULL);
1651
1652         /* child orphan sem protects orphan_dec_test && is_orphan race */
1653         MDS_DOWN_READ_ORPHAN_SEM(child_inode);
1654         cleanup_phase = 4; /* MDS_UP_READ_ORPHAN_SEM(new_inode) when finished */
1655
1656         /* If this is potentially the last reference to this inode, get the
1657          * OBD EA data first so the client can destroy OST objects.  We
1658          * only do the object removal later if no open files/links remain. */
1659         if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
1660             child_inode->i_nlink == 1) {
1661                 if (mds_orphan_open_count(child_inode) > 0) {
1662                         /* need to lock pending_dir before transaction */
1663                         LOCK_INODE_MUTEX(mds->mds_pending_dir->d_inode);
1664                         cleanup_phase = 5; /* UNLOCK_INODE_MUTEX(mds->mds_pending_dir->d_inode); */
1665                 } else if (S_ISREG(child_inode->i_mode)) {
1666                         mds_pack_inode2fid(&body->fid1, child_inode);
1667                         mds_pack_inode2body(body, child_inode);
1668                         mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
1669                                     child_inode, MDS_PACK_MD_LOCK);
1670                 }
1671         }
1672
1673         /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
1674         switch (child_inode->i_mode & S_IFMT) {
1675         case S_IFDIR:
1676                 /* Drop any lingering child directories before we start our
1677                  * transaction, to avoid doing multiple inode dirty/delete
1678                  * in our compound transaction (bug 1321). */
1679                 shrink_dcache_parent(dchild);
1680                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1681                                       NULL);
1682                 if (IS_ERR(handle))
1683                         GOTO(cleanup, rc = PTR_ERR(handle));
1684                 rc = vfs_rmdir(dparent->d_inode, dchild);
1685                 mds_counter_incr(req->rq_export, LPROC_MDS_RMDIR);
1686                 break;
1687         case S_IFREG: {
1688                 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
1689                                                         offset + 1, 0);
1690                 handle = fsfilt_start_log(obd, dparent->d_inode,
1691                                           FSFILT_OP_UNLINK, NULL,
1692                                           le32_to_cpu(lmm->lmm_stripe_count));
1693                 if (IS_ERR(handle))
1694                         GOTO(cleanup, rc = PTR_ERR(handle));
1695                 rc = vfs_unlink(dparent->d_inode, dchild);
1696                 mds_counter_incr(req->rq_export, LPROC_MDS_UNLINK);
1697                 break;
1698         }
1699         case S_IFLNK:
1700         case S_IFCHR:
1701         case S_IFBLK:
1702         case S_IFIFO:
1703         case S_IFSOCK:
1704                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
1705                                       NULL);
1706                 if (IS_ERR(handle))
1707                         GOTO(cleanup, rc = PTR_ERR(handle));
1708                 rc = vfs_unlink(dparent->d_inode, dchild);
1709                 mds_counter_incr(req->rq_export, LPROC_MDS_UNLINK);
1710                 break;
1711         default:
1712                 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
1713                        rec->ur_name);
1714                 LBUG();
1715                 GOTO(cleanup, rc = -EINVAL);
1716         }
1717
1718         if (rc == 0 && child_inode->i_nlink == 0) {
1719                 if (mds_orphan_open_count(child_inode) > 0)
1720                         rc = mds_orphan_add_link(rec, obd, dchild);
1721
1722                 if (rc == 1)
1723                         GOTO(cleanup, rc = 0);
1724
1725                 if (!S_ISREG(child_inode->i_mode))
1726                         GOTO(cleanup, rc);
1727
1728                 if (!(body->valid & OBD_MD_FLEASIZE)) {
1729                         body->valid |=(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1730                                        OBD_MD_FLATIME | OBD_MD_FLMTIME);
1731                 } else if (mds_log_op_unlink(obd,
1732                                 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
1733                                 lustre_msg_buflen(req->rq_repmsg, offset + 1),
1734                                 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
1735                                 lustre_msg_buflen(req->rq_repmsg, offset+2)) >
1736                            0) {
1737                         body->valid |= OBD_MD_FLCOOKIE;
1738                 }
1739         }
1740
1741         GOTO(cleanup, rc);
1742 cleanup:
1743         if (rc == 0) {
1744                 struct iattr iattr;
1745                 int err;
1746
1747                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1748                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1749                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1750
1751                 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1752                 if (err)
1753                         CERROR("error on parent setattr: rc = %d\n", err);
1754         }
1755
1756         rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
1757                                 handle, req, rc, 0, 0);
1758         if (!rc)
1759                 (void)obd_set_info_async(mds->mds_osc_exp, strlen("unlinked"),
1760                                          "unlinked", 0, NULL, NULL);
1761         switch(cleanup_phase) {
1762         case 5: /* pending_dir semaphore */
1763                 UNLOCK_INODE_MUTEX(mds->mds_pending_dir->d_inode);
1764         case 4: /* child inode semaphore */
1765                 MDS_UP_READ_ORPHAN_SEM(child_inode);
1766         case 3: /* child ino-reuse lock */
1767                 if (rc && body != NULL) {
1768                         // Don't unlink the OST objects if the MDS unlink failed
1769                         body->valid = 0;
1770                 }
1771                 if (rc)
1772                         ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
1773                 else
1774                         ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
1775         case 2: /* child lock */
1776                 ldlm_lock_decref(&child_lockh, LCK_EX);
1777         case 1: /* child and parent dentry, parent lock */
1778                 if (rc)
1779                         ldlm_lock_decref(&parent_lockh, LCK_EX);
1780                 else
1781                         ptlrpc_save_lock(req, &parent_lockh, LCK_EX);
1782                 l_dput(dchild);
1783                 l_dput(dchild);
1784                 l_dput(dparent);
1785         case 0:
1786                 break;
1787         default:
1788                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1789                 LBUG();
1790         }
1791         req->rq_status = rc;
1792
1793         mds_shrink_reply(obd, req, body, REPLY_REC_OFF + 1);
1794
1795         /* trigger dqrel on the owner of child and parent */
1796         lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc, FSFILT_OP_UNLINK);
1797         return 0;
1798 }
1799
1800 static int mds_reint_link(struct mds_update_record *rec, int offset,
1801                           struct ptlrpc_request *req,
1802                           struct lustre_handle *lh)
1803 {
1804         struct obd_device *obd = req->rq_export->exp_obd;
1805         struct dentry *de_src = NULL;
1806         struct dentry *de_tgt_dir = NULL;
1807         struct dentry *dchild = NULL;
1808         struct mds_obd *mds = mds_req2mds(req);
1809         struct lustre_handle *handle = NULL, tgt_dir_lockh, src_lockh;
1810         struct ldlm_res_id src_res_id = { .name = {0} };
1811         struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
1812         ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
1813         ldlm_policy_data_t tgt_dir_policy =
1814                                        {.l_inodebits = {MDS_INODELOCK_UPDATE}};
1815         int rc = 0, cleanup_phase = 0;
1816         ENTRY;
1817
1818         LASSERT(offset == REQ_REC_OFF);
1819
1820         DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
1821                   rec->ur_fid1->id, rec->ur_fid1->generation,
1822                   rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_name);
1823         mds_counter_incr(req->rq_export, LPROC_MDS_LINK);
1824
1825         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1826
1827         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1828                 GOTO(cleanup, rc = -ENOENT);
1829
1830         if (rec->ur_dlm)
1831                 ldlm_request_cancel(req, rec->ur_dlm, 0);
1832
1833         /* Step 1: Lookup the source inode and target directory by FID */
1834         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1835         if (IS_ERR(de_src))
1836                 GOTO(cleanup, rc = PTR_ERR(de_src));
1837
1838         cleanup_phase = 1; /* source dentry */
1839
1840         de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
1841         if (IS_ERR(de_tgt_dir)) {
1842                 rc = PTR_ERR(de_tgt_dir);
1843                 de_tgt_dir = NULL;
1844                 GOTO(cleanup, rc);
1845         }
1846
1847         cleanup_phase = 2; /* target directory dentry */
1848
1849         CDEBUG(D_INODE, "linking %.*s/%s to inode %lu\n",
1850                de_tgt_dir->d_name.len, de_tgt_dir->d_name.name, rec->ur_name,
1851                de_src->d_inode->i_ino);
1852
1853         /* Step 2: Take the two locks */
1854         src_res_id.name[0] = de_src->d_inode->i_ino;
1855         src_res_id.name[1] = de_src->d_inode->i_generation;
1856         tgt_dir_res_id.name[0] = de_tgt_dir->d_inode->i_ino;
1857         tgt_dir_res_id.name[1] = de_tgt_dir->d_inode->i_generation;
1858
1859         rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
1860                                    &src_policy,
1861                                    &tgt_dir_res_id, &tgt_dir_lockh, LCK_EX,
1862                                    &tgt_dir_policy);
1863         if (rc)
1864                 GOTO(cleanup, rc);
1865
1866         cleanup_phase = 3; /* locks */
1867
1868         if (mds_inode_is_orphan(de_src->d_inode)) {
1869                 CDEBUG(D_INODE, "an attempt to link an orphan inode %lu/%u\n",
1870                        de_src->d_inode->i_ino,
1871                        de_src->d_inode->i_generation);
1872                 GOTO(cleanup, rc = -ENOENT);
1873         }
1874
1875         /* Step 3: Lookup the child */
1876         dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen-1);
1877         if (IS_ERR(dchild)) {
1878                 rc = PTR_ERR(dchild);
1879                 if (rc != -EPERM && rc != -EACCES && rc != -ENAMETOOLONG)
1880                         CERROR("child lookup error %d\n", rc);
1881                 GOTO(cleanup, rc);
1882         }
1883
1884         cleanup_phase = 4; /* child dentry */
1885
1886         if (dchild->d_inode) {
1887                 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
1888                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
1889                 rc = -EEXIST;
1890                 GOTO(cleanup, rc);
1891         }
1892
1893         /* Step 4: Do it. */
1894         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1895
1896         if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
1897                 GOTO(cleanup, rc = -EROFS);
1898
1899         handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
1900         if (IS_ERR(handle))
1901                 GOTO(cleanup, rc = PTR_ERR(handle));
1902
1903         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
1904         if (rc && rc != -EPERM && rc != -EACCES)
1905                 CERROR("vfs_link error %d\n", rc);
1906 cleanup:
1907         rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
1908                                 handle, req, rc, 0, 0);
1909         EXIT;
1910
1911         switch (cleanup_phase) {
1912         case 4: /* child dentry */
1913                 l_dput(dchild);
1914         case 3: /* locks */
1915                 if (rc) {
1916                         ldlm_lock_decref(&src_lockh, LCK_EX);
1917                         ldlm_lock_decref(&tgt_dir_lockh, LCK_EX);
1918                 } else {
1919                         ptlrpc_save_lock(req, &src_lockh, LCK_EX);
1920                         ptlrpc_save_lock(req, &tgt_dir_lockh, LCK_EX);
1921                 }
1922         case 2: /* target dentry */
1923                 l_dput(de_tgt_dir);
1924         case 1: /* source dentry */
1925                 l_dput(de_src);
1926         case 0:
1927                 break;
1928         default:
1929                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1930                 LBUG();
1931         }
1932         req->rq_status = rc;
1933         return 0;
1934 }
1935
1936 /* The idea here is that we need to get four locks in the end:
1937  * one on each parent directory, one on each child.  We need to take
1938  * these locks in some kind of order (to avoid deadlocks), and the order
1939  * I selected is "increasing resource number" order.  We need to look up
1940  * the children, however, before we know what the resource number(s) are.
1941  * Thus the following plan:
1942  *
1943  * 1,2. Look up the parents
1944  * 3,4. Look up the children
1945  * 5. Take locks on the parents and children, in order
1946  * 6. Verify that the children haven't changed since they were looked up
1947  *
1948  * If there was a race and the children changed since they were first looked
1949  * up, it is possible that mds_verify_child() will be able to just grab the
1950  * lock on the new child resource (if it has a higher resource than any other)
1951  * but we need to compare against not only its parent, but also against the
1952  * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
1953  *
1954  * We need the fancy igrab() on the child inodes because we aren't holding a
1955  * lock on the parent after the lookup is done, so dentry->d_inode may change
1956  * at any time, and igrab() itself doesn't like getting passed a NULL argument.
1957  */
1958 int mds_get_parents_children_locked(struct obd_device *obd,
1959                                     struct mds_obd *mds,
1960                                     struct ll_fid *p1_fid,
1961                                     struct dentry **de_srcdirp,
1962                                     struct ll_fid *p2_fid,
1963                                     struct dentry **de_tgtdirp,
1964                                     int parent_mode,
1965                                     const char *old_name, int old_len,
1966                                     struct dentry **de_oldp,
1967                                     const char *new_name, int new_len,
1968                                     struct dentry **de_newp,
1969                                     struct lustre_handle *dlm_handles,
1970                                     int child_mode)
1971 {
1972         struct ldlm_res_id p1_res_id = { .name = {0} };
1973         struct ldlm_res_id p2_res_id = { .name = {0} };
1974         struct ldlm_res_id c1_res_id = { .name = {0} };
1975         struct ldlm_res_id c2_res_id = { .name = {0} };
1976         ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
1977         /* Only dentry should disappear, but the inode itself would be
1978            intact otherwise. */
1979         ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
1980         /* If something is going to be replaced, both dentry and inode locks are           needed */
1981         ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_FULL}};
1982         struct ldlm_res_id *maxres_src, *maxres_tgt;
1983         struct inode *inode;
1984         int rc = 0, cleanup_phase = 0;
1985         ENTRY;
1986
1987         /* Step 1: Lookup the source directory */
1988         *de_srcdirp = mds_fid2dentry(mds, p1_fid, NULL);
1989         if (IS_ERR(*de_srcdirp))
1990                 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
1991
1992         cleanup_phase = 1; /* source directory dentry */
1993
1994         p1_res_id.name[0] = (*de_srcdirp)->d_inode->i_ino;
1995         p1_res_id.name[1] = (*de_srcdirp)->d_inode->i_generation;
1996
1997         /* Step 2: Lookup the target directory */
1998         if (memcmp(p1_fid, p2_fid, sizeof(*p1_fid)) == 0) {
1999                 *de_tgtdirp = dget(*de_srcdirp);
2000         } else {
2001                 *de_tgtdirp = mds_fid2dentry(mds, p2_fid, NULL);
2002                 if (IS_ERR(*de_tgtdirp)) {
2003                         rc = PTR_ERR(*de_tgtdirp);
2004                         *de_tgtdirp = NULL;
2005                         GOTO(cleanup, rc);
2006                 }
2007         }
2008
2009         cleanup_phase = 2; /* target directory dentry */
2010
2011         p2_res_id.name[0] = (*de_tgtdirp)->d_inode->i_ino;
2012         p2_res_id.name[1] = (*de_tgtdirp)->d_inode->i_generation;
2013
2014         /* Step 3: Lookup the source child entry */
2015         *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp, old_len - 1);
2016         if (IS_ERR(*de_oldp)) {
2017                 rc = PTR_ERR(*de_oldp);
2018                 CDEBUG(D_INODE, "old child lookup error (%.*s): %d\n",
2019                        old_len - 1, old_name, rc);
2020                 GOTO(cleanup, rc);
2021         }
2022
2023         cleanup_phase = 3; /* original name dentry */
2024
2025         inode = (*de_oldp)->d_inode;
2026         if (inode != NULL)
2027                 inode = igrab(inode);
2028         if (inode == NULL)
2029                 GOTO(cleanup, rc = -ENOENT);
2030
2031         c1_res_id.name[0] = inode->i_ino;
2032         c1_res_id.name[1] = inode->i_generation;
2033
2034         iput(inode);
2035
2036         /* Step 4: Lookup the target child entry */
2037         if (!new_name)
2038                 GOTO(retry_locks, rc);
2039         *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp, new_len - 1);
2040         if (IS_ERR(*de_newp)) {
2041                 rc = PTR_ERR(*de_newp);
2042                 if (rc != -ENAMETOOLONG)
2043                 CERROR("new child lookup error (%.*s): %d\n",
2044                        old_len - 1, old_name, rc);
2045                 GOTO(cleanup, rc);
2046         }
2047
2048         cleanup_phase = 4; /* target dentry */
2049
2050         inode = (*de_newp)->d_inode;
2051         if (inode != NULL)
2052                 inode = igrab(inode);
2053         if (inode == NULL)
2054                 goto retry_locks;
2055
2056         c2_res_id.name[0] = inode->i_ino;
2057         c2_res_id.name[1] = inode->i_generation;
2058         iput(inode);
2059
2060 retry_locks:
2061         /* Step 5: Take locks on the parents and child(ren) */
2062         maxres_src = &p1_res_id;
2063         maxres_tgt = &p2_res_id;
2064         cleanup_phase = 4; /* target dentry */
2065
2066         if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id,NULL,NULL))
2067                 maxres_src = &c1_res_id;
2068         if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id,NULL,NULL))
2069                 maxres_tgt = &c2_res_id;
2070
2071         rc = enqueue_4ordered_locks(obd, &p1_res_id,&dlm_handles[0],parent_mode,
2072                                     &p_policy,
2073                                     &p2_res_id, &dlm_handles[1], parent_mode,
2074                                     &p_policy,
2075                                     &c1_res_id, &dlm_handles[2], child_mode,
2076                                     &c1_policy,
2077                                     &c2_res_id, &dlm_handles[3], child_mode,
2078                                     &c2_policy);
2079         if (rc)
2080                 GOTO(cleanup, rc);
2081
2082         cleanup_phase = 6; /* parent and child(ren) locks */
2083
2084         /* Step 6a: Re-lookup source child to verify it hasn't changed */
2085         rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2086                               parent_mode, &c1_res_id, &dlm_handles[2], de_oldp,
2087                               child_mode, &c1_policy, old_name, old_len,
2088                               maxres_tgt);
2089         if (rc) {
2090                 if (c2_res_id.name[0] != 0)
2091                         ldlm_lock_decref(&dlm_handles[3], child_mode);
2092                 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2093                 cleanup_phase = 4;
2094                 if (rc > 0)
2095                         goto retry_locks;
2096                 GOTO(cleanup, rc);
2097         }
2098
2099         if ((*de_oldp)->d_inode == NULL)
2100                 GOTO(cleanup, rc = -ENOENT);
2101
2102         if (!new_name)
2103                 GOTO(cleanup, rc);
2104         /* Step 6b: Re-lookup target child to verify it hasn't changed */
2105         rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2106                               parent_mode, &c2_res_id, &dlm_handles[3], de_newp,
2107                               child_mode, &c2_policy, new_name, new_len,
2108                               maxres_src);
2109         if (rc) {
2110                 ldlm_lock_decref(&dlm_handles[2], child_mode);
2111                 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2112                 cleanup_phase = 4;
2113                 if (rc > 0)
2114                         goto retry_locks;
2115                 GOTO(cleanup, rc);
2116         }
2117
2118         EXIT;
2119 cleanup:
2120         if (rc) {
2121                 switch (cleanup_phase) {
2122                 case 6: /* child lock(s) */
2123                         if (c2_res_id.name[0] != 0)
2124                                 ldlm_lock_decref(&dlm_handles[3], child_mode);
2125                         if (c1_res_id.name[0] != 0)
2126                                 ldlm_lock_decref(&dlm_handles[2], child_mode);
2127                 case 5: /* parent locks */
2128                         ldlm_lock_decref(&dlm_handles[1], parent_mode);
2129                         ldlm_lock_decref(&dlm_handles[0], parent_mode);
2130                 case 4: /* target dentry */
2131                         l_dput(*de_newp);
2132                 case 3: /* source dentry */
2133                         l_dput(*de_oldp);
2134                 case 2: /* target directory dentry */
2135                         l_dput(*de_tgtdirp);
2136                 case 1: /* source directry dentry */
2137                         l_dput(*de_srcdirp);
2138                 }
2139         }
2140
2141         return rc;
2142 }
2143
2144 static int mds_reint_rename(struct mds_update_record *rec, int offset,
2145                             struct ptlrpc_request *req,
2146                             struct lustre_handle *lockh)
2147 {
2148         struct obd_device *obd = req->rq_export->exp_obd;
2149         struct dentry *de_srcdir = NULL;
2150         struct dentry *de_tgtdir = NULL;
2151         struct dentry *de_old = NULL;
2152         struct dentry *de_new = NULL;
2153         struct inode *old_inode = NULL, *new_inode = NULL;
2154         struct mds_obd *mds = mds_req2mds(req);
2155         struct lustre_handle dlm_handles[4];
2156         struct mds_body *body = NULL;
2157         struct lov_mds_md *lmm = NULL;
2158         int rc = 0, lock_count = 3, cleanup_phase = 0;
2159         void *handle = NULL;
2160         unsigned int qcids[MAXQUOTAS] = { 0, 0 };
2161         unsigned int qpids[4] = { 0, 0, 0, 0 };
2162         ENTRY;
2163
2164         LASSERT(offset == REQ_REC_OFF);
2165         offset = REPLY_REC_OFF;
2166
2167         DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u %s to "LPU64"/%u %s",
2168                   rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name,
2169                   rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_tgt);
2170         mds_counter_incr(req->rq_export, LPROC_MDS_RENAME);
2171
2172         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2173
2174         if (rec->ur_dlm)
2175                 ldlm_request_cancel(req, rec->ur_dlm, 0);
2176
2177         rc = mds_get_parents_children_locked(obd, mds, rec->ur_fid1, &de_srcdir,
2178                                              rec->ur_fid2, &de_tgtdir, LCK_EX,
2179                                              rec->ur_name, rec->ur_namelen,
2180                                              &de_old, rec->ur_tgt,
2181                                              rec->ur_tgtlen, &de_new,
2182                                              dlm_handles, LCK_EX);
2183         if (rc)
2184                 GOTO(cleanup, rc);
2185
2186         cleanup_phase = 1; /* parent(s), children, locks */
2187
2188         old_inode = de_old->d_inode;
2189         new_inode = de_new->d_inode;
2190
2191         if (new_inode != NULL)
2192                 lock_count = 4;
2193
2194         /* sanity check for src inode */
2195         if (old_inode->i_ino == de_srcdir->d_inode->i_ino ||
2196             old_inode->i_ino == de_tgtdir->d_inode->i_ino)
2197                 GOTO(cleanup, rc = -EINVAL);
2198
2199         if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2200                 GOTO(cleanup, rc = -EROFS);
2201
2202         if (new_inode == NULL)
2203                 goto no_unlink;
2204
2205         igrab(new_inode);
2206         cleanup_phase = 2; /* iput(new_inode) when finished */
2207
2208         /* sanity check for dest inode */
2209         if (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
2210             new_inode->i_ino == de_tgtdir->d_inode->i_ino)
2211                 GOTO(cleanup, rc = -EINVAL);
2212
2213         if (old_inode == new_inode)
2214                 GOTO(cleanup, rc = 0);
2215
2216         /* save uids/gids for qunit acquire/release */
2217         qcids[USRQUOTA] = old_inode->i_uid;
2218         qcids[GRPQUOTA] = old_inode->i_gid;
2219         qpids[USRQUOTA] = de_tgtdir->d_inode->i_uid;
2220         qpids[GRPQUOTA] = de_tgtdir->d_inode->i_gid;
2221         qpids[2] = de_srcdir->d_inode->i_uid;
2222         qpids[3] = de_srcdir->d_inode->i_gid;
2223
2224         /* if we are about to remove the target at first, pass the EA of
2225          * that inode to client to perform and cleanup on OST */
2226         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
2227         LASSERT(body != NULL);
2228
2229         /* child orphan sem protects orphan_dec_test && is_orphan race */
2230         MDS_DOWN_READ_ORPHAN_SEM(new_inode);
2231         cleanup_phase = 3; /* MDS_UP_READ_ORPHAN_SEM(new_inode) when finished */
2232
2233         if ((S_ISDIR(new_inode->i_mode) && new_inode->i_nlink == 2) ||
2234             new_inode->i_nlink == 1) {
2235                 if (mds_orphan_open_count(new_inode) > 0) {
2236                         /* need to lock pending_dir before transaction */
2237                         LOCK_INODE_MUTEX(mds->mds_pending_dir->d_inode);
2238                         cleanup_phase = 4; /* UNLOCK_INODE_MUTEX(mds->mds_pending_dir->d_inode); */
2239                 } else if (S_ISREG(new_inode->i_mode)) {
2240                         mds_pack_inode2fid(&body->fid1, new_inode);
2241                         mds_pack_inode2body(body, new_inode);
2242                         mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
2243                                     new_inode, MDS_PACK_MD_LOCK);
2244                 }
2245         }
2246
2247 no_unlink:
2248         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
2249                        de_srcdir->d_inode->i_sb);
2250
2251         /* Check if we are moving old entry into its child. 2.6 does not
2252            check for this in vfs_rename() anymore */
2253         if (is_subdir(de_new, de_old))
2254                 GOTO(cleanup, rc = -EINVAL);
2255
2256         lmm = lustre_msg_buf(req->rq_repmsg, offset + 1, 0);
2257         handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
2258                                   NULL, le32_to_cpu(lmm->lmm_stripe_count));
2259
2260         if (IS_ERR(handle))
2261                 GOTO(cleanup, rc = PTR_ERR(handle));
2262
2263         lock_kernel();
2264         de_old->d_fsdata = req;
2265         de_new->d_fsdata = req;
2266
2267         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
2268         unlock_kernel();
2269
2270         if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
2271                 if (mds_orphan_open_count(new_inode) > 0)
2272                         rc = mds_orphan_add_link(rec, obd, de_new);
2273
2274                 if (rc == 1)
2275                         GOTO(cleanup, rc = 0);
2276
2277                 if (!S_ISREG(new_inode->i_mode))
2278                         GOTO(cleanup, rc);
2279
2280                 if (!(body->valid & OBD_MD_FLEASIZE)) {
2281                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2282                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
2283                 } else if (mds_log_op_unlink(obd,
2284                                              lustre_msg_buf(req->rq_repmsg,
2285                                                             offset + 1, 0),
2286                                              lustre_msg_buflen(req->rq_repmsg,
2287                                                                offset + 1),
2288                                              lustre_msg_buf(req->rq_repmsg,
2289                                                             offset + 2, 0),
2290                                              lustre_msg_buflen(req->rq_repmsg,
2291                                                                offset + 2))
2292                            > 0) {
2293                         body->valid |= OBD_MD_FLCOOKIE;
2294                 }
2295         }
2296
2297         GOTO(cleanup, rc);
2298 cleanup:
2299         rc = mds_finish_transno(mds, de_tgtdir ? de_tgtdir->d_inode : NULL,
2300                                 handle, req, rc, 0, 0);
2301
2302         switch (cleanup_phase) {
2303         case 4:
2304                 UNLOCK_INODE_MUTEX(mds->mds_pending_dir->d_inode);
2305         case 3:
2306                 MDS_UP_READ_ORPHAN_SEM(new_inode);
2307         case 2:
2308                 iput(new_inode);
2309         case 1:
2310                 if (rc) {
2311                         if (lock_count == 4)
2312                                 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
2313                         ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
2314                         ldlm_lock_decref(&(dlm_handles[1]), LCK_EX);
2315                         ldlm_lock_decref(&(dlm_handles[0]), LCK_EX);
2316                 } else {
2317                         if (lock_count == 4)
2318                                 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
2319                         ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
2320                         ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_EX);
2321                         ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_EX);
2322                 }
2323                 l_dput(de_new);
2324                 l_dput(de_old);
2325                 l_dput(de_tgtdir);
2326                 l_dput(de_srcdir);
2327         case 0:
2328                 break;
2329         default:
2330                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2331                 LBUG();
2332         }
2333         req->rq_status = rc;
2334
2335         /* acquire/release qunit */
2336         lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc, FSFILT_OP_RENAME);
2337         return 0;
2338 }
2339
2340 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
2341                            struct ptlrpc_request *, struct lustre_handle *);
2342
2343 static mds_reinter reinters[REINT_MAX] = {
2344         [REINT_SETATTR] mds_reint_setattr,
2345         [REINT_CREATE] mds_reint_create,
2346         [REINT_LINK] mds_reint_link,
2347         [REINT_UNLINK] mds_reint_unlink,
2348         [REINT_RENAME] mds_reint_rename,
2349         [REINT_OPEN] mds_open
2350 };
2351
2352 int mds_reint_rec(struct mds_update_record *rec, int offset,
2353                   struct ptlrpc_request *req, struct lustre_handle *lockh)
2354 {
2355         struct obd_device *obd = req->rq_export->exp_obd;
2356 #if 0
2357         struct mds_obd *mds = &obd->u.mds;
2358 #ifdef CRAY_XT3
2359         gid_t fsgid = rec->ur_uc.luc_fsgid;
2360 #endif
2361 #endif
2362         struct lvfs_run_ctxt saved;
2363         int rc;
2364         ENTRY;
2365
2366 #ifdef CRAY_XT3
2367         if (req->rq_uid != LNET_UID_ANY) {
2368                 /* non-root local cluster client
2369                  * NB root's creds are believed... */
2370                 LASSERT (req->rq_uid != 0);
2371                 rec->ur_uc.luc_fsuid = req->rq_uid;
2372                 rec->ur_uc.luc_cap = 0;
2373         }
2374 #endif
2375
2376 #if 0
2377         /* get group info of this user */
2378         rec->ur_uc.luc_uce = upcall_cache_get_entry(mds->mds_group_hash,
2379                                                     rec->ur_uc.luc_fsuid,
2380                                                     rec->ur_uc.luc_fsgid, 2,
2381                                                     &rec->ur_uc.luc_suppgid1);
2382
2383         if (IS_ERR(rec->ur_uc.luc_uce)) {
2384                 rc = PTR_ERR(rec->ur_uc.luc_uce);
2385                 rec->ur_uc.luc_uce = NULL;
2386                 RETURN(rc);
2387         }
2388
2389         /* checked by unpacker */
2390         LASSERT(rec->ur_opcode < REINT_MAX && reinters[rec->ur_opcode] != NULL);
2391
2392 #ifdef CRAY_XT3
2393         if (rec->ur_uc.luc_uce)
2394                 rec->ur_uc.luc_fsgid = rec->ur_uc.luc_uce->ue_primary;
2395 #endif
2396 #endif
2397
2398         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
2399
2400 #if 0
2401 #ifdef CRAY_XT3
2402         if (rec->ur_uc.luc_uce && fsgid != rec->ur_uc.luc_fsgid &&
2403             in_group_p(fsgid)) {
2404                 rec->ur_uc.luc_fsgid = fsgid;
2405                 current->fsgid = saved.luc.luc_fsgid = fsgid;
2406         }
2407 #endif
2408 #endif
2409
2410         rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
2411         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
2412
2413 #if 0
2414         upcall_cache_put_entry(mds->mds_group_hash, rec->ur_uc.luc_uce);
2415 #endif
2416         RETURN(rc);
2417 }