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