Whamcloud - gitweb
LU-1431 ptlrpc: PTLRPC_BRW_MAX_SIZE usage cleanup.
[fs/lustre-release.git] / lustre / mdt / mdt_open.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
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 #define DEBUG_SUBSYSTEM S_MDS
44
45 #include <lustre_acl.h>
46 #include <lustre_mds.h>
47 #include "mdt_internal.h"
48
49 /* we do nothing because we do not have refcount now */
50 static void mdt_mfd_get(void *mfdp)
51 {
52 }
53
54 static struct portals_handle_ops mfd_handle_ops = {
55         .hop_addref = mdt_mfd_get,
56         .hop_free   = NULL,
57 };
58
59 /* Create a new mdt_file_data struct, initialize it,
60  * and insert it to global hash table */
61 struct mdt_file_data *mdt_mfd_new(void)
62 {
63         struct mdt_file_data *mfd;
64         ENTRY;
65
66         OBD_ALLOC_PTR(mfd);
67         if (mfd != NULL) {
68                 CFS_INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
69                 CFS_INIT_LIST_HEAD(&mfd->mfd_list);
70                 class_handle_hash(&mfd->mfd_handle, &mfd_handle_ops);
71         }
72         RETURN(mfd);
73 }
74
75 /*
76  * Find the mfd pointed to by handle in global hash table.
77  * In case of replay the handle is obsoleted
78  * but mfd can be found in mfd list by that handle
79  */
80 struct mdt_file_data *mdt_handle2mfd(struct mdt_thread_info *info,
81                                      const struct lustre_handle *handle)
82 {
83         struct ptlrpc_request *req = mdt_info_req(info);
84         struct mdt_file_data  *mfd;
85         ENTRY;
86
87         LASSERT(handle != NULL);
88         mfd = class_handle2object(handle->cookie);
89         /* during dw/setattr replay the mfd can be found by old handle */
90         if (mfd == NULL && req_is_replay(req)) {
91                 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
92                 cfs_list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
93                         if (mfd->mfd_old_handle.cookie == handle->cookie)
94                                 RETURN (mfd);
95                 }
96                 mfd = NULL;
97         }
98         RETURN (mfd);
99 }
100
101 /* free mfd */
102 void mdt_mfd_free(struct mdt_file_data *mfd)
103 {
104         LASSERT(cfs_list_empty(&mfd->mfd_list));
105         OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
106 }
107
108 static int mdt_create_data(struct mdt_thread_info *info,
109                            struct mdt_object *p, struct mdt_object *o)
110 {
111         struct md_op_spec     *spec = &info->mti_spec;
112         struct md_attr        *ma   = &info->mti_attr;
113         int                    rc   = 0;
114         ENTRY;
115
116         if (!md_should_create(spec->sp_cr_flags))
117                 RETURN(0);
118
119         ma->ma_need = MA_INODE | MA_LOV;
120         ma->ma_valid = 0;
121         mutex_lock(&o->mot_lov_mutex);
122         if (!(o->mot_flags & MOF_LOV_CREATED)) {
123                 rc = mdo_create_data(info->mti_env,
124                                      p ? mdt_object_child(p) : NULL,
125                                      mdt_object_child(o), spec, ma);
126                 if (rc == 0)
127                         rc = mdt_attr_get_complex(info, o, ma);
128
129                 if (rc == 0 && ma->ma_valid & MA_LOV)
130                         o->mot_flags |= MOF_LOV_CREATED;
131         }
132         mutex_unlock(&o->mot_lov_mutex);
133         RETURN(rc);
134 }
135
136 static int mdt_ioepoch_opened(struct mdt_object *mo)
137 {
138         return mo->mot_ioepoch_count;
139 }
140
141 int mdt_object_is_som_enabled(struct mdt_object *mo)
142 {
143         return !mo->mot_ioepoch;
144 }
145
146 /**
147  * Re-enable Size-on-MDS.
148  * Call under ->mot_ioepoch_mutex.
149  */
150 static void mdt_object_som_enable(struct mdt_object *mo, __u64 ioepoch)
151 {
152         if (ioepoch == mo->mot_ioepoch) {
153                 LASSERT(!mdt_ioepoch_opened(mo));
154                 mo->mot_ioepoch = 0;
155                 mo->mot_flags = 0;
156         }
157 }
158
159 /**
160  * Open the IOEpoch. It is allowed if @writecount is not negative.
161  * The epoch and writecount handling is performed under the mot_ioepoch_mutex.
162  */
163 int mdt_ioepoch_open(struct mdt_thread_info *info, struct mdt_object *o,
164                      int created)
165 {
166         struct mdt_device *mdt = info->mti_mdt;
167         int cancel = 0;
168         int rc = 0;
169         ENTRY;
170
171         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
172             !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
173                 RETURN(0);
174
175         mutex_lock(&o->mot_ioepoch_mutex);
176         if (mdt_ioepoch_opened(o)) {
177                 /* Epoch continues even if there is no writers yet. */
178                 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
179                        o->mot_ioepoch, PFID(mdt_object_fid(o)));
180         } else {
181                 /* XXX: ->mdt_ioepoch is not initialized at the mount */
182                 spin_lock(&mdt->mdt_ioepoch_lock);
183                 if (mdt->mdt_ioepoch < info->mti_replayepoch)
184                         mdt->mdt_ioepoch = info->mti_replayepoch;
185
186                 if (info->mti_replayepoch)
187                         o->mot_ioepoch = info->mti_replayepoch;
188                 else if (++mdt->mdt_ioepoch == IOEPOCH_INVAL)
189                         o->mot_ioepoch = ++mdt->mdt_ioepoch;
190                 else
191                         o->mot_ioepoch = mdt->mdt_ioepoch;
192
193                 spin_unlock(&mdt->mdt_ioepoch_lock);
194
195                 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
196                        o->mot_ioepoch, PFID(mdt_object_fid(o)));
197                 if (created)
198                         o->mot_flags |= MOF_SOM_CREATED;
199                 cancel = 1;
200         }
201         o->mot_ioepoch_count++;
202         mutex_unlock(&o->mot_ioepoch_mutex);
203
204         /* Cancel Size-on-MDS attributes cached on clients for the open case.
205          * In the truncate case, see mdt_reint_setattr(). */
206         if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
207                 struct mdt_lock_handle  *lh = &info->mti_lh[MDT_LH_CHILD];
208                 mdt_lock_reg_init(lh, LCK_EX);
209                 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
210                                      MDT_LOCAL_LOCK);
211                 if (rc == 0)
212                         mdt_object_unlock(info, o, lh, 1);
213         }
214         RETURN(rc);
215 }
216
217 /**
218  * Update SOM on-disk attributes.
219  * If enabling, write update inodes and lustre-ea with the proper IOEpoch,
220  * mountid and attributes. If disabling, clean SOM xattr.
221  * Call under ->mot_ioepoch_mutex.
222  */
223 static int mdt_som_attr_set(struct mdt_thread_info *info,
224                             struct mdt_object *obj, __u64 ioepoch, bool enable)
225 {
226         struct md_object        *next = mdt_object_child(obj);
227         int                      rc;
228         ENTRY;
229
230         CDEBUG(D_INODE, "Size-on-MDS attribute %s for epoch "LPU64
231                " on "DFID".\n", enable ? "update" : "disabling",
232                ioepoch, PFID(mdt_object_fid(obj)));
233
234         if (enable) {
235                 struct lu_buf           *buf = &info->mti_buf;
236                 struct som_attrs        *attrs;
237                 struct md_attr          *ma = &info->mti_attr;
238                 struct lu_attr          *la = &ma->ma_attr;
239                 struct obd_device       *obd = info->mti_mdt->mdt_lut.lut_obd;
240
241                 attrs = (struct som_attrs *)info->mti_xattr_buf;
242                 CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*attrs));
243
244                 /* pack SOM attributes */
245                 memset(attrs, 0, sizeof(*attrs));
246                 attrs->som_ioepoch = ioepoch;
247                 attrs->som_mountid = obd->u.obt.obt_mount_count;
248                 if ((la->la_valid & LA_SIZE) != 0)
249                         attrs->som_size = la->la_size;
250                 if ((la->la_valid & LA_BLOCKS) != 0)
251                         attrs->som_blocks = la->la_blocks;
252                 lustre_som_swab(attrs);
253
254                 /* update SOM attributes */
255                 buf->lb_buf = attrs;
256                 buf->lb_len = sizeof(*attrs);
257                 rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_SOM, 0);
258         } else {
259                 /* delete SOM attributes */
260                 rc = mo_xattr_del(info->mti_env, next, XATTR_NAME_SOM);
261         }
262
263         RETURN(rc);
264 }
265
266 /** Perform the eviction specific actions on ioepoch close. */
267 static inline int mdt_ioepoch_close_on_eviction(struct mdt_thread_info *info,
268                                                 struct mdt_object *o)
269 {
270         int rc = 0;
271
272         mutex_lock(&o->mot_ioepoch_mutex);
273         CDEBUG(D_INODE, "Eviction. Closing IOepoch "LPU64" on "DFID". "
274                "Count %d\n", o->mot_ioepoch, PFID(mdt_object_fid(o)),
275                o->mot_ioepoch_count);
276         o->mot_ioepoch_count--;
277
278         /* If eviction occured set MOF_SOM_RECOV,
279          * if no other epoch holders, disable SOM on disk. */
280         o->mot_flags |= MOF_SOM_CHANGE | MOF_SOM_RECOV;
281         if (!mdt_ioepoch_opened(o)) {
282                 rc = mdt_som_attr_set(info, o, o->mot_ioepoch, MDT_SOM_DISABLE);
283                 mdt_object_som_enable(o, o->mot_ioepoch);
284         }
285         mutex_unlock(&o->mot_ioepoch_mutex);
286         RETURN(rc);
287 }
288
289 /**
290  * Perform the replay specific actions on ioepoch close.
291  * Skip SOM attribute update if obtained and just forget about the inode state
292  * for the last ioepoch holder. The SOM cache is invalidated on MDS failure.
293  */
294 static inline int mdt_ioepoch_close_on_replay(struct mdt_thread_info *info,
295                                               struct mdt_object *o)
296 {
297         int rc = MDT_IOEPOCH_CLOSED;
298         ENTRY;
299
300         mutex_lock(&o->mot_ioepoch_mutex);
301         CDEBUG(D_INODE, "Replay. Closing epoch "LPU64" on "DFID". Count %d\n",
302                o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
303         o->mot_ioepoch_count--;
304
305         /* Get an info from the replayed request if client is supposed
306          * to send an Attibute Update, reconstruct @rc if so */
307         if (info->mti_ioepoch->flags & MF_SOM_AU)
308                 rc = MDT_IOEPOCH_GETATTR;
309
310         if (!mdt_ioepoch_opened(o))
311                 mdt_object_som_enable(o, info->mti_ioepoch->ioepoch);
312         mutex_unlock(&o->mot_ioepoch_mutex);
313
314         RETURN(rc);
315 }
316
317 /**
318  * Regular file IOepoch close.
319  * Closes the ioepoch, checks the object state, apply obtained attributes and
320  * re-enable SOM on the object, if possible. Also checks if the recovery is
321  * needed and packs OBD_MD_FLGETATTRLOCK flag into the reply to force the client
322  * to obtain SOM attributes under the server-side OST locks.
323  *
324  * Return value:
325  * MDT_IOEPOCH_CLOSED if ioepoch is closed.
326  * MDT_IOEPOCH_GETATTR if ioepoch is closed but another SOM update is needed.
327  */
328 static inline int mdt_ioepoch_close_reg(struct mdt_thread_info *info,
329                                         struct mdt_object *o)
330 {
331         struct md_attr *tmp_ma;
332         struct lu_attr *la;
333         int achange, opened;
334         int recovery = 0;
335         int rc = 0, ret = MDT_IOEPOCH_CLOSED;
336         ENTRY;
337
338         la = &info->mti_attr.ma_attr;
339         achange = (info->mti_ioepoch->flags & MF_SOM_CHANGE);
340
341         mutex_lock(&o->mot_ioepoch_mutex);
342         o->mot_ioepoch_count--;
343
344         tmp_ma = &info->mti_u.som.attr;
345         tmp_ma->ma_lmm = info->mti_attr.ma_lmm;
346         tmp_ma->ma_lmm_size = info->mti_attr.ma_lmm_size;
347         tmp_ma->ma_som = &info->mti_u.som.data;
348         tmp_ma->ma_need = MA_INODE | MA_LOV | MA_SOM;
349         tmp_ma->ma_valid = 0;
350         rc = mdt_attr_get_complex(info, o, tmp_ma);
351         if (rc)
352                 GOTO(error_up, rc);
353
354         /* Check the on-disk SOM state. */
355         if (o->mot_flags & MOF_SOM_RECOV)
356                 recovery = 1;
357         else if (!(o->mot_flags & MOF_SOM_CREATED) &&
358                  !(tmp_ma->ma_valid & MA_SOM))
359                 recovery = 1;
360
361         CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
362                o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
363
364         opened = mdt_ioepoch_opened(o);
365         /**
366          * If IOEpoch is not opened, check if a Size-on-MDS update is needed.
367          * Skip the check for file with no LOV  or for unlink files.
368          */
369         if (!opened && tmp_ma->ma_valid & MA_LOV &&
370             !(tmp_ma->ma_valid & MA_INODE && tmp_ma->ma_attr.la_nlink == 0)) {
371                 if (recovery)
372                         /* If some previous writer was evicted, re-ask the
373                          * client for attributes. Even if attributes are
374                          * provided, we cannot believe in them.
375                          * Another use case is that there is no SOM cache on
376                          * disk -- first access with SOM or there was an MDS
377                          * failure. */
378                         ret = MDT_IOEPOCH_GETATTR;
379                 else if (o->mot_flags & MOF_SOM_CHANGE)
380                         /* Some previous writer changed the attribute.
381                          * Do not believe to the current Size-on-MDS
382                          * update, re-ask client. */
383                         ret = MDT_IOEPOCH_GETATTR;
384                 else if (!(la->la_valid & LA_SIZE) && achange)
385                         /* Attributes were changed by the last writer
386                          * only but no Size-on-MDS update is received.*/
387                         ret = MDT_IOEPOCH_GETATTR;
388         }
389
390         if (achange || ret == MDT_IOEPOCH_GETATTR)
391                 o->mot_flags |= MOF_SOM_CHANGE;
392
393         /* If epoch ends and relable SOM attributes are obtained, update them.
394          * Create SOM ea for new files even if there is no attributes obtained
395          * (0-length file). */
396         if (ret == MDT_IOEPOCH_CLOSED && !opened) {
397                 if (achange || o->mot_flags & MOF_SOM_CREATED) {
398                         LASSERT(achange || !(la->la_valid & LA_SIZE));
399                         rc = mdt_som_attr_set(info, o, o->mot_ioepoch,
400                                               MDT_SOM_ENABLE);
401                         /* Avoid the following setattrs of these attributes,
402                          * e.g. for atime update. */
403                         info->mti_attr.ma_valid = 0;
404                 }
405                 mdt_object_som_enable(o, o->mot_ioepoch);
406         }
407
408         mutex_unlock(&o->mot_ioepoch_mutex);
409         /* If recovery is needed, tell the client to perform GETATTR under
410          * the lock. */
411         if (ret == MDT_IOEPOCH_GETATTR && recovery) {
412                 struct mdt_body *rep;
413                 rep = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
414                 rep->valid |= OBD_MD_FLGETATTRLOCK;
415         }
416
417         RETURN(rc ? : ret);
418
419 error_up:
420         mutex_unlock(&o->mot_ioepoch_mutex);
421         return rc;
422 }
423
424 /**
425  * Close IOEpoch (opened file or MDS_FMODE_EPOCH state). It happens if:
426  * - a client closes the IOEpoch;
427  * - a client eviction occured.
428  * Return values:
429  * MDT_IOEPOCH_OPENED if the client does not close IOEpoch.
430  * MDT_IOEPOCH_CLOSED if the client closes IOEpoch.
431  * MDT_IOEPOCH_GETATTR if the client closes IOEpoch but another SOM attribute
432  * update is needed.
433  */
434 static int mdt_ioepoch_close(struct mdt_thread_info *info, struct mdt_object *o)
435 {
436         struct ptlrpc_request *req = mdt_info_req(info);
437         ENTRY;
438
439         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
440             !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
441                 RETURN(0);
442
443         LASSERT(o->mot_ioepoch_count);
444         LASSERT(info->mti_ioepoch == NULL ||
445                 info->mti_ioepoch->ioepoch == o->mot_ioepoch);
446
447         /* IOEpoch is closed only if client tells about it or eviction occures.
448          * In the replay case, always close the epoch. */
449         if (req == NULL)
450                 RETURN(mdt_ioepoch_close_on_eviction(info, o));
451         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
452                 RETURN(mdt_ioepoch_close_on_replay(info, o));
453         if (info->mti_ioepoch->flags & MF_EPOCH_CLOSE)
454                 RETURN(mdt_ioepoch_close_reg(info, o));
455         /* IO epoch is not closed. */
456         RETURN(MDT_IOEPOCH_OPENED);
457 }
458
459 /**
460  * Close MDS_FMODE_SOM state, when IOEpoch is already closed and we are waiting
461  * for attribute update. It happens if:
462  * - SOM Attribute Update is obtained;
463  * - the client failed to obtain it and informs MDS about it;
464  * - a client eviction occured.
465  * Apply obtained attributes for the 1st case, wipe out the on-disk SOM
466  * cache otherwise.
467  */
468 int mdt_som_au_close(struct mdt_thread_info *info, struct mdt_object *o)
469 {
470         struct ptlrpc_request *req = mdt_info_req(info);
471         __u64 ioepoch = 0;
472         int act = MDT_SOM_ENABLE;
473         int rc = 0;
474         ENTRY;
475
476         LASSERT(!req || info->mti_ioepoch);
477         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
478             !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
479                 RETURN(0);
480
481         /* No size whereas MF_SOM_CHANGE is set means client failed to
482          * obtain ost attributes, drop the SOM cache on disk if so. */
483         if (!req ||
484             (info->mti_ioepoch &&
485              info->mti_ioepoch->flags & MF_SOM_CHANGE &&
486              !(info->mti_attr.ma_attr.la_valid & LA_SIZE)))
487                 act = MDT_SOM_DISABLE;
488
489         mutex_lock(&o->mot_ioepoch_mutex);
490         /* Mark the object it is the recovery state if we failed to obtain
491          * SOM attributes. */
492         if (act == MDT_SOM_DISABLE)
493                 o->mot_flags |= MOF_SOM_RECOV;
494
495         if (!mdt_ioepoch_opened(o)) {
496                 ioepoch =  info->mti_ioepoch ?
497                         info->mti_ioepoch->ioepoch : o->mot_ioepoch;
498
499                 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))
500                         rc = mdt_som_attr_set(info, o, ioepoch, act);
501                 mdt_object_som_enable(o, ioepoch);
502         }
503         mutex_unlock(&o->mot_ioepoch_mutex);
504         RETURN(rc);
505 }
506
507 int mdt_write_read(struct mdt_object *o)
508 {
509         int rc = 0;
510         ENTRY;
511         mutex_lock(&o->mot_ioepoch_mutex);
512         rc = o->mot_writecount;
513         mutex_unlock(&o->mot_ioepoch_mutex);
514         RETURN(rc);
515 }
516
517 int mdt_write_get(struct mdt_object *o)
518 {
519         int rc = 0;
520         ENTRY;
521         mutex_lock(&o->mot_ioepoch_mutex);
522         if (o->mot_writecount < 0)
523                 rc = -ETXTBSY;
524         else
525                 o->mot_writecount++;
526         mutex_unlock(&o->mot_ioepoch_mutex);
527         RETURN(rc);
528 }
529
530 void mdt_write_put(struct mdt_object *o)
531 {
532         ENTRY;
533         mutex_lock(&o->mot_ioepoch_mutex);
534         o->mot_writecount--;
535         mutex_unlock(&o->mot_ioepoch_mutex);
536         EXIT;
537 }
538
539 static int mdt_write_deny(struct mdt_object *o)
540 {
541         int rc = 0;
542         ENTRY;
543         mutex_lock(&o->mot_ioepoch_mutex);
544         if (o->mot_writecount > 0)
545                 rc = -ETXTBSY;
546         else
547                 o->mot_writecount--;
548         mutex_unlock(&o->mot_ioepoch_mutex);
549         RETURN(rc);
550 }
551
552 static void mdt_write_allow(struct mdt_object *o)
553 {
554         ENTRY;
555         mutex_lock(&o->mot_ioepoch_mutex);
556         o->mot_writecount++;
557         mutex_unlock(&o->mot_ioepoch_mutex);
558         EXIT;
559 }
560
561 /* there can be no real transaction so prepare the fake one */
562 static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
563 {
564         struct mdt_device      *mdt = info->mti_mdt;
565         struct ptlrpc_request  *req = mdt_info_req(info);
566         struct tg_export_data  *ted;
567         struct lsd_client_data *lcd;
568
569         ENTRY;
570         /* transaction has occurred already */
571         if (lustre_msg_get_transno(req->rq_repmsg) != 0)
572                 RETURN_EXIT;
573
574         spin_lock(&mdt->mdt_lut.lut_translock);
575         if (rc != 0) {
576                 if (info->mti_transno != 0) {
577                         struct obd_export *exp = req->rq_export;
578
579                         CERROR("%s: replay trans "LPU64" NID %s: rc = %d\n",
580                                 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name,
581                                 info->mti_transno,
582                                 libcfs_nid2str(exp->exp_connection->c_peer.nid),
583                                 rc);
584                         RETURN_EXIT;
585                 }
586         } else if (info->mti_transno == 0) {
587                 info->mti_transno = ++mdt->mdt_lut.lut_last_transno;
588         } else {
589                 /* should be replay */
590                 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
591                         mdt->mdt_lut.lut_last_transno = info->mti_transno;
592         }
593         spin_unlock(&mdt->mdt_lut.lut_translock);
594
595         CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
596                         info->mti_transno,
597                         req->rq_export->exp_obd->obd_last_committed);
598
599         req->rq_transno = info->mti_transno;
600         lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
601
602         /* update lcd in memory only for resent cases */
603         ted = &req->rq_export->exp_target_data;
604         LASSERT(ted);
605         mutex_lock(&ted->ted_lcd_lock);
606         lcd = ted->ted_lcd;
607         if (info->mti_transno < lcd->lcd_last_transno &&
608             info->mti_transno != 0) {
609                 /* This should happen during replay. Do not update
610                  * last rcvd info if replay req transno < last transno,
611                  * otherwise the following resend(after replay) can not
612                  * be checked correctly by xid */
613                 mutex_unlock(&ted->ted_lcd_lock);
614                 CDEBUG(D_HA, "%s: transno = "LPU64" < last_transno = "LPU64"\n",
615                         mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name,
616                         info->mti_transno, lcd->lcd_last_transno);
617                 RETURN_EXIT;
618         }
619
620         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
621             lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
622                 if (info->mti_transno != 0)
623                         lcd->lcd_last_close_transno = info->mti_transno;
624                 lcd->lcd_last_close_xid = req->rq_xid;
625                 lcd->lcd_last_close_result = rc;
626         } else {
627                 /* VBR: save versions in last_rcvd for reconstruct. */
628                 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
629                 if (pre_versions) {
630                         lcd->lcd_pre_versions[0] = pre_versions[0];
631                         lcd->lcd_pre_versions[1] = pre_versions[1];
632                         lcd->lcd_pre_versions[2] = pre_versions[2];
633                         lcd->lcd_pre_versions[3] = pre_versions[3];
634                 }
635                 if (info->mti_transno != 0)
636                         lcd->lcd_last_transno = info->mti_transno;
637
638                 lcd->lcd_last_xid = req->rq_xid;
639                 lcd->lcd_last_result = rc;
640                 lcd->lcd_last_data = info->mti_opdata;
641         }
642         mutex_unlock(&ted->ted_lcd_lock);
643
644         EXIT;
645 }
646
647 void mdt_mfd_set_mode(struct mdt_file_data *mfd, int mode)
648 {
649         LASSERT(mfd != NULL);
650
651         CDEBUG(D_HA, "Change mfd %p mode 0x%x->0x%x\n",
652                mfd, (unsigned int)mfd->mfd_mode, (unsigned int)mode);
653
654         mfd->mfd_mode = mode;
655 }
656
657 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
658                         struct mdt_object *o, __u64 flags, int created)
659 {
660         struct ptlrpc_request   *req = mdt_info_req(info);
661         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
662         struct mdt_file_data    *mfd;
663         struct md_attr          *ma  = &info->mti_attr;
664         struct lu_attr          *la  = &ma->ma_attr;
665         struct mdt_body         *repbody;
666         int                      rc = 0, isdir, isreg;
667         ENTRY;
668
669         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
670
671         isreg = S_ISREG(la->la_mode);
672         isdir = S_ISDIR(la->la_mode);
673         if (isreg && !(ma->ma_valid & MA_LOV)) {
674                 /*
675                  * No EA, check whether it is will set regEA and dirEA since in
676                  * above attr get, these size might be zero, so reset it, to
677                  * retrieve the MD after create obj.
678                  */
679                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
680                                                        &RMF_MDT_MD,
681                                                        RCL_SERVER);
682                 /* in replay case, p == NULL */
683                 rc = mdt_create_data(info, p, o);
684                 if (rc)
685                         RETURN(rc);
686         }
687
688         CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
689                ma->ma_valid, ma->ma_lmm_size);
690
691         if (ma->ma_valid & MA_LOV) {
692                 LASSERT(ma->ma_lmm_size != 0);
693                 repbody->eadatasize = ma->ma_lmm_size;
694                 if (isdir)
695                         repbody->valid |= OBD_MD_FLDIREA;
696                 else
697                         repbody->valid |= OBD_MD_FLEASIZE;
698         }
699
700         if (flags & FMODE_WRITE) {
701                 rc = mdt_write_get(o);
702                 if (rc == 0) {
703                         mdt_ioepoch_open(info, o, created);
704                         repbody->ioepoch = o->mot_ioepoch;
705                 }
706         } else if (flags & MDS_FMODE_EXEC) {
707                 /* if file is released, we can't deny write because we must
708                  * restore (write) it to access it.*/
709                 if ((ma->ma_valid & MA_HSM) &&
710                     (ma->ma_hsm.mh_flags & HS_RELEASED))
711                         rc = 0;
712                 else
713                         rc = mdt_write_deny(o);
714         }
715         if (rc)
716                 RETURN(rc);
717
718         rc = mo_open(info->mti_env, mdt_object_child(o),
719                      created ? flags | MDS_OPEN_CREATED : flags);
720         if (rc)
721                 GOTO(err_out, rc);
722
723         mfd = mdt_mfd_new();
724         if (mfd != NULL) {
725                 /*
726                  * Keep a reference on this object for this open, and is
727                  * released by mdt_mfd_close().
728                  */
729                 mdt_object_get(info->mti_env, o);
730
731                 /*
732                  * @flags is always not zero. At least it should be FMODE_READ,
733                  * FMODE_WRITE or MDS_FMODE_EXEC.
734                  */
735                 LASSERT(flags != 0);
736
737                 /* Open handling. */
738                 mdt_mfd_set_mode(mfd, flags);
739
740                 mfd->mfd_object = o;
741                 mfd->mfd_xid = req->rq_xid;
742
743                 /* replay handle */
744                 if (req_is_replay(req)) {
745                         struct mdt_file_data *old_mfd;
746                         /* Check wheather old cookie already exist in
747                          * the list, becasue when do recovery, client
748                          * might be disconnected from server, and
749                          * restart replay, so there maybe some orphan
750                          * mfd here, we should remove them */
751                         LASSERT(info->mti_rr.rr_handle != NULL);
752                         old_mfd = mdt_handle2mfd(info, info->mti_rr.rr_handle);
753                         if (old_mfd) {
754                                 CDEBUG(D_HA, "del orph mfd %p fid=("DFID") "
755                                        "cookie=" LPX64"\n", mfd,
756                                        PFID(mdt_object_fid(mfd->mfd_object)),
757                                        info->mti_rr.rr_handle->cookie);
758                                 spin_lock(&med->med_open_lock);
759                                 class_handle_unhash(&old_mfd->mfd_handle);
760                                 cfs_list_del_init(&old_mfd->mfd_list);
761                                 spin_unlock(&med->med_open_lock);
762                                 /* no attr update for that close */
763                                 la->la_valid = 0;
764                                 ma->ma_valid |= MA_FLAGS;
765                                 ma->ma_attr_flags |= MDS_RECOV_OPEN;
766                                 mdt_mfd_close(info, old_mfd);
767                                 ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
768                                 ma->ma_valid &= ~MA_FLAGS;
769                         }
770                         CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
771                                info->mti_rr.rr_handle->cookie);
772                         mfd->mfd_old_handle.cookie =
773                                                 info->mti_rr.rr_handle->cookie;
774                 }
775                 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
776
777                 if (req->rq_export->exp_disconnected) {
778                         spin_lock(&med->med_open_lock);
779                         class_handle_unhash(&mfd->mfd_handle);
780                         cfs_list_del_init(&mfd->mfd_list);
781                         spin_unlock(&med->med_open_lock);
782                         mdt_mfd_close(info, mfd);
783                 } else {
784                         spin_lock(&med->med_open_lock);
785                         cfs_list_add(&mfd->mfd_list, &med->med_open_head);
786                         spin_unlock(&med->med_open_lock);
787                 }
788
789                 mdt_empty_transno(info, rc);
790         } else {
791                 GOTO(err_out, rc = -ENOMEM);
792         }
793
794         RETURN(rc);
795
796 err_out:
797         if (flags & FMODE_WRITE)
798                         /* XXX We also need to close io epoch here.
799                          * See LU-1220 - green */
800                 mdt_write_put(o);
801         else if (flags & FMODE_EXEC)
802                 mdt_write_allow(o);
803         return rc;
804 }
805
806 int mdt_finish_open(struct mdt_thread_info *info,
807                     struct mdt_object *p, struct mdt_object *o,
808                     __u64 flags, int created, struct ldlm_reply *rep)
809 {
810         struct ptlrpc_request   *req = mdt_info_req(info);
811         struct obd_export       *exp = req->rq_export;
812         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
813         struct md_attr          *ma  = &info->mti_attr;
814         struct lu_attr          *la  = &ma->ma_attr;
815         struct mdt_file_data    *mfd;
816         struct mdt_body         *repbody;
817         int                      rc = 0;
818         int                      isreg, isdir, islnk;
819         cfs_list_t              *t;
820         ENTRY;
821
822         LASSERT(ma->ma_valid & MA_INODE);
823
824         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
825
826         isreg = S_ISREG(la->la_mode);
827         isdir = S_ISDIR(la->la_mode);
828         islnk = S_ISLNK(la->la_mode);
829         mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
830
831         /* LU-2275, simulate broken behaviour (esp. prevalent in
832          * pre-2.4 servers where a very strange reply is sent on error
833          * that looks like it was actually almost succesful and a failure at the
834          * same time */
835         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_NEGATIVE_POSITIVE)) {
836                 mdt_set_disposition(info, rep, DISP_OPEN_LOCK | \
837                                     DISP_OPEN_OPEN | DISP_LOOKUP_NEG | \
838                                     DISP_LOOKUP_POS);
839                 RETURN(-ENOENT);
840         }
841
842         if (exp_connect_rmtclient(exp)) {
843                 void *buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
844
845                 rc = mdt_pack_remote_perm(info, o, buf);
846                 if (rc) {
847                         repbody->valid &= ~OBD_MD_FLRMTPERM;
848                         repbody->aclsize = 0;
849                 } else {
850                         repbody->valid |= OBD_MD_FLRMTPERM;
851                         repbody->aclsize = sizeof(struct mdt_remote_perm);
852                 }
853         }
854 #ifdef CONFIG_FS_POSIX_ACL
855         else if (exp_connect_flags(exp) & OBD_CONNECT_ACL) {
856                 const struct lu_env *env = info->mti_env;
857                 struct md_object *next = mdt_object_child(o);
858                 struct lu_buf *buf = &info->mti_buf;
859
860                 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
861                 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
862                                                    RCL_SERVER);
863                 if (buf->lb_len > 0) {
864                         rc = mo_xattr_get(env, next, buf,
865                                           XATTR_NAME_ACL_ACCESS);
866                         if (rc < 0) {
867                                 if (rc == -ENODATA) {
868                                         repbody->aclsize = 0;
869                                         repbody->valid |= OBD_MD_FLACL;
870                                         rc = 0;
871                                 } else if (rc == -EOPNOTSUPP) {
872                                         rc = 0;
873                                 } else {
874                                         CERROR("got acl size: %d\n", rc);
875                                 }
876                         } else {
877                                 repbody->aclsize = rc;
878                                 repbody->valid |= OBD_MD_FLACL;
879                                 rc = 0;
880                         }
881                 }
882         }
883 #endif
884
885         if (info->mti_mdt->mdt_opts.mo_mds_capa &&
886             exp_connect_flags(exp) & OBD_CONNECT_MDS_CAPA) {
887                 struct lustre_capa *capa;
888
889                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
890                 LASSERT(capa);
891                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
892                 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
893                 if (rc)
894                         RETURN(rc);
895                 repbody->valid |= OBD_MD_FLMDSCAPA;
896         }
897         if (info->mti_mdt->mdt_opts.mo_oss_capa &&
898             exp_connect_flags(exp) & OBD_CONNECT_OSS_CAPA &&
899             S_ISREG(lu_object_attr(&o->mot_obj.mo_lu))) {
900                 struct lustre_capa *capa;
901
902                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
903                 LASSERT(capa);
904                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
905                 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
906                 if (rc)
907                         RETURN(rc);
908                 repbody->valid |= OBD_MD_FLOSSCAPA;
909         }
910
911         /*
912          * If we are following a symlink, don't open; and do not return open
913          * handle for special nodes as client required.
914          */
915         if (islnk || (!isreg && !isdir &&
916             (exp_connect_flags(req->rq_export) & OBD_CONNECT_NODEVOH))) {
917                 lustre_msg_set_transno(req->rq_repmsg, 0);
918                 RETURN(0);
919         }
920
921         /*
922          * We need to return the existing object's fid back, so it is done here,
923          * after preparing the reply.
924          */
925         if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
926                 RETURN(-EEXIST);
927
928         /* This can't be done earlier, we need to return reply body */
929         if (isdir) {
930                 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
931                         /* We are trying to create or write an existing dir. */
932                         RETURN(-EISDIR);
933                 }
934         } else if (flags & MDS_OPEN_DIRECTORY)
935                 RETURN(-ENOTDIR);
936
937         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
938                                  OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
939                 RETURN(-EAGAIN);
940         }
941
942         mfd = NULL;
943         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
944                 spin_lock(&med->med_open_lock);
945                 cfs_list_for_each(t, &med->med_open_head) {
946                         mfd = cfs_list_entry(t, struct mdt_file_data, mfd_list);
947                         if (mfd->mfd_xid == req->rq_xid)
948                                 break;
949                         mfd = NULL;
950                 }
951                 spin_unlock(&med->med_open_lock);
952
953                 if (mfd != NULL) {
954                         repbody->handle.cookie = mfd->mfd_handle.h_cookie;
955                         /*set repbody->ea_size for resent case*/
956                         if (ma->ma_valid & MA_LOV) {
957                                 LASSERT(ma->ma_lmm_size != 0);
958                                 repbody->eadatasize = ma->ma_lmm_size;
959                                 if (isdir)
960                                         repbody->valid |= OBD_MD_FLDIREA;
961                                 else
962                                         repbody->valid |= OBD_MD_FLEASIZE;
963                         }
964                         mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
965                         RETURN(0);
966                 }
967         }
968
969         rc = mdt_mfd_open(info, p, o, flags, created);
970         if (!rc)
971                 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
972
973         RETURN(rc);
974 }
975
976 extern void mdt_req_from_lcd(struct ptlrpc_request *req,
977                              struct lsd_client_data *lcd);
978
979 void mdt_reconstruct_open(struct mdt_thread_info *info,
980                           struct mdt_lock_handle *lhc)
981 {
982         const struct lu_env *env = info->mti_env;
983         struct mdt_device       *mdt  = info->mti_mdt;
984         struct req_capsule      *pill = info->mti_pill;
985         struct ptlrpc_request   *req  = mdt_info_req(info);
986         struct tg_export_data   *ted  = &req->rq_export->exp_target_data;
987         struct lsd_client_data  *lcd  = ted->ted_lcd;
988         struct md_attr          *ma   = &info->mti_attr;
989         struct mdt_reint_record *rr   = &info->mti_rr;
990         __u32                   flags = info->mti_spec.sp_cr_flags;
991         struct ldlm_reply       *ldlm_rep;
992         struct mdt_object       *parent;
993         struct mdt_object       *child;
994         struct mdt_body         *repbody;
995         int                      rc;
996         ENTRY;
997
998         LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
999         ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
1000         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1001
1002         ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
1003         ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
1004                                                RCL_SERVER);
1005         ma->ma_need = MA_INODE | MA_HSM;
1006         if (ma->ma_lmm_size > 0)
1007                 ma->ma_need |= MA_LOV;
1008
1009         ma->ma_valid = 0;
1010
1011         mdt_req_from_lcd(req, lcd);
1012         mdt_set_disposition(info, ldlm_rep, lcd->lcd_last_data);
1013
1014         CDEBUG(D_INODE, "This is reconstruct open: disp="LPX64", result=%d\n",
1015                ldlm_rep->lock_policy_res1, req->rq_status);
1016
1017         if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
1018             req->rq_status != 0)
1019                 /* We did not create successfully, return error to client. */
1020                 GOTO(out, rc = req->rq_status);
1021
1022         if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
1023                 struct obd_export *exp = req->rq_export;
1024                 /*
1025                  * We failed after creation, but we do not know in which step
1026                  * we failed. So try to check the child object.
1027                  */
1028                 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1029                 if (IS_ERR(parent)) {
1030                         rc = PTR_ERR(parent);
1031                         LCONSOLE_WARN("Parent "DFID" lookup error %d."
1032                                       " Evicting client %s with export %s.\n",
1033                                       PFID(rr->rr_fid1), rc,
1034                                       obd_uuid2str(&exp->exp_client_uuid),
1035                                       obd_export_nid2str(exp));
1036                         mdt_export_evict(exp);
1037                         RETURN_EXIT;
1038                 }
1039                 child = mdt_object_find(env, mdt, rr->rr_fid2);
1040                 if (IS_ERR(child)) {
1041                         rc = PTR_ERR(child);
1042                         LCONSOLE_WARN("Child "DFID" lookup error %d."
1043                                       " Evicting client %s with export %s.\n",
1044                                       PFID(mdt_object_fid(child)), rc,
1045                                       obd_uuid2str(&exp->exp_client_uuid),
1046                                       obd_export_nid2str(exp));
1047                         mdt_object_put(env, parent);
1048                         mdt_export_evict(exp);
1049                         RETURN_EXIT;
1050                 }
1051                 rc = mdt_object_exists(child);
1052                 if (rc > 0) {
1053
1054                         mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
1055                         rc = mdt_attr_get_complex(info, child, ma);
1056                         if (rc == 0)
1057                               rc = mdt_finish_open(info, parent, child,
1058                                                    flags, 1, ldlm_rep);
1059                 } else if (rc < 0) {
1060                         /* the child object was created on remote server */
1061                         repbody->fid1 = *rr->rr_fid2;
1062                         repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1063                         rc = 0;
1064                 } else if (rc == 0) {
1065                         /* the child does not exist, we should do regular open */
1066                         mdt_object_put(env, parent);
1067                         mdt_object_put(env, child);
1068                         GOTO(regular_open, 0);
1069                 }
1070                 mdt_object_put(env, parent);
1071                 mdt_object_put(env, child);
1072                 GOTO(out, rc);
1073         } else {
1074 regular_open:
1075                 /* We did not try to create, so we are a pure open */
1076                 rc = mdt_reint_open(info, lhc);
1077         }
1078
1079         EXIT;
1080 out:
1081         req->rq_status = rc;
1082         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1083         LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
1084 }
1085
1086 int mdt_open_by_fid(struct mdt_thread_info* info,
1087                     struct ldlm_reply *rep)
1088 {
1089         __u32                    flags = info->mti_spec.sp_cr_flags;
1090         struct mdt_reint_record *rr = &info->mti_rr;
1091         struct md_attr          *ma = &info->mti_attr;
1092         struct mdt_object       *o;
1093         int                      rc;
1094         ENTRY;
1095
1096         o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1097         if (IS_ERR(o))
1098                 RETURN(rc = PTR_ERR(o));
1099
1100         rc = mdt_object_exists(o);
1101         if (rc > 0) {
1102                 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1103                                                 DISP_LOOKUP_EXECD |
1104                                                 DISP_LOOKUP_POS));
1105
1106                 rc = mdt_attr_get_complex(info, o, ma);
1107                 if (rc == 0)
1108                         rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1109         } else if (rc == 0) {
1110                 rc = -ENOENT;
1111         } else  {
1112                 /* the child object was created on remote server */
1113                 struct mdt_body *repbody;
1114                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1115                 repbody->fid1 = *rr->rr_fid2;
1116                 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1117                 rc = 0;
1118         }
1119
1120         mdt_object_put(info->mti_env, o);
1121         RETURN(rc);
1122 }
1123
1124 /* lock object for open */
1125 static int mdt_object_open_lock(struct mdt_thread_info *info,
1126                                 struct mdt_object *obj,
1127                                 struct mdt_lock_handle *lhc,
1128                                 __u64 *ibits)
1129 {
1130         struct md_attr  *ma = &info->mti_attr;
1131         __u64            open_flags = info->mti_spec.sp_cr_flags;
1132         ldlm_mode_t      lm = LCK_CR;
1133         bool             try_layout = false;
1134         bool             create_layout = false;
1135         int              rc = 0;
1136         ENTRY;
1137
1138         *ibits = 0;
1139         if (open_flags & MDS_OPEN_LOCK) {
1140                 if (open_flags & FMODE_WRITE)
1141                         lm = LCK_CW;
1142                 /* if file is released, we can't deny write because we must
1143                  * restore (write) it to access it. */
1144                 else if ((open_flags & MDS_FMODE_EXEC) &&
1145                          !((ma->ma_valid & MA_HSM) &&
1146                            (ma->ma_hsm.mh_flags & HS_RELEASED)))
1147                         lm = LCK_PR;
1148                 else
1149                         lm = LCK_CR;
1150
1151                 *ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN;
1152         }
1153
1154         if (S_ISREG(lu_object_attr(&obj->mot_obj.mo_lu))) {
1155                 if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV) &&
1156                     md_should_create(open_flags))
1157                         create_layout = true;
1158                 if (exp_connect_layout(info->mti_exp) && !create_layout &&
1159                     ma->ma_need & MA_LOV)
1160                         try_layout = true;
1161         }
1162
1163         mdt_lock_handle_init(lhc);
1164         mdt_lock_reg_init(lhc, lm);
1165
1166         /* one problem to return layout lock on open is that it may result
1167          * in too many layout locks cached on the client side. */
1168         if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
1169                 /* return lookup lock to validate inode at the client side,
1170                  * this is pretty important otherwise mdt will return layout
1171                  * lock for each open.
1172                  * However this is a double-edged sword because changing
1173                  * permission will revoke huge # of LOOKUP locks. */
1174                 *ibits |= MDS_INODELOCK_LAYOUT | MDS_INODELOCK_LOOKUP;
1175                 if (!mdt_object_lock_try(info, obj, lhc, *ibits,
1176                                          MDT_CROSS_LOCK)) {
1177                         *ibits &= ~(MDS_INODELOCK_LAYOUT|MDS_INODELOCK_LOOKUP);
1178                         if (*ibits != 0)
1179                                 rc = mdt_object_lock(info, obj, lhc, *ibits,
1180                                                 MDT_CROSS_LOCK);
1181                 }
1182         } else if (*ibits != 0) {
1183                 rc = mdt_object_lock(info, obj, lhc, *ibits, MDT_CROSS_LOCK);
1184         }
1185
1186         CDEBUG(D_INODE, "Requested bits lock:"DFID ", ibits = "LPX64
1187                 ", open_flags = "LPO64", try_layout = %d, rc = %d\n",
1188                 PFID(mdt_object_fid(obj)), *ibits, open_flags, try_layout, rc);
1189
1190         /* will change layout, revoke layout locks by enqueuing EX lock. */
1191         if (rc == 0 && create_layout) {
1192                 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
1193
1194                 CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
1195                         ", open_flags = "LPO64"\n",
1196                         PFID(mdt_object_fid(obj)), open_flags);
1197
1198                 LASSERT(!try_layout);
1199                 mdt_lock_handle_init(ll);
1200                 mdt_lock_reg_init(ll, LCK_EX);
1201                 rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT,
1202                                         MDT_LOCAL_LOCK);
1203
1204                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
1205         }
1206
1207         RETURN(rc);
1208 }
1209
1210 static void mdt_object_open_unlock(struct mdt_thread_info *info,
1211                                    struct mdt_object *obj,
1212                                    struct mdt_lock_handle *lhc,
1213                                    __u64 ibits, int rc)
1214 {
1215         __u64 open_flags = info->mti_spec.sp_cr_flags;
1216         struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
1217
1218         /* Release local layout lock - the layout lock put in MDT_LH_LAYOUT
1219          * will never return to client side. */
1220         if (lustre_handle_is_used(&ll->mlh_reg_lh)) {
1221                 LASSERT(!(ibits & MDS_INODELOCK_LAYOUT));
1222                 mdt_object_unlock(info, obj, ll, 1);
1223         }
1224
1225         /* Cross-ref case, the lock should be returned to the client */
1226         if (ibits == 0 || rc == -EREMOTE)
1227                 return;
1228
1229         if (!(open_flags & MDS_OPEN_LOCK) && !(ibits & MDS_INODELOCK_LAYOUT)) {
1230                 /* for the open request, the lock will only return to client
1231                  * if open or layout lock is granted. */
1232                 rc = 1;
1233         }
1234         if (rc != 0)
1235                 mdt_object_unlock(info, obj, lhc, 1);
1236 }
1237
1238 int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
1239                          struct mdt_lock_handle *lhc)
1240 {
1241         const struct lu_env     *env   = info->mti_env;
1242         struct mdt_device       *mdt   = info->mti_mdt;
1243         __u32                    flags = info->mti_spec.sp_cr_flags;
1244         struct mdt_reint_record *rr    = &info->mti_rr;
1245         struct md_attr          *ma    = &info->mti_attr;
1246         struct mdt_object       *parent= NULL;
1247         struct mdt_object       *o;
1248         int                      rc;
1249         __u64                    ibits;
1250         ENTRY;
1251
1252         if (md_should_create(flags) && !(flags & MDS_OPEN_HAS_EA)) {
1253                 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1254                         parent = mdt_object_find(env, mdt, rr->rr_fid1);
1255                         if (IS_ERR(parent)) {
1256                                 CDEBUG(D_INODE, "Fail to find parent "DFID
1257                                        " for anonymous created %ld, try to"
1258                                        " use server-side parent.\n",
1259                                        PFID(rr->rr_fid1), PTR_ERR(parent));
1260                                 parent = NULL;
1261                         }
1262                 }
1263                 if (parent == NULL)
1264                         ma->ma_need |= MA_PFID;
1265         }
1266
1267         o = mdt_object_find(env, mdt, rr->rr_fid2);
1268         if (IS_ERR(o))
1269                 RETURN(rc = PTR_ERR(o));
1270
1271         rc = mdt_object_exists(o);
1272         if (rc == 0) {
1273                 mdt_set_disposition(info, rep, (DISP_LOOKUP_EXECD |
1274                                     DISP_LOOKUP_NEG));
1275                 GOTO(out, rc = -ENOENT);
1276         } else if (rc < 0) {
1277                 CERROR("NFS remote open shouldn't happen.\n");
1278                 GOTO(out, rc);
1279         }
1280         mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1281                                         DISP_LOOKUP_EXECD));
1282
1283         rc = mdt_attr_get_complex(info, o, ma);
1284         if (rc)
1285                 GOTO(out, rc);
1286
1287         rc = mdt_object_open_lock(info, o, lhc, &ibits);
1288         if (rc)
1289                 GOTO(out, rc);
1290
1291         if (ma->ma_valid & MA_PFID) {
1292                 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1293                 if (IS_ERR(parent)) {
1294                         CDEBUG(D_INODE, "Fail to find parent "DFID
1295                                " for anonymous created %ld, try to"
1296                                " use system default.\n",
1297                                PFID(&ma->ma_pfid), PTR_ERR(parent));
1298                         parent = NULL;
1299                 }
1300         }
1301
1302         rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1303         if (!rc) {
1304                 mdt_set_disposition(info, rep, DISP_LOOKUP_POS);
1305                 if (flags & MDS_OPEN_LOCK)
1306                         mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1307         }
1308         GOTO(out, rc);
1309
1310 out:
1311         mdt_object_open_unlock(info, o, lhc, ibits, rc);
1312         mdt_object_put(env, o);
1313         if (parent != NULL)
1314                 mdt_object_put(env, parent);
1315         return rc;
1316 }
1317
1318 int mdt_pin(struct mdt_thread_info* info)
1319 {
1320         ENTRY;
1321         RETURN(err_serious(-EOPNOTSUPP));
1322 }
1323
1324 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1325 int mdt_cross_open(struct mdt_thread_info* info,
1326                    const struct lu_fid *fid,
1327                    struct ldlm_reply *rep, __u32 flags)
1328 {
1329         struct md_attr    *ma = &info->mti_attr;
1330         struct mdt_object *o;
1331         int                rc;
1332         ENTRY;
1333
1334         o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1335         if (IS_ERR(o))
1336                 RETURN(rc = PTR_ERR(o));
1337
1338         rc = mdt_object_exists(o);
1339         if (rc > 0) {
1340                 /* Do permission check for cross-open. */
1341                 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o),
1342                                    NULL, flags | MDS_OPEN_CROSS);
1343                 if (rc)
1344                         goto out;
1345
1346                 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
1347                 rc = mdt_attr_get_complex(info, o, ma);
1348                 if (rc == 0)
1349                         rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1350         } else if (rc == 0) {
1351                 /*
1352                  * Something is wrong here. lookup was positive but there is
1353                  * no object!
1354                  */
1355                 CERROR("Cross-ref object doesn't exist!\n");
1356                 rc = -EFAULT;
1357         } else  {
1358                 /* Something is wrong here, the object is on another MDS! */
1359                 CERROR("The object isn't on this server! FLD error?\n");
1360                 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1361                                 &o->mot_obj.mo_lu,
1362                                 "Object isn't on this server! FLD error?\n");
1363
1364                 rc = -EFAULT;
1365         }
1366
1367 out:
1368         mdt_object_put(info->mti_env, o);
1369         RETURN(rc);
1370 }
1371
1372 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1373 {
1374         struct mdt_device       *mdt = info->mti_mdt;
1375         struct ptlrpc_request   *req = mdt_info_req(info);
1376         struct mdt_object       *parent;
1377         struct mdt_object       *child;
1378         struct mdt_lock_handle  *lh;
1379         struct ldlm_reply       *ldlm_rep;
1380         struct mdt_body         *repbody;
1381         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
1382         struct md_attr          *ma = &info->mti_attr;
1383         __u64                    create_flags = info->mti_spec.sp_cr_flags;
1384         __u64                    ibits;
1385         struct mdt_reint_record *rr = &info->mti_rr;
1386         struct lu_name          *lname;
1387         int                      result, rc;
1388         int                      created = 0;
1389         __u32                    msg_flags;
1390         ENTRY;
1391
1392         OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1393                                (obd_timeout + 1) / 4);
1394
1395         mdt_counter_incr(req, LPROC_MDT_OPEN);
1396         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1397
1398         ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
1399         ma->ma_lmm_size = req_capsule_get_size(info->mti_pill, &RMF_MDT_MD,
1400                                                RCL_SERVER);
1401         ma->ma_need = MA_INODE;
1402         if (ma->ma_lmm_size > 0)
1403                 ma->ma_need |= MA_LOV;
1404
1405         ma->ma_valid = 0;
1406
1407         LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1408         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1409
1410         if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1411                 CERROR("file join is not supported anymore.\n");
1412                 GOTO(out, result = err_serious(-EOPNOTSUPP));
1413         }
1414         msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1415
1416         if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1417             info->mti_spec.u.sp_ea.eadata == NULL)
1418                 GOTO(out, result = err_serious(-EINVAL));
1419
1420         CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
1421                "cr_flag="LPO64" mode=0%06o msg_flag=0x%x\n",
1422                PFID(rr->rr_fid1), rr->rr_name,
1423                PFID(rr->rr_fid2), create_flags,
1424                ma->ma_attr.la_mode, msg_flags);
1425
1426         if (req_is_replay(req) ||
1427             (req->rq_export->exp_libclient && create_flags & MDS_OPEN_HAS_EA)) {
1428                 /* This is a replay request or from liblustre with ea. */
1429                 result = mdt_open_by_fid(info, ldlm_rep);
1430
1431                 if (result != -ENOENT) {
1432                         if (req->rq_export->exp_libclient &&
1433                             create_flags & MDS_OPEN_HAS_EA)
1434                                 GOTO(out, result = 0);
1435                         GOTO(out, result);
1436                 }
1437                 /* We didn't find the correct object, so we need to re-create it
1438                  * via a regular replay. */
1439                 if (!(create_flags & MDS_OPEN_CREAT)) {
1440                         DEBUG_REQ(D_ERROR, req,
1441                                   "OPEN & CREAT not in open replay/by_fid.");
1442                         GOTO(out, result = -EFAULT);
1443                 }
1444                 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1445         } else if ((rr->rr_namelen == 0 && !info->mti_cross_ref &&
1446                     create_flags & MDS_OPEN_LOCK) ||
1447                    (create_flags & MDS_OPEN_BY_FID)) {
1448                 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1449                 if (result != -ENOENT && !(create_flags & MDS_OPEN_CREAT))
1450                         GOTO(out, result);
1451                 if (unlikely(rr->rr_namelen == 0))
1452                         GOTO(out, result = -EINVAL);
1453                 CDEBUG(D_INFO, "No object(2), continue as regular open.\n");
1454         }
1455
1456         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1457                 GOTO(out, result = err_serious(-ENOMEM));
1458
1459         mdt_set_disposition(info, ldlm_rep,
1460                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1461
1462         if (info->mti_cross_ref) {
1463                 /* This is cross-ref open */
1464                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1465                 result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
1466                                         create_flags);
1467                 GOTO(out, result);
1468         }
1469
1470         lh = &info->mti_lh[MDT_LH_PARENT];
1471         mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
1472                           LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
1473
1474         parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1475                                       MDS_INODELOCK_UPDATE);
1476         if (IS_ERR(parent))
1477                 GOTO(out, result = PTR_ERR(parent));
1478
1479         /* get and check version of parent */
1480         result = mdt_version_get_check(info, parent, 0);
1481         if (result)
1482                 GOTO(out_parent, result);
1483
1484         fid_zero(child_fid);
1485
1486         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1487         result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1488                             lname, child_fid, &info->mti_spec);
1489         LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1490                  "looking for "DFID"/%s, result fid="DFID"\n",
1491                  PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
1492
1493         if (result != 0 && result != -ENOENT && result != -ESTALE)
1494                 GOTO(out_parent, result);
1495
1496         if (result == -ENOENT || result == -ESTALE) {
1497                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1498                 if (result == -ESTALE) {
1499                         /*
1500                          * -ESTALE means the parent is a dead(unlinked) dir, so
1501                          * it should return -ENOENT to in accordance with the
1502                          * original mds implementaion.
1503                          */
1504                         GOTO(out_parent, result = -ENOENT);
1505                 }
1506                 if (!(create_flags & MDS_OPEN_CREAT))
1507                         GOTO(out_parent, result);
1508                 *child_fid = *info->mti_rr.rr_fid2;
1509                 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1510                          PFID(child_fid));
1511                 /* In the function below, .hs_keycmp resolves to
1512                  * lu_obj_hop_keycmp() */
1513                 /* coverity[overrun-buffer-val] */
1514                 child = mdt_object_new(info->mti_env, mdt, child_fid);
1515         } else {
1516                 /*
1517                  * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1518                  * return FID back in that case.
1519                  */
1520                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1521                 child = mdt_object_find(info->mti_env, mdt, child_fid);
1522         }
1523         if (IS_ERR(child))
1524                 GOTO(out_parent, result = PTR_ERR(child));
1525
1526         /** check version of child  */
1527         rc = mdt_version_get_check(info, child, 1);
1528         if (rc)
1529                 GOTO(out_child, result = rc);
1530
1531         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1532         if (result == -ENOENT) {
1533                 if (mdt_object_obf(parent))
1534                         GOTO(out_child, result = -EPERM);
1535
1536                 /* save versions in reply */
1537                 mdt_version_get_save(info, parent, 0);
1538                 mdt_version_get_save(info, child, 1);
1539
1540                 /* version of child will be changed */
1541                 info->mti_mos = child;
1542
1543                 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1544                 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1545
1546                 /* Let lower layers know what is lock mode on directory. */
1547                 info->mti_spec.sp_cr_mode =
1548                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1549
1550                 /*
1551                  * Do not perform lookup sanity check. We know that name does
1552                  * not exist.
1553                  */
1554                 info->mti_spec.sp_cr_lookup = 0;
1555                 info->mti_spec.sp_feat = &dt_directory_features;
1556
1557                 result = mdo_create(info->mti_env,
1558                                     mdt_object_child(parent),
1559                                     lname,
1560                                     mdt_object_child(child),
1561                                     &info->mti_spec,
1562                                     &info->mti_attr);
1563                 if (result == -ERESTART) {
1564                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1565                         GOTO(out_child, result);
1566                 } else {
1567
1568                         /* XXX: we should call this once, see few lines below */
1569                         if (result == 0)
1570                                 result = mdt_attr_get_complex(info, child, ma);
1571
1572                         if (result != 0)
1573                                 GOTO(out_child, result);
1574                 }
1575                 created = 1;
1576         } else {
1577                 /* We have to get attr & LOV EA, HSM bits for this object */
1578                 ma->ma_need |= MA_HSM;
1579                 result = mdt_attr_get_complex(info, child, ma);
1580                 /*
1581                  * The object is on remote node, return its FID for remote open.
1582                  */
1583                 if (result == -EREMOTE) {
1584                         /*
1585                          * Check if this lock already was sent to client and
1586                          * this is resent case. For resent case do not take lock
1587                          * again, use what is already granted.
1588                          */
1589                         LASSERT(lhc != NULL);
1590
1591                         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1592                                 struct ldlm_lock *lock;
1593
1594                                 LASSERT(msg_flags & MSG_RESENT);
1595
1596                                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1597                                 if (!lock) {
1598                                         CERROR("Invalid lock handle "LPX64"\n",
1599                                                lhc->mlh_reg_lh.cookie);
1600                                         LBUG();
1601                                 }
1602                                 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1603                                                         &lock->l_resource->lr_name));
1604                                 LDLM_LOCK_PUT(lock);
1605                                 rc = 0;
1606                         } else {
1607                                 mdt_lock_handle_init(lhc);
1608                                 mdt_lock_reg_init(lhc, LCK_PR);
1609
1610                                 rc = mdt_object_lock(info, child, lhc,
1611                                                      MDS_INODELOCK_LOOKUP,
1612                                                      MDT_CROSS_LOCK);
1613                         }
1614                         repbody->fid1 = *mdt_object_fid(child);
1615                         repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1616                         if (rc != 0)
1617                                 result = rc;
1618                         else
1619                                 result = -EREMOTE;
1620                         GOTO(out_child, result);
1621                 }
1622         }
1623
1624         LASSERT(!lustre_handle_is_used(&lhc->mlh_reg_lh));
1625
1626         /* get openlock if this is not replay and if a client requested it */
1627         if (!req_is_replay(req)) {
1628                 rc = mdt_object_open_lock(info, child, lhc, &ibits);
1629                 if (rc != 0) {
1630                         GOTO(out_child, result = rc);
1631                 } else if (create_flags & MDS_OPEN_LOCK) {
1632                         result = -EREMOTE;
1633                         mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1634                 }
1635         }
1636
1637         /* Try to open it now. */
1638         rc = mdt_finish_open(info, parent, child, create_flags,
1639                              created, ldlm_rep);
1640         if (rc) {
1641                 result = rc;
1642                 /* openlock will be released if mdt_finish_open failed */
1643                 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1644                 if (created) {
1645                         ma->ma_need = 0;
1646                         ma->ma_valid = 0;
1647                         ma->ma_cookie_size = 0;
1648                         rc = mdo_unlink(info->mti_env,
1649                                         mdt_object_child(parent),
1650                                         mdt_object_child(child),
1651                                         lname,
1652                                         &info->mti_attr);
1653                         if (rc != 0)
1654                                 CERROR("Error in cleanup of open\n");
1655                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1656                 }
1657         }
1658         EXIT;
1659 out_child:
1660         mdt_object_open_unlock(info, child, lhc, ibits, result);
1661         mdt_object_put(info->mti_env, child);
1662 out_parent:
1663         mdt_object_unlock_put(info, parent, lh, result || !created);
1664 out:
1665         if (result && result != -EREMOTE)
1666                 lustre_msg_set_transno(req->rq_repmsg, 0);
1667         return result;
1668 }
1669
1670 #define MFD_CLOSED(mode) (((mode) & ~(MDS_FMODE_EPOCH | MDS_FMODE_SOM | \
1671                                       MDS_FMODE_TRUNC)) == MDS_FMODE_CLOSED)
1672
1673 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1674 {
1675         return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1676 }
1677
1678 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1679 {
1680         struct mdt_object *o = mfd->mfd_object;
1681         struct md_object *next = mdt_object_child(o);
1682         struct md_attr *ma = &info->mti_attr;
1683         int ret = MDT_IOEPOCH_CLOSED;
1684         int rc = 0;
1685         int mode;
1686         ENTRY;
1687
1688         mode = mfd->mfd_mode;
1689
1690         if ((mode & FMODE_WRITE) || (mode & MDS_FMODE_TRUNC)) {
1691                 mdt_write_put(o);
1692                 ret = mdt_ioepoch_close(info, o);
1693         } else if (mode & MDS_FMODE_EXEC) {
1694                 mdt_write_allow(o);
1695         } else if (mode & MDS_FMODE_EPOCH) {
1696                 ret = mdt_ioepoch_close(info, o);
1697         } else if (mode & MDS_FMODE_SOM) {
1698                 ret = mdt_som_au_close(info, o);
1699         }
1700
1701         /* Update atime on close only. */
1702         if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1703             && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1704                 /* Set the atime only. */
1705                 ma->ma_valid = MA_INODE;
1706                 ma->ma_attr.la_valid = LA_ATIME;
1707                 rc = mo_attr_set(info->mti_env, next, ma);
1708         }
1709
1710         /* If file data is modified, add the dirty flag.
1711          *
1712          * If MDS_CLOSE_CLEANUP is set, this file is being closed due to an
1713          * eviction, file could have been modified and now dirty
1714          * regarding to HSM archive, check this!
1715          * The logic here is to mark a file dirty if there's a chance it was
1716          * dirtied before the client was evicted, so that we don't have to wait
1717          * for a release attempt before finding out the file was actually dirty
1718          * and fail the release. Aggressively marking it dirty here will cause
1719          * the policy engine to attempt to re-archive it; when rearchiving, we
1720          * can compare the current version to the LMA data_version and make the
1721          * archive request into a noop if it's not actually dirty.
1722          */
1723         if ((ma->ma_attr_flags & MDS_DATA_MODIFIED) ||
1724             ((ma->ma_attr_flags & MDS_CLOSE_CLEANUP) &&
1725              (mode & (FMODE_WRITE|MDS_FMODE_TRUNC))))
1726                 rc = mdt_add_dirty_flag(info, o, ma);
1727
1728         ma->ma_need |= MA_INODE;
1729         ma->ma_valid &= ~MA_INODE;
1730
1731         if (!MFD_CLOSED(mode))
1732                 rc = mo_close(info->mti_env, next, ma, mode);
1733
1734         if (ret == MDT_IOEPOCH_GETATTR || ret == MDT_IOEPOCH_OPENED) {
1735                 struct mdt_export_data *med;
1736
1737                 /* The IOepoch is still opened or SOM update is needed.
1738                  * Put mfd back into the list. */
1739                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1740                 mdt_mfd_set_mode(mfd, ret == MDT_IOEPOCH_OPENED ?
1741                                       MDS_FMODE_EPOCH : MDS_FMODE_SOM);
1742
1743                 LASSERT(mdt_info_req(info));
1744                 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1745                 spin_lock(&med->med_open_lock);
1746                 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
1747                 class_handle_hash_back(&mfd->mfd_handle);
1748                 spin_unlock(&med->med_open_lock);
1749
1750                 if (ret == MDT_IOEPOCH_OPENED) {
1751                         ret = 0;
1752                 } else {
1753                         ret = -EAGAIN;
1754                         CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1755                                "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1756                 }
1757         } else {
1758                 mdt_mfd_free(mfd);
1759                 mdt_object_put(info->mti_env, o);
1760         }
1761
1762         RETURN(rc ? rc : ret);
1763 }
1764
1765 int mdt_close(struct mdt_thread_info *info)
1766 {
1767         struct mdt_export_data *med;
1768         struct mdt_file_data   *mfd;
1769         struct mdt_object      *o;
1770         struct md_attr         *ma = &info->mti_attr;
1771         struct mdt_body        *repbody = NULL;
1772         struct ptlrpc_request  *req = mdt_info_req(info);
1773         int rc, ret = 0;
1774         ENTRY;
1775
1776         mdt_counter_incr(req, LPROC_MDT_CLOSE);
1777         /* Close may come with the Size-on-MDS update. Unpack it. */
1778         rc = mdt_close_unpack(info);
1779         if (rc)
1780                 RETURN(err_serious(rc));
1781
1782         LASSERT(info->mti_ioepoch);
1783
1784         req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1785                              info->mti_mdt->mdt_max_mdsize);
1786         req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1787                              info->mti_mdt->mdt_max_cookiesize);
1788         rc = req_capsule_server_pack(info->mti_pill);
1789         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1790                 mdt_client_compatibility(info);
1791                 if (rc == 0)
1792                         mdt_fix_reply(info);
1793                 mdt_exit_ucred(info);
1794                 RETURN(lustre_msg_get_status(req->rq_repmsg));
1795         }
1796
1797         /* Continue to close handle even if we can not pack reply */
1798         if (rc == 0) {
1799                 repbody = req_capsule_server_get(info->mti_pill,
1800                                                  &RMF_MDT_BODY);
1801                 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
1802                                                     &RMF_MDT_MD);
1803                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
1804                                                        &RMF_MDT_MD,
1805                                                        RCL_SERVER);
1806                 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
1807                                                        &RMF_LOGCOOKIES);
1808                 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
1809                                                           &RMF_LOGCOOKIES,
1810                                                           RCL_SERVER);
1811                 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1812                 repbody->eadatasize = 0;
1813                 repbody->aclsize = 0;
1814         } else {
1815                 rc = err_serious(rc);
1816         }
1817
1818         med = &req->rq_export->exp_mdt_data;
1819         spin_lock(&med->med_open_lock);
1820         mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1821         if (mdt_mfd_closed(mfd)) {
1822                 spin_unlock(&med->med_open_lock);
1823                 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1824                        ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1825                        info->mti_ioepoch->handle.cookie);
1826                 /** not serious error since bug 3633 */
1827                 rc = -ESTALE;
1828         } else {
1829                 class_handle_unhash(&mfd->mfd_handle);
1830                 cfs_list_del_init(&mfd->mfd_list);
1831                 spin_unlock(&med->med_open_lock);
1832
1833                 /* Do not lose object before last unlink. */
1834                 o = mfd->mfd_object;
1835                 mdt_object_get(info->mti_env, o);
1836                 ret = mdt_mfd_close(info, mfd);
1837                 if (repbody != NULL)
1838                         rc = mdt_handle_last_unlink(info, o, ma);
1839                 mdt_empty_transno(info, rc);
1840                 mdt_object_put(info->mti_env, o);
1841         }
1842         if (repbody != NULL) {
1843                 mdt_client_compatibility(info);
1844                 rc = mdt_fix_reply(info);
1845         }
1846
1847         mdt_exit_ucred(info);
1848         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1849                 RETURN(err_serious(-ENOMEM));
1850
1851         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
1852                                  OBD_FAIL_MDS_CLOSE_NET_REP))
1853                 info->mti_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
1854         RETURN(rc ? rc : ret);
1855 }
1856
1857 /**
1858  * DONE_WRITING rpc handler.
1859  *
1860  * As mfd is not kept after replayed CLOSE (see mdt_ioepoch_close_on_replay()),
1861  * only those DONE_WRITING rpc will be replayed which really wrote smth on disk,
1862  * and got a trasid. Waiting for such DONE_WRITING is not reliable, so just
1863  * skip attributes and reconstruct the reply here.
1864  */
1865 int mdt_done_writing(struct mdt_thread_info *info)
1866 {
1867         struct ptlrpc_request   *req = mdt_info_req(info);
1868         struct mdt_body         *repbody = NULL;
1869         struct mdt_export_data  *med;
1870         struct mdt_file_data    *mfd;
1871         int rc;
1872         ENTRY;
1873
1874         rc = req_capsule_server_pack(info->mti_pill);
1875         if (rc)
1876                 RETURN(err_serious(rc));
1877
1878         repbody = req_capsule_server_get(info->mti_pill,
1879                                          &RMF_MDT_BODY);
1880         repbody->eadatasize = 0;
1881         repbody->aclsize = 0;
1882
1883         /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1884         rc = mdt_close_unpack(info);
1885         if (rc)
1886                 RETURN(err_serious(rc));
1887
1888         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1889                 mdt_exit_ucred(info);
1890                 RETURN(lustre_msg_get_status(req->rq_repmsg));
1891         }
1892
1893         med = &info->mti_exp->exp_mdt_data;
1894         spin_lock(&med->med_open_lock);
1895         mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1896         if (mfd == NULL) {
1897                 spin_unlock(&med->med_open_lock);
1898                 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1899                        ": cookie = "LPX64" ioepoch = "LPU64"\n",
1900                        PFID(info->mti_rr.rr_fid1),
1901                        info->mti_ioepoch->handle.cookie,
1902                        info->mti_ioepoch->ioepoch);
1903                 /* If this is a replay, reconstruct the transno. */
1904                 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1905                         rc = info->mti_ioepoch->flags & MF_SOM_AU ?
1906                              -EAGAIN : 0;
1907                         mdt_empty_transno(info, rc);
1908                 } else
1909                         rc = -ESTALE;
1910                 GOTO(error_ucred, rc);
1911         }
1912
1913         LASSERT(mfd->mfd_mode == MDS_FMODE_EPOCH ||
1914                 mfd->mfd_mode == MDS_FMODE_TRUNC);
1915         class_handle_unhash(&mfd->mfd_handle);
1916         cfs_list_del_init(&mfd->mfd_list);
1917         spin_unlock(&med->med_open_lock);
1918
1919         /* Set EPOCH CLOSE flag if not set by client. */
1920         info->mti_ioepoch->flags |= MF_EPOCH_CLOSE;
1921         info->mti_attr.ma_valid = 0;
1922
1923         info->mti_attr.ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
1924         OBD_ALLOC_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1925         if (info->mti_attr.ma_lmm == NULL)
1926                 GOTO(error_ucred, rc = -ENOMEM);
1927
1928         rc = mdt_mfd_close(info, mfd);
1929
1930         OBD_FREE_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1931         mdt_empty_transno(info, rc);
1932 error_ucred:
1933         mdt_exit_ucred(info);
1934         RETURN(rc);
1935 }