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