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