Whamcloud - gitweb
b=21147 be tolerant to setting the same type and version of quota
[fs/lustre-release.git] / lustre / mds / mds_open.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_open.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Andreas Dilger <adilger@clusterfs.com>
40  * Author: Phil Schwan <phil@clusterfs.com>
41  * Author: Mike Shaver <shaver@clusterfs.com>
42  */
43
44 #ifndef EXPORT_SYMTAB
45 # define EXPORT_SYMTAB
46 #endif
47 #define DEBUG_SUBSYSTEM S_MDS
48
49 #include <linux/version.h>
50 #include <linux/module.h>
51 #include <linux/init.h>
52 #include <linux/version.h>
53 #include <linux/buffer_head.h>
54 #include <linux/workqueue.h>
55
56 #include <obd_class.h>
57 #include <obd_lov.h>
58 #include <lustre_fsfilt.h>
59 #include <lprocfs_status.h>
60
61 #include "mds_internal.h"
62
63 /* Exported function from this file are:
64  *
65  * mds_open - called by the intent handler
66  * mds_close - an rpc handling function
67  * mds_pin - an rpc handling function - which will go away
68  * mds_mfd_close - for force closing files when a client dies
69  */
70
71 /*
72  * MDS file data handling: file data holds a handle for a file opened
73  * by a client.
74  */
75
76 static void mds_mfd_addref(void *mfdp)
77 {
78         struct mds_file_data *mfd = mfdp;
79
80         atomic_inc(&mfd->mfd_refcount);
81         CDEBUG(D_INFO, "GETting mfd %p : new refcount %d\n", mfd,
82                atomic_read(&mfd->mfd_refcount));
83 }
84
85 /* Create a new mds_file_data struct.
86  * One reference for handle+med_open_head list and dropped by mds_mfd_unlink(),
87  * one reference for the caller of this function. */
88 struct mds_file_data *mds_mfd_new(void)
89 {
90         struct mds_file_data *mfd;
91
92         OBD_ALLOC(mfd, sizeof *mfd);
93         if (mfd == NULL) {
94                 CERROR("mds: out of memory\n");
95                 return NULL;
96         }
97
98         atomic_set(&mfd->mfd_refcount, 2);
99
100         INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
101         INIT_LIST_HEAD(&mfd->mfd_list);
102         class_handle_hash(&mfd->mfd_handle, mds_mfd_addref);
103
104         return mfd;
105 }
106
107 /* Get a new reference on the mfd pointed to by handle, if handle is still
108  * valid.  Caller must drop reference with mds_mfd_put(). */
109 static struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
110 {
111         ENTRY;
112         LASSERT(handle != NULL);
113         RETURN(class_handle2object(handle->cookie));
114 }
115
116 /* Drop mfd reference, freeing struct if this is the last one. */
117 static void mds_mfd_put(struct mds_file_data *mfd)
118 {
119         CDEBUG(D_INFO, "PUTting mfd %p : new refcount %d\n", mfd,
120                atomic_read(&mfd->mfd_refcount) - 1);
121         LASSERT(atomic_read(&mfd->mfd_refcount) > 0 &&
122                 atomic_read(&mfd->mfd_refcount) < 0x5a5a);
123         if (atomic_dec_and_test(&mfd->mfd_refcount)) {
124                 OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
125         }
126 }
127
128 /* Remove the mfd handle so that it cannot be found by open/close again.
129  * Caller must hold med_open_lock for mfd_list manipulation. */
130 void mds_mfd_unlink(struct mds_file_data *mfd, int decref)
131 {
132         class_handle_unhash(&mfd->mfd_handle);
133         list_del_init(&mfd->mfd_list);
134         if (decref)
135                 mds_mfd_put(mfd);
136 }
137
138 /* Caller must hold mds->mds_epoch_sem */
139 static int mds_alloc_filterdata(struct inode *inode)
140 {
141         LASSERT(INODE_PRIVATE_DATA(inode) == NULL);
142         OBD_ALLOC(INODE_PRIVATE_DATA(inode), sizeof(struct mds_filter_data));
143         if (INODE_PRIVATE_DATA(inode) == NULL)
144                 return -ENOMEM;
145         LASSERT(igrab(inode) == inode);
146         return 0;
147 }
148
149 /* Caller must hold mds->mds_epoch_sem */
150 static void mds_free_filterdata(struct inode *inode)
151 {
152         LASSERT(INODE_PRIVATE_DATA(inode) != NULL);
153         OBD_FREE(INODE_PRIVATE_DATA(inode), sizeof(struct mds_filter_data));
154         INODE_PRIVATE_DATA(inode) = NULL;
155         iput(inode);
156 }
157
158 /* Write access to a file: executors cause a negative count,
159  * writers a positive count.  The semaphore is needed to perform
160  * a check for the sign and then increment or decrement atomically.
161  *
162  * This code is closely tied to the allocation of the d_fsdata and the
163  * MDS epoch, so we use the same semaphore for the whole lot.
164  *
165  * We could use a different semaphore for each file, if it ever shows
166  * up in a profile, which it won't.
167  *
168  * epoch argument is nonzero during recovery */
169 static int mds_get_write_access(struct mds_obd *mds, struct inode *inode,
170                                 __u64 epoch)
171 {
172         int rc = 0;
173
174         down(&mds->mds_epoch_sem);
175
176         if (atomic_read(&inode->i_writecount) < 0) {
177                 up(&mds->mds_epoch_sem);
178                 RETURN(-ETXTBSY);
179         }
180
181
182         if (MDS_FILTERDATA(inode) && MDS_FILTERDATA(inode)->io_epoch != 0) {
183                 CDEBUG(D_INODE, "continuing MDS epoch "LPU64" for ino %lu/%u\n",
184                        MDS_FILTERDATA(inode)->io_epoch, inode->i_ino,
185                        inode->i_generation);
186                 goto out;
187         }
188
189         if (MDS_FILTERDATA(inode) == NULL)
190                 mds_alloc_filterdata(inode);
191         if (MDS_FILTERDATA(inode) == NULL) {
192                 rc = -ENOMEM;
193                 goto out;
194         }
195         if (epoch > mds->mds_io_epoch)
196                 mds->mds_io_epoch = epoch;
197         else
198                 mds->mds_io_epoch++;
199         MDS_FILTERDATA(inode)->io_epoch = mds->mds_io_epoch;
200         CDEBUG(D_INODE, "starting MDS epoch "LPU64" for ino %lu/%u\n",
201                mds->mds_io_epoch, inode->i_ino, inode->i_generation);
202  out:
203         if (rc == 0)
204                 atomic_inc(&inode->i_writecount);
205         up(&mds->mds_epoch_sem);
206         return rc;
207 }
208
209 /* Returns EAGAIN if the client needs to get size and/or cookies and close
210  * again -- which is never true if the file is about to be unlinked.  Otherwise
211  * returns the number of remaining writers. */
212 static int mds_put_write_access(struct mds_obd *mds, struct inode *inode,
213                                 struct mds_body *body, int unlinking)
214 {
215         int rc = 0;
216         ENTRY;
217
218         down(&mds->mds_epoch_sem);
219         atomic_dec(&inode->i_writecount);
220         rc = atomic_read(&inode->i_writecount);
221         if (rc > 0)
222                 GOTO(out, rc);
223 #if 0
224         if (!unlinking && !(body->valid & OBD_MD_FLSIZE))
225                 GOTO(out, rc = EAGAIN);
226 #endif
227         mds_free_filterdata(inode);
228  out:
229         up(&mds->mds_epoch_sem);
230         return rc;
231 }
232
233 static int mds_deny_write_access(struct mds_obd *mds, struct inode *inode)
234 {
235         ENTRY;
236         down(&mds->mds_epoch_sem);
237         if (atomic_read(&inode->i_writecount) > 0) {
238                 up(&mds->mds_epoch_sem);
239                 RETURN(-ETXTBSY);
240         }
241         atomic_dec(&inode->i_writecount);
242         up(&mds->mds_epoch_sem);
243         RETURN(0);
244 }
245
246 static void mds_allow_write_access(struct inode *inode)
247 {
248         ENTRY;
249         atomic_inc(&inode->i_writecount);
250 }
251
252 int mds_query_write_access(struct inode *inode)
253 {
254         ENTRY;
255         RETURN(atomic_read(&inode->i_writecount));
256 }
257
258 /* This replaces the VFS dentry_open, it manages mfd and writecount */
259 static struct mds_file_data *mds_dentry_open(struct dentry *dentry,
260                                              struct vfsmount *mnt, int flags,
261                                              struct ptlrpc_request *req)
262 {
263         struct mds_export_data *med = &req->rq_export->exp_mds_data;
264         struct mds_obd *mds = mds_req2mds(req);
265         struct mds_file_data *mfd;
266         struct mds_body *body;
267         int error;
268         ENTRY;
269
270         mfd = mds_mfd_new();
271         if (mfd == NULL) {
272                 CERROR("mds: out of memory\n");
273                 GOTO(cleanup_dentry, error = -ENOMEM);
274         }
275
276         body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
277
278         if (flags & FMODE_WRITE) {
279                 /* FIXME: in recovery, need to pass old epoch here */
280                 error = mds_get_write_access(mds, dentry->d_inode, 0);
281                 if (error)
282                         GOTO(cleanup_mfd, error);
283                 body->io_epoch = MDS_FILTERDATA(dentry->d_inode)->io_epoch;
284         } else if (flags & MDS_FMODE_EXEC) {
285                 error = mds_deny_write_access(mds, dentry->d_inode);
286                 if (error)
287                         GOTO(cleanup_mfd, error);
288         }
289
290         dget(dentry);
291
292         /* Mark the file as open to handle open-unlink. */
293         MDS_DOWN_WRITE_ORPHAN_SEM(dentry->d_inode);
294         mds_orphan_open_inc(dentry->d_inode);
295         MDS_UP_WRITE_ORPHAN_SEM(dentry->d_inode);
296
297         mfd->mfd_mode = flags;
298         mfd->mfd_dentry = dentry;
299         mfd->mfd_xid = req->rq_xid;
300         body->handle.cookie = mfd->mfd_handle.h_cookie;
301
302         if (req->rq_export->exp_disconnected) {
303                 mds_mfd_unlink(mfd, 0);
304                 MDS_DOWN_WRITE_ORPHAN_SEM(dentry->d_inode);
305                 mds_mfd_close(NULL, REQ_REC_OFF, req->rq_export->exp_obd,
306                               mfd, 0, NULL, 0, NULL, 0, NULL);
307         } else {
308                 spin_lock(&med->med_open_lock);
309                 list_add(&mfd->mfd_list, &med->med_open_head);
310                 spin_unlock(&med->med_open_lock);
311         }
312
313         RETURN(mfd);
314
315 cleanup_mfd:
316         mds_mfd_put(mfd);
317         mds_mfd_unlink(mfd, 1);
318 cleanup_dentry:
319         return ERR_PTR(error);
320 }
321
322 /* Must be called with i_mutex held */
323 static int mds_create_objects(struct ptlrpc_request *req, int offset,
324                               struct mds_update_record *rec,
325                               struct mds_obd *mds, struct obd_device *obd,
326                               struct dentry *dchild, void **handle,
327                               struct lov_mds_md **objid)
328 {
329         struct inode *inode = dchild->d_inode;
330         struct obd_trans_info oti = { 0 };
331         struct lov_mds_md *lmm = NULL;
332         int rc, lmm_size;
333         struct mds_body *body;
334         struct obd_info oinfo = { { { 0 } } };
335         void *lmm_buf;
336         ENTRY;
337
338         *objid = NULL;
339
340         if (!S_ISREG(inode->i_mode))
341                 RETURN(0);
342         if (rec->ur_flags & MDS_OPEN_DELAY_CREATE ||
343             !(rec->ur_flags & FMODE_WRITE))
344                 RETURN(0);
345
346         body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
347
348         if (body->valid & OBD_MD_FLEASIZE)
349                 RETURN(0);
350
351         oti_init(&oti, req);
352
353         /* replay case */
354         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
355                 if (rec->ur_fid2->id == 0) {
356                         DEBUG_REQ(D_ERROR, req, "fid2 not set on open replay");
357                         RETURN(-EFAULT);
358                 }
359
360                 body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
361                 lmm_size = rec->ur_eadatalen;
362                 lmm = rec->ur_eadata;
363                 LASSERT(lmm);
364                 if (lov_mds_md_size(lmm->lmm_stripe_count,
365                                     lmm->lmm_magic) != lmm_size)
366                         CWARN("Bad lmm_size during open replay for inode "
367                               "%lu\n", inode->i_ino);
368
369                 if (*handle == NULL) {
370                         int stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
371                         *handle = fsfilt_start_log(obd, inode, FSFILT_OP_CREATE,
372                                                    NULL, stripe_count);
373                 }
374                 if (IS_ERR(*handle)) {
375                         rc = PTR_ERR(*handle);
376                         *handle = NULL;
377                         GOTO(out_ids, rc);
378                 }
379
380                 rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size, "lov");
381                 if (rc)
382                         CERROR("open replay failed to set md:%d\n", rc);
383
384                 /* for replay we not need send lmm to client, this not used now */
385                 lustre_shrink_reply(req, offset, 0, 1);
386                 *objid = lmm;
387
388                 RETURN(rc);
389         }
390
391         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_ALLOC_OBDO))
392                 GOTO(out_ids, rc = -ENOMEM);
393
394         OBDO_ALLOC(oinfo.oi_oa);
395         if (oinfo.oi_oa == NULL)
396                 GOTO(out_ids, rc = -ENOMEM);
397         oinfo.oi_oa->o_uid = 0; /* must have 0 uid / gid on OST */
398         oinfo.oi_oa->o_gid = 0;
399         oinfo.oi_oa->o_mode = S_IFREG | 0600;
400         oinfo.oi_oa->o_id = inode->i_ino;
401         oinfo.oi_oa->o_gr = 0;
402         oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLFLAGS |
403                 OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID| OBD_MD_FLGROUP;
404         oinfo.oi_oa->o_size = 0;
405
406         obdo_from_inode(oinfo.oi_oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
407                         OBD_MD_FLMTIME | OBD_MD_FLCTIME);
408         if (!(rec->ur_flags & MDS_OPEN_HAS_OBJS)) {
409                 /* check if things like lfs setstripe are sending us the ea */
410                 if (rec->ur_flags & MDS_OPEN_HAS_EA) {
411                         rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
412                                            mds->mds_lov_exp,
413                                            0, &oinfo.oi_md, rec->ur_eadata);
414                         if (rc)
415                                 GOTO(out_oa, rc);
416                 } else {
417                         __u32 lmm_sz = mds->mds_max_mdsize;
418                         OBD_ALLOC(lmm, lmm_sz);
419                         if (lmm == NULL)
420                                 GOTO(out_oa, rc = -ENOMEM);
421
422                         lmm_size = lmm_sz;
423                         rc = mds_get_md(obd, dchild->d_parent->d_inode,
424                                         lmm, &lmm_size, 1, 0,
425                                         req->rq_export->exp_connect_flags);
426                         if (rc > 0)
427                                 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
428                                                    mds->mds_lov_exp,
429                                                    0, &oinfo.oi_md, lmm);
430                         OBD_FREE(lmm, lmm_sz);
431                         if (rc)
432                                 GOTO(out_oa, rc);
433                 }
434                 rc = obd_create(mds->mds_lov_exp, oinfo.oi_oa,
435                                 &oinfo.oi_md, &oti);
436                 if (rc) {
437                         int level = D_ERROR;
438                         if (rc == -ENOSPC)
439                                 level = D_INODE;
440                         CDEBUG(level, "error creating objects for "
441                                       "inode %lu: rc = %d\n",
442                                inode->i_ino, rc);
443                         if (rc > 0) {
444                                 CERROR("obd_create returned invalid "
445                                        "rc %d\n", rc);
446                                 rc = -EIO;
447                         }
448                         GOTO(out_oa, rc);
449                 }
450         } else {
451                 rc = obd_iocontrol(OBD_IOC_LOV_SETEA, mds->mds_lov_exp,
452                                    0, &oinfo.oi_md, rec->ur_eadata);
453                 if (rc) {
454                         GOTO(out_oa, rc);
455                 }
456                 oinfo.oi_md->lsm_object_id = oinfo.oi_oa->o_id;
457         }
458         if (i_size_read(inode)) {
459                 oinfo.oi_oa->o_size = i_size_read(inode);
460                 obdo_from_inode(oinfo.oi_oa, inode, OBD_MD_FLTYPE |
461                                 OBD_MD_FLATIME | OBD_MD_FLMTIME |
462                                 OBD_MD_FLCTIME | OBD_MD_FLSIZE);
463
464                 /* pack lustre id to OST */
465                 oinfo.oi_oa->o_fid = body->fid1.id;
466                 oinfo.oi_oa->o_generation = body->fid1.generation;
467                 oinfo.oi_oa->o_valid |= OBD_MD_FLFID | OBD_MD_FLGENER;
468                 oinfo.oi_policy.l_extent.start = i_size_read(inode);
469                 oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
470
471                 rc = obd_punch_rqset(mds->mds_lov_exp, &oinfo, &oti);
472                 if (rc) {
473                         CERROR("error setting attrs for inode %lu: rc %d\n",
474                                inode->i_ino, rc);
475                         if (rc > 0) {
476                                 CERROR("obd_setattr_async returned bad rc %d\n",
477                                        rc);
478                                 rc = -EIO;
479                         }
480                         GOTO(out_oa, rc);
481                 }
482         }
483
484         body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
485         obdo_refresh_inode(inode, oinfo.oi_oa, OBD_MD_FLBLKSZ);
486
487         LASSERT(oinfo.oi_md && oinfo.oi_md->lsm_object_id);
488         lmm = NULL;
489         rc = obd_packmd(mds->mds_lov_exp, &lmm, oinfo.oi_md);
490         if (rc < 0) {
491                 CERROR("cannot pack lsm, err = %d\n", rc);
492                 GOTO(out_oa, rc);
493         }
494         lmm_size = rc;
495         body->eadatasize = rc;
496
497         if (*handle == NULL)
498                 *handle = fsfilt_start(obd, inode, FSFILT_OP_CREATE, NULL);
499         if (IS_ERR(*handle)) {
500                 rc = PTR_ERR(*handle);
501                 *handle = NULL;
502                 GOTO(free_diskmd, rc);
503         }
504
505         rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size, "lov");
506         lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, lmm_size);
507         LASSERT(lmm_buf);
508         memcpy(lmm_buf, lmm, lmm_size);
509         *objid = lmm_buf; /* save for mds_lov_update_objid */
510
511  free_diskmd:
512         obd_free_diskmd(mds->mds_lov_exp, &lmm);
513  out_oa:
514         oti_free_cookies(&oti);
515         OBDO_FREE(oinfo.oi_oa);
516  out_ids:
517         if (oinfo.oi_md)
518                 obd_free_memmd(mds->mds_lov_exp, &oinfo.oi_md);
519         RETURN(rc);
520 }
521
522 static void reconstruct_open(struct mds_update_record *rec, int offset,
523                              struct ptlrpc_request *req,
524                              struct lustre_handle *child_lockh)
525 {
526         struct mds_export_data *med = &req->rq_export->exp_mds_data;
527         struct lsd_client_data *lcd = med->med_lcd;
528         struct mds_obd *mds = mds_req2mds(req);
529         struct mds_file_data *mfd = NULL;
530         struct obd_export *exp = req->rq_export;
531         struct obd_device *obd = exp->exp_obd;
532         struct dentry *parent, *dchild;
533         struct ldlm_reply *rep;
534         struct mds_body *body;
535         struct list_head *t;
536         int rc;
537         int put_child = 1;
538         ENTRY;
539
540         LASSERT(offset == DLM_INTENT_REC_OFF); /* only called via intent */
541         rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF, sizeof(*rep));
542         body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
543
544         /* copy rc, transno and disp; steal locks */
545         mds_req_from_lcd(req, lcd);
546         ldlm_reply_set_disposition(rep, le32_to_cpu(lcd->lcd_last_data));
547
548         /* Only replay if create or open actually happened. */
549         if (!ldlm_reply_disposition(rep, DISP_OPEN_CREATE | DISP_OPEN_OPEN) ) {
550                 EXIT;
551                 return; /* error looking up parent or child */
552         }
553
554         /* If we failed, then we must have failed opening, so don't look for
555          * file descriptor or anything, just give the client the bad news.
556          */
557         if (req->rq_status) {
558                 EXIT;
559                 return;
560         }
561
562         /* Now let's see if we have file descriptor present.
563          * No need to lookup child as it could be already deleted by another
564          * thread (bug 15010) */
565         spin_lock(&med->med_open_lock);
566         list_for_each(t, &med->med_open_head) {
567                 mfd = list_entry(t, struct mds_file_data, mfd_list);
568                 if (mfd->mfd_xid == req->rq_xid) {
569                         mds_mfd_addref(mfd);
570                         break;
571                 }
572                 mfd = NULL;
573         }
574         spin_unlock(&med->med_open_lock);
575
576         if (mfd && mfd->mfd_dentry && mfd->mfd_dentry->d_inode) {
577                 dchild = mfd->mfd_dentry;
578                 put_child = 0;
579         } else {
580                 parent = mds_fid2dentry(mds, rec->ur_fid1, NULL);
581                 if (IS_ERR(parent)) {
582                         rc = PTR_ERR(parent);
583                         LCONSOLE_WARN("Parent "LPU64"/%u lookup error %d."
584                                       " Evicting client %s with export %s.\n",
585                                       rec->ur_fid1->id,rec->ur_fid1->generation,
586                                       rc, obd_uuid2str(&exp->exp_client_uuid),
587                                       obd_export_nid2str(exp));
588                         mds_export_evict(exp);
589                         EXIT;
590                         return;
591                 }
592
593                 dchild = mds_lookup(obd, rec->ur_name, parent, rec->ur_namelen - 1);
594                 l_dput(parent);
595                 if (IS_ERR(dchild)) {
596                         rc = PTR_ERR(dchild);
597                         LCONSOLE_WARN("Child "LPU64"/%u lookup error %d."
598                                       " Evicting client %s with export %s.\n",
599                                       rec->ur_fid1->id,rec->ur_fid1->generation,
600                                       rc, obd_uuid2str(&exp->exp_client_uuid),
601                                       obd_export_nid2str(exp));
602                         mds_export_evict(exp);
603                         EXIT;
604                         return;
605                 }
606         }
607
608         if (!dchild->d_inode)
609                 GOTO(out_dput, 0); /* child not present to open */
610
611         /* At this point, we know we have a child. We'll send
612          * it back _unless_ it not created and open failed.
613          */
614         if (ldlm_reply_disposition(rep, DISP_OPEN_OPEN) &&
615             !ldlm_reply_disposition(rep, DISP_OPEN_CREATE) &&
616             req->rq_status) {
617                 GOTO(out_dput, 0);
618         }
619
620         mds_pack_inode2body(body, dchild->d_inode);
621         if (S_ISREG(dchild->d_inode->i_mode)) {
622                 rc = mds_pack_md(obd, req->rq_repmsg, DLM_REPLY_REC_OFF + 1,
623                                  body, dchild->d_inode, 1, 0,
624                                  req->rq_export->exp_connect_flags);
625
626                 if (rc)
627                         LASSERT(rc == req->rq_status);
628
629                 /* If we have LOV EA data, the OST holds size, mtime */
630                 if (!(body->valid & OBD_MD_FLEASIZE))
631                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
632                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
633         }
634
635         if (!(rec->ur_flags & MDS_OPEN_JOIN_FILE))
636                 lustre_shrink_reply(req, DLM_REPLY_REC_OFF + 1,
637                                     body->eadatasize, 0);
638
639         if (req->rq_export->exp_connect_flags & OBD_CONNECT_ACL &&
640             !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
641                 int acl_off = DLM_REPLY_REC_OFF + (body->eadatasize ? 2 : 1);
642
643                 rc = mds_pack_acl(med, dchild->d_inode, req->rq_repmsg,
644                                   body, acl_off);
645                 lustre_shrink_reply(req, acl_off, body->aclsize, 0);
646                 if (!req->rq_status && rc)
647                         req->rq_status = rc;
648         }
649
650         /* #warning "XXX fixme" bug 2991 */
651         /* Here it used to LASSERT(mfd) if exp_outstanding_reply != NULL.
652          * Now that exp_outstanding_reply is a list, it's just using mfd != NULL
653          * to detect a re-open */
654         if (mfd == NULL) {
655                 if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
656 #if LUSTRE_FIX >= 50
657                         /* Allow file join in beta builds to allow debugging */
658                         rc = mds_join_file(rec, req, dchild, NULL);
659                         if (rc)
660                                 GOTO(out_dput, rc);
661 #else
662                         CWARN("file join is not supported in this version of "
663                               "Lustre\n");
664                         GOTO(out_dput, req->rq_status = rc = -EOPNOTSUPP);
665 #endif
666                 }
667                 mntget(mds->mds_vfsmnt);
668                 CERROR("Re-opened file \n");
669                 mfd = mds_dentry_open(dchild, mds->mds_vfsmnt,
670                                       rec->ur_flags & ~MDS_OPEN_TRUNC, req);
671                 mntput(mds->mds_vfsmnt);
672                 if (IS_ERR(mfd)) {
673                         req->rq_status = PTR_ERR(mfd);
674                         mfd = NULL;
675                         CERROR("%s: opening inode "LPU64" failed: rc %d\n",
676                                req->rq_export->exp_obd->obd_name,
677                                (__u64)dchild->d_inode->i_ino, req->rq_status);
678                         GOTO(out_dput, req->rq_status);
679                 }
680         } else {
681                 body->handle.cookie = mfd->mfd_handle.h_cookie;
682                 CDEBUG(D_INODE, "resend mfd %p, cookie "LPX64"\n", mfd,
683                        mfd->mfd_handle.h_cookie);
684         }
685
686         if (!ldlm_reply_disposition(rep, DISP_OPEN_LOCK))
687                 GOTO(out_dput, 0);
688
689         /* child_lockh has been set in fixup_handle_for_resent_req called
690          * in mds_intent_policy for resent request */
691         if (child_lockh == NULL || !lustre_handle_is_used(child_lockh)) {
692                 /* the lock is already canceled! clear DISP_OPEN_LOCK */
693                 ldlm_reply_clear_disposition(rep, DISP_OPEN_LOCK);
694         }
695
696  out_dput:
697         if (mfd)
698                 mds_mfd_put(mfd);
699
700         if (put_child)
701                 l_dput(dchild);
702         EXIT;
703 }
704
705 /* if client disconnects during recovery it may resend opens which were replayed
706  * on server but their transno less then last_transno on server so they will not
707  * be detected as reconstructs */
708 static int open_replay_reconstruct(struct ptlrpc_request *req)
709 {
710         struct mds_export_data *med = &req->rq_export->exp_mds_data;
711         struct mds_file_data *mfd = NULL;
712         struct list_head *t;
713
714         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
715                 return 0;
716
717         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))
718                 return 0;
719
720         /* if mfd exists then replay was done already */
721         spin_lock(&med->med_open_lock);
722         list_for_each(t, &med->med_open_head) {
723                 mfd = list_entry(t, struct mds_file_data, mfd_list);
724                 if (mfd->mfd_xid == req->rq_xid) {
725                         mds_mfd_addref(mfd);
726                         break;
727                 }
728                 mfd = NULL;
729         }
730         spin_unlock(&med->med_open_lock);
731
732         if (mfd) {
733                 struct mds_body *body = lustre_msg_buf(req->rq_repmsg,
734                                                        DLM_REPLY_REC_OFF,
735                                                        sizeof(*body));
736                 __u64 *pre_versions = lustre_msg_get_versions(req->rq_reqmsg);
737
738                 body->handle.cookie = mfd->mfd_handle.h_cookie;
739                 CDEBUG(D_INODE, "resend mfd %p, cookie "LPX64"\n", mfd,
740                        mfd->mfd_handle.h_cookie);
741                 mds_mfd_put(mfd);
742                 lustre_msg_set_versions(req->rq_repmsg, pre_versions);
743                 lustre_msg_set_transno(req->rq_repmsg,
744                                        lustre_msg_get_transno(req->rq_reqmsg));
745                 lustre_msg_set_status(req->rq_repmsg, 0);
746                 return 1;
747         }
748         return 0;
749 }
750
751 /* do NOT or the MAY_*'s, you'll get the weakest */
752 static int accmode(struct inode *inode, int flags)
753 {
754         int res = 0;
755
756         /* Sadly, NFSD reopens a file repeatedly during operation, so the
757          * "acc_mode = 0" allowance for newly-created files isn't honoured.
758          * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
759          * owner can write to a file even if it is marked readonly to hide
760          * its brokenness. (bug 5781) */
761         if (flags & MDS_OPEN_OWNEROVERRIDE && inode->i_uid == current->fsuid)
762                 return 0;
763
764         if (flags & FMODE_READ)
765                 res = MAY_READ;
766         if (flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
767                 res |= MAY_WRITE;
768         if (flags & MDS_FMODE_EXEC)
769                 res = MAY_EXEC;
770         return res;
771 }
772
773 /* Handles object creation, actual opening, and I/O epoch */
774 static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
775                            struct mds_body *body, int flags, void **handle,
776                            struct mds_update_record *rec,struct ldlm_reply *rep,
777                            struct lustre_handle *lockh)
778 {
779         struct mds_obd *mds = mds_req2mds(req);
780         struct obd_device *obd = req->rq_export->exp_obd;
781         struct mds_file_data *mfd = NULL;
782         struct lov_mds_md *lmm = NULL; /* object IDs created */
783         int rc = 0;
784         ENTRY;
785
786         /* atomically create objects if necessary */
787         LOCK_INODE_MUTEX(dchild->d_inode);
788
789         if (S_ISREG(dchild->d_inode->i_mode) &&
790             !(body->valid & OBD_MD_FLEASIZE)) {
791                 rc = mds_pack_md(obd, req->rq_repmsg, DLM_REPLY_REC_OFF + 1,
792                                  body, dchild->d_inode, 0, 0,
793                                  req->rq_export->exp_connect_flags);
794                 if (rc) {
795                         UNLOCK_INODE_MUTEX(dchild->d_inode);
796                         RETURN(rc);
797                 }
798         }
799         if (rec != NULL) {
800                 if ((body->valid & OBD_MD_FLEASIZE) &&
801                     (rec->ur_flags & MDS_OPEN_HAS_EA)) {
802                         UNLOCK_INODE_MUTEX(dchild->d_inode);
803                         RETURN(-EEXIST);
804                 }
805                 if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
806 #if LUSTRE_FIX >= 50
807                         /* Allow file join in beta builds to allow debugging */
808                         UNLOCK_INODE_MUTEX(dchild->d_inode);
809                         rc = mds_join_file(rec, req, dchild, lockh);
810                         if (rc)
811                                 RETURN(rc);
812                         LOCK_INODE_MUTEX(dchild->d_inode);
813 #else
814                         CWARN("file join is not supported in this version of "
815                               "Lustre\n");
816                         RETURN(-EOPNOTSUPP);
817 #endif
818                 }
819                 if (!(body->valid & OBD_MD_FLEASIZE) &&
820                     !(body->valid & OBD_MD_FLMODEASIZE)) {
821                         /* split open transactions here */
822                         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_SPLIT_OPEN, 10);
823                         /* no EA: create objects */
824                         rc = mds_create_objects(req, DLM_REPLY_REC_OFF + 1, rec,
825                                                 mds, obd, dchild, handle, &lmm);
826                         if (rc) {
827                                 CERROR("mds_create_objects: rc = %d\n", rc);
828                                 UNLOCK_INODE_MUTEX(dchild->d_inode);
829                                 RETURN(rc);
830                         }
831                 }
832         }
833         /* If the inode has no EA data, then MDS holds size, mtime */
834         if (S_ISREG(dchild->d_inode->i_mode) &&
835             !(body->valid & OBD_MD_FLEASIZE)) {
836                 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
837                                 OBD_MD_FLATIME | OBD_MD_FLMTIME);
838         }
839         UNLOCK_INODE_MUTEX(dchild->d_inode);
840
841         if (req->rq_export->exp_connect_flags & OBD_CONNECT_ACL &&
842             rec && !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
843                 int acl_off = DLM_REPLY_REC_OFF + 2;
844
845                 rc = mds_pack_acl(&req->rq_export->exp_mds_data,
846                                   dchild->d_inode, req->rq_repmsg,
847                                   body, acl_off);
848                 if (rc)
849                         RETURN(rc);
850         }
851
852         if ((rc = mds_lov_prepare_objids(obd,lmm)) != 0)
853                 RETURN(rc);
854
855         mfd = mds_dentry_open(dchild, mds->mds_vfsmnt, flags, req);
856         if (IS_ERR(mfd))
857                 RETURN(PTR_ERR(mfd));
858
859         CDEBUG(D_INODE, "mfd %p, cookie "LPX64"\n", mfd,
860                mfd->mfd_handle.h_cookie);
861
862         mds_lov_update_objids(obd, lmm);
863
864         if (rc)
865                 mds_mfd_unlink(mfd, 1);
866
867         mds_mfd_put(mfd);
868         RETURN(rc);
869 }
870
871 static int mds_open_by_fid(struct ptlrpc_request *req, struct ll_fid *fid,
872                            struct mds_body *body, int flags,
873                            struct mds_update_record *rec,struct ldlm_reply *rep)
874 {
875         struct obd_device *obd = req->rq_export->exp_obd;
876         struct mds_obd *mds = mds_req2mds(req);
877         struct dentry *dchild, *dparent = NULL;
878         char fidname[LL_FID_NAMELEN];
879         int fidlen = 0, rc;
880         void *handle = NULL;
881         struct inode *inodes[PTLRPC_NUM_VERSIONS] = { NULL };
882         ENTRY;
883
884         ldlm_reply_set_disposition(rep, DISP_LOOKUP_EXECD);
885         fidlen = ll_fid2str(fidname, fid->id, fid->generation);
886         dchild = mds_lookup(obd, fidname, mds->mds_pending_dir, fidlen);
887         if (IS_ERR(dchild)) {
888                 rc = PTR_ERR(dchild);
889                 CERROR("error looking up %s in PENDING: rc = %d\n",fidname, rc);
890                 RETURN(rc);
891         }
892
893         if (dchild->d_inode != NULL) {
894                 mds_inode_set_orphan(dchild->d_inode);
895                 CWARN("Orphan %s found and opened in PENDING directory\n",
896                        fidname);
897         } else {
898                 __u64 *pre_versions = lustre_msg_get_versions(req->rq_reqmsg);
899                 l_dput(dchild);
900
901                 /* We didn't find it in PENDING so it isn't an orphan.  See
902                  * if it was a regular inode that was previously created. */
903                 dchild = mds_fid2dentry(mds, fid, NULL);
904                 if (IS_ERR(dchild))
905                         RETURN(PTR_ERR(dchild));
906                 /**
907                  * bug19224
908                  * this can be replay of partially committed open|create,
909                  * the create itself was committed while LOV EA weren't
910                  * We need to set versions again if conditions are:
911                  * - this is replay
912                  * - the transaction is greater than last_committed
913                  * - this was open|create
914                  * - there was real create so parent pre_version was saved
915                  */
916                 if ((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
917                     (lustre_msg_get_transno(req->rq_reqmsg) >
918                      req->rq_export->exp_last_committed) &&
919                     (rec->ur_flags & MDS_OPEN_CREAT) &&
920                     (pre_versions && pre_versions[0] != 0)) {
921                         /* need parent to set version */
922                         dparent = mds_fid2dentry(mds, rec->ur_fid1, NULL);
923                         if (IS_ERR(dparent)) {
924                                 CERROR("Can't find parent for open replay\n");
925                                 l_dput(dchild);
926                                 RETURN(PTR_ERR(dparent));
927                         }
928                         /* though file was created, the versions were not
929                          * changed yet, need to replay that too */
930                         inodes[0] = dparent->d_inode;
931                         inodes[1] = dchild->d_inode;
932                 }
933         }
934
935         mds_pack_inode2body(body, dchild->d_inode);
936         ldlm_reply_set_disposition(rep, DISP_LOOKUP_POS);
937         ldlm_reply_set_disposition(rep, DISP_OPEN_OPEN);
938
939         rc = mds_finish_open(req, dchild, body, flags, &handle, rec, rep, NULL);
940         rc = mds_finish_transno(mds, inodes, handle,
941                                 req, rc, rep ? rep->lock_policy_res1 : 0, 0);
942         /* XXX what do we do here if mds_finish_transno itself failed? */
943
944         l_dput(dparent);
945         l_dput(dchild);
946         RETURN(rc);
947 }
948
949 int mds_pin(struct ptlrpc_request *req, int offset)
950 {
951         struct obd_device *obd = req->rq_export->exp_obd;
952         struct mds_body *reqbody, *repbody;
953         struct lvfs_run_ctxt saved;
954         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
955         ENTRY;
956
957         reqbody = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*reqbody));
958
959         rc = lustre_pack_reply(req, 2, size, NULL);
960         if (rc)
961                 RETURN(rc);
962
963         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
964                                  sizeof(*repbody));
965
966         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
967         rc = mds_open_by_fid(req, &reqbody->fid1, repbody, reqbody->flags, NULL,
968                              NULL);
969         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
970
971         RETURN(rc);
972 }
973
974 /*  Get an internal lock on the inode number (but not generation) to sync
975  *  new inode creation with inode unlink (bug 2029).  If child_lockh is NULL
976  *  we just get the lock as a barrier to wait for other holders of this lock,
977  *  and drop it right away again. */
978 int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
979                        struct lustre_handle *child_lockh)
980 {
981         struct ldlm_res_id child_res_id = { .name = { inode->i_ino, 0, 1, 0 } };
982         struct lustre_handle lockh;
983         int lock_flags = LDLM_FL_ATOMIC_CB;
984         int rc;
985
986         if (child_lockh == NULL)
987                 child_lockh = &lockh;
988
989         rc = ldlm_cli_enqueue_local(obd->obd_namespace, &child_res_id,
990                                     LDLM_PLAIN, NULL, LCK_EX, &lock_flags,
991                                     ldlm_blocking_ast, ldlm_completion_ast,
992                                     NULL, NULL, 0, NULL, child_lockh);
993         if (rc != ELDLM_OK)
994                 CERROR("ldlm_cli_enqueue_local: %d\n", rc);
995         else if (child_lockh == &lockh)
996                 ldlm_lock_decref(child_lockh, LCK_EX);
997
998         RETURN(rc);
999 }
1000
1001 int mds_open(struct mds_update_record *rec, int offset,
1002              struct ptlrpc_request *req, struct lustre_handle *child_lockh)
1003 {
1004         /* XXX ALLOCATE _something_ - 464 bytes on stack here */
1005         struct obd_device *obd = req->rq_export->exp_obd;
1006         struct mds_obd *mds = mds_req2mds(req);
1007         struct ldlm_reply *rep = NULL;
1008         struct mds_body *body = NULL;
1009         struct dentry *dchild = NULL, *dparent = NULL;
1010         struct inode *inodes[PTLRPC_NUM_VERSIONS] = { NULL };
1011         struct mds_export_data *med;
1012         struct lustre_handle parent_lockh;
1013         int rc = 0, cleanup_phase = 0, acc_mode, created = 0;
1014         int parent_mode = LCK_CR;
1015         void *handle = NULL;
1016         struct lvfs_dentry_params dp = LVFS_DENTRY_PARAMS_INIT;
1017         unsigned int qcids[MAXQUOTAS] = { current->fsuid, current->fsgid };
1018         unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1019         unsigned int ids[MAXQUOTAS] = { 0, 0 };
1020         int child_mode = LCK_CR;
1021         /* Always returning LOOKUP lock if open succesful to guard
1022            dentry on client. */
1023         int lock_flags = 0;
1024         int quota_pending[2] = {0, 0};
1025         int use_parent, need_open_lock;
1026         unsigned int gid = current->fsgid;
1027         ENTRY;
1028
1029         mds_counter_incr(req->rq_export, LPROC_MDS_OPEN);
1030
1031         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PAUSE_OPEN | OBD_FAIL_ONCE,
1032                          (obd_timeout + 1) / 4);
1033
1034         CLASSERT(MAXQUOTAS < 4);
1035         if (offset == DLM_INTENT_REC_OFF) { /* intent */
1036                 rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
1037                                      sizeof(*rep));
1038                 body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF,
1039                                       sizeof(*body));
1040         } else if (offset == REQ_REC_OFF) { /* non-intent reint */
1041                 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1042                                       sizeof(*body));
1043                 LBUG(); /* XXX: not supported yet? */
1044         } else {
1045                 body = NULL;
1046                 LBUG();
1047         }
1048
1049         /* check the open resent|replay case */
1050         if (open_replay_reconstruct(req))
1051                 RETURN(0);
1052
1053         MDS_CHECK_RESENT(req, reconstruct_open(rec, offset, req, child_lockh));
1054
1055         /* Step 0: If we are passed a fid, then we assume the client already
1056          * opened this file and is only replaying the RPC, so we open the
1057          * inode by fid (at some large expense in security). */
1058         /*XXX liblustre use mds_open_by_fid to implement LL_IOC_LOV_SETSTRIPE */
1059         if (((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) ||
1060              (req->rq_export->exp_libclient && rec->ur_flags&MDS_OPEN_HAS_EA))&&
1061             !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
1062                 if (rec->ur_fid2->id == 0) {
1063                         struct ldlm_lock *lock = ldlm_handle2lock(child_lockh);
1064                         if (lock) {
1065                                 LDLM_ERROR(lock, "fid2 not set on open replay");
1066                                 LDLM_LOCK_PUT(lock);
1067                         }
1068                         DEBUG_REQ(D_ERROR, req, "fid2 not set on open replay");
1069                         RETURN(-EFAULT);
1070                 }
1071
1072                 /** check there is no stale orphan with same inode number */
1073                 if (rec->ur_flags & MDS_OPEN_CREAT) {
1074                         rc = mds_check_stale_orphan(obd, rec->ur_fid2);
1075                         if (rc)
1076                                 RETURN(rc);
1077                 }
1078
1079                 rc = mds_open_by_fid(req, rec->ur_fid2, body, rec->ur_flags,
1080                                      rec, rep);
1081                 if (rc != -ENOENT) {
1082                         if (req->rq_export->exp_libclient &&
1083                             rec->ur_flags & MDS_OPEN_HAS_EA)
1084                                 RETURN(0);
1085
1086                         RETURN(rc);
1087                 }
1088
1089                 /* We didn't find the correct inode on disk either, so we
1090                  * need to re-create it via a regular replay. */
1091                 if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
1092                         DEBUG_REQ(D_ERROR, req,"OPEN_CREAT not in open replay");
1093                         RETURN(-EFAULT);
1094                 }
1095         } else if (rec->ur_fid2->id) {
1096                 DEBUG_REQ(D_ERROR, req, "fid2 "LPU64"/%u on open non-replay",
1097                           rec->ur_fid2->id, rec->ur_fid2->generation);
1098                 RETURN(-EFAULT);
1099         }
1100
1101         /* If we got here, we must be called via intent */
1102         LASSERT(offset == DLM_INTENT_REC_OFF);
1103
1104         med = &req->rq_export->exp_mds_data;
1105         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
1106                 CERROR("test case OBD_FAIL_MDS_OPEN_PACK\n");
1107                 RETURN(-ENOMEM);
1108         }
1109
1110         if (rec->ur_flags & (MDS_OPEN_CREAT | MDS_OPEN_JOIN_FILE))
1111                 parent_mode = LCK_EX;
1112
1113         /* We cannot use acc_mode here, because it is zeroed in case of
1114            creating a file, so we get wrong lockmode */
1115         if (rec->ur_flags & FMODE_WRITE)
1116                 child_mode = LCK_CW;
1117         else if (rec->ur_flags & MDS_FMODE_EXEC)
1118                 child_mode = LCK_PR;
1119         else
1120                 child_mode = LCK_CR;
1121
1122         /* join file and nfsd can't need lookup dchild as use parent for it */
1123         use_parent = (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
1124                      (rec->ur_flags & MDS_OPEN_LOCK) && (rec->ur_namelen == 1)) ||
1125                      (rec->ur_flags & MDS_OPEN_JOIN_FILE);
1126
1127         need_open_lock = !(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
1128                            (rec->ur_flags & MDS_OPEN_LOCK);
1129
1130         /* Try to lock both parent and child first. If child is not found,
1131          * return only locked parent.  This is enough to prevent other
1132          * threads from changing this directory until creation is finished. */
1133         rc = mds_get_parent_child_locked(obd, &obd->u.mds,
1134                                          rec->ur_fid1,
1135                                          &parent_lockh,
1136                                          &dparent, parent_mode,
1137                                          MDS_INODELOCK_UPDATE,
1138                                          use_parent ? NULL : rec->ur_name,
1139                                          rec->ur_namelen,
1140                                          (rec->ur_flags & MDS_OPEN_LOCK) ?
1141                                                 child_lockh : NULL,
1142                                          &dchild, child_mode,
1143                                          MDS_INODELOCK_LOOKUP |
1144                                          MDS_INODELOCK_OPEN);
1145
1146         if (rc) {
1147                 if (rc != -ENOENT) {
1148                         CERROR("parent "LPU64"/%u lookup/take lock error %d\n",
1149                                rec->ur_fid1->id, rec->ur_fid1->generation, rc);
1150                 } else {
1151                         /* Just cannot find parent - make it look like
1152                          * usual negative lookup to avoid extra MDS RPC */
1153                         ldlm_reply_set_disposition(rep, DISP_LOOKUP_EXECD);
1154                         ldlm_reply_set_disposition(rep, DISP_LOOKUP_NEG);
1155                 }
1156                 GOTO(cleanup, rc);
1157         }
1158         LASSERT(dparent->d_inode != NULL);
1159
1160         cleanup_phase = 1; /* parent dentry and lock */
1161
1162         if (use_parent)
1163                 dchild = dget(dparent);
1164
1165         if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
1166                 acc_mode = accmode(dchild->d_inode, rec->ur_flags);
1167                 GOTO(found_child, rc);
1168         }
1169
1170         cleanup_phase = 2; /* child dentry */
1171
1172         ldlm_reply_set_disposition(rep, DISP_LOOKUP_EXECD);
1173         if (dchild->d_inode)
1174                 ldlm_reply_set_disposition(rep, DISP_LOOKUP_POS);
1175         else
1176                 ldlm_reply_set_disposition(rep, DISP_LOOKUP_NEG);
1177
1178         /*Step 3: If the child was negative, and we're supposed to, create it.*/
1179         if (dchild->d_inode == NULL) {
1180                 unsigned long ino = rec->ur_fid2->id;
1181                 struct iattr iattr;
1182                 struct inode *inode;
1183
1184                 if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
1185                         /* It's negative and we weren't supposed to create it */
1186                         GOTO(cleanup, rc = -ENOENT);
1187                 }
1188
1189                 if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
1190                         GOTO(cleanup, rc = -EROFS);
1191
1192                 /** check there is no stale orphan with same inode number */
1193                 rc = mds_check_stale_orphan(obd, rec->ur_fid2);
1194                 if (rc)
1195                         GOTO(cleanup, rc);
1196
1197                 /* version recovery check */
1198                 rc = mds_version_get_check(req, dparent->d_inode, 0);
1199                 if (rc)
1200                         GOTO(cleanup_no_trans, rc);
1201
1202                 if (dparent->d_inode->i_mode & S_ISGID)
1203                         gid = dparent->d_inode->i_gid;
1204                 else
1205                         gid = current->fsgid;
1206
1207                 /* we try to get enough quota to write here, and let ldiskfs
1208                  * decide if it is out of quota or not b=14783
1209                  * FIXME: after CMD is used, pointer to obd_trans_info* couldn't
1210                  * be NULL, b=14840 */
1211                 ids[0] = current->fsuid;
1212                 ids[1] = gid;
1213                 lquota_chkquota(mds_quota_interface_ref, req->rq_export,
1214                                 ids[0], ids[1], 1, quota_pending,
1215                                 NULL, NULL, 0);
1216
1217                 ldlm_reply_set_disposition(rep, DISP_OPEN_CREATE);
1218                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_CREATE,
1219                                       NULL);
1220                 if (IS_ERR(handle)) {
1221                         rc = PTR_ERR(handle);
1222                         handle = NULL;
1223                         GOTO(cleanup, rc);
1224                 }
1225                 dchild->d_fsdata = (void *) &dp;
1226                 dp.ldp_ptr = req;
1227                 dp.ldp_inum = ino;
1228
1229                 LOCK_INODE_MUTEX(dparent->d_inode);
1230                 rc = ll_vfs_create(dparent->d_inode, dchild, rec->ur_mode,NULL);
1231                 UNLOCK_INODE_MUTEX(dparent->d_inode);
1232                 if (dchild->d_fsdata == (void *)(unsigned long)ino)
1233                         dchild->d_fsdata = NULL;
1234
1235                 if (rc) {
1236                         CDEBUG(D_INODE, "error during create: %d\n", rc);
1237                         GOTO(cleanup, rc);
1238                 }
1239                 inode = dchild->d_inode;
1240                 created = 1;
1241                 if (ino) {
1242                         if (ino != inode->i_ino) {
1243                                 /* FID support is needed to replay this
1244                                  * correctly. Now fail gracefully like there is
1245                                  * version mismatch */
1246                                 if (req->rq_export->exp_delayed)
1247                                         rc = -EOVERFLOW;
1248                                 else
1249                                         rc = -EFAULT;
1250                                 GOTO(cleanup, rc);
1251                         }
1252                         /* Written as part of setattr */
1253                         inode->i_generation = rec->ur_fid2->generation;
1254                         CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
1255                                inode->i_ino, inode->i_generation);
1256                 }
1257
1258                 LTIME_S(iattr.ia_atime) = rec->ur_time;
1259                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1260                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1261
1262                 iattr.ia_uid = current->fsuid;  /* set by push_ctxt already */
1263                 iattr.ia_gid = gid;
1264
1265                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
1266                         ATTR_MTIME | ATTR_CTIME;
1267
1268                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
1269                 if (rc)
1270                         CERROR("error on child setattr: rc = %d\n", rc);
1271
1272                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1273
1274                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1275                 if (rc)
1276                         CERROR("error on parent setattr: rc = %d\n", rc);
1277
1278                 rc = fsfilt_commit(obd, dchild->d_inode, handle, 0);
1279                 handle = NULL;
1280                 acc_mode = 0;           /* Don't check for permissions */
1281         } else {
1282                 acc_mode = accmode(dchild->d_inode, rec->ur_flags);
1283                 /* Child previously existed so the lookup and lock is already
1284                  * done, so no further locking is needed. */
1285                 /* for nfs and join - we need two locks for same fid, but
1286                  * with different mode */
1287                 if (need_open_lock && !use_parent)  {
1288                         ldlm_reply_set_disposition(rep, DISP_OPEN_LOCK);
1289                         need_open_lock = 0;
1290                 }
1291         }
1292
1293         LASSERTF(!mds_inode_is_orphan(dchild->d_inode),
1294                  "dchild %.*s (%p) inode %p/%lu/%u\n", dchild->d_name.len,
1295                  dchild->d_name.name, dchild, dchild->d_inode,
1296                  dchild->d_inode->i_ino, dchild->d_inode->i_generation);
1297
1298 found_child:
1299         mds_pack_inode2body(body, dchild->d_inode);
1300         if (!created && (rec->ur_flags & MDS_OPEN_CREAT) &&
1301             (rec->ur_flags & MDS_OPEN_EXCL)) {
1302                 /* File already exists, we didn't just create it, and we
1303                  * were passed O_EXCL; err-or. */
1304                 GOTO(cleanup, rc = -EEXIST); // returns a lock to the client
1305         }
1306
1307         /* if we are following special file, don't open */
1308         if (!S_ISREG(dchild->d_inode->i_mode) &&
1309             !S_ISDIR(dchild->d_inode->i_mode))
1310                 GOTO(cleanup_no_trans, rc = 0);
1311
1312         ldlm_reply_set_disposition(rep, DISP_OPEN_OPEN);
1313         if (S_ISREG(dchild->d_inode->i_mode)) {
1314                 /* Check permissions etc */
1315                 rc = ll_permission(dchild->d_inode, acc_mode, NULL);
1316                 if (rc != 0)
1317                         GOTO(cleanup, rc);
1318
1319                 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY) &&
1320                     (acc_mode & MAY_WRITE))
1321                         GOTO(cleanup, rc = -EROFS);
1322
1323                 /* An append-only file must be opened in append mode for
1324                  * writing */
1325                 if (IS_APPEND(dchild->d_inode) && (acc_mode & MAY_WRITE) != 0 &&
1326                     ((rec->ur_flags & MDS_OPEN_APPEND) == 0 ||
1327                      (rec->ur_flags & MDS_OPEN_TRUNC) != 0))
1328                         GOTO(cleanup, rc = -EPERM);
1329         } else {
1330                 if (S_ISDIR(dchild->d_inode->i_mode)) {
1331                         if (rec->ur_flags & MDS_OPEN_CREAT ||
1332                             rec->ur_flags & FMODE_WRITE) {
1333                                 /* we are trying to create or write a exist dir*/
1334                                 GOTO(cleanup, rc = -EISDIR);
1335                         }
1336                         if (rec->ur_flags & MDS_FMODE_EXEC) {
1337                                 /* we are trying to exec a directory */
1338                                 GOTO(cleanup, rc = -EACCES);
1339                         }
1340                         if (ll_permission(dchild->d_inode, acc_mode, NULL))
1341                                 GOTO(cleanup, rc = -EACCES);
1342                 } else if (rec->ur_flags & MDS_OPEN_DIRECTORY) {
1343                         GOTO(cleanup, rc = -ENOTDIR);
1344                 }
1345         }
1346
1347         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_CREATE)) {
1348                 obd_fail_loc = OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE;
1349                 GOTO(cleanup, rc = -EAGAIN);
1350         }
1351
1352         if (need_open_lock) {
1353                 ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN } };
1354                 struct ldlm_res_id child_res_id;
1355
1356                 /* In case of replay we do not get a lock assuming that the
1357                    caller has it already */
1358                 child_res_id.name[0] = dchild->d_inode->i_ino;
1359                 child_res_id.name[1] = dchild->d_inode->i_generation;
1360
1361                 rc = ldlm_cli_enqueue_local(obd->obd_namespace, &child_res_id,
1362                                             LDLM_IBITS, &policy, child_mode,
1363                                             &lock_flags, ldlm_blocking_ast,
1364                                             ldlm_completion_ast, NULL, NULL,
1365                                             0, NULL, child_lockh);
1366                 if (rc != ELDLM_OK)
1367                         GOTO(cleanup, rc);
1368
1369                 /* Let mds_intent_policy know that we have a lock to return */
1370                 ldlm_reply_set_disposition(rep, DISP_OPEN_LOCK);
1371         }
1372
1373         if (!S_ISREG(dchild->d_inode->i_mode) &&
1374             !S_ISDIR(dchild->d_inode->i_mode) &&
1375             (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH)) {
1376                 /* If client supports this, do not return open handle for
1377                  * special device nodes */
1378                 GOTO(cleanup_no_trans, rc = 0);
1379         }
1380
1381         /* Step 5: mds_open it */
1382         rc = mds_finish_open(req, dchild, body, rec->ur_flags, &handle, rec,
1383                              rep, &parent_lockh);
1384         GOTO(cleanup, rc);
1385
1386  cleanup:
1387         inodes[0] = (!created || IS_ERR(dparent)) ? NULL : dparent->d_inode;
1388         inodes[1] = (created && dchild) ? dchild->d_inode : NULL;
1389         rc = mds_finish_transno(mds, inodes, handle, req, rc,
1390                                 rep ? rep->lock_policy_res1 : 0, 0);
1391
1392  cleanup_no_trans:
1393         if (quota_pending[0] || quota_pending[1])
1394                 lquota_pending_commit(mds_quota_interface_ref, obd,
1395                                       ids[0], ids[1], quota_pending);
1396         switch (cleanup_phase) {
1397         case 2:
1398                 if (rc && created) {
1399                         int err;
1400                         LOCK_INODE_MUTEX(dparent->d_inode);
1401                         err = ll_vfs_unlink(dparent->d_inode, dchild,
1402                                             mds->mds_vfsmnt);
1403                         UNLOCK_INODE_MUTEX(dparent->d_inode);
1404                         if (err) {
1405                                 CERROR("unlink(%.*s) in error path: %d\n",
1406                                        dchild->d_name.len, dchild->d_name.name,
1407                                        err);
1408                         }
1409                 } else if (created) {
1410                         mds_lock_new_child(obd, dchild->d_inode, NULL);
1411                         /* save uid/gid for quota acquire/release */
1412                         qpids[USRQUOTA] = dparent->d_inode->i_uid;
1413                         qpids[GRPQUOTA] = dparent->d_inode->i_gid;
1414                 }
1415         case 1:
1416                 if (dchild) {
1417                         l_dput(dchild);
1418                         /* It is safe to leave IT_OPEN_LOCK set, if rc is not 0,
1419                          * mds_intent_policy won't try to return any locks */
1420                         if (rc && child_lockh->cookie)
1421                                 ldlm_lock_decref(child_lockh, child_mode);
1422                 }
1423                 if (dparent == NULL)
1424                         break;
1425
1426                 l_dput(dparent);
1427                 if (rc)
1428                         ldlm_lock_decref(&parent_lockh, parent_mode);
1429                 else
1430                         ptlrpc_save_lock(req, &parent_lockh, parent_mode);
1431         }
1432         /* trigger dqacq on the owner of child and parent */
1433         lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1434                       FSFILT_OP_CREATE);
1435
1436         RETURN(rc);
1437 }
1438
1439 /* Close a "file descriptor" and possibly unlink an orphan from the
1440  * PENDING directory.  Caller must hold child->i_mutex, this drops it.
1441  *
1442  * If we are being called from mds_disconnect() because the client has
1443  * disappeared, then req == NULL and we do not update last_rcvd because
1444  * there is nothing that could be recovered by the client at this stage
1445  * (it will not even _have_ an entry in last_rcvd anymore). */
1446 int mds_mfd_close(struct ptlrpc_request *req, int offset,
1447                   struct obd_device *obd, struct mds_file_data *mfd,
1448                   int unlink_orphan, struct lov_mds_md *lmm, int lmm_size,
1449                   struct llog_cookie *logcookies, int cookies_size,
1450                   __u64 *valid)
1451 {
1452         struct inode *inode = mfd->mfd_dentry->d_inode;
1453         char fidname[LL_FID_NAMELEN];
1454         int last_orphan, fidlen, rc = 0, cleanup_phase = 0;
1455         struct dentry *pending_child = NULL;
1456         struct mds_obd *mds = &obd->u.mds;
1457         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1458         void *handle = NULL;
1459         struct mds_body *request_body = NULL, *reply_body = NULL;
1460         struct lvfs_dentry_params dp = LVFS_DENTRY_PARAMS_INIT;
1461         struct iattr iattr = { 0 };
1462         ENTRY;
1463
1464         if (req && req->rq_reqmsg != NULL)
1465                 request_body = lustre_msg_buf(req->rq_reqmsg, offset,
1466                                               sizeof(*request_body));
1467         if (req && req->rq_repmsg != NULL)
1468                 reply_body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1469                                             sizeof(*reply_body));
1470
1471         fidlen = ll_fid2str(fidname, inode->i_ino, inode->i_generation);
1472
1473         CDEBUG(D_INODE, "inode %p ino %s nlink %d orphan %d\n", inode, fidname,
1474                inode->i_nlink, mds_orphan_open_count(inode));
1475
1476         last_orphan = (mds_orphan_open_dec_test(inode) &&
1477                        mds_inode_is_orphan(inode) &&
1478                        !obd->obd_recovering);
1479
1480         /* this is half of the actual "close" */
1481         if (mfd->mfd_mode & FMODE_WRITE) {
1482                 rc = mds_put_write_access(mds, inode, request_body,
1483                                           last_orphan && unlink_orphan);
1484         } else if (mfd->mfd_mode & MDS_FMODE_EXEC) {
1485                 mds_allow_write_access(inode);
1486         }
1487         /* here writecount change also needs protection from orphan write sem.
1488          * so drop orphan write sem after mds_put_write_access, bz 12888. */
1489         MDS_UP_WRITE_ORPHAN_SEM(inode);
1490
1491         if (last_orphan && unlink_orphan) {
1492                 int stripe_count = 0;
1493                 /* mds_put_write_access must have succeeded */
1494                 LASSERTF(rc == 0, "inode %lu/%u: rc %d",
1495                          inode->i_ino, inode->i_generation, rc);
1496
1497                 CDEBUG(D_INODE, "destroying orphan object %s\n", fidname);
1498
1499                 if ((S_ISREG(inode->i_mode) && inode->i_nlink != 1) ||
1500                     (S_ISDIR(inode->i_mode) && inode->i_nlink != 2))
1501                         CERROR("found \"orphan\" %s %s with link count %d\n",
1502                                S_ISREG(inode->i_mode) ? "file" : "dir",
1503                                fidname, inode->i_nlink);
1504
1505                 /* Sadly, there is no easy way to save pending_child from
1506                  * mds_reint_unlink() into mfd, so we need to re-lookup,
1507                  * but normally it will still be in the dcache. */
1508                 LOCK_INODE_MUTEX(pending_dir);
1509                 cleanup_phase = 1; /* UNLOCK_INODE_MUTEX(pending_dir) when finished */
1510                 pending_child = lookup_one_len(fidname, mds->mds_pending_dir,
1511                                                fidlen);
1512                 if (IS_ERR(pending_child))
1513                         GOTO(cleanup, rc = PTR_ERR(pending_child));
1514                 LASSERT(pending_child->d_inode != NULL);
1515
1516                 cleanup_phase = 2; /* dput(pending_child) when finished */
1517                 if (S_ISDIR(pending_child->d_inode->i_mode)) {
1518                         rc = ll_vfs_rmdir(pending_dir, pending_child,
1519                                           mds->mds_vfsmnt);
1520                         if (rc)
1521                                 CERROR("error unlinking orphan dir %s: rc %d\n",
1522                                        fidname,rc);
1523                         goto out;
1524                 }
1525
1526                 if (lmm != NULL) {
1527                         stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
1528                 }
1529
1530                 handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK,
1531                                           NULL, stripe_count);
1532                 if (IS_ERR(handle)) {
1533                         rc = PTR_ERR(handle);
1534                         handle = NULL;
1535                         GOTO(cleanup, rc);
1536                 }
1537                 if (lmm != NULL && (*valid & OBD_MD_FLEASIZE) &&
1538                     mds_log_op_unlink(obd, lmm, lmm_size,
1539                                       logcookies, cookies_size) > 0) {
1540                         *valid |= OBD_MD_FLCOOKIE;
1541                 }
1542
1543                 dp.ldp_inum = 0;
1544                 dp.ldp_ptr = req;
1545                 pending_child->d_fsdata = (void *) &dp;
1546                 rc = ll_vfs_unlink(pending_dir, pending_child, mds->mds_vfsmnt);
1547                 if (rc)
1548                         CERROR("error unlinking orphan %s: rc %d\n",fidname,rc);
1549
1550                 goto out; /* Don't bother updating attrs on unlinked inode */
1551         }
1552
1553         if (request_body != NULL) {
1554                 /* Only start a transaction to write out only the atime if it
1555                  * is more out-of-date than the specified limit.  If we are
1556                  * already going to write out the atime then do it anyway. */
1557                 if ((request_body->valid & OBD_MD_FLATIME) &&
1558                     ((LTIME_S(iattr.ia_atime) >
1559                       LTIME_S(inode->i_atime) + mds->mds_atime_diff) ||
1560                      (iattr.ia_valid != 0 &&
1561                       LTIME_S(iattr.ia_atime) > LTIME_S(inode->i_atime)))) {
1562                         LTIME_S(iattr.ia_atime) = request_body->atime;
1563                         iattr.ia_valid |= ATTR_ATIME;
1564                 }
1565
1566                 /* Store a rough estimate of the file size on the MDS for
1567                  * tools like e2scan and HSM that are just using this for *
1568                  * rough decision making and will get the proper size later.
1569                  * * This is NOT guaranteed to be correct with multiple *
1570                  * writers, but is only needed until SOM is done. b=11063 */
1571                 if (S_ISREG(inode->i_mode) &&
1572                     (request_body->valid & OBD_MD_FLSIZE) &&
1573                     (iattr.ia_valid != 0)) {
1574                         iattr.ia_size = request_body->size;
1575                         iattr.ia_valid |= ATTR_SIZE;
1576                 }
1577         }
1578
1579         if (iattr.ia_valid != 0) {
1580                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1581                 if (IS_ERR(handle)) {
1582                         rc = PTR_ERR(handle);
1583                         handle = NULL;
1584                         GOTO(cleanup, rc);
1585                 }
1586                 rc = fsfilt_setattr(obd, mfd->mfd_dentry, handle, &iattr, 0);
1587                 if (rc)
1588                         CERROR("error in setattr(%s): rc %d\n", fidname, rc);
1589         }
1590 out:
1591         /* If other clients have this file open for write, rc will be > 0 */
1592         if (rc > 0)
1593                 rc = 0;
1594
1595  cleanup:
1596         l_dput(mfd->mfd_dentry);
1597         mds_mfd_put(mfd);
1598         if (req != NULL && reply_body != NULL) {
1599                 rc = mds_finish_transno(mds, NULL, handle, req, rc, 0, 0);
1600         } else if (handle) {
1601                 int err = fsfilt_commit(obd, pending_dir, handle, 0);
1602                 if (err) {
1603                         CERROR("error committing close: %d\n", err);
1604                         if (!rc)
1605                                 rc = err;
1606                 }
1607         }
1608
1609         switch (cleanup_phase) {
1610         case 2:
1611                 dput(pending_child);
1612         case 1:
1613                 UNLOCK_INODE_MUTEX(pending_dir);
1614         }
1615         RETURN(rc);
1616 }
1617
1618 int mds_close(struct ptlrpc_request *req, int offset)
1619 {
1620         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1621         struct obd_device *obd = req->rq_export->exp_obd;
1622         struct mds_body *body;
1623         struct mds_file_data *mfd;
1624         struct lvfs_run_ctxt saved;
1625         struct inode *inode;
1626         int rc, repsize[4] = { sizeof(struct ptlrpc_body),
1627                                sizeof(struct mds_body),
1628                                obd->u.mds.mds_max_mdsize,
1629                                obd->u.mds.mds_max_cookiesize };
1630         struct mds_body *reply_body;
1631         struct lov_mds_md *lmm;
1632         int lmm_size;
1633         struct llog_cookie *logcookies;
1634         int cookies_size;
1635         ENTRY;
1636
1637         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1638                                   lustre_swab_mds_body);
1639         if (body == NULL) {
1640                 CERROR("Can't unpack body\n");
1641                 req->rq_status = -EFAULT;
1642                 GOTO(cleanup, rc = -EFAULT);
1643         }
1644         /*XXX need indicase - close is need to return LOV EA */
1645         body->valid |= OBD_MD_FLEASIZE;
1646
1647         rc = lustre_pack_reply(req, 4, repsize, NULL);
1648         if (rc)
1649                 req->rq_status = rc;
1650                 /* continue on to drop local open even if we can't send reply */
1651         else
1652                 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1653
1654         CDEBUG(D_INODE, "close req->rep_len %d mdsize %d cookiesize %d\n",
1655                req->rq_replen,
1656                obd->u.mds.mds_max_mdsize, obd->u.mds.mds_max_cookiesize);
1657         mds_counter_incr(req->rq_export, LPROC_MDS_CLOSE);
1658
1659         if (body->flags & MDS_BFLAG_UNCOMMITTED_WRITES)
1660                 /* do some stuff */ ;
1661
1662         spin_lock(&med->med_open_lock);
1663         mfd = mds_handle2mfd(&body->handle);
1664         if (mfd == NULL) {
1665                 spin_unlock(&med->med_open_lock);
1666                 DEBUG_REQ(D_ERROR, req, "no handle for file close ino "LPD64
1667                           ": cookie "LPX64, body->fid1.id, body->handle.cookie);
1668                 req->rq_status = -ESTALE;
1669                 GOTO(cleanup, rc = -ESTALE);
1670         }
1671         /* Remove mfd handle so it can't be found again.  We consume mfd_list
1672          * reference here, but still have mds_handle2mfd ref until mfd_close. */
1673         mds_mfd_unlink(mfd, 1);
1674         spin_unlock(&med->med_open_lock);
1675
1676         inode = mfd->mfd_dentry->d_inode;
1677         /* child orphan sem protects orphan_dec_test && is_orphan race */
1678         MDS_DOWN_WRITE_ORPHAN_SEM(inode); /* mds_mfd_close drops this */
1679         if (!obd->obd_recovering &&
1680             mds_inode_is_orphan(inode) && mds_orphan_open_count(inode) == 1) {
1681                 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1682                                       sizeof(*body));
1683                 LASSERT(body != NULL);
1684
1685                 mds_pack_inode2body(body, inode);
1686                 mds_pack_md(obd, req->rq_repmsg, REPLY_REC_OFF + 1, body, inode,
1687                             MDS_PACK_MD_LOCK, 0,
1688                             req->rq_export->exp_connect_flags);
1689         }
1690
1691         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1692         reply_body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*reply_body));
1693         lmm = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, 0);
1694         lmm_size = lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 1),
1695         logcookies = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 2, 0);
1696         cookies_size = lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 2);
1697         req->rq_status = mds_mfd_close(req, offset, obd, mfd, 1,
1698                                        lmm, lmm_size, logcookies, cookies_size,
1699                                        &reply_body->valid);
1700         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1701
1702 cleanup:
1703         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
1704                 CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
1705                 req->rq_status = -ENOMEM;
1706                 RETURN(-ENOMEM);
1707         }
1708
1709         RETURN(rc);
1710 }
1711
1712 int mds_done_writing(struct ptlrpc_request *req, int offset)
1713 {
1714         struct mds_body *body;
1715         int rc, size[2] = { sizeof(struct ptlrpc_body),
1716                             sizeof(struct mds_body) };
1717         ENTRY;
1718
1719         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1720
1721         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1722                                   lustre_swab_mds_body);
1723         if (body == NULL) {
1724                 CERROR("Can't unpack body\n");
1725                 req->rq_status = -EFAULT;
1726                 RETURN(-EFAULT);
1727         }
1728
1729         rc = lustre_pack_reply(req, 2, size, NULL);
1730         if (rc)
1731                 req->rq_status = rc;
1732
1733         RETURN(0);
1734 }