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