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