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