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