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