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