Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros
[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_anon_by_fid(struct mdt_thread_info *info,
1078                          struct ldlm_reply *rep,
1079                          struct mdt_lock_handle *lhc)
1080 {
1081         const struct lu_env     *env   = info->mti_env;
1082         struct mdt_device       *mdt   = info->mti_mdt;
1083         __u32                    flags = info->mti_spec.sp_cr_flags;
1084         struct mdt_reint_record *rr    = &info->mti_rr;
1085         struct md_attr          *ma    = &info->mti_attr;
1086         struct mdt_object       *parent= NULL;
1087         struct mdt_object       *o;
1088         int                      rc;
1089         ldlm_mode_t              lm;
1090         ENTRY;
1091
1092         if (md_should_create(flags)) {
1093                 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1094                         parent = mdt_object_find(env, mdt, rr->rr_fid1);
1095                         if (IS_ERR(parent)) {
1096                                 CDEBUG(D_INODE, "Fail to find parent "DFID
1097                                        " for anonymous created %ld, try to"
1098                                        " use server-side parent.\n",
1099                                        PFID(rr->rr_fid1), PTR_ERR(parent));
1100                                 parent = NULL;
1101                         }
1102                 }
1103                 if (parent == NULL)
1104                         ma->ma_need |= MA_PFID;
1105         }
1106
1107         o = mdt_object_find(env, mdt, rr->rr_fid2);
1108         if (IS_ERR(o))
1109                 RETURN(rc = PTR_ERR(o));
1110
1111         rc = mdt_object_exists(o);
1112         if (rc == 0) {
1113                 mdt_set_disposition(info, rep, (DISP_LOOKUP_EXECD |
1114                                     DISP_LOOKUP_NEG));
1115                 GOTO(out, rc = -ENOENT);
1116         } else if (rc < 0) {
1117                 CERROR("NFS remote open shouldn't happen.\n");
1118                 GOTO(out, rc);
1119         }
1120         mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1121                                         DISP_LOOKUP_EXECD |
1122                                         DISP_LOOKUP_POS));
1123
1124         if (flags & FMODE_WRITE)
1125                 lm = LCK_CW;
1126         else if (flags & MDS_FMODE_EXEC)
1127                 lm = LCK_PR;
1128         else
1129                 lm = LCK_CR;
1130
1131         mdt_lock_handle_init(lhc);
1132         mdt_lock_reg_init(lhc, lm);
1133         rc = mdt_object_lock(info, o, lhc,
1134                              MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1135                              MDT_CROSS_LOCK);
1136         if (rc)
1137                 GOTO(out, rc);
1138
1139         rc = mo_attr_get(env, mdt_object_child(o), ma);
1140         if (rc)
1141                 GOTO(out, rc);
1142
1143         if (ma->ma_valid & MA_PFID) {
1144                 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1145                 if (IS_ERR(parent)) {
1146                         CDEBUG(D_INODE, "Fail to find parent "DFID
1147                                " for anonymous created %ld, try to"
1148                                " use system default.\n",
1149                                PFID(&ma->ma_pfid), PTR_ERR(parent));
1150                         parent = NULL;
1151                 }
1152         }
1153
1154         if (flags & MDS_OPEN_LOCK)
1155                 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1156         rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1157
1158         if (!(flags & MDS_OPEN_LOCK) || rc)
1159                 mdt_object_unlock(info, o, lhc, 1);
1160
1161         GOTO(out, rc);
1162 out:
1163         mdt_object_put(env, o);
1164         if (parent != NULL)
1165                 mdt_object_put(env, parent);
1166         return rc;
1167 }
1168
1169 int mdt_pin(struct mdt_thread_info* info)
1170 {
1171         ENTRY;
1172         RETURN(err_serious(-EOPNOTSUPP));
1173 }
1174
1175 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1176 int mdt_cross_open(struct mdt_thread_info* info,
1177                    const struct lu_fid *fid,
1178                    struct ldlm_reply *rep, __u32 flags)
1179 {
1180         struct md_attr    *ma = &info->mti_attr;
1181         struct mdt_object *o;
1182         int                rc;
1183         ENTRY;
1184
1185         o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1186         if (IS_ERR(o))
1187                 RETURN(rc = PTR_ERR(o));
1188
1189         rc = mdt_object_exists(o);
1190         if (rc > 0) {
1191                 /* Do permission check for cross-open. */
1192                 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o),
1193                                    NULL, flags | MDS_OPEN_CROSS);
1194                 if (rc)
1195                         goto out;
1196
1197                 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
1198                 rc = mo_attr_get(info->mti_env, mdt_object_child(o), ma);
1199                 if (rc == 0)
1200                         rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1201         } else if (rc == 0) {
1202                 /*
1203                  * Something is wrong here. lookup was positive but there is
1204                  * no object!
1205                  */
1206                 CERROR("Cross-ref object doesn't exist!\n");
1207                 rc = -EFAULT;
1208         } else  {
1209                 /* Something is wrong here, the object is on another MDS! */
1210                 CERROR("The object isn't on this server! FLD error?\n");
1211                 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1212                                 &o->mot_obj.mo_lu,
1213                                 "Object isn't on this server! FLD error?\n");
1214
1215                 rc = -EFAULT;
1216         }
1217
1218 out:
1219         mdt_object_put(info->mti_env, o);
1220         RETURN(rc);
1221 }
1222
1223 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1224 {
1225         struct mdt_device       *mdt = info->mti_mdt;
1226         struct ptlrpc_request   *req = mdt_info_req(info);
1227         struct mdt_object       *parent;
1228         struct mdt_object       *child;
1229         struct mdt_lock_handle  *lh;
1230         struct ldlm_reply       *ldlm_rep;
1231         struct mdt_body         *repbody;
1232         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
1233         struct md_attr          *ma = &info->mti_attr;
1234         __u64                    create_flags = info->mti_spec.sp_cr_flags;
1235         struct mdt_reint_record *rr = &info->mti_rr;
1236         struct lu_name          *lname;
1237         int                      result, rc;
1238         int                      created = 0;
1239         __u32                    msg_flags;
1240         ENTRY;
1241
1242         OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1243                                (obd_timeout + 1) / 4);
1244
1245         mdt_counter_incr(req, LPROC_MDT_OPEN);
1246         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1247
1248         ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
1249         ma->ma_lmm_size = req_capsule_get_size(info->mti_pill, &RMF_MDT_MD,
1250                                                RCL_SERVER);
1251         ma->ma_need = MA_INODE;
1252         if (ma->ma_lmm_size > 0)
1253                 ma->ma_need |= MA_LOV;
1254
1255         ma->ma_valid = 0;
1256
1257         LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1258         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1259
1260         if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1261                 CERROR("file join is not supported anymore.\n");
1262                 GOTO(out, result = err_serious(-EOPNOTSUPP));
1263         }
1264         msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1265
1266         if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1267             info->mti_spec.u.sp_ea.eadata == NULL)
1268                 GOTO(out, result = err_serious(-EINVAL));
1269
1270         CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
1271                "cr_flag="LPO64" mode=0%06o msg_flag=0x%x\n",
1272                PFID(rr->rr_fid1), rr->rr_name,
1273                PFID(rr->rr_fid2), create_flags,
1274                ma->ma_attr.la_mode, msg_flags);
1275
1276         if (req_is_replay(req) ||
1277             (req->rq_export->exp_libclient && create_flags&MDS_OPEN_HAS_EA)) {
1278                 /* This is a replay request or from liblustre with ea. */
1279                 result = mdt_open_by_fid(info, ldlm_rep);
1280
1281                 if (result != -ENOENT) {
1282                         if (req->rq_export->exp_libclient &&
1283                             create_flags & MDS_OPEN_HAS_EA)
1284                                 GOTO(out, result = 0);
1285                         GOTO(out, result);
1286                 }
1287                 /*
1288                  * We didn't find the correct object, so we need to re-create it
1289                  * via a regular replay.
1290                  */
1291                 if (!(create_flags & MDS_OPEN_CREAT)) {
1292                         DEBUG_REQ(D_ERROR, req,
1293                                   "OPEN & CREAT not in open replay.");
1294                         GOTO(out, result = -EFAULT);
1295                 }
1296                 CDEBUG(D_INFO, "Open replay did find object, continue as "
1297                        "regular open\n");
1298         } else if (rr->rr_namelen == 0 && !info->mti_cross_ref &&
1299                    create_flags & MDS_OPEN_LOCK) {
1300                 result = mdt_open_anon_by_fid(info, ldlm_rep, lhc);
1301                 GOTO(out, result);
1302         }
1303
1304         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1305                 GOTO(out, result = err_serious(-ENOMEM));
1306
1307         mdt_set_disposition(info, ldlm_rep,
1308                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1309
1310         if (info->mti_cross_ref) {
1311                 /* This is cross-ref open */
1312                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1313                 result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
1314                                         create_flags);
1315                 GOTO(out, result);
1316         }
1317
1318         lh = &info->mti_lh[MDT_LH_PARENT];
1319         mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
1320                           LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
1321
1322         parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1323                                       MDS_INODELOCK_UPDATE);
1324         if (IS_ERR(parent))
1325                 GOTO(out, result = PTR_ERR(parent));
1326
1327         /* get and check version of parent */
1328         result = mdt_version_get_check(info, parent, 0);
1329         if (result)
1330                 GOTO(out_parent, result);
1331
1332         fid_zero(child_fid);
1333
1334         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1335         result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1336                             lname, child_fid, &info->mti_spec);
1337         LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1338                  "looking for "DFID"/%s, result fid="DFID"\n",
1339                  PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
1340
1341         if (result != 0 && result != -ENOENT && result != -ESTALE)
1342                 GOTO(out_parent, result);
1343
1344         if (result == -ENOENT || result == -ESTALE) {
1345                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1346                 if (result == -ESTALE) {
1347                         /*
1348                          * -ESTALE means the parent is a dead(unlinked) dir, so
1349                          * it should return -ENOENT to in accordance with the
1350                          * original mds implementaion.
1351                          */
1352                         GOTO(out_parent, result = -ENOENT);
1353                 }
1354                 if (!(create_flags & MDS_OPEN_CREAT))
1355                         GOTO(out_parent, result);
1356                 *child_fid = *info->mti_rr.rr_fid2;
1357                 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1358                          PFID(child_fid));
1359         } else {
1360                 /*
1361                  * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1362                  * return FID back in that case.
1363                  */
1364                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1365         }
1366
1367         child = mdt_object_find(info->mti_env, mdt, child_fid);
1368         if (IS_ERR(child))
1369                 GOTO(out_parent, result = PTR_ERR(child));
1370
1371         /** check version of child  */
1372         rc = mdt_version_get_check(info, child, 1);
1373         if (rc)
1374                 GOTO(out_child, result = rc);
1375
1376         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1377         if (result == -ENOENT) {
1378                 if (mdt_object_obf(parent))
1379                         GOTO(out_child, result = -EPERM);
1380
1381                 /* save versions in reply */
1382                 mdt_version_get_save(info, parent, 0);
1383                 mdt_version_get_save(info, child, 1);
1384
1385                 /* version of child will be changed */
1386                 info->mti_mos = child;
1387
1388                 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1389                 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1390
1391                 /* Let lower layers know what is lock mode on directory. */
1392                 info->mti_spec.sp_cr_mode =
1393                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1394
1395                 /*
1396                  * Do not perform lookup sanity check. We know that name does
1397                  * not exist.
1398                  */
1399                 info->mti_spec.sp_cr_lookup = 0;
1400                 info->mti_spec.sp_feat = &dt_directory_features;
1401
1402                 result = mdo_create(info->mti_env,
1403                                     mdt_object_child(parent),
1404                                     lname,
1405                                     mdt_object_child(child),
1406                                     &info->mti_spec,
1407                                     &info->mti_attr);
1408                 if (result == -ERESTART) {
1409                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1410                         GOTO(out_child, result);
1411                 } else {
1412                         if (result != 0)
1413                                 GOTO(out_child, result);
1414                 }
1415                 created = 1;
1416         } else {
1417                 /* We have to get attr & lov ea for this object */
1418                 result = mo_attr_get(info->mti_env, mdt_object_child(child),
1419                                      ma);
1420                 /*
1421                  * The object is on remote node, return its FID for remote open.
1422                  */
1423                 if (result == -EREMOTE) {
1424                         /*
1425                          * Check if this lock already was sent to client and
1426                          * this is resent case. For resent case do not take lock
1427                          * again, use what is already granted.
1428                          */
1429                         LASSERT(lhc != NULL);
1430
1431                         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1432                                 struct ldlm_lock *lock;
1433
1434                                 LASSERT(msg_flags & MSG_RESENT);
1435
1436                                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1437                                 if (!lock) {
1438                                         CERROR("Invalid lock handle "LPX64"\n",
1439                                                lhc->mlh_reg_lh.cookie);
1440                                         LBUG();
1441                                 }
1442                                 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1443                                                         &lock->l_resource->lr_name));
1444                                 LDLM_LOCK_PUT(lock);
1445                                 rc = 0;
1446                         } else {
1447                                 mdt_lock_handle_init(lhc);
1448                                 mdt_lock_reg_init(lhc, LCK_PR);
1449
1450                                 rc = mdt_object_lock(info, child, lhc,
1451                                                      MDS_INODELOCK_LOOKUP,
1452                                                      MDT_CROSS_LOCK);
1453                         }
1454                         repbody->fid1 = *mdt_object_fid(child);
1455                         repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1456                         if (rc != 0)
1457                                 result = rc;
1458                         GOTO(out_child, result);
1459                 }
1460         }
1461
1462         LASSERT(!lustre_handle_is_used(&lhc->mlh_reg_lh));
1463
1464         /* get openlock if this is not replay and if a client requested it */
1465         if (!req_is_replay(req) && create_flags & MDS_OPEN_LOCK) {
1466                 ldlm_mode_t lm;
1467
1468                 if (create_flags & FMODE_WRITE)
1469                         lm = LCK_CW;
1470                 else if (create_flags & MDS_FMODE_EXEC)
1471                         lm = LCK_PR;
1472                 else
1473                         lm = LCK_CR;
1474                 mdt_lock_handle_init(lhc);
1475                 mdt_lock_reg_init(lhc, lm);
1476                 rc = mdt_object_lock(info, child, lhc,
1477                                      MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1478                                      MDT_CROSS_LOCK);
1479                 if (rc) {
1480                         result = rc;
1481                         GOTO(out_child, result);
1482                 } else {
1483                         result = -EREMOTE;
1484                         mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1485                 }
1486         }
1487
1488         /* Try to open it now. */
1489         rc = mdt_finish_open(info, parent, child, create_flags,
1490                              created, ldlm_rep);
1491         if (rc) {
1492                 result = rc;
1493                 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1494                         /* openlock was acquired and mdt_finish_open failed -
1495                            drop the openlock */
1496                         mdt_object_unlock(info, child, lhc, 1);
1497                 if (created) {
1498                         ma->ma_need = 0;
1499                         ma->ma_valid = 0;
1500                         ma->ma_cookie_size = 0;
1501                         info->mti_no_need_trans = 1;
1502                         rc = mdo_unlink(info->mti_env,
1503                                         mdt_object_child(parent),
1504                                         mdt_object_child(child),
1505                                         lname,
1506                                         &info->mti_attr);
1507                         if (rc != 0)
1508                                 CERROR("Error in cleanup of open\n");
1509                 }
1510         }
1511         EXIT;
1512 out_child:
1513         mdt_object_put(info->mti_env, child);
1514 out_parent:
1515         mdt_object_unlock_put(info, parent, lh, result || !created);
1516 out:
1517         if (result && result != -EREMOTE)
1518                 lustre_msg_set_transno(req->rq_repmsg, 0);
1519         return result;
1520 }
1521
1522 #define MFD_CLOSED(mode) (((mode) & ~(MDS_FMODE_EPOCH | MDS_FMODE_SOM | \
1523                                       MDS_FMODE_TRUNC)) == MDS_FMODE_CLOSED)
1524
1525 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1526 {
1527         return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1528 }
1529
1530 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1531 {
1532         struct mdt_object *o = mfd->mfd_object;
1533         struct md_object *next = mdt_object_child(o);
1534         struct md_attr *ma = &info->mti_attr;
1535         int ret = MDT_IOEPOCH_CLOSED;
1536         int rc = 0;
1537         int mode;
1538         ENTRY;
1539
1540         mode = mfd->mfd_mode;
1541
1542         if ((mode & FMODE_WRITE) || (mode & MDS_FMODE_TRUNC)) {
1543                 mdt_write_put(o);
1544                 ret = mdt_ioepoch_close(info, o);
1545         } else if (mode & MDS_FMODE_EXEC) {
1546                 mdt_write_allow(o);
1547         } else if (mode & MDS_FMODE_EPOCH) {
1548                 ret = mdt_ioepoch_close(info, o);
1549         } else if (mode & MDS_FMODE_SOM) {
1550                 ret = mdt_som_au_close(info, o);
1551         }
1552
1553         /* Update atime on close only. */
1554         if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1555             && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1556                 /* Set the atime only. */
1557                 ma->ma_valid = MA_INODE;
1558                 ma->ma_attr.la_valid = LA_ATIME;
1559                 rc = mo_attr_set(info->mti_env, next, ma);
1560         }
1561
1562         ma->ma_need |= MA_INODE;
1563         ma->ma_valid &= ~MA_INODE;
1564
1565         if (!MFD_CLOSED(mode))
1566                 rc = mo_close(info->mti_env, next, ma, mode);
1567
1568         if (ret == MDT_IOEPOCH_GETATTR || ret == MDT_IOEPOCH_OPENED) {
1569                 struct mdt_export_data *med;
1570
1571                 /* The IOepoch is still opened or SOM update is needed.
1572                  * Put mfd back into the list. */
1573                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1574                 mdt_mfd_set_mode(mfd, ret == MDT_IOEPOCH_OPENED ?
1575                                       MDS_FMODE_EPOCH : MDS_FMODE_SOM);
1576
1577                 LASSERT(mdt_info_req(info));
1578                 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1579                 cfs_spin_lock(&med->med_open_lock);
1580                 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
1581                 class_handle_hash_back(&mfd->mfd_handle);
1582                 cfs_spin_unlock(&med->med_open_lock);
1583
1584                 if (ret == MDT_IOEPOCH_OPENED) {
1585                         ret = 0;
1586                 } else {
1587                         ret = -EAGAIN;
1588                         CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1589                                "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1590                 }
1591         } else {
1592                 mdt_mfd_free(mfd);
1593                 mdt_object_put(info->mti_env, o);
1594         }
1595
1596         RETURN(rc ? rc : ret);
1597 }
1598
1599 int mdt_close(struct mdt_thread_info *info)
1600 {
1601         struct mdt_export_data *med;
1602         struct mdt_file_data   *mfd;
1603         struct mdt_object      *o;
1604         struct md_attr         *ma = &info->mti_attr;
1605         struct mdt_body        *repbody = NULL;
1606         struct ptlrpc_request  *req = mdt_info_req(info);
1607         int rc, ret = 0;
1608         ENTRY;
1609
1610         mdt_counter_incr(req, LPROC_MDT_CLOSE);
1611         /* Close may come with the Size-on-MDS update. Unpack it. */
1612         rc = mdt_close_unpack(info);
1613         if (rc)
1614                 RETURN(err_serious(rc));
1615
1616         LASSERT(info->mti_ioepoch);
1617
1618         req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1619                              info->mti_mdt->mdt_max_mdsize);
1620         req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1621                              info->mti_mdt->mdt_max_cookiesize);
1622         rc = req_capsule_server_pack(info->mti_pill);
1623         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1624                 mdt_client_compatibility(info);
1625                 if (rc == 0)
1626                         mdt_fix_reply(info);
1627                 RETURN(lustre_msg_get_status(req->rq_repmsg));
1628         }
1629
1630         /* Continue to close handle even if we can not pack reply */
1631         if (rc == 0) {
1632                 repbody = req_capsule_server_get(info->mti_pill,
1633                                                  &RMF_MDT_BODY);
1634                 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
1635                                                     &RMF_MDT_MD);
1636                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
1637                                                        &RMF_MDT_MD,
1638                                                        RCL_SERVER);
1639                 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
1640                                                        &RMF_LOGCOOKIES);
1641                 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
1642                                                           &RMF_LOGCOOKIES,
1643                                                           RCL_SERVER);
1644                 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1645                 repbody->eadatasize = 0;
1646                 repbody->aclsize = 0;
1647         } else {
1648                 rc = err_serious(rc);
1649         }
1650
1651         med = &req->rq_export->exp_mdt_data;
1652         cfs_spin_lock(&med->med_open_lock);
1653         mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1654         if (mdt_mfd_closed(mfd)) {
1655                 cfs_spin_unlock(&med->med_open_lock);
1656                 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1657                        ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1658                        info->mti_ioepoch->handle.cookie);
1659                 /** not serious error since bug 3633 */
1660                 rc = -ESTALE;
1661         } else {
1662                 class_handle_unhash(&mfd->mfd_handle);
1663                 cfs_list_del_init(&mfd->mfd_list);
1664                 cfs_spin_unlock(&med->med_open_lock);
1665
1666                 /* Do not lose object before last unlink. */
1667                 o = mfd->mfd_object;
1668                 mdt_object_get(info->mti_env, o);
1669                 ret = mdt_mfd_close(info, mfd);
1670                 if (repbody != NULL)
1671                         rc = mdt_handle_last_unlink(info, o, ma);
1672                 mdt_empty_transno(info, rc);
1673                 mdt_object_put(info->mti_env, o);
1674         }
1675         if (repbody != NULL) {
1676                 mdt_client_compatibility(info);
1677                 rc = mdt_fix_reply(info);
1678         }
1679
1680         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1681                 RETURN(err_serious(-ENOMEM));
1682
1683         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
1684                                  OBD_FAIL_MDS_CLOSE_NET_REP))
1685                 info->mti_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
1686         RETURN(rc ? rc : ret);
1687 }
1688
1689 /**
1690  * DONE_WRITING rpc handler.
1691  *
1692  * As mfd is not kept after replayed CLOSE (see mdt_ioepoch_close_on_replay()),
1693  * only those DONE_WRITING rpc will be replayed which really wrote smth on disk,
1694  * and got a trasid. Waiting for such DONE_WRITING is not reliable, so just
1695  * skip attributes and reconstruct the reply here.
1696  */
1697 int mdt_done_writing(struct mdt_thread_info *info)
1698 {
1699         struct ptlrpc_request   *req = mdt_info_req(info);
1700         struct mdt_body         *repbody = NULL;
1701         struct mdt_export_data  *med;
1702         struct mdt_file_data    *mfd;
1703         int rc;
1704         ENTRY;
1705
1706         rc = req_capsule_server_pack(info->mti_pill);
1707         if (rc)
1708                 RETURN(err_serious(rc));
1709
1710         repbody = req_capsule_server_get(info->mti_pill,
1711                                          &RMF_MDT_BODY);
1712         repbody->eadatasize = 0;
1713         repbody->aclsize = 0;
1714
1715         /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1716         rc = mdt_close_unpack(info);
1717         if (rc)
1718                 RETURN(err_serious(rc));
1719
1720         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL))
1721                 RETURN(lustre_msg_get_status(req->rq_repmsg));
1722
1723         med = &info->mti_exp->exp_mdt_data;
1724         cfs_spin_lock(&med->med_open_lock);
1725         mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1726         if (mfd == NULL) {
1727                 cfs_spin_unlock(&med->med_open_lock);
1728                 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1729                        ": cookie = "LPX64" ioepoch = "LPU64"\n",
1730                        PFID(info->mti_rr.rr_fid1),
1731                        info->mti_ioepoch->handle.cookie,
1732                        info->mti_ioepoch->ioepoch);
1733                 /* If this is a replay, reconstruct the transno. */
1734                 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1735                         rc = info->mti_ioepoch->flags & MF_SOM_AU ?
1736                              -EAGAIN : 0;
1737                         mdt_empty_transno(info, rc);
1738                         RETURN(rc);
1739                 }
1740                 RETURN(-ESTALE);
1741         }
1742
1743         LASSERT(mfd->mfd_mode == MDS_FMODE_EPOCH ||
1744                 mfd->mfd_mode == MDS_FMODE_TRUNC);
1745         class_handle_unhash(&mfd->mfd_handle);
1746         cfs_list_del_init(&mfd->mfd_list);
1747         cfs_spin_unlock(&med->med_open_lock);
1748
1749         /* Set EPOCH CLOSE flag if not set by client. */
1750         info->mti_ioepoch->flags |= MF_EPOCH_CLOSE;
1751         info->mti_attr.ma_valid = 0;
1752
1753         info->mti_attr.ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
1754         OBD_ALLOC_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1755         if (info->mti_attr.ma_lmm == NULL)
1756                 RETURN(-ENOMEM);
1757
1758         rc = mdt_mfd_close(info, mfd);
1759
1760         OBD_FREE_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1761         mdt_empty_transno(info, rc);
1762         RETURN(rc);
1763 }