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