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                 struct md_ucred *ucred = mdt_ucred(info);
570
571                 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA2);
572                 LASSERT(capa);
573                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
574                 capa->lc_uid = ucred->mu_o_fsuid;
575                 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
576                 if (rc)
577                         RETURN(rc);
578                 repbody->valid |= OBD_MD_FLOSSCAPA;
579         }
580
581         /*
582          * If we are following a symlink, don't open; and do not return open
583          * handle for special nodes as client required.
584          */
585         if (islnk || (!isreg && !isdir &&
586             (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH))) {
587                 lustre_msg_set_transno(req->rq_repmsg, 0);
588                 RETURN(0);
589         }
590
591         mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
592
593         /*
594          * We need to return the existing object's fid back, so it is done here,
595          * after preparing the reply.
596          */
597         if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
598                 RETURN(-EEXIST);
599
600         /* This can't be done earlier, we need to return reply body */
601         if (isdir) {
602                 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
603                         /* We are trying to create or write an existing dir. */
604                         RETURN(-EISDIR);
605                 }
606         } else if (flags & MDS_OPEN_DIRECTORY)
607                 RETURN(-ENOTDIR);
608
609         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_OPEN_CREATE)) {
610                 obd_fail_loc = OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE;
611                 RETURN(-EAGAIN);
612         }
613
614         mfd = NULL;
615         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
616                 spin_lock(&med->med_open_lock);
617                 list_for_each(t, &med->med_open_head) {
618                         mfd = list_entry(t, struct mdt_file_data, mfd_list);
619                         if (mfd->mfd_xid == req->rq_xid) {
620                                 break;
621                         }
622                         mfd = NULL;
623                 }
624                 spin_unlock(&med->med_open_lock);
625
626                 if (mfd != NULL) {
627                         repbody->handle.cookie = mfd->mfd_handle.h_cookie;
628                         /*set repbody->ea_size for resent case*/
629                         if (ma->ma_valid & MA_LOV) {
630                                 LASSERT(ma->ma_lmm_size != 0);
631                                 repbody->eadatasize = ma->ma_lmm_size;
632                                 if (isdir)
633                                         repbody->valid |= OBD_MD_FLDIREA;
634                                 else
635                                         repbody->valid |= OBD_MD_FLEASIZE;
636                         }
637                         RETURN(0);
638                 }
639         }
640
641         rc = mdt_mfd_open(info, p, o, flags, created);
642         RETURN(rc);
643 }
644
645 extern void mdt_req_from_mcd(struct ptlrpc_request *req,
646                              struct mdt_client_data *mcd);
647
648 void mdt_reconstruct_open(struct mdt_thread_info *info,
649                           struct mdt_lock_handle *lhc)
650 {
651         const struct lu_env *env = info->mti_env;
652         struct mdt_device       *mdt  = info->mti_mdt;
653         struct req_capsule      *pill = &info->mti_pill;
654         struct ptlrpc_request   *req  = mdt_info_req(info);
655         struct mdt_export_data  *med  = &req->rq_export->exp_mdt_data;
656         struct mdt_client_data  *mcd  = med->med_mcd;
657         struct md_attr          *ma   = &info->mti_attr;
658         struct mdt_reint_record *rr   = &info->mti_rr;
659         __u32                   flags = info->mti_spec.sp_cr_flags;
660         struct ldlm_reply       *ldlm_rep;
661         struct mdt_object       *parent;
662         struct mdt_object       *child;
663         struct mdt_body         *repbody;
664         int                      rc;
665         ENTRY;
666
667         LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
668         ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
669         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
670
671         ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
672         ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
673                                                RCL_SERVER);
674         ma->ma_need = MA_INODE | MA_LOV;
675         ma->ma_valid = 0;
676
677         mdt_req_from_mcd(req, med->med_mcd);
678         mdt_set_disposition(info, ldlm_rep, mcd->mcd_last_data);
679
680         CERROR("This is reconstruct open: disp="LPX64", result=%d\n",
681                 ldlm_rep->lock_policy_res1, req->rq_status);
682
683         if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
684             req->rq_status != 0) {
685                 /* We did not create successfully, return error to client. */
686                 mdt_shrink_reply(info);
687                 GOTO(out, rc = req->rq_status);
688         }
689
690         if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
691                 /*
692                  * We failed after creation, but we do not know in which step
693                  * we failed. So try to check the child object.
694                  */
695                 parent = mdt_object_find(env, mdt, rr->rr_fid1);
696                 LASSERT(!IS_ERR(parent));
697
698                 child = mdt_object_find(env, mdt, rr->rr_fid2);
699                 LASSERT(!IS_ERR(child));
700
701                 rc = mdt_object_exists(child);
702                 if (rc > 0) {
703                         struct md_object *next;
704
705                         mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
706                         next = mdt_object_child(child);
707                         rc = mo_attr_get(env, next, ma);
708                         if (rc == 0)
709                               rc = mdt_finish_open(info, parent, child,
710                                                    flags, 1, ldlm_rep);
711                 } else if (rc < 0) {
712                         /* the child object was created on remote server */
713                         repbody->fid1 = *rr->rr_fid2;
714                         repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
715                         rc = 0;
716                 } else if (rc == 0) {
717                         /* the child does not exist, we should do regular open */
718                         mdt_object_put(env, parent);
719                         mdt_object_put(env, child);
720                         GOTO(regular_open, 0);
721                 }
722                 mdt_object_put(env, parent);
723                 mdt_object_put(env, child);
724                 mdt_shrink_reply(info);
725                 GOTO(out, rc);
726         } else {
727 regular_open:
728                 /* We did not try to create, so we are a pure open */
729                 rc = mdt_reint_open(info, lhc);
730         }
731
732         EXIT;
733 out:
734         req->rq_status = rc;
735         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
736         LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
737 }
738
739 static int mdt_open_by_fid(struct mdt_thread_info* info,
740                            struct ldlm_reply *rep)
741 {
742         const struct lu_env     *env = info->mti_env;
743         __u32                    flags = info->mti_spec.sp_cr_flags;
744         struct mdt_reint_record *rr = &info->mti_rr;
745         struct md_attr          *ma = &info->mti_attr;
746         struct mdt_object       *o;
747         int                      rc;
748         ENTRY;
749
750         LASSERT(info->mti_spec.u.sp_ea.no_lov_create);
751         o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
752         if (IS_ERR(o))
753                 RETURN(rc = PTR_ERR(o));
754
755         rc = mdt_object_exists(o);
756         if (rc > 0) {
757                 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
758                                                 DISP_LOOKUP_EXECD |
759                                                 DISP_LOOKUP_POS));
760
761                 rc = mo_attr_get(env, mdt_object_child(o), ma);
762                 if (rc == 0)
763                         rc = mdt_mfd_open(info, NULL, o, flags, 0);
764         } else if (rc == 0) {
765                 rc = -ENOENT;
766         } else  {
767                 /* the child object was created on remote server */
768                 struct mdt_body *repbody;
769                 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
770                 repbody->fid1 = *rr->rr_fid2;
771                 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
772                 rc = 0;
773         }
774
775         mdt_object_put(info->mti_env, o);
776         RETURN(rc);
777 }
778
779 int mdt_pin(struct mdt_thread_info* info)
780 {
781         ENTRY;
782         RETURN(err_serious(-EOPNOTSUPP));
783 }
784
785 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
786 static int mdt_cross_open(struct mdt_thread_info* info,
787                           const struct lu_fid *fid,
788                           struct ldlm_reply *rep, __u32 flags)
789 {
790         struct md_attr    *ma = &info->mti_attr;
791         struct mdt_object *o;
792         int                rc;
793         ENTRY;
794
795         o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
796         if (IS_ERR(o))
797                 RETURN(rc = PTR_ERR(o));
798
799         rc = mdt_object_exists(o);
800         if (rc > 0) {
801                 /* Do permission check for cross-open. */
802                 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o),
803                                    NULL, flags | MDS_OPEN_CROSS);
804                 if (rc)
805                         goto out;
806
807                 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
808                 rc = mo_attr_get(info->mti_env, mdt_object_child(o), ma);
809                 if (rc == 0)
810                         rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
811         } else if (rc == 0) {
812                 /*
813                  * Something is wrong here. lookup was positive but there is
814                  * no object!
815                  */
816                 CERROR("Cross-ref object doesn't exist!\n");
817                 rc = -EFAULT;
818         } else  {
819                 /* Something is wrong here, the object is on another MDS! */
820                 CERROR("The object isn't on this server! FLD error?\n");
821                 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
822                                 &o->mot_obj.mo_lu,
823                                 "Object isn't on this server! FLD error?\n");
824
825                 rc = -EFAULT;
826         }
827
828 out:
829         mdt_object_put(info->mti_env, o);
830         RETURN(rc);
831 }
832
833 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
834 {
835         struct mdt_device       *mdt = info->mti_mdt;
836         struct ptlrpc_request   *req = mdt_info_req(info);
837         struct mdt_object       *parent;
838         struct mdt_object       *child;
839         struct mdt_lock_handle  *lh;
840         struct ldlm_reply       *ldlm_rep;
841         struct mdt_body         *repbody;
842         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
843         struct md_attr          *ma = &info->mti_attr;
844         __u32                    create_flags = info->mti_spec.sp_cr_flags;
845         struct mdt_reint_record *rr = &info->mti_rr;
846         struct lu_name          *lname;
847         int                      result;
848         int                      created = 0;
849         ENTRY;
850
851         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PAUSE_OPEN | OBD_FAIL_ONCE,
852                          (obd_timeout + 1) / 4);
853
854         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
855
856         ma->ma_lmm = req_capsule_server_get(&info->mti_pill, &RMF_MDT_MD);
857         ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill, &RMF_MDT_MD,
858                                                RCL_SERVER);
859         ma->ma_need = MA_INODE | MA_LOV;
860         ma->ma_valid = 0;
861
862         LASSERT(info->mti_pill.rc_fmt == &RQF_LDLM_INTENT_OPEN);
863         ldlm_rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
864
865         /* TODO: JOIN file */
866         if (create_flags & MDS_OPEN_JOIN_FILE) {
867                 CERROR("JOIN file will be supported soon\n");
868                 GOTO(out, result = err_serious(-EOPNOTSUPP));
869         }
870
871         CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
872                "cr_flag=0%o mode=0%06o msg_flag=0x%x\n",
873                PFID(rr->rr_fid1), rr->rr_name,
874                PFID(rr->rr_fid2), create_flags,
875                ma->ma_attr.la_mode, lustre_msg_get_flags(req->rq_reqmsg));
876
877         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
878                 /* This is a replay request. */
879                 result = mdt_open_by_fid(info, ldlm_rep);
880
881                 if (result != -ENOENT)
882                         GOTO(out, result);
883
884                 /*
885                  * We didn't find the correct object, so we need to re-create it
886                  * via a regular replay.
887                  */
888                 if (!(create_flags & MDS_OPEN_CREAT)) {
889                         DEBUG_REQ(D_ERROR, req,"OPEN & CREAT not in open replay.");
890                         GOTO(out, result = -EFAULT);
891                 }
892                 CDEBUG(D_INFO, "Open replay did find object, continue as "
893                        "regular open\n");
894         }
895
896         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
897                 GOTO(out, result = err_serious(-ENOMEM));
898
899         mdt_set_disposition(info, ldlm_rep,
900                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
901
902         if (info->mti_cross_ref) {
903                 /* This is cross-ref open */
904                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
905                 result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
906                                         create_flags);
907                 GOTO(out, result);
908         }
909
910         lh = &info->mti_lh[MDT_LH_PARENT];
911         mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
912                           LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
913
914         parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
915                                       MDS_INODELOCK_UPDATE);
916         if (IS_ERR(parent))
917                 GOTO(out, result = PTR_ERR(parent));
918
919         fid_zero(child_fid);
920
921         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
922
923         result = mdo_lookup(info->mti_env, mdt_object_child(parent),
924                             lname, child_fid, &info->mti_spec);
925         LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
926                  "looking for "DFID"/%s, result fid="DFID"\n",
927                  PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
928
929         if (result != 0 && result != -ENOENT && result != -ESTALE)
930                 GOTO(out_parent, result);
931
932         if (result == -ENOENT || result == -ESTALE) {
933                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
934                 if (result == -ESTALE) {
935                         /*
936                          * -ESTALE means the parent is a dead(unlinked) dir, so
937                          * it should return -ENOENT to in accordance with the
938                          * original mds implementaion.
939                          */
940                         GOTO(out_parent, result = -ENOENT);
941                 }
942                 if (!(create_flags & MDS_OPEN_CREAT))
943                         GOTO(out_parent, result);
944                 *child_fid = *info->mti_rr.rr_fid2;
945                 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
946                          PFID(child_fid));
947         } else {
948                 /*
949                  * Check for O_EXCL is moved to the mdt_finish_open(), we need to
950                  * return FID back in that case.
951                  */
952                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
953         }
954
955         child = mdt_object_find(info->mti_env, mdt, child_fid);
956         if (IS_ERR(child))
957                 GOTO(out_parent, result = PTR_ERR(child));
958
959         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
960         if (result == -ENOENT) {
961                 /* Not found and with MDS_OPEN_CREAT: let's create it. */
962                 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
963
964                 /* Let lower layers know what is lock mode on directory. */
965                 info->mti_spec.sp_cr_mode =
966                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
967
968                 /*
969                  * Do not perform lookup sanity check. We know that name does
970                  * not exist.
971                  */
972                 info->mti_spec.sp_cr_lookup = 0;
973
974                 result = mdo_create(info->mti_env,
975                                     mdt_object_child(parent),
976                                     lname,
977                                     mdt_object_child(child),
978                                     &info->mti_spec,
979                                     &info->mti_attr);
980                 if (result == -ERESTART) {
981                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
982                         GOTO(out_child, result);
983                 } else {
984                         if (result != 0)
985                                 GOTO(out_child, result);
986                 }
987                 created = 1;
988         } else {
989                 /* We have to get attr & lov ea for this object */
990                 result = mo_attr_get(info->mti_env, mdt_object_child(child),
991                                      ma);
992                 /*
993                  * The object is on remote node, return its FID for remote open.
994                  */
995                 if (result == -EREMOTE) {
996                         int rc;
997
998                         /*
999                          * Check if this lock already was sent to client and
1000                          * this is resent case. For resent case do not take lock
1001                          * again, use what is already granted.
1002                          */
1003                         LASSERT(lhc != NULL);
1004
1005                         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1006                                 struct ldlm_lock *lock;
1007
1008                                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
1009                                         MSG_RESENT);
1010
1011                                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1012                                 if (!lock) {
1013                                         CERROR("Invalid lock handle "LPX64"\n",
1014                                                lhc->mlh_reg_lh.cookie);
1015                                         LBUG();
1016                                 }
1017                                 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1018                                                         &lock->l_resource->lr_name));
1019                                 LDLM_LOCK_PUT(lock);
1020                                 rc = 0;
1021                         } else {
1022                                 mdt_lock_handle_init(lhc);
1023                                 mdt_lock_reg_init(lhc, LCK_PR);
1024
1025                                 rc = mdt_object_lock(info, child, lhc,
1026                                                      MDS_INODELOCK_LOOKUP,
1027                                                      MDT_CROSS_LOCK);
1028                         }
1029                         repbody->fid1 = *mdt_object_fid(child);
1030                         repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1031                         if (rc != 0)
1032                                 result = rc;
1033                         GOTO(out_child, result);
1034                 }
1035         }
1036
1037         /* Try to open it now. */
1038         result = mdt_finish_open(info, parent, child, create_flags,
1039                                  created, ldlm_rep);
1040
1041         if (result != 0 && created) {
1042                 int rc2;
1043                 ma->ma_need = 0;
1044                 ma->ma_valid = 0;
1045                 ma->ma_cookie_size = 0;
1046                 info->mti_no_need_trans = 1;
1047                 rc2 = mdo_unlink(info->mti_env,
1048                                  mdt_object_child(parent),
1049                                  mdt_object_child(child),
1050                                  lname,
1051                                  &info->mti_attr);
1052                 if (rc2 != 0)
1053                         CERROR("Error in cleanup of open\n");
1054         }
1055         EXIT;
1056 out_child:
1057         mdt_object_put(info->mti_env, child);
1058 out_parent:
1059         mdt_object_unlock_put(info, parent, lh, result);
1060 out:
1061         mdt_shrink_reply(info);
1062         if (result)
1063                 lustre_msg_set_transno(req->rq_repmsg, 0);
1064         return result;
1065 }
1066
1067 #define MFD_CLOSED(mode) (((mode) & ~(FMODE_EPOCH | FMODE_SOM | \
1068                                       FMODE_EPOCHLCK)) == FMODE_CLOSED)
1069
1070 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1071 {
1072         return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1073 }
1074
1075 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1076 {
1077         struct mdt_object *o = mfd->mfd_object;
1078         struct md_object *next = mdt_object_child(o);
1079         struct md_attr *ma = &info->mti_attr;
1080         int rc = 0, ret = 0;
1081         int mode;
1082         ENTRY;
1083
1084         mode = mfd->mfd_mode;
1085
1086         if ((mode & FMODE_WRITE) || (mode & FMODE_EPOCHLCK)) {
1087                 mdt_write_put(info->mti_mdt, o);
1088                 ret = mdt_epoch_close(info, o);
1089         } else if (mode & MDS_FMODE_EXEC) {
1090                 mdt_write_allow(info->mti_mdt, o);
1091         } else if (mode & FMODE_EPOCH) {
1092                 ret = mdt_epoch_close(info, o);
1093         } 
1094         
1095         /* Update atime on close only. */
1096         if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1097             && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1098                 /* Set the atime only. */
1099                 ma->ma_attr.la_valid = LA_ATIME;
1100                 rc = mo_attr_set(info->mti_env, next, ma);
1101         }
1102
1103         ma->ma_need |= MA_INODE;
1104         ma->ma_valid = 0;
1105
1106         if (!MFD_CLOSED(mode))
1107                 rc = mo_close(info->mti_env, next, ma);
1108         else if (ret == -EAGAIN)
1109                 rc = mo_attr_get(info->mti_env, next, ma);
1110
1111         /* If the object is unlinked, do not try to re-enable SIZEONMDS */
1112         if ((ret == -EAGAIN) && (ma->ma_valid & MA_INODE) &&
1113             (ma->ma_attr.la_nlink == 0)) {
1114                 ret = 0;
1115         }
1116
1117         if ((ret == -EAGAIN) || (ret == 1)) {
1118                 struct mdt_export_data *med;
1119
1120                 /* The epoch has not closed or Size-on-MDS update is needed.
1121                  * Put mfd back into the list. */
1122                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1123                 mdt_mfd_set_mode(mfd, (ret == 1 ? FMODE_EPOCH : FMODE_SOM));
1124
1125                 LASSERT(mdt_info_req(info));
1126                 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1127                 spin_lock(&med->med_open_lock);
1128                 list_add(&mfd->mfd_list, &med->med_open_head);
1129                 class_handle_hash_back(&mfd->mfd_handle);
1130                 spin_unlock(&med->med_open_lock);
1131
1132                 if (ret == 1) {
1133                         ret = 0;
1134                 } else {
1135                         CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1136                                "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1137                 }
1138         } else {
1139                 mdt_mfd_free(mfd);
1140                 mdt_object_put(info->mti_env, o);
1141         }
1142
1143         RETURN(rc ? rc : ret);
1144 }
1145
1146 int mdt_close(struct mdt_thread_info *info)
1147 {
1148         struct mdt_export_data *med;
1149         struct mdt_file_data   *mfd;
1150         struct mdt_object      *o;
1151         struct md_attr         *ma = &info->mti_attr;
1152         struct mdt_body        *repbody = NULL;
1153         struct ptlrpc_request  *req = mdt_info_req(info);
1154         int rc, ret = 0;
1155         ENTRY;
1156
1157         /* Close may come with the Size-on-MDS update. Unpack it. */
1158         rc = mdt_close_unpack(info);
1159         if (rc)
1160                 RETURN(err_serious(rc));
1161
1162         LASSERT(info->mti_epoch);
1163
1164         req_capsule_set_size(&info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1165                              info->mti_mdt->mdt_max_mdsize);
1166         req_capsule_set_size(&info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1167                              info->mti_mdt->mdt_max_cookiesize);
1168         rc = req_capsule_pack(&info->mti_pill);
1169         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL))
1170                 RETURN(lustre_msg_get_status(req->rq_repmsg));
1171
1172         /* Continue to close handle even if we can not pack reply */
1173         if (rc == 0) {
1174                 repbody = req_capsule_server_get(&info->mti_pill,
1175                                                  &RMF_MDT_BODY);
1176                 ma->ma_lmm = req_capsule_server_get(&info->mti_pill,
1177                                                     &RMF_MDT_MD);
1178                 ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill,
1179                                                        &RMF_MDT_MD,
1180                                                        RCL_SERVER);
1181                 ma->ma_cookie = req_capsule_server_get(&info->mti_pill,
1182                                                        &RMF_LOGCOOKIES);
1183                 ma->ma_cookie_size = req_capsule_get_size(&info->mti_pill,
1184                                                           &RMF_LOGCOOKIES,
1185                                                           RCL_SERVER);
1186                 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1187                 repbody->eadatasize = 0;
1188                 repbody->aclsize = 0;
1189         } else
1190                 rc = err_serious(rc);
1191
1192         med = &req->rq_export->exp_mdt_data;
1193         spin_lock(&med->med_open_lock);
1194         mfd = mdt_handle2mfd(info, &info->mti_epoch->handle);
1195         if (mdt_mfd_closed(mfd)) {
1196                 spin_unlock(&med->med_open_lock);
1197                 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1198                        ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1199                        info->mti_epoch->handle.cookie);
1200                 rc = err_serious(-ESTALE);
1201         } else {
1202                 class_handle_unhash(&mfd->mfd_handle);
1203                 list_del_init(&mfd->mfd_list);
1204                 spin_unlock(&med->med_open_lock);
1205
1206                 /* Do not lose object before last unlink. */
1207                 o = mfd->mfd_object;
1208                 mdt_object_get(info->mti_env, o);
1209                 ret = mdt_mfd_close(info, mfd);
1210                 if (repbody != NULL)
1211                         rc = mdt_handle_last_unlink(info, o, ma);
1212                 mdt_empty_transno(info);
1213                 mdt_object_put(info->mti_env, o);
1214         }
1215         if (repbody != NULL)
1216                 mdt_shrink_reply(info);
1217
1218         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1219                 RETURN(err_serious(-ENOMEM));
1220
1221         RETURN(rc ? rc : ret);
1222 }
1223
1224 int mdt_done_writing(struct mdt_thread_info *info)
1225 {
1226         struct mdt_body         *repbody = NULL;
1227         struct mdt_export_data  *med;
1228         struct mdt_file_data    *mfd;
1229         int rc;
1230         ENTRY;
1231
1232         rc = req_capsule_pack(&info->mti_pill);
1233         if (rc)
1234                 RETURN(err_serious(rc));
1235
1236         repbody = req_capsule_server_get(&info->mti_pill,
1237                                          &RMF_MDT_BODY);
1238         repbody->eadatasize = 0;
1239         repbody->aclsize = 0;
1240
1241         /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1242         rc = mdt_close_unpack(info);
1243         if (rc)
1244                 RETURN(err_serious(rc));
1245
1246         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL))
1247                 RETURN(lustre_msg_get_status(mdt_info_req(info)->rq_repmsg));
1248
1249         med = &info->mti_exp->exp_mdt_data;
1250         spin_lock(&med->med_open_lock);
1251         mfd = mdt_handle2mfd(info, &info->mti_epoch->handle);
1252         if (mfd == NULL) {
1253                 spin_unlock(&med->med_open_lock);
1254                 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1255                        ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1256                        info->mti_epoch->handle.cookie);
1257                 RETURN(-ESTALE);
1258         } 
1259  
1260         LASSERT(mfd->mfd_mode == FMODE_EPOCH ||
1261                 mfd->mfd_mode == FMODE_EPOCHLCK);
1262         class_handle_unhash(&mfd->mfd_handle);
1263         list_del_init(&mfd->mfd_list);
1264         spin_unlock(&med->med_open_lock);
1265
1266         /* Set EPOCH CLOSE flag if not set by client. */
1267         info->mti_epoch->flags |= MF_EPOCH_CLOSE;
1268         info->mti_attr.ma_valid = 0;
1269         rc = mdt_mfd_close(info, mfd);
1270         mdt_empty_transno(info);
1271         RETURN(rc);
1272 }
1273