Whamcloud - gitweb
LU-4152 mdt: Don't enqueue two locks on the same resource
[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, 2013, 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(const struct mdt_export_data *med)
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                 mfd->mfd_handle.h_owner = med;
70                 CFS_INIT_LIST_HEAD(&mfd->mfd_list);
71                 class_handle_hash(&mfd->mfd_handle, &mfd_handle_ops);
72         }
73
74         RETURN(mfd);
75 }
76
77 /*
78  * Find the mfd pointed to by handle in global hash table.
79  * In case of replay the handle is obsoleted
80  * but mfd can be found in mfd list by that handle.
81  * Callers need to be holding med_open_lock.
82  */
83 struct mdt_file_data *mdt_handle2mfd(struct mdt_export_data *med,
84                                      const struct lustre_handle *handle,
85                                      bool is_replay_or_resent)
86 {
87         struct mdt_file_data   *mfd;
88         ENTRY;
89
90         LASSERT(handle != NULL);
91         mfd = class_handle2object(handle->cookie, med);
92         /* during dw/setattr replay the mfd can be found by old handle */
93         if (mfd == NULL && is_replay_or_resent) {
94                 cfs_list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
95                         if (mfd->mfd_old_handle.cookie == handle->cookie)
96                                 RETURN(mfd);
97                 }
98                 mfd = NULL;
99         }
100
101         RETURN(mfd);
102 }
103
104 /* free mfd */
105 void mdt_mfd_free(struct mdt_file_data *mfd)
106 {
107         LASSERT(cfs_list_empty(&mfd->mfd_list));
108         OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
109 }
110
111 static int mdt_create_data(struct mdt_thread_info *info,
112                            struct mdt_object *p, struct mdt_object *o)
113 {
114         struct md_op_spec     *spec = &info->mti_spec;
115         struct md_attr        *ma   = &info->mti_attr;
116         int                    rc   = 0;
117         ENTRY;
118
119         if (!md_should_create(spec->sp_cr_flags))
120                 RETURN(0);
121
122         ma->ma_need = MA_INODE | MA_LOV;
123         ma->ma_valid = 0;
124         mutex_lock(&o->mot_lov_mutex);
125         if (!(o->mot_flags & MOF_LOV_CREATED)) {
126                 if (p != NULL && (fid_is_obf(mdt_object_fid(p)) ||
127                                   fid_is_dot_lustre(mdt_object_fid(p))))
128                         GOTO(unlock, rc = -EPERM);
129
130                 rc = mdo_create_data(info->mti_env,
131                                      p ? mdt_object_child(p) : NULL,
132                                      mdt_object_child(o), spec, ma);
133                 if (rc == 0)
134                         rc = mdt_attr_get_complex(info, o, ma);
135
136                 if (rc == 0 && ma->ma_valid & MA_LOV)
137                         o->mot_flags |= MOF_LOV_CREATED;
138         }
139 unlock:
140         mutex_unlock(&o->mot_lov_mutex);
141         RETURN(rc);
142 }
143
144 static int mdt_ioepoch_opened(struct mdt_object *mo)
145 {
146         return mo->mot_ioepoch_count;
147 }
148
149 int mdt_object_is_som_enabled(struct mdt_object *mo)
150 {
151         return !mo->mot_ioepoch;
152 }
153
154 /**
155  * Re-enable Size-on-MDS.
156  * Call under ->mot_ioepoch_mutex.
157  */
158 static void mdt_object_som_enable(struct mdt_object *mo, __u64 ioepoch)
159 {
160         if (ioepoch == mo->mot_ioepoch) {
161                 LASSERT(!mdt_ioepoch_opened(mo));
162                 mo->mot_ioepoch = 0;
163                 mo->mot_flags = 0;
164         }
165 }
166
167 /**
168  * Open the IOEpoch. It is allowed if @writecount is not negative.
169  * The epoch and writecount handling is performed under the mot_ioepoch_mutex.
170  */
171 int mdt_ioepoch_open(struct mdt_thread_info *info, struct mdt_object *o,
172                      int created)
173 {
174         struct mdt_device *mdt = info->mti_mdt;
175         int cancel = 0;
176         int rc = 0;
177         ENTRY;
178
179         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
180             !S_ISREG(lu_object_attr(&o->mot_obj)))
181                 RETURN(0);
182
183         mutex_lock(&o->mot_ioepoch_mutex);
184         if (mdt_ioepoch_opened(o)) {
185                 /* Epoch continues even if there is no writers yet. */
186                 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
187                        o->mot_ioepoch, PFID(mdt_object_fid(o)));
188         } else {
189                 /* XXX: ->mdt_ioepoch is not initialized at the mount */
190                 spin_lock(&mdt->mdt_ioepoch_lock);
191                 if (mdt->mdt_ioepoch < info->mti_replayepoch)
192                         mdt->mdt_ioepoch = info->mti_replayepoch;
193
194                 if (info->mti_replayepoch)
195                         o->mot_ioepoch = info->mti_replayepoch;
196                 else if (++mdt->mdt_ioepoch == IOEPOCH_INVAL)
197                         o->mot_ioepoch = ++mdt->mdt_ioepoch;
198                 else
199                         o->mot_ioepoch = mdt->mdt_ioepoch;
200
201                 spin_unlock(&mdt->mdt_ioepoch_lock);
202
203                 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
204                        o->mot_ioepoch, PFID(mdt_object_fid(o)));
205                 if (created)
206                         o->mot_flags |= MOF_SOM_CREATED;
207                 cancel = 1;
208         }
209         o->mot_ioepoch_count++;
210         mutex_unlock(&o->mot_ioepoch_mutex);
211
212         /* Cancel Size-on-MDS attributes cached on clients for the open case.
213          * In the truncate case, see mdt_reint_setattr(). */
214         if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
215                 struct mdt_lock_handle  *lh = &info->mti_lh[MDT_LH_CHILD];
216                 mdt_lock_reg_init(lh, LCK_EX);
217                 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
218                                      MDT_LOCAL_LOCK);
219                 if (rc == 0)
220                         mdt_object_unlock(info, o, lh, 1);
221         }
222         RETURN(rc);
223 }
224
225 /**
226  * Update SOM on-disk attributes.
227  * If enabling, write update inodes and lustre-ea with the proper IOEpoch,
228  * mountid and attributes. If disabling, clean SOM xattr.
229  * Call under ->mot_ioepoch_mutex.
230  */
231 static int mdt_som_attr_set(struct mdt_thread_info *info,
232                             struct mdt_object *obj, __u64 ioepoch, bool enable)
233 {
234         struct md_object        *next = mdt_object_child(obj);
235         int                      rc;
236         ENTRY;
237
238         CDEBUG(D_INODE, "Size-on-MDS attribute %s for epoch "LPU64
239                " on "DFID".\n", enable ? "update" : "disabling",
240                ioepoch, PFID(mdt_object_fid(obj)));
241
242         if (enable) {
243                 struct lu_buf           *buf = &info->mti_buf;
244                 struct som_attrs        *attrs;
245                 struct md_attr          *ma = &info->mti_attr;
246                 struct lu_attr          *la = &ma->ma_attr;
247                 struct obd_device       *obd = info->mti_mdt->mdt_lut.lut_obd;
248
249                 attrs = (struct som_attrs *)info->mti_xattr_buf;
250                 CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*attrs));
251
252                 /* pack SOM attributes */
253                 memset(attrs, 0, sizeof(*attrs));
254                 attrs->som_ioepoch = ioepoch;
255                 attrs->som_mountid = obd->u.obt.obt_mount_count;
256                 if ((la->la_valid & LA_SIZE) != 0)
257                         attrs->som_size = la->la_size;
258                 if ((la->la_valid & LA_BLOCKS) != 0)
259                         attrs->som_blocks = la->la_blocks;
260                 lustre_som_swab(attrs);
261
262                 /* update SOM attributes */
263                 buf->lb_buf = attrs;
264                 buf->lb_len = sizeof(*attrs);
265                 rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_SOM, 0);
266         } else {
267                 /* delete SOM attributes */
268                 rc = mo_xattr_del(info->mti_env, next, XATTR_NAME_SOM);
269         }
270
271         RETURN(rc);
272 }
273
274 /** Perform the eviction specific actions on ioepoch close. */
275 static inline int mdt_ioepoch_close_on_eviction(struct mdt_thread_info *info,
276                                                 struct mdt_object *o)
277 {
278         int rc = 0;
279
280         mutex_lock(&o->mot_ioepoch_mutex);
281         CDEBUG(D_INODE, "Eviction. Closing IOepoch "LPU64" on "DFID". "
282                "Count %d\n", o->mot_ioepoch, PFID(mdt_object_fid(o)),
283                o->mot_ioepoch_count);
284         o->mot_ioepoch_count--;
285
286         /* If eviction occured set MOF_SOM_RECOV,
287          * if no other epoch holders, disable SOM on disk. */
288         o->mot_flags |= MOF_SOM_CHANGE | MOF_SOM_RECOV;
289         if (!mdt_ioepoch_opened(o)) {
290                 rc = mdt_som_attr_set(info, o, o->mot_ioepoch, MDT_SOM_DISABLE);
291                 mdt_object_som_enable(o, o->mot_ioepoch);
292         }
293         mutex_unlock(&o->mot_ioepoch_mutex);
294         RETURN(rc);
295 }
296
297 /**
298  * Perform the replay specific actions on ioepoch close.
299  * Skip SOM attribute update if obtained and just forget about the inode state
300  * for the last ioepoch holder. The SOM cache is invalidated on MDS failure.
301  */
302 static inline int mdt_ioepoch_close_on_replay(struct mdt_thread_info *info,
303                                               struct mdt_object *o)
304 {
305         int rc = MDT_IOEPOCH_CLOSED;
306         ENTRY;
307
308         mutex_lock(&o->mot_ioepoch_mutex);
309         CDEBUG(D_INODE, "Replay. Closing epoch "LPU64" on "DFID". Count %d\n",
310                o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
311         o->mot_ioepoch_count--;
312
313         /* Get an info from the replayed request if client is supposed
314          * to send an Attibute Update, reconstruct @rc if so */
315         if (info->mti_ioepoch->flags & MF_SOM_AU)
316                 rc = MDT_IOEPOCH_GETATTR;
317
318         if (!mdt_ioepoch_opened(o))
319                 mdt_object_som_enable(o, info->mti_ioepoch->ioepoch);
320         mutex_unlock(&o->mot_ioepoch_mutex);
321
322         RETURN(rc);
323 }
324
325 /**
326  * Regular file IOepoch close.
327  * Closes the ioepoch, checks the object state, apply obtained attributes and
328  * re-enable SOM on the object, if possible. Also checks if the recovery is
329  * needed and packs OBD_MD_FLGETATTRLOCK flag into the reply to force the client
330  * to obtain SOM attributes under the server-side OST locks.
331  *
332  * Return value:
333  * MDT_IOEPOCH_CLOSED if ioepoch is closed.
334  * MDT_IOEPOCH_GETATTR if ioepoch is closed but another SOM update is needed.
335  */
336 static inline int mdt_ioepoch_close_reg(struct mdt_thread_info *info,
337                                         struct mdt_object *o)
338 {
339         struct md_attr *tmp_ma;
340         struct lu_attr *la;
341         int achange, opened;
342         int recovery = 0;
343         int rc = 0, ret = MDT_IOEPOCH_CLOSED;
344         ENTRY;
345
346         la = &info->mti_attr.ma_attr;
347         achange = (info->mti_ioepoch->flags & MF_SOM_CHANGE);
348
349         mutex_lock(&o->mot_ioepoch_mutex);
350         o->mot_ioepoch_count--;
351
352         tmp_ma = &info->mti_u.som.attr;
353         tmp_ma->ma_lmm = info->mti_attr.ma_lmm;
354         tmp_ma->ma_lmm_size = info->mti_attr.ma_lmm_size;
355         tmp_ma->ma_som = &info->mti_u.som.data;
356         tmp_ma->ma_need = MA_INODE | MA_LOV | MA_SOM;
357         tmp_ma->ma_valid = 0;
358         rc = mdt_attr_get_complex(info, o, tmp_ma);
359         if (rc)
360                 GOTO(error_up, rc);
361
362         /* Check the on-disk SOM state. */
363         if (o->mot_flags & MOF_SOM_RECOV)
364                 recovery = 1;
365         else if (!(o->mot_flags & MOF_SOM_CREATED) &&
366                  !(tmp_ma->ma_valid & MA_SOM))
367                 recovery = 1;
368
369         CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
370                o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
371
372         opened = mdt_ioepoch_opened(o);
373         /**
374          * If IOEpoch is not opened, check if a Size-on-MDS update is needed.
375          * Skip the check for file with no LOV  or for unlink files.
376          */
377         if (!opened && tmp_ma->ma_valid & MA_LOV &&
378             !(tmp_ma->ma_valid & MA_INODE && tmp_ma->ma_attr.la_nlink == 0)) {
379                 if (recovery)
380                         /* If some previous writer was evicted, re-ask the
381                          * client for attributes. Even if attributes are
382                          * provided, we cannot believe in them.
383                          * Another use case is that there is no SOM cache on
384                          * disk -- first access with SOM or there was an MDS
385                          * failure. */
386                         ret = MDT_IOEPOCH_GETATTR;
387                 else if (o->mot_flags & MOF_SOM_CHANGE)
388                         /* Some previous writer changed the attribute.
389                          * Do not believe to the current Size-on-MDS
390                          * update, re-ask client. */
391                         ret = MDT_IOEPOCH_GETATTR;
392                 else if (!(la->la_valid & LA_SIZE) && achange)
393                         /* Attributes were changed by the last writer
394                          * only but no Size-on-MDS update is received.*/
395                         ret = MDT_IOEPOCH_GETATTR;
396         }
397
398         if (achange || ret == MDT_IOEPOCH_GETATTR)
399                 o->mot_flags |= MOF_SOM_CHANGE;
400
401         /* If epoch ends and relable SOM attributes are obtained, update them.
402          * Create SOM ea for new files even if there is no attributes obtained
403          * (0-length file). */
404         if (ret == MDT_IOEPOCH_CLOSED && !opened) {
405                 if (achange || o->mot_flags & MOF_SOM_CREATED) {
406                         LASSERT(achange || !(la->la_valid & LA_SIZE));
407                         rc = mdt_som_attr_set(info, o, o->mot_ioepoch,
408                                               MDT_SOM_ENABLE);
409                         /* Avoid the following setattrs of these attributes,
410                          * e.g. for atime update. */
411                         info->mti_attr.ma_valid = 0;
412                 }
413                 mdt_object_som_enable(o, o->mot_ioepoch);
414         }
415
416         mutex_unlock(&o->mot_ioepoch_mutex);
417         /* If recovery is needed, tell the client to perform GETATTR under
418          * the lock. */
419         if (ret == MDT_IOEPOCH_GETATTR && recovery) {
420                 struct mdt_body *rep;
421                 rep = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
422                 rep->valid |= OBD_MD_FLGETATTRLOCK;
423         }
424
425         RETURN(rc ? : ret);
426
427 error_up:
428         mutex_unlock(&o->mot_ioepoch_mutex);
429         return rc;
430 }
431
432 /**
433  * Close IOEpoch (opened file or MDS_FMODE_EPOCH state). It happens if:
434  * - a client closes the IOEpoch;
435  * - a client eviction occured.
436  * Return values:
437  * MDT_IOEPOCH_OPENED if the client does not close IOEpoch.
438  * MDT_IOEPOCH_CLOSED if the client closes IOEpoch.
439  * MDT_IOEPOCH_GETATTR if the client closes IOEpoch but another SOM attribute
440  * update is needed.
441  */
442 static int mdt_ioepoch_close(struct mdt_thread_info *info, struct mdt_object *o)
443 {
444         struct ptlrpc_request *req = mdt_info_req(info);
445         ENTRY;
446
447         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
448             !S_ISREG(lu_object_attr(&o->mot_obj)))
449                 RETURN(0);
450
451         LASSERT(o->mot_ioepoch_count);
452         LASSERT(info->mti_ioepoch == NULL ||
453                 info->mti_ioepoch->ioepoch == o->mot_ioepoch);
454
455         /* IOEpoch is closed only if client tells about it or eviction occures.
456          * In the replay case, always close the epoch. */
457         if (req == NULL)
458                 RETURN(mdt_ioepoch_close_on_eviction(info, o));
459         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
460                 RETURN(mdt_ioepoch_close_on_replay(info, o));
461         if (info->mti_ioepoch && (info->mti_ioepoch->flags & MF_EPOCH_CLOSE))
462                 RETURN(mdt_ioepoch_close_reg(info, o));
463         /* IO epoch is not closed. */
464         RETURN(MDT_IOEPOCH_OPENED);
465 }
466
467 /**
468  * Close MDS_FMODE_SOM state, when IOEpoch is already closed and we are waiting
469  * for attribute update. It happens if:
470  * - SOM Attribute Update is obtained;
471  * - the client failed to obtain it and informs MDS about it;
472  * - a client eviction occured.
473  * Apply obtained attributes for the 1st case, wipe out the on-disk SOM
474  * cache otherwise.
475  */
476 int mdt_som_au_close(struct mdt_thread_info *info, struct mdt_object *o)
477 {
478         struct ptlrpc_request   *req = mdt_info_req(info);
479         __u64                    ioepoch = 0;
480         int                      act = MDT_SOM_ENABLE;
481         int                      rc = 0;
482         ENTRY;
483
484         LASSERT(!req || info->mti_ioepoch);
485         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
486             !S_ISREG(lu_object_attr(&o->mot_obj)))
487                 RETURN(0);
488
489         /* No size whereas MF_SOM_CHANGE is set means client failed to
490          * obtain ost attributes, drop the SOM cache on disk if so. */
491         if (!req ||
492             (info->mti_ioepoch &&
493              info->mti_ioepoch->flags & MF_SOM_CHANGE &&
494              !(info->mti_attr.ma_attr.la_valid & LA_SIZE)))
495                 act = MDT_SOM_DISABLE;
496
497         mutex_lock(&o->mot_ioepoch_mutex);
498         /* Mark the object it is the recovery state if we failed to obtain
499          * SOM attributes. */
500         if (act == MDT_SOM_DISABLE)
501                 o->mot_flags |= MOF_SOM_RECOV;
502
503         if (!mdt_ioepoch_opened(o)) {
504                 ioepoch =  info->mti_ioepoch ?
505                         info->mti_ioepoch->ioepoch : o->mot_ioepoch;
506
507                 if (req != NULL
508                     && !(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))
509                         rc = mdt_som_attr_set(info, o, ioepoch, act);
510                 mdt_object_som_enable(o, ioepoch);
511         }
512         mutex_unlock(&o->mot_ioepoch_mutex);
513         RETURN(rc);
514 }
515
516 int mdt_write_read(struct mdt_object *o)
517 {
518         int rc = 0;
519         ENTRY;
520         mutex_lock(&o->mot_ioepoch_mutex);
521         rc = o->mot_writecount;
522         mutex_unlock(&o->mot_ioepoch_mutex);
523         RETURN(rc);
524 }
525
526 int mdt_write_get(struct mdt_object *o)
527 {
528         int rc = 0;
529         ENTRY;
530         mutex_lock(&o->mot_ioepoch_mutex);
531         if (o->mot_writecount < 0)
532                 rc = -ETXTBSY;
533         else
534                 o->mot_writecount++;
535         mutex_unlock(&o->mot_ioepoch_mutex);
536         RETURN(rc);
537 }
538
539 void mdt_write_put(struct mdt_object *o)
540 {
541         ENTRY;
542         mutex_lock(&o->mot_ioepoch_mutex);
543         o->mot_writecount--;
544         mutex_unlock(&o->mot_ioepoch_mutex);
545         EXIT;
546 }
547
548 static int mdt_write_deny(struct mdt_object *o)
549 {
550         int rc = 0;
551         ENTRY;
552         mutex_lock(&o->mot_ioepoch_mutex);
553         if (o->mot_writecount > 0)
554                 rc = -ETXTBSY;
555         else
556                 o->mot_writecount--;
557         mutex_unlock(&o->mot_ioepoch_mutex);
558         RETURN(rc);
559 }
560
561 static void mdt_write_allow(struct mdt_object *o)
562 {
563         ENTRY;
564         mutex_lock(&o->mot_ioepoch_mutex);
565         o->mot_writecount++;
566         mutex_unlock(&o->mot_ioepoch_mutex);
567         EXIT;
568 }
569
570 /* there can be no real transaction so prepare the fake one */
571 static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
572 {
573         struct mdt_device      *mdt = info->mti_mdt;
574         struct ptlrpc_request  *req = mdt_info_req(info);
575         struct tg_export_data  *ted;
576         struct lsd_client_data *lcd;
577
578         ENTRY;
579         /* transaction has occurred already */
580         if (lustre_msg_get_transno(req->rq_repmsg) != 0)
581                 RETURN_EXIT;
582
583         spin_lock(&mdt->mdt_lut.lut_translock);
584         if (rc != 0) {
585                 if (info->mti_transno != 0) {
586                         struct obd_export *exp = req->rq_export;
587
588                         CERROR("%s: replay trans "LPU64" NID %s: rc = %d\n",
589                                mdt_obd_name(mdt), info->mti_transno,
590                                libcfs_nid2str(exp->exp_connection->c_peer.nid),
591                                rc);
592                         spin_unlock(&mdt->mdt_lut.lut_translock);
593                         RETURN_EXIT;
594                 }
595         } else if (info->mti_transno == 0) {
596                 info->mti_transno = ++mdt->mdt_lut.lut_last_transno;
597         } else {
598                 /* should be replay */
599                 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
600                         mdt->mdt_lut.lut_last_transno = info->mti_transno;
601         }
602         spin_unlock(&mdt->mdt_lut.lut_translock);
603
604         CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
605                info->mti_transno,
606                req->rq_export->exp_obd->obd_last_committed);
607
608         req->rq_transno = info->mti_transno;
609         lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
610
611         /* update lcd in memory only for resent cases */
612         ted = &req->rq_export->exp_target_data;
613         LASSERT(ted);
614         mutex_lock(&ted->ted_lcd_lock);
615         lcd = ted->ted_lcd;
616         if (info->mti_transno < lcd->lcd_last_transno &&
617             info->mti_transno != 0) {
618                 /* This should happen during replay. Do not update
619                  * last rcvd info if replay req transno < last transno,
620                  * otherwise the following resend(after replay) can not
621                  * be checked correctly by xid */
622                 mutex_unlock(&ted->ted_lcd_lock);
623                 CDEBUG(D_HA, "%s: transno = "LPU64" < last_transno = "LPU64"\n",
624                        mdt_obd_name(mdt), info->mti_transno,
625                        lcd->lcd_last_transno);
626                 RETURN_EXIT;
627         }
628
629         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
630             lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
631                 if (info->mti_transno != 0)
632                         lcd->lcd_last_close_transno = info->mti_transno;
633                 lcd->lcd_last_close_xid = req->rq_xid;
634                 lcd->lcd_last_close_result = rc;
635         } else {
636                 /* VBR: save versions in last_rcvd for reconstruct. */
637                 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
638                 if (pre_versions) {
639                         lcd->lcd_pre_versions[0] = pre_versions[0];
640                         lcd->lcd_pre_versions[1] = pre_versions[1];
641                         lcd->lcd_pre_versions[2] = pre_versions[2];
642                         lcd->lcd_pre_versions[3] = pre_versions[3];
643                 }
644                 if (info->mti_transno != 0)
645                         lcd->lcd_last_transno = info->mti_transno;
646
647                 lcd->lcd_last_xid = req->rq_xid;
648                 lcd->lcd_last_result = rc;
649                 lcd->lcd_last_data = info->mti_opdata;
650         }
651         mutex_unlock(&ted->ted_lcd_lock);
652
653         EXIT;
654 }
655
656 void mdt_mfd_set_mode(struct mdt_file_data *mfd, __u64 mode)
657 {
658         LASSERT(mfd != NULL);
659
660         CDEBUG(D_HA, DFID " Change mfd mode "LPO64" -> "LPO64".\n",
661                PFID(mdt_object_fid(mfd->mfd_object)), mfd->mfd_mode, mode);
662
663         mfd->mfd_mode = mode;
664 }
665
666 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
667                         struct mdt_object *o, __u64 flags, int created,
668                         struct ldlm_reply *rep)
669 {
670         struct ptlrpc_request   *req = mdt_info_req(info);
671         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
672         struct mdt_file_data    *mfd;
673         struct md_attr          *ma  = &info->mti_attr;
674         struct lu_attr          *la  = &ma->ma_attr;
675         struct mdt_body         *repbody;
676         int                      rc = 0, isdir, isreg;
677         ENTRY;
678
679         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
680
681         isreg = S_ISREG(la->la_mode);
682         isdir = S_ISDIR(la->la_mode);
683         if (isreg && !(ma->ma_valid & MA_LOV) && !(flags & MDS_OPEN_RELEASE)) {
684                 /*
685                  * No EA, check whether it is will set regEA and dirEA since in
686                  * above attr get, these size might be zero, so reset it, to
687                  * retrieve the MD after create obj.
688                  */
689                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
690                                                        &RMF_MDT_MD,
691                                                        RCL_SERVER);
692                 /* in replay case, p == NULL */
693                 rc = mdt_create_data(info, p, o);
694                 if (rc)
695                         RETURN(rc);
696
697                 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_DISP_STRIPE)
698                         mdt_set_disposition(info, rep, DISP_OPEN_STRIPE);
699         }
700
701         CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
702                ma->ma_valid, ma->ma_lmm_size);
703
704         if (ma->ma_valid & MA_LOV) {
705                 LASSERT(ma->ma_lmm_size != 0);
706                 repbody->eadatasize = ma->ma_lmm_size;
707                 if (isdir)
708                         repbody->valid |= OBD_MD_FLDIREA;
709                 else
710                         repbody->valid |= OBD_MD_FLEASIZE;
711         }
712
713         if (flags & FMODE_WRITE) {
714                 rc = mdt_write_get(o);
715                 if (rc == 0) {
716                         mdt_ioepoch_open(info, o, created);
717                         repbody->ioepoch = o->mot_ioepoch;
718                 }
719         } else if (flags & MDS_FMODE_EXEC) {
720                 rc = mdt_write_deny(o);
721         }
722         if (rc)
723                 RETURN(rc);
724
725         rc = mo_open(info->mti_env, mdt_object_child(o),
726                      created ? flags | MDS_OPEN_CREATED : flags);
727         if (rc)
728                 GOTO(err_out, rc);
729
730         mfd = mdt_mfd_new(med);
731         if (mfd == NULL)
732                 GOTO(err_out, rc = -ENOMEM);
733
734         /*
735          * Keep a reference on this object for this open, and is
736          * released by mdt_mfd_close().
737          */
738         mdt_object_get(info->mti_env, o);
739         mfd->mfd_object = o;
740         mfd->mfd_xid = req->rq_xid;
741
742         /*
743          * @flags is always not zero. At least it should be FMODE_READ,
744          * FMODE_WRITE or MDS_FMODE_EXEC.
745          */
746         LASSERT(flags != 0);
747
748         /* Open handling. */
749         mdt_mfd_set_mode(mfd, flags);
750
751         atomic_inc(&o->mot_open_count);
752         if (flags & MDS_OPEN_LEASE)
753                 atomic_inc(&o->mot_lease_count);
754
755         /* replay handle */
756         if (req_is_replay(req)) {
757                 struct mdt_file_data *old_mfd;
758                 /* Check wheather old cookie already exist in
759                  * the list, becasue when do recovery, client
760                  * might be disconnected from server, and
761                  * restart replay, so there maybe some orphan
762                  * mfd here, we should remove them */
763                 LASSERT(info->mti_rr.rr_handle != NULL);
764                 spin_lock(&med->med_open_lock);
765                 old_mfd = mdt_handle2mfd(med, info->mti_rr.rr_handle, true);
766                 if (old_mfd != NULL) {
767                         CDEBUG(D_HA, "delete orphan mfd = %p, fid = "DFID", "
768                                "cookie = "LPX64"\n", mfd,
769                                PFID(mdt_object_fid(mfd->mfd_object)),
770                                info->mti_rr.rr_handle->cookie);
771                         class_handle_unhash(&old_mfd->mfd_handle);
772                         cfs_list_del_init(&old_mfd->mfd_list);
773                         spin_unlock(&med->med_open_lock);
774                         /* no attr update for that close */
775                         la->la_valid = 0;
776                         ma->ma_valid |= MA_FLAGS;
777                         ma->ma_attr_flags |= MDS_RECOV_OPEN;
778                         mdt_mfd_close(info, old_mfd);
779                         ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
780                         ma->ma_valid &= ~MA_FLAGS;
781                 } else {
782                         spin_unlock(&med->med_open_lock);
783                         CDEBUG(D_HA, "orphan mfd not found, fid = "DFID", "
784                                "cookie = "LPX64"\n",
785                                PFID(mdt_object_fid(mfd->mfd_object)),
786                                info->mti_rr.rr_handle->cookie);
787                 }
788
789                 CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
790                        info->mti_rr.rr_handle->cookie);
791
792                 mfd->mfd_old_handle.cookie = info->mti_rr.rr_handle->cookie;
793         }
794
795         repbody->handle.cookie = mfd->mfd_handle.h_cookie;
796
797         if (req->rq_export->exp_disconnected) {
798                 spin_lock(&med->med_open_lock);
799                 class_handle_unhash(&mfd->mfd_handle);
800                 cfs_list_del_init(&mfd->mfd_list);
801                 spin_unlock(&med->med_open_lock);
802                 mdt_mfd_close(info, mfd);
803         } else {
804                 spin_lock(&med->med_open_lock);
805                 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
806                 spin_unlock(&med->med_open_lock);
807         }
808
809         mdt_empty_transno(info, rc);
810
811         RETURN(rc);
812
813 err_out:
814         if (flags & FMODE_WRITE)
815                         /* XXX We also need to close io epoch here.
816                          * See LU-1220 - green */
817                 mdt_write_put(o);
818         else if (flags & FMODE_EXEC)
819                 mdt_write_allow(o);
820         return rc;
821 }
822
823 int mdt_finish_open(struct mdt_thread_info *info,
824                     struct mdt_object *p, struct mdt_object *o,
825                     __u64 flags, int created, struct ldlm_reply *rep)
826 {
827         struct ptlrpc_request   *req = mdt_info_req(info);
828         struct obd_export       *exp = req->rq_export;
829         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
830         struct md_attr          *ma  = &info->mti_attr;
831         struct lu_attr          *la  = &ma->ma_attr;
832         struct mdt_file_data    *mfd;
833         struct mdt_body         *repbody;
834         int                      rc = 0;
835         int                      isreg, isdir, islnk;
836         cfs_list_t              *t;
837         ENTRY;
838
839         LASSERT(ma->ma_valid & MA_INODE);
840
841         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
842
843         isreg = S_ISREG(la->la_mode);
844         isdir = S_ISDIR(la->la_mode);
845         islnk = S_ISLNK(la->la_mode);
846         mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
847
848         /* LU-2275, simulate broken behaviour (esp. prevalent in
849          * pre-2.4 servers where a very strange reply is sent on error
850          * that looks like it was actually almost succesful and a failure at the
851          * same time */
852         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_NEGATIVE_POSITIVE)) {
853                 mdt_set_disposition(info, rep, DISP_OPEN_OPEN |
854                                                DISP_LOOKUP_NEG |
855                                                DISP_LOOKUP_POS);
856
857                 if (flags & MDS_OPEN_LOCK)
858                         mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
859
860                 RETURN(-ENOENT);
861         }
862
863         if (exp_connect_rmtclient(exp)) {
864                 void *buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
865
866                 rc = mdt_pack_remote_perm(info, o, buf);
867                 if (rc) {
868                         repbody->valid &= ~OBD_MD_FLRMTPERM;
869                         repbody->aclsize = 0;
870                 } else {
871                         repbody->valid |= OBD_MD_FLRMTPERM;
872                         repbody->aclsize = sizeof(struct mdt_remote_perm);
873                 }
874         }
875 #ifdef CONFIG_FS_POSIX_ACL
876         else if (exp_connect_flags(exp) & OBD_CONNECT_ACL) {
877                 const struct lu_env *env = info->mti_env;
878                 struct md_object *next = mdt_object_child(o);
879                 struct lu_buf *buf = &info->mti_buf;
880
881                 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
882                 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
883                                                    RCL_SERVER);
884                 if (buf->lb_len > 0) {
885                         rc = mo_xattr_get(env, next, buf,
886                                           XATTR_NAME_ACL_ACCESS);
887                         if (rc < 0) {
888                                 if (rc == -ENODATA) {
889                                         repbody->aclsize = 0;
890                                         repbody->valid |= OBD_MD_FLACL;
891                                         rc = 0;
892                                 } else if (rc == -EOPNOTSUPP) {
893                                         rc = 0;
894                                 } else {
895                                         CERROR("got acl size: %d\n", rc);
896                                 }
897                         } else {
898                                 repbody->aclsize = rc;
899                                 repbody->valid |= OBD_MD_FLACL;
900                                 rc = 0;
901                         }
902                 }
903         }
904 #endif
905
906         if (info->mti_mdt->mdt_lut.lut_mds_capa &&
907             exp_connect_flags(exp) & OBD_CONNECT_MDS_CAPA) {
908                 struct lustre_capa *capa;
909
910                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
911                 LASSERT(capa);
912                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
913                 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
914                 if (rc)
915                         RETURN(rc);
916                 repbody->valid |= OBD_MD_FLMDSCAPA;
917         }
918         if (info->mti_mdt->mdt_lut.lut_oss_capa &&
919             exp_connect_flags(exp) & OBD_CONNECT_OSS_CAPA &&
920             S_ISREG(lu_object_attr(&o->mot_obj))) {
921                 struct lustre_capa *capa;
922
923                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
924                 LASSERT(capa);
925                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
926                 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
927                 if (rc)
928                         RETURN(rc);
929                 repbody->valid |= OBD_MD_FLOSSCAPA;
930         }
931
932         /*
933          * If we are following a symlink, don't open; and do not return open
934          * handle for special nodes as client required.
935          */
936         if (islnk || (!isreg && !isdir &&
937             (exp_connect_flags(req->rq_export) & OBD_CONNECT_NODEVOH))) {
938                 lustre_msg_set_transno(req->rq_repmsg, 0);
939                 RETURN(0);
940         }
941
942         /*
943          * We need to return the existing object's fid back, so it is done here,
944          * after preparing the reply.
945          */
946         if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
947                 RETURN(-EEXIST);
948
949         /* This can't be done earlier, we need to return reply body */
950         if (isdir) {
951                 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
952                         /* We are trying to create or write an existing dir. */
953                         RETURN(-EISDIR);
954                 }
955         } else if (flags & MDS_OPEN_DIRECTORY)
956                 RETURN(-ENOTDIR);
957
958         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
959                                  OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
960                 RETURN(-EAGAIN);
961         }
962
963         mfd = NULL;
964         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
965                 spin_lock(&med->med_open_lock);
966                 cfs_list_for_each(t, &med->med_open_head) {
967                         mfd = cfs_list_entry(t, struct mdt_file_data, mfd_list);
968                         if (mfd->mfd_xid == req->rq_xid)
969                                 break;
970                         mfd = NULL;
971                 }
972                 spin_unlock(&med->med_open_lock);
973
974                 if (mfd != NULL) {
975                         repbody->handle.cookie = mfd->mfd_handle.h_cookie;
976                         /*set repbody->ea_size for resent case*/
977                         if (ma->ma_valid & MA_LOV) {
978                                 LASSERT(ma->ma_lmm_size != 0);
979                                 repbody->eadatasize = ma->ma_lmm_size;
980                                 if (isdir)
981                                         repbody->valid |= OBD_MD_FLDIREA;
982                                 else
983                                         repbody->valid |= OBD_MD_FLEASIZE;
984                         }
985                         mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
986                         RETURN(0);
987                 }
988         }
989
990         rc = mdt_mfd_open(info, p, o, flags, created, rep);
991         if (!rc)
992                 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
993
994         RETURN(rc);
995 }
996
997 extern void mdt_req_from_lcd(struct ptlrpc_request *req,
998                              struct lsd_client_data *lcd);
999
1000 void mdt_reconstruct_open(struct mdt_thread_info *info,
1001                           struct mdt_lock_handle *lhc)
1002 {
1003         const struct lu_env *env = info->mti_env;
1004         struct mdt_device       *mdt  = info->mti_mdt;
1005         struct req_capsule      *pill = info->mti_pill;
1006         struct ptlrpc_request   *req  = mdt_info_req(info);
1007         struct tg_export_data   *ted  = &req->rq_export->exp_target_data;
1008         struct lsd_client_data  *lcd  = ted->ted_lcd;
1009         struct md_attr          *ma   = &info->mti_attr;
1010         struct mdt_reint_record *rr   = &info->mti_rr;
1011         __u64                   flags = info->mti_spec.sp_cr_flags;
1012         struct ldlm_reply       *ldlm_rep;
1013         struct mdt_object       *parent;
1014         struct mdt_object       *child;
1015         struct mdt_body         *repbody;
1016         int                      rc;
1017         ENTRY;
1018
1019         LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1020         ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
1021         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1022
1023         ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
1024         ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
1025                                                RCL_SERVER);
1026         ma->ma_need = MA_INODE | MA_HSM;
1027         if (ma->ma_lmm_size > 0)
1028                 ma->ma_need |= MA_LOV;
1029
1030         ma->ma_valid = 0;
1031
1032         mdt_req_from_lcd(req, lcd);
1033         mdt_set_disposition(info, ldlm_rep, lcd->lcd_last_data);
1034
1035         CDEBUG(D_INODE, "This is reconstruct open: disp="LPX64", result=%d\n",
1036                ldlm_rep->lock_policy_res1, req->rq_status);
1037
1038         if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
1039             req->rq_status != 0)
1040                 /* We did not create successfully, return error to client. */
1041                 GOTO(out, rc = req->rq_status);
1042
1043         if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
1044                 struct obd_export *exp = req->rq_export;
1045                 /*
1046                  * We failed after creation, but we do not know in which step
1047                  * we failed. So try to check the child object.
1048                  */
1049                 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1050                 if (IS_ERR(parent)) {
1051                         rc = PTR_ERR(parent);
1052                         LCONSOLE_WARN("Parent "DFID" lookup error %d."
1053                                       " Evicting client %s with export %s.\n",
1054                                       PFID(rr->rr_fid1), rc,
1055                                       obd_uuid2str(&exp->exp_client_uuid),
1056                                       obd_export_nid2str(exp));
1057                         mdt_export_evict(exp);
1058                         RETURN_EXIT;
1059                 }
1060                 child = mdt_object_find(env, mdt, rr->rr_fid2);
1061                 if (IS_ERR(child)) {
1062                         rc = PTR_ERR(child);
1063                         LCONSOLE_WARN("Child "DFID" lookup error %d."
1064                                       " Evicting client %s with export %s.\n",
1065                                       PFID(mdt_object_fid(child)), rc,
1066                                       obd_uuid2str(&exp->exp_client_uuid),
1067                                       obd_export_nid2str(exp));
1068                         mdt_object_put(env, parent);
1069                         mdt_export_evict(exp);
1070                         RETURN_EXIT;
1071                 }
1072
1073                 if (unlikely(mdt_object_remote(child))) {
1074                         /* the child object was created on remote server */
1075                         if (!mdt_is_dne_client(exp)) {
1076                                 /* Return -EIO for old client */
1077                                 mdt_object_put(env, parent);
1078                                 mdt_object_put(env, child);
1079                                 GOTO(out, rc = -EIO);
1080                         }
1081                         repbody->fid1 = *rr->rr_fid2;
1082                         repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1083                         rc = 0;
1084                 } else {
1085                         if (mdt_object_exists(child)) {
1086                                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1087                                                  BYPASS_CAPA);
1088                                 rc = mdt_attr_get_complex(info, child, ma);
1089                                 if (rc == 0)
1090                                         rc = mdt_finish_open(info, parent,
1091                                                              child, flags,
1092                                                              1, ldlm_rep);
1093                         } else {
1094                                 /* the child does not exist, we should do
1095                                  * regular open */
1096                                 mdt_object_put(env, parent);
1097                                 mdt_object_put(env, child);
1098                                 GOTO(regular_open, 0);
1099                         }
1100                 }
1101                 mdt_object_put(env, parent);
1102                 mdt_object_put(env, child);
1103                 GOTO(out, rc);
1104         } else {
1105 regular_open:
1106                 /* We did not try to create, so we are a pure open */
1107                 rc = mdt_reint_open(info, lhc);
1108         }
1109
1110         EXIT;
1111 out:
1112         req->rq_status = rc;
1113         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1114         LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
1115 }
1116
1117 int mdt_open_by_fid(struct mdt_thread_info *info, struct ldlm_reply *rep)
1118 {
1119         __u64                    flags = info->mti_spec.sp_cr_flags;
1120         struct mdt_reint_record *rr = &info->mti_rr;
1121         struct md_attr          *ma = &info->mti_attr;
1122         struct mdt_object       *o;
1123         int                      rc;
1124         ENTRY;
1125
1126         o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1127         if (IS_ERR(o))
1128                 RETURN(rc = PTR_ERR(o));
1129
1130         if (unlikely(mdt_object_remote(o))) {
1131                 /* the child object was created on remote server */
1132                 struct mdt_body *repbody;
1133
1134                 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1135                                                 DISP_LOOKUP_EXECD |
1136                                                 DISP_LOOKUP_POS));
1137                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1138                 repbody->fid1 = *rr->rr_fid2;
1139                 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1140                 rc = 0;
1141         } else {
1142                 if (mdt_object_exists(o)) {
1143                         mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1144                                                         DISP_LOOKUP_EXECD |
1145                                                         DISP_LOOKUP_POS));
1146
1147                         rc = mdt_attr_get_complex(info, o, ma);
1148                         if (rc == 0)
1149                                 rc = mdt_finish_open(info, NULL, o, flags, 0,
1150                                                      rep);
1151                 } else {
1152                         rc = -ENOENT;
1153                 }
1154         }
1155
1156         mdt_object_put(info->mti_env, o);
1157         RETURN(rc);
1158 }
1159
1160 /* lock object for open */
1161 static int mdt_object_open_lock(struct mdt_thread_info *info,
1162                                 struct mdt_object *obj,
1163                                 struct mdt_lock_handle *lhc,
1164                                 __u64 *ibits)
1165 {
1166         struct md_attr  *ma = &info->mti_attr;
1167         __u64            open_flags = info->mti_spec.sp_cr_flags;
1168         ldlm_mode_t      lm = LCK_CR;
1169         bool             acq_lease = !!(open_flags & MDS_OPEN_LEASE);
1170         bool             try_layout = false;
1171         bool             create_layout = false;
1172         int              rc = 0;
1173         ENTRY;
1174
1175         *ibits = 0;
1176         mdt_lock_handle_init(lhc);
1177
1178         if (req_is_replay(mdt_info_req(info)))
1179                 RETURN(0);
1180
1181         if (S_ISREG(lu_object_attr(&obj->mot_obj))) {
1182                 if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV) &&
1183                     md_should_create(open_flags))
1184                         create_layout = true;
1185                 if (exp_connect_layout(info->mti_exp) && !create_layout &&
1186                     ma->ma_need & MA_LOV)
1187                         try_layout = true;
1188         }
1189
1190         if (acq_lease) {
1191                 /* lease open, acquire write mode of open sem */
1192                 down_write(&obj->mot_open_sem);
1193
1194                 /* Lease exists and ask for new lease */
1195                 if (atomic_read(&obj->mot_lease_count) > 0) {
1196                         /* only exclusive open is supported, so lease
1197                          * are conflicted to each other */
1198                         GOTO(out, rc = -EBUSY);
1199                 }
1200
1201                 /* Lease must be with open lock */
1202                 if (!(open_flags & MDS_OPEN_LOCK)) {
1203                         CERROR("Request lease for file:"DFID ", but open lock "
1204                                 "is missed, open_flags = "LPO64".\n",
1205                                 PFID(mdt_object_fid(obj)), open_flags);
1206                         GOTO(out, rc = -EPROTO);
1207                 }
1208
1209                 /* XXX: only exclusive open is supported. */
1210                 lm = LCK_EX;
1211                 *ibits = MDS_INODELOCK_OPEN;
1212
1213                 /* never grant LCK_EX layout lock to client */
1214                 try_layout = false;
1215         } else { /* normal open */
1216                 /* normal open holds read mode of open sem */
1217                 down_read(&obj->mot_open_sem);
1218
1219                 if (open_flags & MDS_OPEN_LOCK) {
1220                         if (open_flags & FMODE_WRITE)
1221                                 lm = LCK_CW;
1222                         else if (open_flags & MDS_FMODE_EXEC)
1223                                 lm = LCK_PR;
1224                         else
1225                                 lm = LCK_CR;
1226
1227                         *ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN;
1228                 } else if (atomic_read(&obj->mot_lease_count) > 0) {
1229                         if (open_flags & FMODE_WRITE)
1230                                 lm = LCK_CW;
1231                         else
1232                                 lm = LCK_CR;
1233
1234                         /* revoke lease */
1235                         *ibits = MDS_INODELOCK_OPEN;
1236                         try_layout = false;
1237
1238                         lhc = &info->mti_lh[MDT_LH_LOCAL];
1239                 }
1240                 CDEBUG(D_INODE, "normal open:"DFID" lease count: %d, lm: %d\n",
1241                         PFID(mdt_object_fid(obj)),
1242                         atomic_read(&obj->mot_open_count), lm);
1243         }
1244
1245         mdt_lock_reg_init(lhc, lm);
1246
1247         /* one problem to return layout lock on open is that it may result
1248          * in too many layout locks cached on the client side. */
1249         if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
1250                 /* return lookup lock to validate inode at the client side,
1251                  * this is pretty important otherwise mdt will return layout
1252                  * lock for each open.
1253                  * However this is a double-edged sword because changing
1254                  * permission will revoke huge # of LOOKUP locks. */
1255                 *ibits |= MDS_INODELOCK_LAYOUT | MDS_INODELOCK_LOOKUP;
1256                 if (!mdt_object_lock_try(info, obj, lhc, *ibits,
1257                                          MDT_CROSS_LOCK)) {
1258                         *ibits &= ~(MDS_INODELOCK_LAYOUT|MDS_INODELOCK_LOOKUP);
1259                         if (*ibits != 0)
1260                                 rc = mdt_object_lock(info, obj, lhc, *ibits,
1261                                                 MDT_CROSS_LOCK);
1262                 }
1263         } else if (*ibits != 0) {
1264                 rc = mdt_object_lock(info, obj, lhc, *ibits, MDT_CROSS_LOCK);
1265         }
1266
1267         CDEBUG(D_INODE, "Requested bits lock:"DFID ", ibits = "LPX64
1268                 ", open_flags = "LPO64", try_layout = %d, rc = %d\n",
1269                 PFID(mdt_object_fid(obj)), *ibits, open_flags, try_layout, rc);
1270
1271         /* will change layout, revoke layout locks by enqueuing EX lock. */
1272         if (rc == 0 && create_layout) {
1273                 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
1274
1275                 CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
1276                         ", open_flags = "LPO64"\n",
1277                         PFID(mdt_object_fid(obj)), open_flags);
1278
1279                 /* We cannot enqueue another lock for the same resource we
1280                  * already have a lock for, due to mechanics of waiting list
1281                  * iterating in ldlm, see LU-3601.
1282                  * As such we'll drop the open lock we just got above here,
1283                  * it's ok not to have this open lock as it's main purpose is to
1284                  * flush unused cached client open handles. */
1285                 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1286                         mdt_object_unlock(info, obj, lhc, 1);
1287
1288                 LASSERT(!try_layout);
1289                 mdt_lock_handle_init(ll);
1290                 mdt_lock_reg_init(ll, LCK_EX);
1291                 rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT,
1292                                         MDT_LOCAL_LOCK);
1293
1294                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
1295         }
1296
1297         /* Check if there is any other open handles after acquiring
1298          * open lock. At this point, caching open handles have been revoked
1299          * by open lock.
1300          * XXX: Now only exclusive open is supported. Need to check the
1301          * type of open for generic lease support. */
1302         if (rc == 0 && acq_lease) {
1303                 struct ptlrpc_request *req = mdt_info_req(info);
1304                 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
1305                 struct mdt_file_data *mfd;
1306                 bool is_replay_or_resent;
1307                 int open_count = 0;
1308
1309                 /* For lease: application can open a file and then apply lease,
1310                  * @handle contains original open handle in that case.
1311                  * In recovery, open REQ will be replayed and the lease REQ may
1312                  * be resent that means the open handle is already stale, so we
1313                  * need to fix it up here by finding new handle. */
1314                 is_replay_or_resent = req_is_replay(req) ||
1315                         lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT;
1316
1317                 /* if the request is _not_ a replay request, rr_handle
1318                  * may be used to hold an openhandle which is issuing the
1319                  * lease request, so that this openhandle doesn't count. */
1320                 mfd = mdt_handle2mfd(med, info->mti_rr.rr_handle,
1321                                      is_replay_or_resent);
1322                 if (mfd != NULL)
1323                         ++open_count;
1324
1325                 CDEBUG(D_INODE, "acq_lease "DFID": openers: %d, want: %d\n",
1326                         PFID(mdt_object_fid(obj)),
1327                         atomic_read(&obj->mot_open_count), open_count);
1328
1329                 if (atomic_read(&obj->mot_open_count) > open_count)
1330                         GOTO(out, rc = -EBUSY);
1331         }
1332         GOTO(out, rc);
1333
1334 out:
1335         RETURN(rc);
1336 }
1337
1338 static void mdt_object_open_unlock(struct mdt_thread_info *info,
1339                                    struct mdt_object *obj,
1340                                    struct mdt_lock_handle *lhc,
1341                                    __u64 ibits, int rc)
1342 {
1343         __u64 open_flags = info->mti_spec.sp_cr_flags;
1344         struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LOCAL];
1345         ENTRY;
1346
1347         if (req_is_replay(mdt_info_req(info)))
1348                 RETURN_EXIT;
1349
1350         /* Release local lock - the lock put in MDT_LH_LOCAL will never
1351          * return to client side. */
1352         if (lustre_handle_is_used(&ll->mlh_reg_lh))
1353                 mdt_object_unlock(info, obj, ll, 1);
1354
1355         ll = &info->mti_lh[MDT_LH_LAYOUT];
1356         /* Release local layout lock, layout was created */
1357         if (lustre_handle_is_used(&ll->mlh_reg_lh)) {
1358                 LASSERT(!(ibits & MDS_INODELOCK_LAYOUT));
1359                 mdt_object_unlock(info, obj, ll, 1);
1360         }
1361
1362         if (open_flags & MDS_OPEN_LEASE)
1363                 up_write(&obj->mot_open_sem);
1364         else
1365                 up_read(&obj->mot_open_sem);
1366
1367         /* Cross-ref case, the lock should be returned to the client */
1368         if (ibits == 0 || rc == -EREMOTE)
1369                 RETURN_EXIT;
1370
1371         if (!(open_flags & MDS_OPEN_LOCK) && !(ibits & MDS_INODELOCK_LAYOUT)) {
1372                 /* for the open request, the lock will only return to client
1373                  * if open or layout lock is granted. */
1374                 rc = 1;
1375         }
1376
1377         if (rc != 0 || !lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1378                 struct ldlm_reply       *ldlm_rep;
1379
1380                 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1381                 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1382                 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1383                         mdt_object_unlock(info, obj, lhc, 1);
1384         }
1385         RETURN_EXIT;
1386 }
1387
1388 /**
1389  * Check release is permitted for the current HSM flags.
1390  */
1391 static bool mdt_hsm_release_allow(const struct md_attr *ma)
1392 {
1393         if (!(ma->ma_valid & MA_HSM))
1394                 return false;
1395
1396         if (ma->ma_hsm.mh_flags & (HS_DIRTY|HS_NORELEASE|HS_LOST))
1397                 return false;
1398
1399         if (!(ma->ma_hsm.mh_flags & HS_ARCHIVED))
1400                 return false;
1401
1402         return true;
1403 }
1404
1405 int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
1406                          struct mdt_lock_handle *lhc)
1407 {
1408         const struct lu_env     *env   = info->mti_env;
1409         struct mdt_device       *mdt   = info->mti_mdt;
1410         __u64                    flags = info->mti_spec.sp_cr_flags;
1411         struct mdt_reint_record *rr    = &info->mti_rr;
1412         struct md_attr          *ma    = &info->mti_attr;
1413         struct mdt_object       *parent= NULL;
1414         struct mdt_object       *o;
1415         int                      rc;
1416         __u64                    ibits = 0;
1417         ENTRY;
1418
1419         if (md_should_create(flags) && !(flags & MDS_OPEN_HAS_EA)) {
1420                 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1421                         parent = mdt_object_find(env, mdt, rr->rr_fid1);
1422                         if (IS_ERR(parent)) {
1423                                 CDEBUG(D_INODE, "Fail to find parent "DFID
1424                                        " for anonymous created %ld, try to"
1425                                        " use server-side parent.\n",
1426                                        PFID(rr->rr_fid1), PTR_ERR(parent));
1427                                 parent = NULL;
1428                         }
1429                 }
1430                 if (parent == NULL)
1431                         ma->ma_need |= MA_PFID;
1432         }
1433
1434         o = mdt_object_find(env, mdt, rr->rr_fid2);
1435         if (IS_ERR(o))
1436                 RETURN(rc = PTR_ERR(o));
1437
1438         if (mdt_object_remote(o)) {
1439                 CDEBUG(D_INFO, "%s: "DFID" is on remote MDT.\n",
1440                        mdt_obd_name(info->mti_mdt),
1441                        PFID(rr->rr_fid2));
1442                 GOTO(out, rc = -EREMOTE);
1443         } else if (!mdt_object_exists(o)) {
1444                 mdt_set_disposition(info, rep,
1445                                     DISP_IT_EXECD |
1446                                     DISP_LOOKUP_EXECD |
1447                                     DISP_LOOKUP_NEG);
1448                 GOTO(out, rc = -ENOENT);
1449         }
1450
1451         mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1452
1453         if (flags & MDS_OPEN_RELEASE)
1454                 ma->ma_need |= MA_HSM;
1455         rc = mdt_attr_get_complex(info, o, ma);
1456         if (rc)
1457                 GOTO(out, rc);
1458
1459         /* If a release request, check file flags are fine and ask for an
1460          * exclusive open access. */
1461         if (flags & MDS_OPEN_RELEASE && !mdt_hsm_release_allow(ma))
1462                 GOTO(out, rc = -EPERM);
1463
1464         rc = mdt_object_open_lock(info, o, lhc, &ibits);
1465         if (rc)
1466                 GOTO(out_unlock, rc);
1467
1468         if (ma->ma_valid & MA_PFID) {
1469                 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1470                 if (IS_ERR(parent)) {
1471                         CDEBUG(D_INODE, "Fail to find parent "DFID
1472                                " for anonymous created %ld, try to"
1473                                " use system default.\n",
1474                                PFID(&ma->ma_pfid), PTR_ERR(parent));
1475                         parent = NULL;
1476                 }
1477         }
1478
1479         rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1480         if (!rc) {
1481                 mdt_set_disposition(info, rep, DISP_LOOKUP_POS);
1482                 if (flags & MDS_OPEN_LOCK)
1483                         mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1484                 if (flags & MDS_OPEN_LEASE)
1485                         mdt_set_disposition(info, rep, DISP_OPEN_LEASE);
1486         }
1487         GOTO(out_unlock, rc);
1488
1489 out_unlock:
1490         mdt_object_open_unlock(info, o, lhc, ibits, rc);
1491 out:
1492         mdt_object_put(env, o);
1493         if (parent != NULL)
1494                 mdt_object_put(env, parent);
1495         return rc;
1496 }
1497
1498 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1499 static int mdt_cross_open(struct mdt_thread_info *info,
1500                           const struct lu_fid *parent_fid,
1501                           const struct lu_fid *fid,
1502                           struct ldlm_reply *rep, __u32 flags)
1503 {
1504         struct md_attr    *ma = &info->mti_attr;
1505         struct mdt_object *o;
1506         int                rc;
1507         ENTRY;
1508
1509         o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1510         if (IS_ERR(o))
1511                 RETURN(rc = PTR_ERR(o));
1512
1513         if (mdt_object_remote(o)) {
1514                 /* Something is wrong here, the object is on another MDS! */
1515                 CERROR("%s: "DFID" isn't on this server!: rc = %d\n",
1516                        mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1517                 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1518                                 &o->mot_obj,
1519                                 "Object isn't on this server! FLD error?\n");
1520                 rc = -EFAULT;
1521         } else {
1522                 if (mdt_object_exists(o)) {
1523                         /* Do permission check for cross-open. */
1524                         rc = mo_permission(info->mti_env, NULL,
1525                                            mdt_object_child(o),
1526                                            NULL, flags | MDS_OPEN_CROSS);
1527                         if (rc)
1528                                 goto out;
1529
1530                         mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
1531                         rc = mdt_attr_get_complex(info, o, ma);
1532                         if (rc != 0)
1533                                 GOTO(out, rc);
1534
1535                         /* Do not create lov object if the fid is opened
1536                          * under OBF */
1537                         if (S_ISREG(ma->ma_attr.la_mode) &&
1538                             !(ma->ma_valid & MA_LOV) && (flags & FMODE_WRITE) &&
1539                             fid_is_obf(parent_fid))
1540                                 GOTO(out, rc = -EPERM);
1541
1542                         rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1543                 } else {
1544                         /*
1545                          * Something is wrong here. lookup was positive but
1546                          * there is no object!
1547                          */
1548                         CERROR("%s: "DFID" doesn't exist!: rc = %d\n",
1549                               mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1550                         rc = -EFAULT;
1551                 }
1552         }
1553 out:
1554         mdt_object_put(info->mti_env, o);
1555         RETURN(rc);
1556 }
1557
1558 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1559 {
1560         struct mdt_device       *mdt = info->mti_mdt;
1561         struct ptlrpc_request   *req = mdt_info_req(info);
1562         struct mdt_object       *parent;
1563         struct mdt_object       *child;
1564         struct mdt_lock_handle  *lh;
1565         struct ldlm_reply       *ldlm_rep;
1566         struct mdt_body         *repbody;
1567         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
1568         struct md_attr          *ma = &info->mti_attr;
1569         __u64                    create_flags = info->mti_spec.sp_cr_flags;
1570         __u64                    ibits = 0;
1571         struct mdt_reint_record *rr = &info->mti_rr;
1572         struct lu_name          *lname;
1573         int                      result, rc;
1574         int                      created = 0;
1575         __u32                    msg_flags;
1576         ENTRY;
1577
1578         OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1579                                (obd_timeout + 1) / 4);
1580
1581         mdt_counter_incr(req, LPROC_MDT_OPEN);
1582         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1583
1584         ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
1585         ma->ma_lmm_size = req_capsule_get_size(info->mti_pill, &RMF_MDT_MD,
1586                                                RCL_SERVER);
1587         ma->ma_need = MA_INODE;
1588         if (ma->ma_lmm_size > 0)
1589                 ma->ma_need |= MA_LOV;
1590
1591         ma->ma_valid = 0;
1592
1593         LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1594         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1595
1596         if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1597                 CERROR("file join is not supported anymore.\n");
1598                 GOTO(out, result = err_serious(-EOPNOTSUPP));
1599         }
1600         msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1601
1602         if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1603             info->mti_spec.u.sp_ea.eadata == NULL)
1604                 GOTO(out, result = err_serious(-EINVAL));
1605
1606         CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
1607                "cr_flag="LPO64" mode=0%06o msg_flag=0x%x\n",
1608                PFID(rr->rr_fid1), rr->rr_name,
1609                PFID(rr->rr_fid2), create_flags,
1610                ma->ma_attr.la_mode, msg_flags);
1611         if (info->mti_cross_ref) {
1612                 /* This is cross-ref open */
1613                 mdt_set_disposition(info, ldlm_rep,
1614                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD |
1615                              DISP_LOOKUP_POS));
1616                 result = mdt_cross_open(info, rr->rr_fid2, rr->rr_fid1,
1617                                         ldlm_rep, create_flags);
1618                 GOTO(out, result);
1619         } else if (req_is_replay(req) ||
1620             (req->rq_export->exp_libclient && create_flags & MDS_OPEN_HAS_EA)) {
1621                 /* This is a replay request or from liblustre with ea. */
1622                 result = mdt_open_by_fid(info, ldlm_rep);
1623
1624                 if (result != -ENOENT) {
1625                         if (req->rq_export->exp_libclient &&
1626                             create_flags & MDS_OPEN_HAS_EA)
1627                                 GOTO(out, result = 0);
1628                         GOTO(out, result);
1629                 }
1630                 /* We didn't find the correct object, so we need to re-create it
1631                  * via a regular replay. */
1632                 if (!(create_flags & MDS_OPEN_CREAT)) {
1633                         DEBUG_REQ(D_ERROR, req,
1634                                   "OPEN & CREAT not in open replay/by_fid.");
1635                         GOTO(out, result = -EFAULT);
1636                 }
1637                 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1638         } else if ((rr->rr_namelen == 0 && create_flags & MDS_OPEN_LOCK) ||
1639                    (create_flags & MDS_OPEN_BY_FID)) {
1640                 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1641                 /* If result is 0 then open by FID has found the file
1642                  * and there is nothing left for us to do here.  More
1643                  * generally if it is anything other than -ENOENT or
1644                  * -EREMOTE then we return that now.  If -ENOENT and
1645                  * MDS_OPEN_CREAT is set then we must create the file
1646                  * below.  If -EREMOTE then we need to return a LOOKUP
1647                  * lock to the client, which we do below.  Hence this
1648                  * odd looking condition.  See LU-2523. */
1649                 if (!(result == -ENOENT && (create_flags & MDS_OPEN_CREAT)) &&
1650                     result != -EREMOTE)
1651                         GOTO(out, result);
1652
1653                 if (unlikely(rr->rr_namelen == 0))
1654                         GOTO(out, result = -EINVAL);
1655
1656                 CDEBUG(D_INFO, "No object(2), continue as regular open.\n");
1657         }
1658
1659         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1660                 GOTO(out, result = err_serious(-ENOMEM));
1661
1662         mdt_set_disposition(info, ldlm_rep,
1663                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1664
1665         lh = &info->mti_lh[MDT_LH_PARENT];
1666         mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
1667                           LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
1668
1669         parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1670                                       MDS_INODELOCK_UPDATE);
1671         if (IS_ERR(parent))
1672                 GOTO(out, result = PTR_ERR(parent));
1673
1674         /* get and check version of parent */
1675         result = mdt_version_get_check(info, parent, 0);
1676         if (result)
1677                 GOTO(out_parent, result);
1678
1679         fid_zero(child_fid);
1680
1681         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1682         result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1683                             lname, child_fid, &info->mti_spec);
1684         LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1685                  "looking for "DFID"/%s, result fid="DFID"\n",
1686                  PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
1687
1688         if (result != 0 && result != -ENOENT && result != -ESTALE)
1689                 GOTO(out_parent, result);
1690
1691         if (result == -ENOENT || result == -ESTALE) {
1692                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1693                 if (result == -ESTALE) {
1694                         /*
1695                          * -ESTALE means the parent is a dead(unlinked) dir, so
1696                          * it should return -ENOENT to in accordance with the
1697                          * original mds implementaion.
1698                          */
1699                         GOTO(out_parent, result = -ENOENT);
1700                 }
1701                 if (!(create_flags & MDS_OPEN_CREAT))
1702                         GOTO(out_parent, result);
1703                 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
1704                         GOTO(out_parent, result = -EROFS);
1705                 *child_fid = *info->mti_rr.rr_fid2;
1706                 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1707                          PFID(child_fid));
1708                 /* In the function below, .hs_keycmp resolves to
1709                  * lu_obj_hop_keycmp() */
1710                 /* coverity[overrun-buffer-val] */
1711                 child = mdt_object_new(info->mti_env, mdt, child_fid);
1712         } else {
1713                 /*
1714                  * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1715                  * return FID back in that case.
1716                  */
1717                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1718                 child = mdt_object_find(info->mti_env, mdt, child_fid);
1719         }
1720         if (IS_ERR(child))
1721                 GOTO(out_parent, result = PTR_ERR(child));
1722
1723         /** check version of child  */
1724         rc = mdt_version_get_check(info, child, 1);
1725         if (rc)
1726                 GOTO(out_child, result = rc);
1727
1728         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1729         if (result == -ENOENT) {
1730                 /* Create under OBF and .lustre is not permitted */
1731                 if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1))
1732                         GOTO(out_child, result = -EPERM);
1733
1734                 /* save versions in reply */
1735                 mdt_version_get_save(info, parent, 0);
1736                 mdt_version_get_save(info, child, 1);
1737
1738                 /* version of child will be changed */
1739                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
1740
1741                 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1742                 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1743
1744                 /* Let lower layers know what is lock mode on directory. */
1745                 info->mti_spec.sp_cr_mode =
1746                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1747
1748                 /*
1749                  * Do not perform lookup sanity check. We know that name does
1750                  * not exist.
1751                  */
1752                 info->mti_spec.sp_cr_lookup = 0;
1753                 info->mti_spec.sp_feat = &dt_directory_features;
1754
1755                 result = mdo_create(info->mti_env,
1756                                     mdt_object_child(parent),
1757                                     lname,
1758                                     mdt_object_child(child),
1759                                     &info->mti_spec,
1760                                     &info->mti_attr);
1761                 if (result == -ERESTART) {
1762                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1763                         GOTO(out_child, result);
1764                 } else {
1765
1766                         /* XXX: we should call this once, see few lines below */
1767                         if (result == 0)
1768                                 result = mdt_attr_get_complex(info, child, ma);
1769
1770                         if (result != 0)
1771                                 GOTO(out_child, result);
1772                 }
1773                 created = 1;
1774         } else {
1775                 /*
1776                  * The object is on remote node, return its FID for remote open.
1777                  */
1778                 if (mdt_object_remote(child)) {
1779                         /*
1780                          * Check if this lock already was sent to client and
1781                          * this is resent case. For resent case do not take lock
1782                          * again, use what is already granted.
1783                          */
1784                         LASSERT(lhc != NULL);
1785
1786                         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1787                                 struct ldlm_lock *lock;
1788
1789                                 LASSERT(msg_flags & MSG_RESENT);
1790
1791                                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1792                                 if (!lock) {
1793                                         CERROR("Invalid lock handle "LPX64"\n",
1794                                                lhc->mlh_reg_lh.cookie);
1795                                         LBUG();
1796                                 }
1797                                 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1798                                                         &lock->l_resource->lr_name));
1799                                 LDLM_LOCK_PUT(lock);
1800                                 rc = 0;
1801                         } else {
1802                                 mdt_lock_handle_init(lhc);
1803                                 mdt_lock_reg_init(lhc, LCK_PR);
1804
1805                                 rc = mdt_object_lock(info, child, lhc,
1806                                                      MDS_INODELOCK_LOOKUP,
1807                                                      MDT_CROSS_LOCK);
1808                         }
1809                         repbody->fid1 = *mdt_object_fid(child);
1810                         repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1811                         if (rc != 0)
1812                                 result = rc;
1813                         else
1814                                 result = -EREMOTE;
1815                         GOTO(out_child, result);
1816                 } else {
1817                         if (mdt_object_exists(child)) {
1818                                 /* We have to get attr & LOV EA & HSM for this
1819                                  * object */
1820                                 ma->ma_need |= MA_HSM;
1821                                 result = mdt_attr_get_complex(info, child, ma);
1822                         } else {
1823                                 /*object non-exist!!! Likely an fs corruption*/
1824                                 CERROR("%s: name %s present, but fid " DFID
1825                                        " invalid\n",mdt_obd_name(info->mti_mdt),
1826                                        rr->rr_name, PFID(child_fid));
1827                                 GOTO(out_child, result = -EIO);
1828                         }
1829                 }
1830         }
1831
1832         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1833                 /* the open lock might already be gotten in
1834                  * mdt_intent_fixup_resent */
1835                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1836                 if (create_flags & MDS_OPEN_LOCK)
1837                         mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1838         } else {
1839                 /* get openlock if this isn't replay and client requested it */
1840                 if (!req_is_replay(req)) {
1841                         rc = mdt_object_open_lock(info, child, lhc, &ibits);
1842                         if (rc != 0)
1843                                 GOTO(out_child_unlock, result = rc);
1844                         else if (create_flags & MDS_OPEN_LOCK)
1845                                 mdt_set_disposition(info, ldlm_rep,
1846                                                     DISP_OPEN_LOCK);
1847                 }
1848         }
1849         /* Try to open it now. */
1850         rc = mdt_finish_open(info, parent, child, create_flags,
1851                              created, ldlm_rep);
1852         if (rc) {
1853                 result = rc;
1854                 /* openlock will be released if mdt_finish_open failed */
1855                 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1856
1857                 if (created && create_flags & MDS_OPEN_VOLATILE) {
1858                         CERROR("%s: cannot open volatile file "DFID", orphan "
1859                                "file will be left in PENDING directory until "
1860                                "next reboot, rc = %d\n", mdt_obd_name(mdt),
1861                                PFID(mdt_object_fid(child)), rc);
1862                         GOTO(out_child_unlock, result);
1863                 }
1864
1865                 if (created) {
1866                         ma->ma_need = 0;
1867                         ma->ma_valid = 0;
1868                         ma->ma_cookie_size = 0;
1869                         rc = mdo_unlink(info->mti_env,
1870                                         mdt_object_child(parent),
1871                                         mdt_object_child(child),
1872                                         lname,
1873                                         &info->mti_attr, 0);
1874                         if (rc != 0)
1875                                 CERROR("%s: "DFID" cleanup of open: rc = %d\n",
1876                                        mdt_obd_name(info->mti_mdt),
1877                                        PFID(mdt_object_fid(child)), rc);
1878                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1879                 }
1880         }
1881         EXIT;
1882 out_child_unlock:
1883         mdt_object_open_unlock(info, child, lhc, ibits, result);
1884 out_child:
1885         mdt_object_put(info->mti_env, child);
1886 out_parent:
1887         mdt_object_unlock_put(info, parent, lh, result || !created);
1888 out:
1889         if (result)
1890                 lustre_msg_set_transno(req->rq_repmsg, 0);
1891         return result;
1892 }
1893
1894 /**
1895  * Create an orphan object use local root.
1896  */
1897 static struct mdt_object *mdt_orphan_open(struct mdt_thread_info *info,
1898                                           struct mdt_device *mdt,
1899                                           const struct lu_fid *fid,
1900                                           struct md_attr *attr, fmode_t fmode)
1901 {
1902         const struct lu_env *env = info->mti_env;
1903         struct md_op_spec *spec = &info->mti_spec;
1904         struct lu_fid *local_root_fid = &info->mti_tmp_fid1;
1905         struct mdt_object *obj = NULL;
1906         struct mdt_object *local_root;
1907         static const char name[] = "i_am_nobody";
1908         struct lu_name *lname;
1909         struct lu_ucred *uc;
1910         cfs_cap_t uc_cap_save;
1911         int rc;
1912         ENTRY;
1913
1914         rc = dt_root_get(env, mdt->mdt_bottom, local_root_fid);
1915         if (rc != 0)
1916                 RETURN(ERR_PTR(rc));
1917
1918         local_root = mdt_object_find(env, mdt, local_root_fid);
1919         if (IS_ERR(local_root))
1920                 RETURN(local_root);
1921
1922         obj = mdt_object_new(env, mdt, fid);
1923         if (IS_ERR(obj))
1924                 GOTO(out, rc = PTR_ERR(obj));
1925
1926         spec->sp_cr_lookup = 0;
1927         spec->sp_feat = &dt_directory_features;
1928         spec->sp_cr_mode = MDL_MINMODE; /* no lock */
1929         spec->sp_cr_flags = MDS_OPEN_VOLATILE | fmode;
1930         if (attr->ma_valid & MA_LOV) {
1931                 spec->u.sp_ea.eadata = attr->ma_lmm;
1932                 spec->u.sp_ea.eadatalen = attr->ma_lmm_size;
1933                 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1934         } else {
1935                 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
1936         }
1937
1938         lname = mdt_name(env, (char *)name, sizeof(name) - 1);
1939
1940         uc = lu_ucred(env);
1941         uc_cap_save = uc->uc_cap;
1942         uc->uc_cap |= 1 << CFS_CAP_DAC_OVERRIDE;
1943         rc = mdo_create(env, mdt_object_child(local_root), lname,
1944                         mdt_object_child(obj), spec, attr);
1945         uc->uc_cap = uc_cap_save;
1946         if (rc < 0) {
1947                 CERROR("%s: cannot create volatile file "DFID": rc = %d\n",
1948                        mdt_obd_name(mdt), PFID(fid), rc);
1949                 GOTO(out, rc);
1950         }
1951
1952         rc = mo_open(env, mdt_object_child(obj), MDS_OPEN_CREATED);
1953         if (rc < 0)
1954                 CERROR("%s: cannot open volatile file "DFID", orphan "
1955                        "file will be left in PENDING directory until "
1956                        "next reboot, rc = %d\n", mdt_obd_name(mdt),
1957                        PFID(fid), rc);
1958         GOTO(out, rc);
1959
1960 out:
1961         if (rc < 0) {
1962                 if (!IS_ERR(obj))
1963                         mdt_object_put(env, obj);
1964                 obj = ERR_PTR(rc);
1965         }
1966         mdt_object_put(env, local_root);
1967         return obj;
1968 }
1969
1970 static int mdt_hsm_release(struct mdt_thread_info *info, struct mdt_object *o,
1971                            struct md_attr *ma)
1972 {
1973         struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LAYOUT];
1974         struct close_data      *data;
1975         struct ldlm_lock       *lease;
1976         struct mdt_object      *orphan;
1977         struct md_attr         *orp_ma;
1978         struct lu_buf          *buf;
1979         bool                    lease_broken;
1980         int                     rc;
1981         int                     rc2;
1982         ENTRY;
1983
1984         if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
1985                 RETURN(-EROFS);
1986
1987         data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1988         if (data == NULL)
1989                 RETURN(-EPROTO);
1990
1991         lease = ldlm_handle2lock(&data->cd_handle);
1992         if (lease == NULL)
1993                 RETURN(-ESTALE);
1994
1995         /* try to hold open_sem so that nobody else can open the file */
1996         if (!down_write_trylock(&o->mot_open_sem)) {
1997                 ldlm_lock_cancel(lease);
1998                 GOTO(out_reprocess, rc = -EBUSY);
1999         }
2000
2001         /* Check if the lease open lease has already canceled */
2002         lock_res_and_lock(lease);
2003         lease_broken = ldlm_is_cancel(lease);
2004         unlock_res_and_lock(lease);
2005
2006         LDLM_DEBUG(lease, DFID " lease broken? %d\n",
2007                    PFID(mdt_object_fid(o)), lease_broken);
2008
2009         /* Cancel server side lease. Client side counterpart should
2010          * have been cancelled. It's okay to cancel it now as we've
2011          * held mot_open_sem. */
2012         ldlm_lock_cancel(lease);
2013
2014         if (lease_broken) /* don't perform release task */
2015                 GOTO(out_unlock, rc = -ESTALE);
2016
2017         if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
2018                 GOTO(out_unlock, rc = -EINVAL);
2019
2020         /* ma_need was set before but it seems fine to change it in order to
2021          * avoid modifying the one from RPC */
2022         ma->ma_need = MA_HSM;
2023         rc = mdt_attr_get_complex(info, o, ma);
2024         if (rc != 0)
2025                 GOTO(out_unlock, rc);
2026
2027         if (!mdt_hsm_release_allow(ma))
2028                 GOTO(out_unlock, rc = -EPERM);
2029
2030         /* already released? */
2031         if (ma->ma_hsm.mh_flags & HS_RELEASED)
2032                 GOTO(out_unlock, rc = 0);
2033
2034         /* Compare on-disk and packed data_version */
2035         if (data->cd_data_version != ma->ma_hsm.mh_arch_ver) {
2036                 CDEBUG(D_HSM, DFID" data_version mismatches: packed="LPU64
2037                        " and on-disk="LPU64"\n", PFID(mdt_object_fid(o)),
2038                        data->cd_data_version, ma->ma_hsm.mh_arch_ver);
2039                 GOTO(out_unlock, rc = -EPERM);
2040         }
2041
2042         ma->ma_valid = MA_INODE;
2043         ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME | LA_SIZE;
2044         rc = mo_attr_set(info->mti_env, mdt_object_child(o), ma);
2045         if (rc < 0)
2046                 GOTO(out_unlock, rc);
2047
2048         ma->ma_need = MA_INODE | MA_LOV;
2049         rc = mdt_attr_get_complex(info, o, ma);
2050         if (rc < 0)
2051                 GOTO(out_unlock, rc);
2052
2053         if (!(ma->ma_valid & MA_LOV)) {
2054                 /* Even empty file are released */
2055                 memset(ma->ma_lmm, 0, sizeof(*ma->ma_lmm));
2056                 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
2057                 ma->ma_lmm->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
2058                 ma->ma_lmm->lmm_stripe_size = cpu_to_le32(LOV_MIN_STRIPE_SIZE);
2059                 ma->ma_lmm_size = sizeof(*ma->ma_lmm);
2060         } else {
2061                 /* Magic must be LOV_MAGIC_Vx_DEF otherwise LOD will interpret
2062                  * ma_lmm as lov_user_md, then it will be confused by union of
2063                  * layout_gen and stripe_offset. */
2064                 if (le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V1)
2065                         ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
2066                 else if (le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V3)
2067                         ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V3_DEF);
2068                 else
2069                         GOTO(out_unlock, rc = -EINVAL);
2070         }
2071
2072         /* Set file as released */
2073         ma->ma_lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
2074
2075         /* Hopefully it's not used in this call path */
2076         orp_ma = &info->mti_u.som.attr;
2077         orp_ma->ma_attr.la_mode = S_IFREG | S_IWUSR;
2078         orp_ma->ma_attr.la_uid = ma->ma_attr.la_uid;
2079         orp_ma->ma_attr.la_gid = ma->ma_attr.la_gid;
2080         orp_ma->ma_attr.la_valid = LA_MODE | LA_UID | LA_GID;
2081         orp_ma->ma_lmm = ma->ma_lmm;
2082         orp_ma->ma_lmm_size = ma->ma_lmm_size;
2083         orp_ma->ma_valid = MA_INODE | MA_LOV;
2084         orphan = mdt_orphan_open(info, info->mti_mdt, &data->cd_fid, orp_ma,
2085                                  FMODE_WRITE);
2086         if (IS_ERR(orphan)) {
2087                 CERROR("%s: cannot open orphan file "DFID": rc = %ld\n",
2088                        mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid),
2089                        PTR_ERR(orphan));
2090                 GOTO(out_unlock, rc = PTR_ERR(orphan));
2091         }
2092
2093         /* Set up HSM attribute for orphan object */
2094         CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
2095         buf = &info->mti_buf;
2096         buf->lb_buf = info->mti_xattr_buf;
2097         buf->lb_len = sizeof(struct hsm_attrs);
2098         ma->ma_hsm.mh_flags |= HS_RELEASED;
2099         lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
2100         ma->ma_hsm.mh_flags &= ~HS_RELEASED;
2101
2102         mdt_lock_reg_init(lh, LCK_EX);
2103         rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_LAYOUT |
2104                              MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
2105         if (rc != 0)
2106                 GOTO(out_close, rc);
2107
2108         rc = mo_xattr_set(info->mti_env, mdt_object_child(orphan), buf,
2109                           XATTR_NAME_HSM, 0);
2110
2111         if (rc == 0)
2112                 /* Swap layout with orphan object */
2113                 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o),
2114                                      mdt_object_child(orphan),
2115                                      SWAP_LAYOUTS_MDS_HSM);
2116
2117         /* Release exclusive LL */
2118         mdt_object_unlock(info, o, lh, 1);
2119
2120         EXIT;
2121
2122 out_close:
2123         /* Close orphan object anyway */
2124         rc2 = mo_close(info->mti_env, mdt_object_child(orphan), orp_ma,
2125                        FMODE_WRITE);
2126         if (rc2 < 0)
2127                 CERROR("%s: error closing volatile file "DFID": rc = %d\n",
2128                        mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid), rc2);
2129         LU_OBJECT_DEBUG(D_HSM, info->mti_env, &orphan->mot_obj,
2130                         "object closed\n");
2131         mdt_object_put(info->mti_env, orphan);
2132
2133 out_unlock:
2134         up_write(&o->mot_open_sem);
2135
2136         if (rc == 0) { /* already released */
2137                 struct mdt_body *repbody;
2138                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2139                 LASSERT(repbody != NULL);
2140                 repbody->valid |= OBD_MD_FLRELEASED;
2141         }
2142
2143 out_reprocess:
2144         ldlm_reprocess_all(lease->l_resource);
2145         LDLM_LOCK_PUT(lease);
2146
2147         ma->ma_valid = 0;
2148         ma->ma_need = 0;
2149
2150         return rc;
2151 }
2152
2153 #define MFD_CLOSED(mode) (((mode) & ~(MDS_FMODE_EPOCH | MDS_FMODE_SOM | \
2154                                       MDS_FMODE_TRUNC)) == MDS_FMODE_CLOSED)
2155
2156 static int mdt_mfd_closed(struct mdt_file_data *mfd)
2157 {
2158         return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
2159 }
2160
2161 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
2162 {
2163         struct mdt_object *o = mfd->mfd_object;
2164         struct md_object *next = mdt_object_child(o);
2165         struct md_attr *ma = &info->mti_attr;
2166         int ret = MDT_IOEPOCH_CLOSED;
2167         int rc = 0;
2168         __u64 mode;
2169         ENTRY;
2170
2171         mode = mfd->mfd_mode;
2172
2173         if (ma->ma_attr_flags & MDS_HSM_RELEASE) {
2174                 rc = mdt_hsm_release(info, o, ma);
2175                 if (rc < 0) {
2176                         CDEBUG(D_HSM, "%s: File " DFID " release failed: %d\n",
2177                                 mdt_obd_name(info->mti_mdt),
2178                                 PFID(mdt_object_fid(o)), rc);
2179                         /* continue to close even error occurred. */
2180                 }
2181         }
2182
2183         if ((mode & FMODE_WRITE) || (mode & MDS_FMODE_TRUNC)) {
2184                 mdt_write_put(o);
2185                 ret = mdt_ioepoch_close(info, o);
2186         } else if (mode & MDS_FMODE_EXEC) {
2187                 mdt_write_allow(o);
2188         } else if (mode & MDS_FMODE_EPOCH) {
2189                 ret = mdt_ioepoch_close(info, o);
2190         } else if (mode & MDS_FMODE_SOM) {
2191                 ret = mdt_som_au_close(info, o);
2192         }
2193
2194         /* Update atime on close only. */
2195         if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
2196             && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
2197                 /* Set the atime only. */
2198                 ma->ma_valid = MA_INODE;
2199                 ma->ma_attr.la_valid = LA_ATIME;
2200                 rc = mo_attr_set(info->mti_env, next, ma);
2201         }
2202
2203         /* If file data is modified, add the dirty flag. */
2204         if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
2205                 rc = mdt_add_dirty_flag(info, o, ma);
2206
2207         ma->ma_need |= MA_INODE;
2208         ma->ma_valid &= ~MA_INODE;
2209
2210         if (!MFD_CLOSED(mode))
2211                 rc = mo_close(info->mti_env, next, ma, mode);
2212
2213         if (ret == MDT_IOEPOCH_GETATTR || ret == MDT_IOEPOCH_OPENED) {
2214                 struct mdt_export_data *med;
2215
2216                 /* The IOepoch is still opened or SOM update is needed.
2217                  * Put mfd back into the list. */
2218                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
2219                 mdt_mfd_set_mode(mfd, ret == MDT_IOEPOCH_OPENED ?
2220                                       MDS_FMODE_EPOCH : MDS_FMODE_SOM);
2221
2222                 LASSERT(mdt_info_req(info));
2223                 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
2224                 spin_lock(&med->med_open_lock);
2225                 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
2226                 class_handle_hash_back(&mfd->mfd_handle);
2227                 spin_unlock(&med->med_open_lock);
2228
2229                 if (ret == MDT_IOEPOCH_OPENED) {
2230                         ret = 0;
2231                 } else {
2232                         ret = -EAGAIN;
2233                         CDEBUG(D_INODE, "Size-on-MDS attribute update is "
2234                                "needed on "DFID"\n", PFID(mdt_object_fid(o)));
2235                 }
2236         } else {
2237                 /* adjust open and lease count */
2238                 if (mode & MDS_OPEN_LEASE) {
2239                         LASSERT(atomic_read(&o->mot_lease_count) > 0);
2240                         atomic_dec(&o->mot_lease_count);
2241                 }
2242                 LASSERT(atomic_read(&o->mot_open_count) > 0);
2243                 atomic_dec(&o->mot_open_count);
2244
2245                 mdt_mfd_free(mfd);
2246                 mdt_object_put(info->mti_env, o);
2247         }
2248
2249         RETURN(rc ? rc : ret);
2250 }
2251
2252 int mdt_close(struct tgt_session_info *tsi)
2253 {
2254         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
2255         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2256         struct mdt_export_data *med;
2257         struct mdt_file_data   *mfd;
2258         struct mdt_object      *o;
2259         struct md_attr         *ma = &info->mti_attr;
2260         struct mdt_body        *repbody = NULL;
2261         int rc, ret = 0;
2262         ENTRY;
2263
2264         mdt_counter_incr(req, LPROC_MDT_CLOSE);
2265         /* Close may come with the Size-on-MDS update. Unpack it. */
2266         rc = mdt_close_unpack(info);
2267         if (rc)
2268                 GOTO(out, rc = err_serious(rc));
2269
2270         LASSERT(info->mti_ioepoch);
2271
2272         req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
2273                              info->mti_mdt->mdt_max_mdsize);
2274         req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
2275                              info->mti_mdt->mdt_max_cookiesize);
2276         rc = req_capsule_server_pack(info->mti_pill);
2277         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
2278                 mdt_client_compatibility(info);
2279                 if (rc == 0)
2280                         mdt_fix_reply(info);
2281                 mdt_exit_ucred(info);
2282                 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2283         }
2284
2285         /* Continue to close handle even if we can not pack reply */
2286         if (rc == 0) {
2287                 repbody = req_capsule_server_get(info->mti_pill,
2288                                                  &RMF_MDT_BODY);
2289                 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
2290                                                     &RMF_MDT_MD);
2291                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
2292                                                        &RMF_MDT_MD,
2293                                                        RCL_SERVER);
2294                 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
2295                                                        &RMF_LOGCOOKIES);
2296                 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
2297                                                           &RMF_LOGCOOKIES,
2298                                                           RCL_SERVER);
2299                 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
2300                 repbody->eadatasize = 0;
2301                 repbody->aclsize = 0;
2302         } else {
2303                 rc = err_serious(rc);
2304         }
2305
2306         med = &req->rq_export->exp_mdt_data;
2307         spin_lock(&med->med_open_lock);
2308         mfd = mdt_handle2mfd(med, &info->mti_ioepoch->handle,
2309                              req_is_replay(req));
2310         if (mdt_mfd_closed(mfd)) {
2311                 spin_unlock(&med->med_open_lock);
2312                 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
2313                        ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
2314                        info->mti_ioepoch->handle.cookie);
2315                 /** not serious error since bug 3633 */
2316                 rc = -ESTALE;
2317         } else {
2318                 class_handle_unhash(&mfd->mfd_handle);
2319                 cfs_list_del_init(&mfd->mfd_list);
2320                 spin_unlock(&med->med_open_lock);
2321
2322                 /* Do not lose object before last unlink. */
2323                 o = mfd->mfd_object;
2324                 mdt_object_get(info->mti_env, o);
2325                 ret = mdt_mfd_close(info, mfd);
2326                 if (repbody != NULL)
2327                         rc = mdt_handle_last_unlink(info, o, ma);
2328                 mdt_empty_transno(info, rc);
2329                 mdt_object_put(info->mti_env, o);
2330         }
2331         if (repbody != NULL) {
2332                 mdt_client_compatibility(info);
2333                 rc = mdt_fix_reply(info);
2334         }
2335
2336         mdt_exit_ucred(info);
2337         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
2338                 GOTO(out, rc = err_serious(-ENOMEM));
2339
2340         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
2341                                  OBD_FAIL_MDS_CLOSE_NET_REP))
2342                 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
2343 out:
2344         mdt_thread_info_fini(info);
2345         RETURN(rc ? rc : ret);
2346 }
2347
2348 /**
2349  * DONE_WRITING rpc handler.
2350  *
2351  * As mfd is not kept after replayed CLOSE (see mdt_ioepoch_close_on_replay()),
2352  * only those DONE_WRITING rpc will be replayed which really wrote smth on disk,
2353  * and got a trasid. Waiting for such DONE_WRITING is not reliable, so just
2354  * skip attributes and reconstruct the reply here.
2355  */
2356 int mdt_done_writing(struct tgt_session_info *tsi)
2357 {
2358         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2359         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
2360         struct mdt_body         *repbody = NULL;
2361         struct mdt_export_data  *med;
2362         struct mdt_file_data    *mfd;
2363         int rc;
2364         ENTRY;
2365
2366         rc = req_capsule_server_pack(tsi->tsi_pill);
2367         if (rc)
2368                 GOTO(out, rc = err_serious(rc));
2369
2370         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2371         repbody->eadatasize = 0;
2372         repbody->aclsize = 0;
2373
2374         /* Done Writing may come with the Size-on-MDS update. Unpack it. */
2375         rc = mdt_close_unpack(info);
2376         if (rc)
2377                 GOTO(out, rc = err_serious(rc));
2378
2379         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
2380                 mdt_exit_ucred(info);
2381                 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2382         }
2383
2384         med = &info->mti_exp->exp_mdt_data;
2385         spin_lock(&med->med_open_lock);
2386         mfd = mdt_handle2mfd(med, &info->mti_ioepoch->handle,
2387                              req_is_replay(req));
2388         if (mfd == NULL) {
2389                 spin_unlock(&med->med_open_lock);
2390                 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
2391                        ": cookie = "LPX64" ioepoch = "LPU64"\n",
2392                        PFID(info->mti_rr.rr_fid1),
2393                        info->mti_ioepoch->handle.cookie,
2394                        info->mti_ioepoch->ioepoch);
2395                 /* If this is a replay, reconstruct the transno. */
2396                 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2397                         rc = info->mti_ioepoch->flags & MF_SOM_AU ?
2398                              -EAGAIN : 0;
2399                         mdt_empty_transno(info, rc);
2400                 } else
2401                         rc = -ESTALE;
2402                 GOTO(error_ucred, rc);
2403         }
2404
2405         LASSERT(mfd->mfd_mode == MDS_FMODE_EPOCH ||
2406                 mfd->mfd_mode == MDS_FMODE_TRUNC);
2407         class_handle_unhash(&mfd->mfd_handle);
2408         cfs_list_del_init(&mfd->mfd_list);
2409         spin_unlock(&med->med_open_lock);
2410
2411         /* Set EPOCH CLOSE flag if not set by client. */
2412         info->mti_ioepoch->flags |= MF_EPOCH_CLOSE;
2413         info->mti_attr.ma_valid = 0;
2414
2415         info->mti_attr.ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
2416         OBD_ALLOC_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
2417         if (info->mti_attr.ma_lmm == NULL)
2418                 GOTO(error_ucred, rc = -ENOMEM);
2419
2420         rc = mdt_mfd_close(info, mfd);
2421
2422         OBD_FREE_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
2423         mdt_empty_transno(info, rc);
2424 error_ucred:
2425         mdt_exit_ucred(info);
2426 out:
2427         mdt_thread_info_fini(info);
2428         RETURN(rc);
2429 }