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