Whamcloud - gitweb
land b_eq on HEAD
[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  *  Copyright (c) 2003 Cluster File Systems, Inc.
5  *   Author: Peter Braam <braam@clusterfs.com>
6  *   Author: Andreas Dilger <adilger@clusterfs.com>
7  *   Author: Phil Schwan <phil@clusterfs.com>
8  *   Author: Mike Shaver <shaver@clusterfs.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
12  *   Lustre is free software; you can redistribute it and/or
13  *   modify it under the terms of version 2 of the GNU General Public
14  *   License as published by the Free Software Foundation.
15  *
16  *   Lustre is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with Lustre; if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #ifndef EXPORT_SYMTAB
27 # define EXPORT_SYMTAB
28 #endif
29 #define DEBUG_SUBSYSTEM S_MDS
30
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/version.h>
34 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
35 # include <linux/buffer_head.h>
36 # include <linux/workqueue.h>
37 #else
38 # include <linux/locks.h>
39 #endif
40
41 #include <linux/obd_class.h>
42 #include <linux/lustre_mds.h>
43 #include <linux/obd_lov.h>
44 #include <linux/lustre_fsfilt.h>
45 #include <linux/lprocfs_status.h>
46
47 #include "mds_internal.h"
48
49 /* Exported function from this file are:
50  *
51  * mds_open - called by the intent handler
52  * mds_close - an rpc handling function
53  * mds_pin - an rpc handling function - which will go away
54  * mds_mfd_close - for force closing files when a client dies
55  */
56
57 /*
58  * MDS file data handling: file data holds a handle for a file opened
59  * by a client.
60  */
61
62 static void mds_mfd_addref(void *mfdp)
63 {
64         struct mds_file_data *mfd = mfdp;
65
66         atomic_inc(&mfd->mfd_refcount);
67         CDEBUG(D_INFO, "GETting mfd %p : new refcount %d\n", mfd,
68                atomic_read(&mfd->mfd_refcount));
69 }
70
71 struct mds_file_data *mds_mfd_new(void)
72 {
73         struct mds_file_data *mfd;
74
75         OBD_ALLOC(mfd, sizeof *mfd);
76         if (mfd == NULL) {
77                 CERROR("mds: out of memory\n");
78                 return NULL;
79         }
80
81         atomic_set(&mfd->mfd_refcount, 2);
82
83         INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
84         class_handle_hash(&mfd->mfd_handle, mds_mfd_addref);
85
86         return mfd;
87 }
88
89 static struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
90 {
91         ENTRY;
92         LASSERT(handle != NULL);
93         RETURN(class_handle2object(handle->cookie));
94 }
95
96 static void mds_mfd_put(struct mds_file_data *mfd)
97 {
98         CDEBUG(D_INFO, "PUTting mfd %p : new refcount %d\n", mfd,
99                atomic_read(&mfd->mfd_refcount) - 1);
100         LASSERT(atomic_read(&mfd->mfd_refcount) > 0 &&
101                 atomic_read(&mfd->mfd_refcount) < 0x5a5a);
102         if (atomic_dec_and_test(&mfd->mfd_refcount)) {
103                 LASSERT(list_empty(&mfd->mfd_handle.h_link));
104                 OBD_FREE(mfd, sizeof *mfd);
105         }
106 }
107
108 static void mds_mfd_destroy(struct mds_file_data *mfd)
109 {
110         class_handle_unhash(&mfd->mfd_handle);
111         mds_mfd_put(mfd);
112 }
113
114
115 /* Caller must hold mds->mds_epoch_sem */
116 static int mds_alloc_filterdata(struct inode *inode)
117 {
118         LASSERT(inode->i_filterdata == NULL);
119         OBD_ALLOC(inode->i_filterdata, sizeof(struct mds_filter_data));
120         if (inode->i_filterdata == NULL)
121                 return -ENOMEM;
122         LASSERT(igrab(inode) == inode);
123         return 0;
124 }
125
126 /* Caller must hold mds->mds_epoch_sem */
127 static void mds_free_filterdata(struct inode *inode)
128 {
129         LASSERT(inode->i_filterdata != NULL);
130         OBD_FREE(inode->i_filterdata, sizeof(struct mds_filter_data));
131         inode->i_filterdata = NULL;
132         iput(inode);
133 }
134
135 /* Write access to a file: executors cause a negative count,
136  * writers a positive count.  The semaphore is needed to perform
137  * a check for the sign and then increment or decrement atomically.
138  *
139  * This code is closely tied to the allocation of the d_fsdata and the
140  * MDS epoch, so we use the same semaphore for the whole lot.
141  *
142  * We could use a different semaphore for each file, if it ever shows
143  * up in a profile, which it won't.
144  *
145  * epoch argument is nonzero during recovery */
146 static int mds_get_write_access(struct mds_obd *mds, struct inode *inode,
147                                 __u64 epoch)
148 {
149         int rc = 0;
150
151         down(&mds->mds_epoch_sem);
152
153         if (atomic_read(&inode->i_writecount) < 0) {
154                 up(&mds->mds_epoch_sem);
155                 RETURN(-ETXTBSY);
156         }
157
158
159         if (MDS_FILTERDATA(inode) && MDS_FILTERDATA(inode)->io_epoch != 0) {
160                 CDEBUG(D_INODE, "continuing MDS epoch "LPU64" for ino %lu/%u\n",
161                        MDS_FILTERDATA(inode)->io_epoch, inode->i_ino,
162                        inode->i_generation);
163                 goto out;
164         }
165
166         if (inode->i_filterdata == NULL)
167                 mds_alloc_filterdata(inode);
168         if (inode->i_filterdata == NULL) {
169                 rc = -ENOMEM;
170                 goto out;
171         }
172         if (epoch > mds->mds_io_epoch)
173                 mds->mds_io_epoch = epoch;
174         else
175                 mds->mds_io_epoch++;
176         MDS_FILTERDATA(inode)->io_epoch = mds->mds_io_epoch;
177         CDEBUG(D_INODE, "starting MDS epoch "LPU64" for ino %lu/%u\n",
178                mds->mds_io_epoch, inode->i_ino, inode->i_generation);
179  out:
180         if (rc == 0)
181                 atomic_inc(&inode->i_writecount);
182         up(&mds->mds_epoch_sem);
183         return rc;
184 }
185
186 /* Returns EAGAIN if the client needs to get size and/or cookies and close
187  * again -- which is never true if the file is about to be unlinked.  Otherwise
188  * returns the number of remaining writers. */
189 static int mds_put_write_access(struct mds_obd *mds, struct inode *inode,
190                                 struct mds_body *body, int unlinking)
191 {
192         int rc = 0;
193         ENTRY;
194
195         down(&mds->mds_epoch_sem);
196         atomic_dec(&inode->i_writecount);
197         rc = atomic_read(&inode->i_writecount);
198         if (rc > 0)
199                 GOTO(out, rc);
200 #if 0
201         if (!unlinking && !(body->valid & OBD_MD_FLSIZE))
202                 GOTO(out, rc = EAGAIN);
203 #endif
204         mds_free_filterdata(inode);
205  out:
206         up(&mds->mds_epoch_sem);
207         return rc;
208 }
209
210 static int mds_deny_write_access(struct mds_obd *mds, struct inode *inode)
211 {
212         ENTRY;
213         down(&mds->mds_epoch_sem);
214         if (atomic_read(&inode->i_writecount) > 0) {
215                 up(&mds->mds_epoch_sem);
216                 RETURN(-ETXTBSY);
217         }
218         atomic_dec(&inode->i_writecount);
219         up(&mds->mds_epoch_sem);
220         RETURN(0);
221 }
222
223 static void mds_allow_write_access(struct inode *inode)
224 {
225         ENTRY;
226         atomic_inc(&inode->i_writecount);
227 }
228
229 int mds_query_write_access(struct inode *inode)
230 {
231         ENTRY;
232         RETURN(atomic_read(&inode->i_writecount));
233 }
234
235 /* This replaces the VFS mds_dentry_open, it manages mfd and writecount */
236 static struct mds_file_data *mds_dentry_open(struct dentry *dentry,
237                                              struct vfsmount *mnt, int flags,
238                                              struct ptlrpc_request *req)
239 {
240         struct mds_export_data *med = &req->rq_export->exp_mds_data;
241         struct mds_obd *mds = mds_req2mds(req);
242         struct mds_file_data *mfd;
243         struct mds_body *body;
244         int error;
245         ENTRY;
246
247         mfd = mds_mfd_new();
248         if (mfd == NULL) {
249                 CERROR("mds: out of memory\n");
250                 GOTO(cleanup_dentry, error = -ENOMEM);
251         }
252
253         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
254
255         if (flags & FMODE_WRITE) {
256                 /* FIXME: in recovery, need to pass old epoch here */
257                 error = mds_get_write_access(mds, dentry->d_inode, 0);
258                 if (error)
259                         GOTO(cleanup_mfd, error);
260                 body->io_epoch = MDS_FILTERDATA(dentry->d_inode)->io_epoch;
261         } else if (flags & FMODE_EXEC) {
262                 error = mds_deny_write_access(mds, dentry->d_inode);
263                 if (error)
264                         GOTO(cleanup_mfd, error);
265         }
266
267         dget(dentry);
268
269         /* Mark the file as open to handle open-unlink. */
270         mds_open_orphan_inc(dentry->d_inode);
271
272         mfd->mfd_mode = flags;
273         mfd->mfd_dentry = dentry;
274         mfd->mfd_xid = req->rq_xid;
275
276         spin_lock(&med->med_open_lock);
277         list_add(&mfd->mfd_list, &med->med_open_head);
278         spin_unlock(&med->med_open_lock);
279         mds_mfd_put(mfd);
280
281         body->handle.cookie = mfd->mfd_handle.h_cookie;
282
283         RETURN(mfd);
284
285 cleanup_mfd:
286         mds_mfd_put(mfd);
287         mds_mfd_destroy(mfd);
288 cleanup_dentry:
289         return ERR_PTR(error);
290 }
291
292 static void mds_objids_from_lmm(obd_id *ids, struct lov_mds_md *lmm,
293                                 struct lov_desc *desc)
294 {
295         int i;
296         for (i = 0; i < le32_to_cpu(lmm->lmm_stripe_count); i++) {
297                 ids[le32_to_cpu(lmm->lmm_objects[i].l_ost_idx)] =
298                         le64_to_cpu(lmm->lmm_objects[i].l_object_id);
299         }
300 }
301
302 /* Must be called with i_sem held */
303 static int mds_create_objects(struct ptlrpc_request *req, int offset,
304                               struct mds_update_record *rec,
305                               struct mds_obd *mds, struct obd_device *obd,
306                               struct inode *inode, void **handle, obd_id **ids)
307 {
308         struct obdo *oa;
309         struct obd_trans_info oti = { 0 };
310         struct mds_body *body;
311         struct lov_stripe_md *lsm = NULL;
312         struct lov_mds_md *lmm = NULL;
313         void *lmm_buf;
314         int rc, lmm_bufsize, lmm_size;
315         ENTRY;
316
317         if (rec->ur_flags & MDS_OPEN_DELAY_CREATE ||
318             !(rec->ur_flags & FMODE_WRITE))
319                 RETURN(0);
320
321         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*body));
322
323         if (!S_ISREG(inode->i_mode))
324                 RETURN(0);
325         if (body->valid & OBD_MD_FLEASIZE)
326                 RETURN(0);
327
328         OBD_ALLOC(*ids, mds->mds_lov_desc.ld_tgt_count * sizeof(**ids));
329         if (*ids == NULL)
330                 RETURN(-ENOMEM);
331         oti.oti_objid = *ids;
332
333         if (*handle == NULL)
334                 *handle = fsfilt_start(obd, inode, FSFILT_OP_CREATE, NULL);
335         if (IS_ERR(*handle)) {
336                 rc = PTR_ERR(*handle);
337                 *handle = NULL;
338                 GOTO(out_ids, rc);
339         }
340
341         /* replay case */
342         if (rec->ur_fid2->id) {
343                 body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
344                 lmm_size = rec->ur_eadatalen;
345                 lmm = rec->ur_eadata;
346                 LASSERT(lmm);
347
348                 mds_objids_from_lmm(*ids, lmm, &mds->mds_lov_desc);
349
350                 lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, 0);
351                 lmm_bufsize = req->rq_repmsg->buflens[offset];
352                 LASSERT(lmm_buf);
353                 LASSERT(lmm_bufsize >= lmm_size);
354                 memcpy(lmm_buf, lmm, lmm_size);
355                 rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size);
356                 if (rc)
357                         CERROR("open replay failed to set md:%d\n", rc);
358                 RETURN(0);
359         }
360
361         oa = obdo_alloc();
362         if (oa == NULL)
363                 GOTO(out_ids, rc = -ENOMEM);
364         oa->o_mode = S_IFREG | 0600;
365         oa->o_id = inode->i_ino;
366         oa->o_generation = inode->i_generation;
367         oa->o_uid = 0; /* must have 0 uid / gid on OST */
368         oa->o_gid = 0;
369         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLTYPE |
370                 OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID;
371         oa->o_size = 0;
372
373         obdo_from_inode(oa, inode, OBD_MD_FLTYPE|OBD_MD_FLATIME|OBD_MD_FLMTIME|
374                         OBD_MD_FLCTIME);
375
376         if (!(rec->ur_flags & MDS_OPEN_HAS_OBJS)) {
377                 /* check if things like lstripe/lfs stripe are sending us the ea */
378                 if (rec->ur_flags & MDS_OPEN_HAS_EA) {
379                         rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, 
380                                            mds->mds_osc_exp,
381                                            0, &lsm, rec->ur_eadata);
382                         if (rc)
383                                 GOTO(out_oa, rc);
384                 }
385                 rc = obd_create(mds->mds_osc_exp, oa, &lsm, &oti);
386                 if (rc) {
387                         int level = D_ERROR;
388                         if (rc == -ENOSPC)
389                                 level = D_INODE;
390                         CDEBUG(level, "error creating objects for "
391                                       "inode %lu: rc = %d\n",
392                                inode->i_ino, rc);
393                         if (rc > 0) {
394                                 CERROR("obd_create returned invalid "
395                                        "rc %d\n", rc);
396                                 rc = -EIO;
397                         }
398                         GOTO(out_oa, rc);
399                 }
400         } else {
401                 rc = obd_iocontrol(OBD_IOC_LOV_SETEA, mds->mds_osc_exp,
402                                    0, &lsm, rec->ur_eadata);
403                 if (rc) {
404                         GOTO(out_oa, rc);
405                 }
406                 lsm->lsm_object_id = oa->o_id;
407         }
408         if (inode->i_size) {
409                 oa->o_size = inode->i_size;
410                 obdo_from_inode(oa, inode, OBD_MD_FLTYPE|OBD_MD_FLATIME|
411                                 OBD_MD_FLMTIME| OBD_MD_FLCTIME| OBD_MD_FLSIZE);
412                 rc = obd_setattr(mds->mds_osc_exp, oa, lsm, &oti);
413                 if (rc) {
414                         CERROR("error setting attrs for inode %lu: rc %d\n",
415                                inode->i_ino, rc);
416                         if (rc > 0) {
417                                 CERROR("obd_setattr returned bad rc %d\n", rc);
418                                 rc = -EIO;
419                         }
420                         GOTO(out_oa, rc);
421                 }
422         }
423
424         body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
425         obdo_refresh_inode(inode, oa, OBD_MD_FLBLKSZ);
426
427         LASSERT(lsm && lsm->lsm_object_id);
428         lmm = NULL;
429         rc = obd_packmd(mds->mds_osc_exp, &lmm, lsm);
430         if (!rec->ur_fid2->id)
431                 obd_free_memmd(mds->mds_osc_exp, &lsm);
432         LASSERT(rc >= 0);
433         lmm_size = rc;
434         body->eadatasize = rc;
435         rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size);
436         lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, 0);
437         lmm_bufsize = req->rq_repmsg->buflens[offset];
438         LASSERT(lmm_buf);
439         LASSERT(lmm_bufsize >= lmm_size);
440
441         memcpy(lmm_buf, lmm, lmm_size);
442         obd_free_diskmd(mds->mds_osc_exp, &lmm);
443  out_oa:
444         oti_free_cookies(&oti);
445         obdo_free(oa);
446  out_ids:
447         if (rc) {
448                 OBD_FREE(*ids, mds->mds_lov_desc.ld_tgt_count * sizeof(**ids));
449                 *ids = NULL;
450         }
451         RETURN(rc);
452 }
453
454 static void reconstruct_open(struct mds_update_record *rec, int offset,
455                              struct ptlrpc_request *req,
456                              struct lustre_handle *child_lockh)
457 {
458         struct mds_export_data *med = &req->rq_export->exp_mds_data;
459         struct mds_client_data *mcd = med->med_mcd;
460         struct mds_obd *mds = mds_req2mds(req);
461         struct mds_file_data *mfd;
462         struct obd_device *obd = req->rq_export->exp_obd;
463         struct dentry *parent, *child;
464         struct ldlm_reply *rep;
465         struct mds_body *body;
466         int rc;
467         struct list_head *t;
468         int put_child = 1;
469         ENTRY;
470
471         LASSERT(offset == 2);                  /* only called via intent */
472         rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
473         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
474
475         /* copy rc, transno and disp; steal locks */
476         mds_req_from_mcd(req, mcd);
477         intent_set_disposition(rep, mcd->mcd_last_data);
478
479         /* Only replay if create or open actually happened. */
480         if (!intent_disposition(rep, DISP_OPEN_CREATE | DISP_OPEN_OPEN) ) {
481                 EXIT;
482                 return; /* error looking up parent or child */
483         }
484
485         parent = mds_fid2dentry(mds, rec->ur_fid1, NULL);
486         LASSERT(!IS_ERR(parent));
487
488         child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
489         LASSERT(!IS_ERR(child));
490
491         if (!child->d_inode) {
492                 GOTO(out_dput, 0); /* child not present to open */
493         }
494
495         /* At this point, we know we have a child. We'll send
496          * it back _unless_ it not created and open failed.
497          */
498         if (intent_disposition(rep, DISP_OPEN_OPEN) &&
499             !intent_disposition(rep, DISP_OPEN_CREATE) &&
500             req->rq_status) {
501                 GOTO(out_dput, 0);
502         }
503
504         /* get lock (write for O_CREAT, read otherwise) */
505
506         mds_pack_inode2fid(&body->fid1, child->d_inode);
507         mds_pack_inode2body(body, child->d_inode);
508         if (S_ISREG(child->d_inode->i_mode)) {
509                 rc = mds_pack_md(obd, req->rq_repmsg, 2, body,
510                                  child->d_inode, 1);
511
512                 if (rc)
513                         LASSERT(rc == req->rq_status);
514
515                 /* If we have LOV EA data, the OST holds size, mtime */
516                 if (!(body->valid & OBD_MD_FLEASIZE))
517                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
518                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
519         } else {
520                 /* XXX need to check this case */
521         }
522
523         /* If we're opening a file without an EA, change to a write
524            lock (unless we already have one). */
525
526         /* If we have -EEXIST as the status, and we were asked to create
527          * exclusively, we can tell we failed because the file already existed.
528          */
529         if (req->rq_status == -EEXIST &&
530             ((rec->ur_flags & (MDS_OPEN_CREAT | MDS_OPEN_EXCL)) ==
531              (MDS_OPEN_CREAT | MDS_OPEN_EXCL))) {
532                 GOTO(out_dput, 0);
533         }
534
535         /* If we didn't get as far as trying to open, then some locking thing
536          * probably went wrong, and we'll just bail here.
537          */
538         if (!intent_disposition(rep, DISP_OPEN_OPEN))
539                 GOTO(out_dput, 0);
540
541         /* If we failed, then we must have failed opening, so don't look for
542          * file descriptor or anything, just give the client the bad news.
543          */
544         if (req->rq_status)
545                 GOTO(out_dput, 0);
546
547         mfd = NULL;
548         list_for_each(t, &med->med_open_head) {
549                 mfd = list_entry(t, struct mds_file_data, mfd_list);
550                 if (mfd->mfd_xid == req->rq_xid)
551                         break;
552                 mfd = NULL;
553         }
554
555 #warning "XXX fixme"
556         /* Here it used to LASSERT(mfd) if exp_outstanding_reply != NULL.
557          * Now that exp_outstanding_reply is a list, it's just using mfd != NULL
558          * to detect a re-open */
559         if (mfd == NULL) {
560                 mntget(mds->mds_vfsmnt);
561                 CERROR("Re-opened file \n");
562                 mfd = mds_dentry_open(child, mds->mds_vfsmnt,
563                                       rec->ur_flags & ~MDS_OPEN_TRUNC, req);
564                 if (!mfd) {
565                         CERROR("mds: out of memory\n");
566                         GOTO(out_dput, req->rq_status = -ENOMEM);
567                 }
568                 put_child = 0;
569         }
570
571  out_dput:
572         if (put_child)
573                 l_dput(child);
574         l_dput(parent);
575         EXIT;
576 }
577
578 /* do NOT or the MAY_*'s, you'll get the weakest */
579 static int accmode(int flags)
580 {
581         int res = 0;
582
583         if (flags & FMODE_READ)
584                 res = MAY_READ;
585         if (flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
586                 res |= MAY_WRITE;
587         if (flags & FMODE_EXEC)
588                 res = MAY_EXEC;
589         return res;
590 }
591
592 /* Handles object creation, actual opening, and I/O epoch */
593 static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
594                            struct mds_body *body, int flags, void **handle,
595                            struct mds_update_record *rec,struct ldlm_reply *rep)
596 {
597         struct mds_obd *mds = mds_req2mds(req);
598         struct obd_device *obd = req->rq_export->exp_obd;
599         struct mds_file_data *mfd = NULL;
600         obd_id *ids = NULL; /* object IDs created */
601         int rc = 0;
602         ENTRY;
603
604         /* atomically create objects if necessary */
605         down(&dchild->d_inode->i_sem);
606         if (S_ISREG(dchild->d_inode->i_mode) &&
607             !(body->valid & OBD_MD_FLEASIZE)) {
608                 rc = mds_pack_md(obd, req->rq_repmsg, 2, body,
609                                  dchild->d_inode, 0);
610                 if (rc) {
611                         up(&dchild->d_inode->i_sem);
612                         RETURN(rc);
613                 }
614         }
615         if (rec != NULL) {
616                 /* no EA: create objects */
617                 rc = mds_create_objects(req, 2, rec, mds, obd,
618                                         dchild->d_inode, handle, &ids);
619                 if (rc) {
620                         CERROR("mds_create_objects: rc = %d\n", rc);
621                         up(&dchild->d_inode->i_sem);
622                         RETURN(rc);
623                 }
624         }
625         /* If the inode has EA data, then OSTs hold size, mtime */
626         if (S_ISREG(dchild->d_inode->i_mode) &&
627             !(body->valid & OBD_MD_FLEASIZE)) {
628                 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
629                                 OBD_MD_FLATIME | OBD_MD_FLMTIME);
630         }
631         up(&dchild->d_inode->i_sem);
632
633         intent_set_disposition(rep, DISP_OPEN_OPEN);
634         mfd = mds_dentry_open(dchild, mds->mds_vfsmnt, flags, req);
635         if (IS_ERR(mfd))
636                 RETURN(PTR_ERR(mfd));
637
638         CDEBUG(D_INODE, "mfd %p, cookie "LPX64"\n", mfd,
639                mfd->mfd_handle.h_cookie);
640         if (ids != NULL) {
641                 mds_lov_update_objids(obd, ids);
642                 OBD_FREE(ids, sizeof(*ids) * mds->mds_lov_desc.ld_tgt_count);
643         }
644         //if (rc)
645         //        mds_mfd_destroy(mfd);
646         RETURN(rc);
647 }
648
649 static int mds_open_by_fid(struct ptlrpc_request *req, struct ll_fid *fid,
650                            struct mds_body *body, int flags,
651                            struct mds_update_record *rec,struct ldlm_reply *rep)
652 {
653         struct mds_obd *mds = mds_req2mds(req);
654         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
655         struct dentry *dchild;
656         char fidname[LL_FID_NAMELEN];
657         int fidlen = 0, rc;
658         void *handle = NULL;
659         ENTRY;
660
661         down(&pending_dir->i_sem);
662         fidlen = ll_fid2str(fidname, fid->id, fid->generation);
663         dchild = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
664         if (IS_ERR(dchild)) {
665                 up(&pending_dir->i_sem);
666                 rc = PTR_ERR(dchild);
667                 CERROR("error looking up %s in PENDING: rc = %d\n",fidname, rc);
668                 RETURN(rc);
669         }
670
671         if (dchild->d_inode != NULL) {
672                 up(&pending_dir->i_sem);
673                 mds_inode_set_orphan(dchild->d_inode);
674                 mds_pack_inode2fid(&body->fid1, dchild->d_inode);
675                 mds_pack_inode2body(body, dchild->d_inode);
676                 intent_set_disposition(rep, DISP_LOOKUP_EXECD);
677                 intent_set_disposition(rep, DISP_LOOKUP_POS);
678                 CWARN("Orphan %s found and opened in PENDING directory\n",
679                        fidname);
680                 goto open;
681         }
682         dput(dchild);
683         up(&pending_dir->i_sem);
684
685         /* We didn't find it in PENDING so it isn't an orphan.  See
686          * if it was a regular inode that was previously created. */
687         dchild = mds_fid2dentry(mds, fid, NULL);
688         if (IS_ERR(dchild))
689                 RETURN(PTR_ERR(dchild));
690
691         mds_pack_inode2fid(&body->fid1, dchild->d_inode);
692         mds_pack_inode2body(body, dchild->d_inode);
693         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
694         intent_set_disposition(rep, DISP_LOOKUP_POS);
695
696  open:
697         rc = mds_finish_open(req, dchild, body, flags, &handle, rec, rep);
698         rc = mds_finish_transno(mds, dchild ? dchild->d_inode : NULL, handle,
699                                 req, rc, rep ? rep->lock_policy_res1 : 0);
700         /* XXX what do we do here if mds_finish_transno itself failed? */
701
702         l_dput(dchild);
703         RETURN(rc);
704 }
705
706 int mds_pin(struct ptlrpc_request *req)
707 {
708         struct obd_device *obd = req->rq_export->exp_obd;
709         struct mds_body *request_body, *reply_body;
710         struct obd_run_ctxt saved;
711         int rc, size = sizeof(*reply_body);
712         ENTRY;
713
714         request_body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*request_body));
715
716         rc = lustre_pack_reply(req, 1, &size, NULL);
717         if (rc)
718                 RETURN(rc);
719         reply_body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reply_body));
720
721         push_ctxt(&saved, &obd->obd_ctxt, NULL);
722         rc = mds_open_by_fid(req, &request_body->fid1, reply_body,
723                              request_body->flags, NULL, NULL);
724         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
725
726         RETURN(rc);
727 }
728
729 /*  Get a lock on the ino to sync with creation WRT inode reuse (bug 2029).
730  *  If child_lockh is NULL we just get the lock as a barrier to wait for
731  *  other holders of this lock, and drop it right away again. */
732 int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
733                        struct lustre_handle *child_lockh)
734 {
735         struct ldlm_res_id child_res_id = { .name = { inode->i_ino, 0, 1, 0 } };
736         struct lustre_handle lockh;
737         int lock_flags = 0;
738         int rc;
739
740         if (child_lockh == NULL)
741                 child_lockh = &lockh;
742
743         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
744                               child_res_id, LDLM_PLAIN, NULL, 0,
745                               LCK_EX, &lock_flags, ldlm_completion_ast,
746                               mds_blocking_ast, NULL, child_lockh);
747         if (rc != ELDLM_OK)
748                 CERROR("ldlm_cli_enqueue: %d\n", rc);
749         else if (child_lockh == &lockh)
750                 ldlm_lock_decref(child_lockh, LCK_EX);
751
752         RETURN(rc);
753 }
754
755 int mds_open(struct mds_update_record *rec, int offset,
756              struct ptlrpc_request *req, struct lustre_handle *child_lockh)
757 {
758         /* XXX ALLOCATE _something_ - 464 bytes on stack here */
759         struct obd_device *obd = req->rq_export->exp_obd;
760         struct mds_obd *mds = mds_req2mds(req);
761         struct ldlm_reply *rep = NULL;
762         struct mds_body *body = NULL;
763         struct dentry *dchild = NULL, *dparent = NULL;
764         struct mds_export_data *med;
765         struct lustre_handle parent_lockh;
766         int rc = 0, cleanup_phase = 0, acc_mode, created = 0;
767         int parent_mode = LCK_PR;
768         void *handle = NULL;
769         struct dentry_params dp;
770         ENTRY;
771
772         if (offset == 2) { /* intent */
773                 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
774                 body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
775         } else if (offset == 0) { /* non-intent reint */
776                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
777         } else {
778                 body = NULL;
779                 LBUG();
780         }
781
782         MDS_CHECK_RESENT(req, reconstruct_open(rec, offset, req, child_lockh));
783
784         /* Step 0: If we are passed a fid, then we assume the client already
785          * opened this file and is only replaying the RPC, so we open the
786          * inode by fid (at some large expense in security). */
787         if (rec->ur_fid2->id) {
788                 rc = mds_open_by_fid(req, rec->ur_fid2, body, rec->ur_flags,
789                                      rec, rep);
790                 if (rc != -ENOENT)
791                         RETURN(rc);
792                 /* We didn't find the correct inode on disk either, so we
793                  * need to re-create it via a regular replay. */
794                 LASSERT(rec->ur_flags & MDS_OPEN_CREAT);
795         }
796         LASSERT(offset == 2); /* If we got here, we must be called via intent */
797
798         med = &req->rq_export->exp_mds_data;
799         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
800                 CERROR("test case OBD_FAIL_MDS_OPEN_PACK\n");
801                 RETURN(-ENOMEM);
802         }
803
804         acc_mode = accmode(rec->ur_flags);
805
806         /* Step 1: Find and lock the parent */
807         if (rec->ur_flags & O_CREAT)
808                 parent_mode = LCK_PW;
809         dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, parent_mode,
810                                         &parent_lockh, rec->ur_name,
811                                         rec->ur_namelen - 1);
812         if (IS_ERR(dparent)) {
813                 rc = PTR_ERR(dparent);
814                 CERROR("parent lookup error %d\n", rc);
815                 GOTO(cleanup, rc);
816         }
817         LASSERT(dparent->d_inode != NULL);
818
819         cleanup_phase = 1; /* parent dentry and lock */
820
821         /* Step 2: Lookup the child */
822         dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
823         if (IS_ERR(dchild)) {
824                 rc = PTR_ERR(dchild);
825                 dchild = NULL; /* don't confuse mds_finish_transno() below */
826                 GOTO(cleanup, rc);
827         }
828
829         cleanup_phase = 2; /* child dentry */
830
831         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
832         if (dchild->d_inode)
833                 intent_set_disposition(rep, DISP_LOOKUP_POS);
834         else
835                 intent_set_disposition(rep, DISP_LOOKUP_NEG);
836
837         /*Step 3: If the child was negative, and we're supposed to, create it.*/
838         if (dchild->d_inode == NULL) {
839                 unsigned long ino = rec->ur_fid2->id;
840                 struct iattr iattr;
841                 struct inode *inode;
842
843                 if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
844                         /* It's negative and we weren't supposed to create it */
845                         GOTO(cleanup, rc = -ENOENT);
846                 }
847
848                 intent_set_disposition(rep, DISP_OPEN_CREATE);
849                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_CREATE,
850                                       NULL);
851                 if (IS_ERR(handle)) {
852                         rc = PTR_ERR(handle);
853                         handle = NULL;
854                         GOTO(cleanup, rc);
855                 }
856                 dchild->d_fsdata = (void *) &dp;
857                 dp.p_ptr = req;
858                 dp.p_inum = ino;
859
860                 rc = ll_vfs_create(dparent->d_inode, dchild, rec->ur_mode,NULL);
861                 if (dchild->d_fsdata == (void *)(unsigned long)ino)
862                         dchild->d_fsdata = NULL;
863
864                 if (rc) {
865                         CDEBUG(D_INODE, "error during create: %d\n", rc);
866                         GOTO(cleanup, rc);
867                 }
868                 inode = dchild->d_inode;
869                 if (ino) {
870                         LASSERT(ino == inode->i_ino);
871                         /* Written as part of setattr */
872                         inode->i_generation = rec->ur_fid2->generation;
873                         CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
874                                inode->i_ino, inode->i_generation);
875                 }
876
877                 created = 1;
878                 LTIME_S(iattr.ia_atime) = rec->ur_time;
879                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
880                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
881
882                 iattr.ia_uid = rec->ur_fsuid;
883                 if (dparent->d_inode->i_mode & S_ISGID)
884                         iattr.ia_gid = dparent->d_inode->i_gid;
885                 else
886                         iattr.ia_gid = rec->ur_fsgid;
887
888                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
889                         ATTR_MTIME | ATTR_CTIME;
890
891                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
892                 if (rc)
893                         CERROR("error on child setattr: rc = %d\n", rc);
894
895                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
896
897                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
898                 if (rc)
899                         CERROR("error on parent setattr: rc = %d\n", rc);
900
901                 acc_mode = 0;                  /* Don't check for permissions */
902         }
903
904         LASSERT(!mds_inode_is_orphan(dchild->d_inode));
905
906         mds_pack_inode2fid(&body->fid1, dchild->d_inode);
907         mds_pack_inode2body(body, dchild->d_inode);
908
909         if (S_ISREG(dchild->d_inode->i_mode)) {
910                 /* Check permissions etc */
911                 rc = ll_permission(dchild->d_inode, acc_mode, NULL);
912                 if (rc != 0)
913                         GOTO(cleanup, rc);
914
915                 /* Can't write to a read-only file */
916                 if (IS_RDONLY(dchild->d_inode) && (acc_mode & MAY_WRITE) != 0)
917                         GOTO(cleanup, rc = -EPERM);
918
919                 /* An append-only file must be opened in append mode for
920                  * writing */
921                 if (IS_APPEND(dchild->d_inode) && (acc_mode & MAY_WRITE) != 0 &&
922                     ((rec->ur_flags & MDS_OPEN_APPEND) == 0 ||
923                      (rec->ur_flags & MDS_OPEN_TRUNC) != 0))
924                         GOTO(cleanup, rc = -EPERM);
925         }
926
927         if (!created && (rec->ur_flags & MDS_OPEN_CREAT) &&
928             (rec->ur_flags & MDS_OPEN_EXCL)) {
929                 /* File already exists, we didn't just create it, and we
930                  * were passed O_EXCL; err-or. */
931                 GOTO(cleanup, rc = -EEXIST); // returns a lock to the client
932         }
933
934         /* if we are following a symlink, don't open */
935         if (S_ISLNK(dchild->d_inode->i_mode))
936                 GOTO(cleanup, rc = 0);
937
938         if ((rec->ur_flags & MDS_OPEN_DIRECTORY) &&
939             !S_ISDIR(dchild->d_inode->i_mode))
940                 GOTO(cleanup, rc = -ENOTDIR);
941
942         /* Step 5: mds_open it */
943         rc = mds_finish_open(req, dchild, body, rec->ur_flags, &handle, rec,
944                              rep);
945         GOTO(cleanup, rc);
946
947  cleanup:
948         rc = mds_finish_transno(mds, dchild ? dchild->d_inode : NULL, handle,
949                                 req, rc, rep ? rep->lock_policy_res1 : 0);
950
951         switch (cleanup_phase) {
952         case 2:
953                 if (rc && created) {
954                         int err = vfs_unlink(dparent->d_inode, dchild);
955                         if (err) {
956                                 CERROR("unlink(%*s) in error path: %d\n",
957                                        dchild->d_name.len, dchild->d_name.name,
958                                        err);
959                         }
960                 } else if (created) {
961                         mds_lock_new_child(obd, dchild->d_inode, NULL);
962                 }
963                 l_dput(dchild);
964         case 1:
965                 if (dparent == NULL)
966                         break;
967
968                 l_dput(dparent);
969                 if (rc)
970                         ldlm_lock_decref(&parent_lockh, parent_mode);
971                 else
972                         ptlrpc_save_lock (req, &parent_lockh, parent_mode);
973         }
974         if (rc == 0)
975                 atomic_inc(&mds->mds_open_count);
976         RETURN(rc);
977 }
978
979 /* Close a "file descriptor" and possibly unlink an orphan from the
980  * PENDING directory.
981  *
982  * If we are being called from mds_disconnect() because the client has
983  * disappeared, then req == NULL and we do not update last_rcvd because
984  * there is nothing that could be recovered by the client at this stage
985  * (it will not even _have_ an entry in last_rcvd anymore).
986  *
987  * Returns EAGAIN if the client needs to get more data and re-close. */
988 int mds_mfd_close(struct ptlrpc_request *req, struct obd_device *obd,
989                   struct mds_file_data *mfd, int unlink_orphan)
990 {
991         struct inode *inode = mfd->mfd_dentry->d_inode;
992         char fidname[LL_FID_NAMELEN];
993         int last_orphan, fidlen, rc = 0, cleanup_phase = 0;
994         struct dentry *pending_child = NULL;
995         struct mds_obd *mds = &obd->u.mds;
996         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
997         void *handle = NULL;
998         struct mds_body *request_body = NULL, *reply_body = NULL;
999         struct dentry_params dp;
1000         ENTRY;
1001
1002         if (req != NULL) {
1003                 request_body = lustre_msg_buf(req->rq_reqmsg, 0,
1004                                               sizeof(*request_body));
1005                 reply_body = lustre_msg_buf(req->rq_repmsg, 0,
1006                                             sizeof(*reply_body));
1007         }
1008
1009         fidlen = ll_fid2str(fidname, inode->i_ino, inode->i_generation);
1010
1011         last_orphan = mds_open_orphan_dec_test(inode) &&
1012                 mds_inode_is_orphan(inode);
1013
1014         /* this is half of the actual "close" */
1015         if (mfd->mfd_mode & FMODE_WRITE) {
1016                 rc = mds_put_write_access(mds, inode, request_body,
1017                                           last_orphan && unlink_orphan);
1018         } else if (mfd->mfd_mode & FMODE_EXEC) {
1019                 mds_allow_write_access(inode);
1020         }
1021
1022         if (last_orphan && unlink_orphan) {
1023                 LASSERT(rc == 0); /* mds_put_write_access must have succeeded */
1024
1025                 CDEBUG(D_HA, "destroying orphan object %s\n", fidname);
1026
1027                 /* Sadly, there is no easy way to save pending_child from
1028                  * mds_reint_unlink() into mfd, so we need to re-lookup,
1029                  * but normally it will still be in the dcache. */
1030                 pending_dir = mds->mds_pending_dir->d_inode;
1031                 down(&pending_dir->i_sem);
1032                 cleanup_phase = 1; /* up(i_sem) when finished */
1033                 pending_child = lookup_one_len(fidname, mds->mds_pending_dir,
1034                                                fidlen);
1035                 if (IS_ERR(pending_child))
1036                         GOTO(cleanup, rc = PTR_ERR(pending_child));
1037                 LASSERT(pending_child->d_inode != NULL);
1038
1039                 cleanup_phase = 2; /* dput(pending_child) when finished */
1040                 handle = fsfilt_start(obd, pending_dir, FSFILT_OP_UNLINK_LOG,
1041                                       NULL);
1042                 if (IS_ERR(handle)) {
1043                         rc = PTR_ERR(handle);
1044                         handle = NULL;
1045                         GOTO(cleanup, rc);
1046                 }
1047
1048                 if (req != NULL &&
1049                     (reply_body->valid & OBD_MD_FLEASIZE) &&
1050                     mds_log_op_unlink(obd, pending_child->d_inode,
1051                                 lustre_msg_buf(req->rq_repmsg, 1, 0),
1052                                 req->rq_repmsg->buflens[1],
1053                                 lustre_msg_buf(req->rq_repmsg, 2, 0),
1054                                 req->rq_repmsg->buflens[2]) > 0) {
1055                         reply_body->valid |= OBD_MD_FLCOOKIE;
1056                 }
1057
1058                 pending_child->d_fsdata = (void *) &dp;
1059                 dp.p_inum = 0;
1060                 dp.p_ptr = req;
1061                 if (S_ISDIR(pending_child->d_inode->i_mode))
1062                         rc = vfs_rmdir(pending_dir, pending_child);
1063                 else
1064                         rc = vfs_unlink(pending_dir, pending_child);
1065                 if (rc)
1066                         CERROR("error unlinking orphan %s: rc %d\n",fidname,rc);
1067         } else if (mfd->mfd_mode & FMODE_WRITE && rc == 0) {
1068                 /* Update the on-disk attributes if this was the last write
1069                  * close, and all information was provided (i.e., rc == 0)
1070                  *
1071                  * XXX this should probably be abstracted with mds_reint_setattr
1072                  */
1073 #if 0
1074                 struct iattr iattr;
1075
1076                 /* XXX can't set block count with fsfilt_setattr (!) */
1077                 iattr.ia_valid = ATTR_CTIME | ATTR_ATIME |
1078                         ATTR_MTIME | ATTR_SIZE;
1079                 iattr.ia_atime = request_body->atime;
1080                 iattr.ia_ctime = request_body->ctime;
1081                 iattr.ia_mtime = request_body->mtime;
1082                 iattr.ia_size = request_body->size;
1083                 /* iattr.ia_blocks = request_body->blocks */
1084
1085                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1086                 if (IS_ERR(handle))
1087                         GOTO(cleanup, rc = PTR_ERR(handle));
1088                 rc = fsfilt_setattr(obd, mfd->mfd_dentry, handle, &iattr, 0);
1089                 if (rc)
1090                         CERROR("error in setattr(%s): rc %d\n", fidname, rc);
1091 #endif
1092         }
1093         /* If other clients have this file open for write, rc will be > 0 */
1094         if (rc > 0)
1095                 rc = 0;
1096         l_dput(mfd->mfd_dentry);
1097         mds_mfd_destroy(mfd);
1098
1099  cleanup:
1100         atomic_dec(&mds->mds_open_count);
1101         if (req) {
1102                 rc = mds_finish_transno(mds, pending_dir, handle, req, rc, 0);
1103         } else if (handle) {
1104                 int err = fsfilt_commit(obd, pending_dir, handle, 0);
1105                 if (err) {
1106                         CERROR("error committing close: %d\n", err);
1107                         if (!rc)
1108                                 rc = err;
1109                 }
1110         }
1111
1112         switch (cleanup_phase) {
1113         case 2:
1114                 dput(pending_child);
1115         case 1:
1116                 up(&pending_dir->i_sem);
1117         }
1118         RETURN(rc);
1119 }
1120
1121 int mds_close(struct ptlrpc_request *req)
1122 {
1123         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1124         struct obd_device *obd = req->rq_export->exp_obd;
1125         struct mds_body *body;
1126         struct mds_file_data *mfd;
1127         struct obd_run_ctxt saved;
1128         struct inode *inode;
1129         int rc, repsize[3] = {sizeof(struct mds_body),
1130                               obd->u.mds.mds_max_mdsize,
1131                               obd->u.mds.mds_max_cookiesize};
1132         ENTRY;
1133
1134         rc = lustre_pack_reply(req, 3, repsize, NULL);
1135         if (rc) {
1136                 CERROR("lustre_pack_reply: rc = %d\n", rc);
1137                 req->rq_status = rc;
1138         } else {
1139                 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1140         }
1141
1142         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
1143         if (body == NULL) {
1144                 CERROR("Can't unpack body\n");
1145                 req->rq_status = -EFAULT;
1146                 RETURN(-EFAULT);
1147         }
1148
1149         if (body->flags & MDS_BFLAG_UNCOMMITTED_WRITES)
1150                 /* do some stuff */ ;
1151
1152         mfd = mds_handle2mfd(&body->handle);
1153         if (mfd == NULL) {
1154                 DEBUG_REQ(D_ERROR, req, "no handle for file close ino "LPD64
1155                           ": cookie "LPX64, body->fid1.id, body->handle.cookie);
1156                 req->rq_status = -ESTALE;
1157                 RETURN(-ESTALE);
1158         }
1159
1160         inode = mfd->mfd_dentry->d_inode;
1161         if (mds_inode_is_orphan(inode) && mds_open_orphan_count(inode) == 1) {
1162                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
1163                 LASSERT(body != NULL);
1164
1165                 mds_pack_inode2fid(&body->fid1, inode);
1166                 mds_pack_inode2body(body, inode);
1167                 mds_pack_md(obd, req->rq_repmsg, 1, body, inode, 1);
1168         }
1169         spin_lock(&med->med_open_lock);
1170         list_del(&mfd->mfd_list);
1171         spin_unlock(&med->med_open_lock);
1172
1173         push_ctxt(&saved, &obd->obd_ctxt, NULL);
1174         req->rq_status = mds_mfd_close(rc ? NULL : req, obd, mfd, 1);
1175         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
1176
1177         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
1178                 CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
1179                 req->rq_status = -ENOMEM;
1180                 mds_mfd_put(mfd);
1181                 RETURN(-ENOMEM);
1182         }
1183
1184         mds_mfd_put(mfd);
1185         RETURN(0);
1186 }
1187
1188 int mds_done_writing(struct ptlrpc_request *req)
1189 {
1190         struct mds_body *body;
1191         int rc, size = sizeof(struct mds_body);
1192         ENTRY;
1193
1194         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1195
1196         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
1197         if (body == NULL) {
1198                 CERROR("Can't unpack body\n");
1199                 req->rq_status = -EFAULT;
1200                 RETURN(-EFAULT);
1201         }
1202
1203         rc = lustre_pack_reply(req, 1, &size, NULL);
1204         if (rc) {
1205                 CERROR("lustre_pack_reply: rc = %d\n", rc);
1206                 req->rq_status = rc;
1207         }
1208
1209         RETURN(0);
1210 }