Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / mdt / mdt_open.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/mdt/mdt_open.c
5  *  Lustre Metadata Target (mdt) open/close file handling
6  *
7  *  Copyright (C) 2002-2006 Cluster File Systems, Inc.
8  *   Author: Huang Hua <huanghua@clusterfs.com>
9  *
10  *   This file is part of the Lustre file system, http://www.lustre.org
11  *   Lustre is a trademark of Cluster File Systems, Inc.
12  *
13  *   You may have signed or agreed to another license before downloading
14  *   this software.  If so, you are bound by the terms and conditions
15  *   of that agreement, and the following does not apply to you.  See the
16  *   LICENSE file included with this distribution for more information.
17  *
18  *   If you did not agree to a different license, then this copy of Lustre
19  *   is open source software; you can redistribute it and/or modify it
20  *   under the terms of version 2 of the GNU General Public License as
21  *   published by the Free Software Foundation.
22  *
23  *   In either case, Lustre is distributed in the hope that it will be
24  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *   license text for more details.
27  */
28
29 #ifndef EXPORT_SYMTAB
30 # define EXPORT_SYMTAB
31 #endif
32 #define DEBUG_SUBSYSTEM S_MDS
33
34 #include <linux/lustre_acl.h>
35 #include <lustre_mds.h>
36 #include "mdt_internal.h"
37
38 /* we do nothing because we do not have refcount now */
39 static void mdt_mfd_get(void *mfdp)
40 {
41 }
42
43 /* Create a new mdt_file_data struct, initialize it,
44  * and insert it to global hash table */
45 struct mdt_file_data *mdt_mfd_new(void)
46 {
47         struct mdt_file_data *mfd;
48         ENTRY;
49
50         OBD_ALLOC_PTR(mfd);
51         if (mfd != NULL) {
52                 INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
53                 INIT_LIST_HEAD(&mfd->mfd_list);
54                 class_handle_hash(&mfd->mfd_handle, mdt_mfd_get);
55         }
56         RETURN(mfd);
57 }
58
59 /*
60  * Find the mfd pointed to by handle in global hash table.
61  * In case of replay the handle is obsoleted
62  * but mfd can be found in mfd list by that handle
63  */
64 struct mdt_file_data *mdt_handle2mfd(struct mdt_thread_info *info,
65                                      const struct lustre_handle *handle)
66 {
67         struct ptlrpc_request *req = mdt_info_req(info);
68         struct mdt_file_data  *mfd;
69         ENTRY;
70
71         LASSERT(handle != NULL);
72         mfd = class_handle2object(handle->cookie);
73         /* during dw/setattr replay the mfd can be found by old handle */
74         if (mfd == NULL &&
75             lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
76                 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
77                 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
78                         if (mfd->mfd_old_handle.cookie == handle->cookie)
79                                 RETURN (mfd);
80                 }
81                 mfd = NULL;
82         }
83         RETURN (mfd);
84 }
85
86 /* free mfd */
87 void mdt_mfd_free(struct mdt_file_data *mfd)
88 {
89         LASSERT(list_empty(&mfd->mfd_list));
90         OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
91 }
92
93 static int mdt_create_data(struct mdt_thread_info *info,
94                            struct mdt_object *p, struct mdt_object *o)
95 {
96         struct md_op_spec     *spec = &info->mti_spec;
97         struct md_attr        *ma = &info->mti_attr;
98         int rc;
99         ENTRY;
100
101         if ((spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE) ||
102             !(spec->sp_cr_flags & FMODE_WRITE))
103                 RETURN(0);
104
105         ma->ma_need = MA_INODE | MA_LOV;
106         ma->ma_valid = 0;
107         rc = mdo_create_data(info->mti_env,
108                              p ? mdt_object_child(p) : NULL,
109                              mdt_object_child(o), spec, ma);
110         RETURN(rc);
111 }
112
113 static int mdt_epoch_opened(struct mdt_object *mo)
114 {
115         return mo->mot_epochcount;
116 }
117
118 int mdt_sizeonmds_enabled(struct mdt_object *mo)
119 {
120         return !mo->mot_ioepoch;
121 }
122
123 /* Re-enable Size-on-MDS. */
124 void mdt_sizeonmds_enable(struct mdt_thread_info *info,
125                           struct mdt_object *mo)
126 {
127        spin_lock(&info->mti_mdt->mdt_ioepoch_lock);
128        if (info->mti_epoch->ioepoch == mo->mot_ioepoch) {
129                 LASSERT(!mdt_epoch_opened(mo)); 
130                 mo->mot_ioepoch = 0;
131                 mo->mot_flags = 0;
132        }
133        spin_unlock(&info->mti_mdt->mdt_ioepoch_lock);
134 }
135
136 /* Open the epoch. Epoch open is allowed if @writecount is not negative.
137  * The epoch and writecount handling is performed under the mdt_ioepoch_lock. */
138 int mdt_epoch_open(struct mdt_thread_info *info, struct mdt_object *o)
139 {
140         struct mdt_device *mdt = info->mti_mdt;
141         int cancel = 0;
142         int rc = 0;
143         ENTRY;
144
145         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) || 
146             !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
147                 RETURN(0);
148
149         spin_lock(&mdt->mdt_ioepoch_lock);
150         if (mdt_epoch_opened(o)) {
151                 /* Epoch continues even if there is no writers yet. */
152                 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
153                        o->mot_ioepoch, PFID(mdt_object_fid(o)));
154         } else {
155                 if (info->mti_replayepoch > mdt->mdt_ioepoch)
156                         mdt->mdt_ioepoch = info->mti_replayepoch;
157                 else
158                         mdt->mdt_ioepoch++;
159                 o->mot_ioepoch = info->mti_replayepoch ?
160                         info->mti_replayepoch : mdt->mdt_ioepoch;
161                 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
162                        mdt->mdt_ioepoch, PFID(mdt_object_fid(o)));
163                 cancel = 1;
164         }
165         o->mot_epochcount++;
166         spin_unlock(&mdt->mdt_ioepoch_lock);
167
168         /* Cancel Size-on-MDS attributes on clients if not truncate.
169          * In the later case, mdt_reint_setattr will do it. */
170         if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
171                 struct mdt_lock_handle  *lh = &info->mti_lh[MDT_LH_CHILD];
172                 mdt_lock_reg_init(lh, LCK_EX);
173                 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
174                                      MDT_LOCAL_LOCK);
175                 if (rc == 0)
176                         mdt_object_unlock(info, o, lh, 1);
177         }
178         RETURN(rc);
179 }
180
181 /* Update the on-disk attributes if needed and re-enable Size-on-MDS caching. */
182 static int mdt_sizeonmds_update(struct mdt_thread_info *info,
183                                 struct mdt_object *o)
184 {
185         ENTRY;
186
187         CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
188                o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_epochcount);
189
190         if (info->mti_attr.ma_attr.la_valid & LA_SIZE) {
191                 /* Do Size-on-MDS attribute update.
192                  * Size-on-MDS is re-enabled inside. */
193                 /* XXX: since we have opened the file, it is unnecessary
194                  * to check permission when close it. Between the "open"
195                  * and "close", maybe someone has changed the file mode
196                  * or flags, or the file created mode do not permit wirte,
197                  * and so on. Just set MDS_PERM_BYPASS for all the cases. */
198                 info->mti_attr.ma_attr_flags |= MDS_PERM_BYPASS;
199                 info->mti_attr.ma_attr.la_valid &= LA_SIZE | LA_BLOCKS |
200                                                 LA_ATIME | LA_MTIME | LA_CTIME;
201                 RETURN(mdt_attr_set(info, o, 0));
202         } else
203                 mdt_sizeonmds_enable(info, o);
204         RETURN(0);
205 }
206
207 /* Epoch closes.
208  * Returns 1 if epoch does not close.
209  * Returns 0 if epoch closes.
210  * Returns -EAGAIN if epoch closes but an Size-on-MDS Update is still needed
211  * from the client. */
212 static int mdt_epoch_close(struct mdt_thread_info *info, struct mdt_object *o)
213 {
214         int eviction = (mdt_info_req(info) == NULL ? 1 : 0);
215         struct lu_attr *la = &info->mti_attr.ma_attr;
216         int achange = 0;
217         int opened;
218         int rc = 1;
219         ENTRY;
220
221         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
222             !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
223                 RETURN(0);
224
225         spin_lock(&info->mti_mdt->mdt_ioepoch_lock);
226
227         /* Epoch closes only if client tells about it or eviction occures. */
228         if (eviction || (info->mti_epoch->flags & MF_EPOCH_CLOSE)) {
229                 LASSERT(o->mot_epochcount);
230                 o->mot_epochcount--;
231
232                 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
233                        o->mot_ioepoch, PFID(mdt_object_fid(o)),
234                        o->mot_epochcount);
235
236                 if (!eviction)
237                         achange = (info->mti_epoch->flags & MF_SOM_CHANGE);
238
239                 rc = 0;
240                 if (!eviction && !mdt_epoch_opened(o)) {
241                         /* Epoch ends. Is an Size-on-MDS update needed? */
242                         if (o->mot_flags & MF_SOM_CHANGE) {
243                                 /* Some previous writer changed the attribute.
244                                  * Do not believe to the current Size-on-MDS
245                                  * update, re-ask client. */
246                                 rc = -EAGAIN;
247                         } else if (!(la->la_valid & LA_SIZE) && achange) {
248                                 /* Attributes were changed by the last writer
249                                  * only but no Size-on-MDS update is received.*/
250                                 rc = -EAGAIN;
251                         }
252                 }
253
254                 if (achange || eviction)
255                         o->mot_flags |= MF_SOM_CHANGE;
256         }
257
258         opened = mdt_epoch_opened(o);
259         spin_unlock(&info->mti_mdt->mdt_ioepoch_lock);
260
261         /* If eviction occurred, do nothing. */
262         if ((rc == 0) && !opened && !eviction) {
263                 /* Epoch ends and wanted Size-on-MDS update is obtained. */
264                 rc = mdt_sizeonmds_update(info, o);
265                 /* Avoid the following setattrs of these attributes, e.g.
266                  * for atime update. */
267                 info->mti_attr.ma_valid = 0;
268         }
269         RETURN(rc);
270 }
271
272 int mdt_write_read(struct mdt_device *mdt, struct mdt_object *o)
273 {
274         int rc = 0;
275         ENTRY;
276         spin_lock(&mdt->mdt_ioepoch_lock);
277         rc = o->mot_writecount;
278         spin_unlock(&mdt->mdt_ioepoch_lock);
279         RETURN(rc);
280 }
281
282 int mdt_write_get(struct mdt_device *mdt, struct mdt_object *o)
283 {
284         int rc = 0;
285         ENTRY;
286         spin_lock(&mdt->mdt_ioepoch_lock);
287         if (o->mot_writecount < 0)
288                 rc = -ETXTBSY;
289         else
290                 o->mot_writecount++;
291         spin_unlock(&mdt->mdt_ioepoch_lock);
292         RETURN(rc);
293 }
294
295 static void mdt_write_put(struct mdt_device *mdt, struct mdt_object *o)
296 {
297         ENTRY;
298         spin_lock(&mdt->mdt_ioepoch_lock);
299         o->mot_writecount--;
300         spin_unlock(&mdt->mdt_ioepoch_lock);
301         EXIT;
302 }
303
304 static int mdt_write_deny(struct mdt_device *mdt, struct mdt_object *o)
305 {
306         int rc = 0;
307         ENTRY;
308         spin_lock(&mdt->mdt_ioepoch_lock);
309         if (o->mot_writecount > 0)
310                 rc = -ETXTBSY;
311         else
312                 o->mot_writecount--;
313         spin_unlock(&mdt->mdt_ioepoch_lock);
314         RETURN(rc);
315 }
316
317 static void mdt_write_allow(struct mdt_device *mdt, struct mdt_object *o)
318 {
319         ENTRY;
320         spin_lock(&mdt->mdt_ioepoch_lock);
321         o->mot_writecount++;
322         spin_unlock(&mdt->mdt_ioepoch_lock);
323         EXIT;
324 }
325
326 /* there can be no real transaction so prepare the fake one */
327 static void mdt_empty_transno(struct mdt_thread_info* info)
328 {
329         struct mdt_device *mdt = info->mti_mdt;
330         struct ptlrpc_request *req = mdt_info_req(info);
331
332         ENTRY;
333         /* transaction is occured already */
334         if (lustre_msg_get_transno(req->rq_repmsg) != 0) {
335                 EXIT;
336                 return;
337         }
338
339         spin_lock(&mdt->mdt_transno_lock);
340         if (info->mti_transno == 0) {
341                 info->mti_transno = ++ mdt->mdt_last_transno;
342         } else {
343                 /* should be replay */
344                 if (info->mti_transno > mdt->mdt_last_transno)
345                         mdt->mdt_last_transno = info->mti_transno;
346         }
347         spin_unlock(&mdt->mdt_transno_lock);
348
349         CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
350                         info->mti_transno,
351                         req->rq_export->exp_obd->obd_last_committed);
352
353         req->rq_transno = info->mti_transno;
354         lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
355         lustre_msg_set_last_xid(req->rq_repmsg, req->rq_xid);
356         EXIT;
357 }
358
359 void mdt_mfd_set_mode(struct mdt_file_data *mfd, int mode)
360 {
361         LASSERT(mfd != NULL);
362
363         CDEBUG(D_HA, "Change mfd %p mode 0x%x->0x%x\n",
364                mfd, (unsigned int)mfd->mfd_mode, (unsigned int)mode);
365
366         mfd->mfd_mode = mode;
367 }
368
369 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
370                         struct mdt_object *o, int flags, int created)
371 {
372         struct ptlrpc_request   *req = mdt_info_req(info);
373         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
374         struct mdt_file_data    *mfd;
375         struct md_attr          *ma  = &info->mti_attr;
376         struct lu_attr          *la  = &ma->ma_attr;
377         struct mdt_body         *repbody;
378         int                      rc = 0, isdir, isreg;
379         ENTRY;
380
381         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
382
383         isreg = S_ISREG(la->la_mode);
384         isdir = S_ISDIR(la->la_mode);
385         if ((isreg && !(ma->ma_valid & MA_LOV))) {
386                 /*
387                  * No EA, check whether it is will set regEA and dirEA since in
388                  * above attr get, these size might be zero, so reset it, to
389                  * retrieve the MD after create obj.
390                  */
391                 ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill,
392                                                        &RMF_MDT_MD,
393                                                        RCL_SERVER);
394                 /* in replay case, p == NULL */
395                 rc = mdt_create_data(info, p, o);
396                 if (rc)
397                         RETURN(rc);
398         }
399
400         CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
401                ma->ma_valid, ma->ma_lmm_size);
402
403         if (ma->ma_valid & MA_LOV) {
404                 LASSERT(ma->ma_lmm_size != 0);
405                 repbody->eadatasize = ma->ma_lmm_size;
406                 if (isdir)
407                         repbody->valid |= OBD_MD_FLDIREA;
408                 else
409                         repbody->valid |= OBD_MD_FLEASIZE;
410         }
411
412         if (flags & FMODE_WRITE) {
413                 rc = mdt_write_get(info->mti_mdt, o);
414                 if (rc == 0) {
415                         mdt_epoch_open(info, o);
416                         repbody->ioepoch = o->mot_ioepoch;
417                 }
418         } else if (flags & MDS_FMODE_EXEC) {
419                 rc = mdt_write_deny(info->mti_mdt, o);
420         }
421         if (rc)
422                 RETURN(rc);
423
424         rc = mo_open(info->mti_env, mdt_object_child(o),
425                      created ? flags | MDS_OPEN_CREATED : flags);
426         if (rc)
427                 RETURN(rc);
428
429         mfd = mdt_mfd_new();
430         if (mfd != NULL) {
431                 /*
432                  * Keep a reference on this object for this open, and is
433                  * released by mdt_mfd_close().
434                  */
435                 mdt_object_get(info->mti_env, o);
436
437                 /*
438                  * @flags is always not zero. At least it should be FMODE_READ,
439                  * FMODE_WRITE or FMODE_EXEC.
440                  */
441                 LASSERT(flags != 0);
442
443                 /* Open handling. */
444                 mdt_mfd_set_mode(mfd, flags);
445
446                 mfd->mfd_object = o;
447                 mfd->mfd_xid = req->rq_xid;
448
449                 /* replay handle */
450                 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
451                         struct mdt_file_data *old_mfd;
452                         /* Check wheather old cookie already exist in
453                          * the list, becasue when do recovery, client
454                          * might be disconnected from server, and
455                          * restart replay, so there maybe some orphan 
456                          * mfd here, we should remove them */
457                         LASSERT(info->mti_rr.rr_handle != NULL);
458                         old_mfd = mdt_handle2mfd(info, info->mti_rr.rr_handle);
459                         if (old_mfd) {
460                                 CDEBUG(D_HA, "del orph mfd %p cookie" LPX64"\n",
461                                        mfd, info->mti_rr.rr_handle->cookie);
462                                 spin_lock(&med->med_open_lock);
463                                 class_handle_unhash(&old_mfd->mfd_handle);
464                                 list_del_init(&old_mfd->mfd_list);
465                                 spin_unlock(&med->med_open_lock);
466                                 mdt_mfd_free(old_mfd);
467                         }
468                         CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
469                                info->mti_rr.rr_handle->cookie);
470                         mfd->mfd_old_handle.cookie =
471                                                 info->mti_rr.rr_handle->cookie;
472                 }
473                 spin_lock(&med->med_open_lock);
474                 list_add(&mfd->mfd_list, &med->med_open_head);
475                 spin_unlock(&med->med_open_lock);
476
477                 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
478                 mdt_empty_transno(info);
479         } else
480                 rc = -ENOMEM;
481
482         RETURN(rc);
483 }
484
485
486 static int mdt_finish_open(struct mdt_thread_info *info,
487                            struct mdt_object *p, struct mdt_object *o,
488                            int flags, int created, struct ldlm_reply *rep)
489 {
490         struct ptlrpc_request   *req = mdt_info_req(info);
491         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
492         struct mdt_device       *mdt = info->mti_mdt;
493         struct md_attr          *ma  = &info->mti_attr;
494         struct lu_attr          *la  = &ma->ma_attr;
495         struct mdt_file_data    *mfd;
496         struct mdt_body         *repbody;
497         int                      rc = 0;
498         int                      isreg, isdir, islnk;
499         struct list_head        *t;
500         ENTRY;
501
502         LASSERT(ma->ma_valid & MA_INODE);
503
504         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
505
506         isreg = S_ISREG(la->la_mode);
507         isdir = S_ISDIR(la->la_mode);
508         islnk = S_ISLNK(la->la_mode);
509         mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
510
511         if (med->med_rmtclient) {
512                 void *buf = req_capsule_server_get(&info->mti_pill, &RMF_ACL);
513
514                 rc = mdt_pack_remote_perm(info, o, buf);
515                 if (rc) {
516                         repbody->valid &= ~OBD_MD_FLRMTPERM;
517                         repbody->aclsize = 0;
518                 } else {
519                         repbody->valid |= OBD_MD_FLRMTPERM;
520                         repbody->aclsize = sizeof(struct mdt_remote_perm);
521                 }
522         }
523 #ifdef CONFIG_FS_POSIX_ACL
524         else if (req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) {
525                 const struct lu_env *env = info->mti_env;
526                 struct md_object *next = mdt_object_child(o);
527                 struct lu_buf *buf = &info->mti_buf;
528
529                 buf->lb_buf = req_capsule_server_get(&info->mti_pill, &RMF_ACL);
530                 buf->lb_len = req_capsule_get_size(&info->mti_pill, &RMF_ACL,
531                                                    RCL_SERVER);
532                 if (buf->lb_len > 0) {
533                         rc = mo_xattr_get(env, next, buf,
534                                           XATTR_NAME_ACL_ACCESS);
535                         if (rc < 0) {
536                                 if (rc == -ENODATA) {
537                                         repbody->aclsize = 0;
538                                         repbody->valid |= OBD_MD_FLACL;
539                                         rc = 0;
540                                 } else if (rc == -EOPNOTSUPP) {
541                                         rc = 0;
542                                 } else {
543                                         CERROR("got acl size: %d\n", rc);
544                                 }
545                         } else {
546                                 repbody->aclsize = rc;
547                                 repbody->valid |= OBD_MD_FLACL;
548                                 rc = 0;
549                         }
550                 }
551         }
552 #endif
553
554         if (mdt->mdt_opts.mo_mds_capa) {
555                 struct lustre_capa *capa;
556
557                 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
558                 LASSERT(capa);
559                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
560                 capa->lc_uid = 0;
561                 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
562                 if (rc)
563                         RETURN(rc);
564                 repbody->valid |= OBD_MD_FLMDSCAPA;
565         }
566         if (mdt->mdt_opts.mo_oss_capa &&
567             S_ISREG(lu_object_attr(&o->mot_obj.mo_lu))) {
568                 struct lustre_capa *capa;
569
570                 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA2);
571                 LASSERT(capa);
572                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
573                 capa->lc_uid = 0;
574                 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
575                 if (rc)
576                         RETURN(rc);
577                 repbody->valid |= OBD_MD_FLOSSCAPA;
578         }
579
580         /*
581          * If we are following a symlink, don't open; and do not return open
582          * handle for special nodes as client required.
583          */
584         if (islnk || (!isreg && !isdir &&
585             (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH))) {
586                 lustre_msg_set_transno(req->rq_repmsg, 0);
587                 RETURN(0);
588         }
589
590         mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
591
592         /*
593          * We need to return the existing object's fid back, so it is done here,
594          * after preparing the reply.
595          */
596         if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
597                 RETURN(-EEXIST);
598
599         /* This can't be done earlier, we need to return reply body */
600         if (isdir) {
601                 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
602                         /* We are trying to create or write an existing dir. */
603                         RETURN(-EISDIR);
604                 }
605         } else if (flags & MDS_OPEN_DIRECTORY)
606                 RETURN(-ENOTDIR);
607
608         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_OPEN_CREATE)) {
609                 obd_fail_loc = OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE;
610                 RETURN(-EAGAIN);
611         }
612
613         mfd = NULL;
614         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
615                 spin_lock(&med->med_open_lock);
616                 list_for_each(t, &med->med_open_head) {
617                         mfd = list_entry(t, struct mdt_file_data, mfd_list);
618                         if (mfd->mfd_xid == req->rq_xid) {
619                                 break;
620                         }
621                         mfd = NULL;
622                 }
623                 spin_unlock(&med->med_open_lock);
624
625                 if (mfd != NULL) {
626                         repbody->handle.cookie = mfd->mfd_handle.h_cookie;
627                         /*set repbody->ea_size for resent case*/
628                         if (ma->ma_valid & MA_LOV) {
629                                 LASSERT(ma->ma_lmm_size != 0);
630                                 repbody->eadatasize = ma->ma_lmm_size;
631                                 if (isdir)
632                                         repbody->valid |= OBD_MD_FLDIREA;
633                                 else
634                                         repbody->valid |= OBD_MD_FLEASIZE;
635                         }
636                         RETURN(0);
637                 }
638         }
639
640         rc = mdt_mfd_open(info, p, o, flags, created);
641         RETURN(rc);
642 }
643
644 extern void mdt_req_from_mcd(struct ptlrpc_request *req,
645                              struct mdt_client_data *mcd);
646
647 void mdt_reconstruct_open(struct mdt_thread_info *info,
648                           struct mdt_lock_handle *lhc)
649 {
650         const struct lu_env *env = info->mti_env;
651         struct mdt_device       *mdt  = info->mti_mdt;
652         struct req_capsule      *pill = &info->mti_pill;
653         struct ptlrpc_request   *req  = mdt_info_req(info);
654         struct mdt_export_data  *med  = &req->rq_export->exp_mdt_data;
655         struct mdt_client_data  *mcd  = med->med_mcd;
656         struct md_attr          *ma   = &info->mti_attr;
657         struct mdt_reint_record *rr   = &info->mti_rr;
658         __u32                   flags = info->mti_spec.sp_cr_flags;
659         struct ldlm_reply       *ldlm_rep;
660         struct mdt_object       *parent;
661         struct mdt_object       *child;
662         struct mdt_body         *repbody;
663         int                      rc;
664         ENTRY;
665
666         LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
667         ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
668         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
669
670         ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
671         ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
672                                                RCL_SERVER);
673         ma->ma_need = MA_INODE | MA_LOV;
674         ma->ma_valid = 0;
675
676         mdt_req_from_mcd(req, med->med_mcd);
677         mdt_set_disposition(info, ldlm_rep, mcd->mcd_last_data);
678
679         CERROR("This is reconstruct open: disp="LPX64", result=%d\n",
680                 ldlm_rep->lock_policy_res1, req->rq_status);
681
682         if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
683             req->rq_status != 0) {
684                 /* We did not create successfully, return error to client. */
685                 mdt_shrink_reply(info);
686                 GOTO(out, rc = req->rq_status);
687         }
688
689         if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
690                 /*
691                  * We failed after creation, but we do not know in which step
692                  * we failed. So try to check the child object.
693                  */
694                 parent = mdt_object_find(env, mdt, rr->rr_fid1);
695                 LASSERT(!IS_ERR(parent));
696
697                 child = mdt_object_find(env, mdt, rr->rr_fid2);
698                 LASSERT(!IS_ERR(child));
699
700                 rc = mdt_object_exists(child);
701                 if (rc > 0) {
702                         struct md_object *next;
703
704                         mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
705                         next = mdt_object_child(child);
706                         rc = mo_attr_get(env, next, ma);
707                         if (rc == 0)
708                               rc = mdt_finish_open(info, parent, child,
709                                                    flags, 1, ldlm_rep);
710                 } else if (rc < 0) {
711                         /* the child object was created on remote server */
712                         repbody->fid1 = *rr->rr_fid2;
713                         repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
714                         rc = 0;
715                 } else if (rc == 0) {
716                         /* the child does not exist, we should do regular open */
717                         mdt_object_put(env, parent);
718                         mdt_object_put(env, child);
719                         GOTO(regular_open, 0);
720                 }
721                 mdt_object_put(env, parent);
722                 mdt_object_put(env, child);
723                 mdt_shrink_reply(info);
724                 GOTO(out, rc);
725         } else {
726 regular_open:
727                 /* We did not try to create, so we are a pure open */
728                 rc = mdt_reint_open(info, lhc);
729         }
730
731         EXIT;
732 out:
733         req->rq_status = rc;
734         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
735         LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
736 }
737
738 static int mdt_open_by_fid(struct mdt_thread_info* info,
739                            struct ldlm_reply *rep)
740 {
741         const struct lu_env     *env = info->mti_env;
742         __u32                    flags = info->mti_spec.sp_cr_flags;
743         struct mdt_reint_record *rr = &info->mti_rr;
744         struct md_attr          *ma = &info->mti_attr;
745         struct mdt_object       *o;
746         int                      rc;
747         ENTRY;
748
749         LASSERT(info->mti_spec.u.sp_ea.no_lov_create);
750         o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
751         if (IS_ERR(o))
752                 RETURN(rc = PTR_ERR(o));
753
754         rc = mdt_object_exists(o);
755         if (rc > 0) {
756                 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
757                                                 DISP_LOOKUP_EXECD |
758                                                 DISP_LOOKUP_POS));
759
760                 rc = mo_attr_get(env, mdt_object_child(o), ma);
761                 if (rc == 0)
762                         rc = mdt_mfd_open(info, NULL, o, flags, 0);
763         } else if (rc == 0) {
764                 rc = -ENOENT;
765         } else  {
766                 /* the child object was created on remote server */
767                 struct mdt_body *repbody;
768                 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
769                 repbody->fid1 = *rr->rr_fid2;
770                 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
771                 rc = 0;
772         }
773
774         mdt_object_put(info->mti_env, o);
775         RETURN(rc);
776 }
777
778 int mdt_pin(struct mdt_thread_info* info)
779 {
780         ENTRY;
781         RETURN(err_serious(-EOPNOTSUPP));
782 }
783
784 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
785 static int mdt_cross_open(struct mdt_thread_info* info,
786                           const struct lu_fid *fid,
787                           struct ldlm_reply *rep, __u32 flags)
788 {
789         struct md_attr    *ma = &info->mti_attr;
790         struct mdt_object *o;
791         int                rc;
792         ENTRY;
793
794         o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
795         if (IS_ERR(o))
796                 RETURN(rc = PTR_ERR(o));
797
798         rc = mdt_object_exists(o);
799         if (rc > 0) {
800                 /* Do permission check for cross-open. */
801                 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o),
802                                    NULL, flags | MDS_OPEN_CROSS);
803                 if (rc)
804                         goto out;
805
806                 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
807                 rc = mo_attr_get(info->mti_env, mdt_object_child(o), ma);
808                 if (rc == 0)
809                         rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
810         } else if (rc == 0) {
811                 /*
812                  * Something is wrong here. lookup was positive but there is
813                  * no object!
814                  */
815                 CERROR("Cross-ref object doesn't exist!\n");
816                 rc = -EFAULT;
817         } else  {
818                 /* Something is wrong here, the object is on another MDS! */
819                 CERROR("The object isn't on this server! FLD error?\n");
820                 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
821                                 &o->mot_obj.mo_lu,
822                                 "Object isn't on this server! FLD error?\n");
823
824                 rc = -EFAULT;
825         }
826
827 out:
828         mdt_object_put(info->mti_env, o);
829         RETURN(rc);
830 }
831
832 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
833 {
834         struct mdt_device       *mdt = info->mti_mdt;
835         struct ptlrpc_request   *req = mdt_info_req(info);
836         struct mdt_object       *parent;
837         struct mdt_object       *child;
838         struct mdt_lock_handle  *lh;
839         struct ldlm_reply       *ldlm_rep;
840         struct mdt_body         *repbody;
841         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
842         struct md_attr          *ma = &info->mti_attr;
843         __u32                    create_flags = info->mti_spec.sp_cr_flags;
844         struct mdt_reint_record *rr = &info->mti_rr;
845         struct lu_name          *lname;
846         int                      result;
847         int                      created = 0;
848         ENTRY;
849
850         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PAUSE_OPEN | OBD_FAIL_ONCE,
851                          (obd_timeout + 1) / 4);
852
853         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
854
855         ma->ma_lmm = req_capsule_server_get(&info->mti_pill, &RMF_MDT_MD);
856         ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill, &RMF_MDT_MD,
857                                                RCL_SERVER);
858         ma->ma_need = MA_INODE | MA_LOV;
859         ma->ma_valid = 0;
860
861         LASSERT(info->mti_pill.rc_fmt == &RQF_LDLM_INTENT_OPEN);
862         ldlm_rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
863
864         /* TODO: JOIN file */
865         if (create_flags & MDS_OPEN_JOIN_FILE) {
866                 CERROR("JOIN file will be supported soon\n");
867                 GOTO(out, result = err_serious(-EOPNOTSUPP));
868         }
869
870         CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
871                "cr_flag=0%o mode=0%06o msg_flag=0x%x\n",
872                PFID(rr->rr_fid1), rr->rr_name,
873                PFID(rr->rr_fid2), create_flags,
874                ma->ma_attr.la_mode, lustre_msg_get_flags(req->rq_reqmsg));
875
876         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
877                 /* This is a replay request. */
878                 result = mdt_open_by_fid(info, ldlm_rep);
879
880                 if (result != -ENOENT)
881                         GOTO(out, result);
882
883                 /*
884                  * We didn't find the correct object, so we need to re-create it
885                  * via a regular replay.
886                  */
887                 if (!(create_flags & MDS_OPEN_CREAT)) {
888                         DEBUG_REQ(D_ERROR, req,"OPEN & CREAT not in open replay.");
889                         GOTO(out, result = -EFAULT);
890                 }
891                 CDEBUG(D_INFO, "Open replay did find object, continue as "
892                        "regular open\n");
893         }
894
895         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
896                 GOTO(out, result = err_serious(-ENOMEM));
897
898         mdt_set_disposition(info, ldlm_rep,
899                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
900
901         if (info->mti_cross_ref) {
902                 /* This is cross-ref open */
903                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
904                 result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
905                                         create_flags);
906                 GOTO(out, result);
907         }
908
909         lh = &info->mti_lh[MDT_LH_PARENT];
910         mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
911                           LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
912
913         parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
914                                       MDS_INODELOCK_UPDATE);
915         if (IS_ERR(parent))
916                 GOTO(out, result = PTR_ERR(parent));
917
918         fid_zero(child_fid);
919
920         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
921
922         result = mdo_lookup(info->mti_env, mdt_object_child(parent),
923                             lname, child_fid, &info->mti_spec);
924         LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
925                  "looking for "DFID"/%s, result fid="DFID"\n",
926                  PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
927
928         if (result != 0 && result != -ENOENT && result != -ESTALE)
929                 GOTO(out_parent, result);
930
931         if (result == -ENOENT || result == -ESTALE) {
932                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
933                 if (result == -ESTALE) {
934                         /*
935                          * -ESTALE means the parent is a dead(unlinked) dir, so
936                          * it should return -ENOENT to in accordance with the
937                          * original mds implementaion.
938                          */
939                         GOTO(out_parent, result = -ENOENT);
940                 }
941                 if (!(create_flags & MDS_OPEN_CREAT))
942                         GOTO(out_parent, result);
943                 *child_fid = *info->mti_rr.rr_fid2;
944                 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
945                          PFID(child_fid));
946         } else {
947                 /*
948                  * Check for O_EXCL is moved to the mdt_finish_open(), we need to
949                  * return FID back in that case.
950                  */
951                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
952         }
953
954         child = mdt_object_find(info->mti_env, mdt, child_fid);
955         if (IS_ERR(child))
956                 GOTO(out_parent, result = PTR_ERR(child));
957
958         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
959         if (result == -ENOENT) {
960                 /* Not found and with MDS_OPEN_CREAT: let's create it. */
961                 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
962
963                 /* Let lower layers know what is lock mode on directory. */
964                 info->mti_spec.sp_cr_mode =
965                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
966
967                 /*
968                  * Do not perform lookup sanity check. We know that name does
969                  * not exist.
970                  */
971                 info->mti_spec.sp_cr_lookup = 0;
972
973                 result = mdo_create(info->mti_env,
974                                     mdt_object_child(parent),
975                                     lname,
976                                     mdt_object_child(child),
977                                     &info->mti_spec,
978                                     &info->mti_attr);
979                 if (result == -ERESTART) {
980                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
981                         GOTO(out_child, result);
982                 } else {
983                         if (result != 0)
984                                 GOTO(out_child, result);
985                 }
986                 created = 1;
987         } else {
988                 /* We have to get attr & lov ea for this object */
989                 result = mo_attr_get(info->mti_env, mdt_object_child(child),
990                                      ma);
991                 /*
992                  * The object is on remote node, return its FID for remote open.
993                  */
994                 if (result == -EREMOTE) {
995                         int rc;
996
997                         /*
998                          * Check if this lock already was sent to client and
999                          * this is resent case. For resent case do not take lock
1000                          * again, use what is already granted.
1001                          */
1002                         LASSERT(lhc != NULL);
1003
1004                         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1005                                 struct ldlm_lock *lock;
1006
1007                                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
1008                                         MSG_RESENT);
1009
1010                                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1011                                 if (!lock) {
1012                                         CERROR("Invalid lock handle "LPX64"\n",
1013                                                lhc->mlh_reg_lh.cookie);
1014                                         LBUG();
1015                                 }
1016                                 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1017                                                         &lock->l_resource->lr_name));
1018                                 LDLM_LOCK_PUT(lock);
1019                                 rc = 0;
1020                         } else {
1021                                 mdt_lock_handle_init(lhc);
1022                                 mdt_lock_reg_init(lhc, LCK_PR);
1023
1024                                 rc = mdt_object_lock(info, child, lhc,
1025                                                      MDS_INODELOCK_LOOKUP,
1026                                                      MDT_CROSS_LOCK);
1027                         }
1028                         repbody->fid1 = *mdt_object_fid(child);
1029                         repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1030                         if (rc != 0)
1031                                 result = rc;
1032                         GOTO(out_child, result);
1033                 }
1034         }
1035
1036         /* Try to open it now. */
1037         result = mdt_finish_open(info, parent, child, create_flags,
1038                                  created, ldlm_rep);
1039
1040         if (result != 0 && created) {
1041                 int rc2;
1042                 ma->ma_need = 0;
1043                 ma->ma_valid = 0;
1044                 ma->ma_cookie_size = 0;
1045                 info->mti_no_need_trans = 1;
1046                 rc2 = mdo_unlink(info->mti_env,
1047                                  mdt_object_child(parent),
1048                                  mdt_object_child(child),
1049                                  lname,
1050                                  &info->mti_attr);
1051                 if (rc2 != 0)
1052                         CERROR("Error in cleanup of open\n");
1053         }
1054         EXIT;
1055 out_child:
1056         mdt_object_put(info->mti_env, child);
1057 out_parent:
1058         mdt_object_unlock_put(info, parent, lh, result);
1059 out:
1060         mdt_shrink_reply(info);
1061         if (result)
1062                 lustre_msg_set_transno(req->rq_repmsg, 0);
1063         return result;
1064 }
1065
1066 #define MFD_CLOSED(mode) (((mode) & ~(FMODE_EPOCH | FMODE_SOM | \
1067                                       FMODE_EPOCHLCK)) == FMODE_CLOSED)
1068
1069 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1070 {
1071         return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1072 }
1073
1074 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1075 {
1076         struct mdt_object *o = mfd->mfd_object;
1077         struct md_object *next = mdt_object_child(o);
1078         struct md_attr *ma = &info->mti_attr;
1079         int rc = 0, ret = 0;
1080         int mode;
1081         ENTRY;
1082
1083         mode = mfd->mfd_mode;
1084
1085         if ((mode & FMODE_WRITE) || (mode & FMODE_EPOCHLCK)) {
1086                 mdt_write_put(info->mti_mdt, o);
1087                 ret = mdt_epoch_close(info, o);
1088         } else if (mode & MDS_FMODE_EXEC) {
1089                 mdt_write_allow(info->mti_mdt, o);
1090         } else if (mode & FMODE_EPOCH) {
1091                 ret = mdt_epoch_close(info, o);
1092         } 
1093         
1094         /* Update atime on close only. */
1095         if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1096             && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1097                 /* Set the atime only. */
1098                 ma->ma_attr.la_valid = LA_ATIME;
1099                 rc = mo_attr_set(info->mti_env, next, ma);
1100         }
1101
1102         ma->ma_need |= MA_INODE;
1103         ma->ma_valid = 0;
1104
1105         if (!MFD_CLOSED(mode))
1106                 rc = mo_close(info->mti_env, next, ma);
1107         else if (ret == -EAGAIN)
1108                 rc = mo_attr_get(info->mti_env, next, ma);
1109
1110         /* If the object is unlinked, do not try to re-enable SIZEONMDS */
1111         if ((ret == -EAGAIN) && (ma->ma_valid & MA_INODE) &&
1112             (ma->ma_attr.la_nlink == 0)) {
1113                 ret = 0;
1114         }
1115
1116         if ((ret == -EAGAIN) || (ret == 1)) {
1117                 struct mdt_export_data *med;
1118
1119                 /* The epoch has not closed or Size-on-MDS update is needed.
1120                  * Put mfd back into the list. */
1121                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1122                 mdt_mfd_set_mode(mfd, (ret == 1 ? FMODE_EPOCH : FMODE_SOM));
1123
1124                 LASSERT(mdt_info_req(info));
1125                 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1126                 spin_lock(&med->med_open_lock);
1127                 list_add(&mfd->mfd_list, &med->med_open_head);
1128                 class_handle_hash_back(&mfd->mfd_handle);
1129                 spin_unlock(&med->med_open_lock);
1130
1131                 if (ret == 1) {
1132                         ret = 0;
1133                 } else {
1134                         CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1135                                "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1136                 }
1137         } else {
1138                 mdt_mfd_free(mfd);
1139                 mdt_object_put(info->mti_env, o);
1140         }
1141
1142         RETURN(rc ? rc : ret);
1143 }
1144
1145 int mdt_close(struct mdt_thread_info *info)
1146 {
1147         struct mdt_export_data *med;
1148         struct mdt_file_data   *mfd;
1149         struct mdt_object      *o;
1150         struct md_attr         *ma = &info->mti_attr;
1151         struct mdt_body        *repbody = NULL;
1152         struct ptlrpc_request  *req = mdt_info_req(info);
1153         int rc, ret = 0;
1154         ENTRY;
1155
1156         /* Close may come with the Size-on-MDS update. Unpack it. */
1157         rc = mdt_close_unpack(info);
1158         if (rc)
1159                 RETURN(err_serious(rc));
1160
1161         LASSERT(info->mti_epoch);
1162
1163         req_capsule_set_size(&info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1164                              info->mti_mdt->mdt_max_mdsize);
1165         req_capsule_set_size(&info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1166                              info->mti_mdt->mdt_max_cookiesize);
1167         rc = req_capsule_pack(&info->mti_pill);
1168         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL))
1169                 RETURN(lustre_msg_get_status(req->rq_repmsg));
1170
1171         /* Continue to close handle even if we can not pack reply */
1172         if (rc == 0) {
1173                 repbody = req_capsule_server_get(&info->mti_pill,
1174                                                  &RMF_MDT_BODY);
1175                 ma->ma_lmm = req_capsule_server_get(&info->mti_pill,
1176                                                     &RMF_MDT_MD);
1177                 ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill,
1178                                                        &RMF_MDT_MD,
1179                                                        RCL_SERVER);
1180                 ma->ma_cookie = req_capsule_server_get(&info->mti_pill,
1181                                                        &RMF_LOGCOOKIES);
1182                 ma->ma_cookie_size = req_capsule_get_size(&info->mti_pill,
1183                                                           &RMF_LOGCOOKIES,
1184                                                           RCL_SERVER);
1185                 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1186                 repbody->eadatasize = 0;
1187                 repbody->aclsize = 0;
1188         } else
1189                 rc = err_serious(rc);
1190
1191         med = &req->rq_export->exp_mdt_data;
1192         spin_lock(&med->med_open_lock);
1193         mfd = mdt_handle2mfd(info, &info->mti_epoch->handle);
1194         if (mdt_mfd_closed(mfd)) {
1195                 spin_unlock(&med->med_open_lock);
1196                 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1197                        ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1198                        info->mti_epoch->handle.cookie);
1199                 rc = err_serious(-ESTALE);
1200         } else {
1201                 class_handle_unhash(&mfd->mfd_handle);
1202                 list_del_init(&mfd->mfd_list);
1203                 spin_unlock(&med->med_open_lock);
1204
1205                 /* Do not lose object before last unlink. */
1206                 o = mfd->mfd_object;
1207                 mdt_object_get(info->mti_env, o);
1208                 ret = mdt_mfd_close(info, mfd);
1209                 if (repbody != NULL)
1210                         rc = mdt_handle_last_unlink(info, o, ma);
1211                 mdt_empty_transno(info);
1212                 mdt_object_put(info->mti_env, o);
1213         }
1214         if (repbody != NULL)
1215                 mdt_shrink_reply(info);
1216
1217         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1218                 RETURN(err_serious(-ENOMEM));
1219
1220         RETURN(rc ? rc : ret);
1221 }
1222
1223 int mdt_done_writing(struct mdt_thread_info *info)
1224 {
1225         struct mdt_body         *repbody = NULL;
1226         struct mdt_export_data  *med;
1227         struct mdt_file_data    *mfd;
1228         int rc;
1229         ENTRY;
1230
1231         rc = req_capsule_pack(&info->mti_pill);
1232         if (rc)
1233                 RETURN(err_serious(rc));
1234
1235         repbody = req_capsule_server_get(&info->mti_pill,
1236                                          &RMF_MDT_BODY);
1237         repbody->eadatasize = 0;
1238         repbody->aclsize = 0;
1239
1240         /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1241         rc = mdt_close_unpack(info);
1242         if (rc)
1243                 RETURN(err_serious(rc));
1244
1245         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL))
1246                 RETURN(lustre_msg_get_status(mdt_info_req(info)->rq_repmsg));
1247
1248         med = &info->mti_exp->exp_mdt_data;
1249         spin_lock(&med->med_open_lock);
1250         mfd = mdt_handle2mfd(info, &info->mti_epoch->handle);
1251         if (mfd == NULL) {
1252                 spin_unlock(&med->med_open_lock);
1253                 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1254                        ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1255                        info->mti_epoch->handle.cookie);
1256                 RETURN(-ESTALE);
1257         } 
1258  
1259         LASSERT(mfd->mfd_mode == FMODE_EPOCH ||
1260                 mfd->mfd_mode == FMODE_EPOCHLCK);
1261         class_handle_unhash(&mfd->mfd_handle);
1262         list_del_init(&mfd->mfd_list);
1263         spin_unlock(&med->med_open_lock);
1264
1265         /* Set EPOCH CLOSE flag if not set by client. */
1266         info->mti_epoch->flags |= MF_EPOCH_CLOSE;
1267         info->mti_attr.ma_valid = 0;
1268         rc = mdt_mfd_close(info, mfd);
1269         mdt_empty_transno(info);
1270         RETURN(rc);
1271 }
1272