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