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