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