Whamcloud - gitweb
LU-3963 libcfs: convert lod, mdt, and gss to linux list api
[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, 2013, Intel Corporation.
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(const struct mdt_export_data *med)
62 {
63         struct mdt_file_data *mfd;
64         ENTRY;
65
66         OBD_ALLOC_PTR(mfd);
67         if (mfd != NULL) {
68                 INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
69                 mfd->mfd_handle.h_owner = med;
70                 INIT_LIST_HEAD(&mfd->mfd_list);
71                 class_handle_hash(&mfd->mfd_handle, &mfd_handle_ops);
72         }
73
74         RETURN(mfd);
75 }
76
77 /*
78  * Find the mfd pointed to by handle in global hash table.
79  * In case of replay the handle is obsoleted
80  * but mfd can be found in mfd list by that handle.
81  * Callers need to be holding med_open_lock.
82  */
83 struct mdt_file_data *mdt_handle2mfd(struct mdt_export_data *med,
84                                      const struct lustre_handle *handle,
85                                      bool is_replay_or_resent)
86 {
87         struct mdt_file_data   *mfd;
88         ENTRY;
89
90         LASSERT(handle != NULL);
91         mfd = class_handle2object(handle->cookie, med);
92         /* during dw/setattr replay the mfd can be found by old handle */
93         if (mfd == NULL && is_replay_or_resent) {
94                 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
95                         if (mfd->mfd_old_handle.cookie == handle->cookie)
96                                 RETURN(mfd);
97                 }
98                 mfd = NULL;
99         }
100
101         RETURN(mfd);
102 }
103
104 /* free mfd */
105 void mdt_mfd_free(struct mdt_file_data *mfd)
106 {
107         LASSERT(list_empty(&mfd->mfd_list));
108         OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
109 }
110
111 static int mdt_create_data(struct mdt_thread_info *info,
112                            struct mdt_object *p, struct mdt_object *o)
113 {
114         struct md_op_spec     *spec = &info->mti_spec;
115         struct md_attr        *ma   = &info->mti_attr;
116         int                    rc   = 0;
117         ENTRY;
118
119         if (!md_should_create(spec->sp_cr_flags))
120                 RETURN(0);
121
122         ma->ma_need = MA_INODE | MA_LOV;
123         ma->ma_valid = 0;
124         mutex_lock(&o->mot_lov_mutex);
125         if (!(o->mot_flags & MOF_LOV_CREATED)) {
126                 rc = mdo_create_data(info->mti_env,
127                                      p ? mdt_object_child(p) : NULL,
128                                      mdt_object_child(o), spec, ma);
129                 if (rc == 0)
130                         rc = mdt_attr_get_complex(info, o, ma);
131
132                 if (rc == 0 && ma->ma_valid & MA_LOV)
133                         o->mot_flags |= MOF_LOV_CREATED;
134         }
135
136         mutex_unlock(&o->mot_lov_mutex);
137         RETURN(rc);
138 }
139
140 static int mdt_ioepoch_opened(struct mdt_object *mo)
141 {
142         return mo->mot_ioepoch_count;
143 }
144
145 int mdt_object_is_som_enabled(struct mdt_object *mo)
146 {
147         return !mo->mot_ioepoch;
148 }
149
150 /**
151  * Re-enable Size-on-MDS.
152  * Call under ->mot_ioepoch_mutex.
153  */
154 static void mdt_object_som_enable(struct mdt_object *mo, __u64 ioepoch)
155 {
156         if (ioepoch == mo->mot_ioepoch) {
157                 LASSERT(!mdt_ioepoch_opened(mo));
158                 mo->mot_ioepoch = 0;
159                 mo->mot_flags = 0;
160         }
161 }
162
163 /**
164  * Open the IOEpoch. It is allowed if @writecount is not negative.
165  * The epoch and writecount handling is performed under the mot_ioepoch_mutex.
166  */
167 int mdt_ioepoch_open(struct mdt_thread_info *info, struct mdt_object *o,
168                      int created)
169 {
170         struct mdt_device *mdt = info->mti_mdt;
171         int cancel = 0;
172         int rc = 0;
173         ENTRY;
174
175         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
176             !S_ISREG(lu_object_attr(&o->mot_obj)))
177                 RETURN(0);
178
179         mutex_lock(&o->mot_ioepoch_mutex);
180         if (mdt_ioepoch_opened(o)) {
181                 /* Epoch continues even if there is no writers yet. */
182                 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
183                        o->mot_ioepoch, PFID(mdt_object_fid(o)));
184         } else {
185                 /* XXX: ->mdt_ioepoch is not initialized at the mount */
186                 spin_lock(&mdt->mdt_ioepoch_lock);
187                 if (mdt->mdt_ioepoch < info->mti_replayepoch)
188                         mdt->mdt_ioepoch = info->mti_replayepoch;
189
190                 if (info->mti_replayepoch)
191                         o->mot_ioepoch = info->mti_replayepoch;
192                 else if (++mdt->mdt_ioepoch == IOEPOCH_INVAL)
193                         o->mot_ioepoch = ++mdt->mdt_ioepoch;
194                 else
195                         o->mot_ioepoch = mdt->mdt_ioepoch;
196
197                 spin_unlock(&mdt->mdt_ioepoch_lock);
198
199                 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
200                        o->mot_ioepoch, PFID(mdt_object_fid(o)));
201                 if (created)
202                         o->mot_flags |= MOF_SOM_CREATED;
203                 cancel = 1;
204         }
205         o->mot_ioepoch_count++;
206         mutex_unlock(&o->mot_ioepoch_mutex);
207
208         /* Cancel Size-on-MDS attributes cached on clients for the open case.
209          * In the truncate case, see mdt_reint_setattr(). */
210         if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
211                 struct mdt_lock_handle  *lh = &info->mti_lh[MDT_LH_CHILD];
212                 mdt_lock_reg_init(lh, LCK_EX);
213                 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
214                                      MDT_LOCAL_LOCK);
215                 if (rc == 0)
216                         mdt_object_unlock(info, o, lh, 1);
217         }
218         RETURN(rc);
219 }
220
221 /**
222  * Update SOM on-disk attributes.
223  * If enabling, write update inodes and lustre-ea with the proper IOEpoch,
224  * mountid and attributes. If disabling, clean SOM xattr.
225  * Call under ->mot_ioepoch_mutex.
226  */
227 static int mdt_som_attr_set(struct mdt_thread_info *info,
228                             struct mdt_object *obj, __u64 ioepoch, bool enable)
229 {
230         struct md_object        *next = mdt_object_child(obj);
231         int                      rc;
232         ENTRY;
233
234         CDEBUG(D_INODE, "Size-on-MDS attribute %s for epoch "LPU64
235                " on "DFID".\n", enable ? "update" : "disabling",
236                ioepoch, PFID(mdt_object_fid(obj)));
237
238         if (enable) {
239                 struct lu_buf           *buf = &info->mti_buf;
240                 struct som_attrs        *attrs;
241                 struct md_attr          *ma = &info->mti_attr;
242                 struct lu_attr          *la = &ma->ma_attr;
243                 struct obd_device       *obd = info->mti_mdt->mdt_lut.lut_obd;
244
245                 attrs = (struct som_attrs *)info->mti_xattr_buf;
246                 CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*attrs));
247
248                 /* pack SOM attributes */
249                 memset(attrs, 0, sizeof(*attrs));
250                 attrs->som_ioepoch = ioepoch;
251                 attrs->som_mountid = obd->u.obt.obt_mount_count;
252                 if ((la->la_valid & LA_SIZE) != 0)
253                         attrs->som_size = la->la_size;
254                 if ((la->la_valid & LA_BLOCKS) != 0)
255                         attrs->som_blocks = la->la_blocks;
256                 lustre_som_swab(attrs);
257
258                 /* update SOM attributes */
259                 buf->lb_buf = attrs;
260                 buf->lb_len = sizeof(*attrs);
261                 rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_SOM, 0);
262         } else {
263                 /* delete SOM attributes */
264                 rc = mo_xattr_del(info->mti_env, next, XATTR_NAME_SOM);
265         }
266
267         RETURN(rc);
268 }
269
270 /** Perform the eviction specific actions on ioepoch close. */
271 static inline int mdt_ioepoch_close_on_eviction(struct mdt_thread_info *info,
272                                                 struct mdt_object *o)
273 {
274         int rc = 0;
275
276         mutex_lock(&o->mot_ioepoch_mutex);
277         CDEBUG(D_INODE, "Eviction. Closing IOepoch "LPU64" on "DFID". "
278                "Count %d\n", o->mot_ioepoch, PFID(mdt_object_fid(o)),
279                o->mot_ioepoch_count);
280         o->mot_ioepoch_count--;
281
282         /* If eviction occured set MOF_SOM_RECOV,
283          * if no other epoch holders, disable SOM on disk. */
284         o->mot_flags |= MOF_SOM_CHANGE | MOF_SOM_RECOV;
285         if (!mdt_ioepoch_opened(o)) {
286                 rc = mdt_som_attr_set(info, o, o->mot_ioepoch, MDT_SOM_DISABLE);
287                 mdt_object_som_enable(o, o->mot_ioepoch);
288         }
289         mutex_unlock(&o->mot_ioepoch_mutex);
290         RETURN(rc);
291 }
292
293 /**
294  * Perform the replay specific actions on ioepoch close.
295  * Skip SOM attribute update if obtained and just forget about the inode state
296  * for the last ioepoch holder. The SOM cache is invalidated on MDS failure.
297  */
298 static inline int mdt_ioepoch_close_on_replay(struct mdt_thread_info *info,
299                                               struct mdt_object *o)
300 {
301         int rc = MDT_IOEPOCH_CLOSED;
302         ENTRY;
303
304         mutex_lock(&o->mot_ioepoch_mutex);
305         CDEBUG(D_INODE, "Replay. Closing epoch "LPU64" on "DFID". Count %d\n",
306                o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
307         o->mot_ioepoch_count--;
308
309         /* Get an info from the replayed request if client is supposed
310          * to send an Attibute Update, reconstruct @rc if so */
311         if (info->mti_ioepoch->flags & MF_SOM_AU)
312                 rc = MDT_IOEPOCH_GETATTR;
313
314         if (!mdt_ioepoch_opened(o))
315                 mdt_object_som_enable(o, info->mti_ioepoch->ioepoch);
316         mutex_unlock(&o->mot_ioepoch_mutex);
317
318         RETURN(rc);
319 }
320
321 /**
322  * Regular file IOepoch close.
323  * Closes the ioepoch, checks the object state, apply obtained attributes and
324  * re-enable SOM on the object, if possible. Also checks if the recovery is
325  * needed and packs OBD_MD_FLGETATTRLOCK flag into the reply to force the client
326  * to obtain SOM attributes under the server-side OST locks.
327  *
328  * Return value:
329  * MDT_IOEPOCH_CLOSED if ioepoch is closed.
330  * MDT_IOEPOCH_GETATTR if ioepoch is closed but another SOM update is needed.
331  */
332 static inline int mdt_ioepoch_close_reg(struct mdt_thread_info *info,
333                                         struct mdt_object *o)
334 {
335         struct md_attr *tmp_ma;
336         struct lu_attr *la;
337         int achange, opened;
338         int recovery = 0;
339         int rc = 0, ret = MDT_IOEPOCH_CLOSED;
340         ENTRY;
341
342         la = &info->mti_attr.ma_attr;
343         achange = (info->mti_ioepoch->flags & MF_SOM_CHANGE);
344
345         mutex_lock(&o->mot_ioepoch_mutex);
346         o->mot_ioepoch_count--;
347
348         tmp_ma = &info->mti_u.som.attr;
349         tmp_ma->ma_lmm = info->mti_attr.ma_lmm;
350         tmp_ma->ma_lmm_size = info->mti_attr.ma_lmm_size;
351         tmp_ma->ma_som = &info->mti_u.som.data;
352         tmp_ma->ma_need = MA_INODE | MA_LOV | MA_SOM;
353         tmp_ma->ma_valid = 0;
354         rc = mdt_attr_get_complex(info, o, tmp_ma);
355         if (rc)
356                 GOTO(error_up, rc);
357
358         /* Check the on-disk SOM state. */
359         if (o->mot_flags & MOF_SOM_RECOV)
360                 recovery = 1;
361         else if (!(o->mot_flags & MOF_SOM_CREATED) &&
362                  !(tmp_ma->ma_valid & MA_SOM))
363                 recovery = 1;
364
365         CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
366                o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
367
368         opened = mdt_ioepoch_opened(o);
369         /**
370          * If IOEpoch is not opened, check if a Size-on-MDS update is needed.
371          * Skip the check for file with no LOV  or for unlink files.
372          */
373         if (!opened && tmp_ma->ma_valid & MA_LOV &&
374             !(tmp_ma->ma_valid & MA_INODE && tmp_ma->ma_attr.la_nlink == 0)) {
375                 if (recovery)
376                         /* If some previous writer was evicted, re-ask the
377                          * client for attributes. Even if attributes are
378                          * provided, we cannot believe in them.
379                          * Another use case is that there is no SOM cache on
380                          * disk -- first access with SOM or there was an MDS
381                          * failure. */
382                         ret = MDT_IOEPOCH_GETATTR;
383                 else if (o->mot_flags & MOF_SOM_CHANGE)
384                         /* Some previous writer changed the attribute.
385                          * Do not believe to the current Size-on-MDS
386                          * update, re-ask client. */
387                         ret = MDT_IOEPOCH_GETATTR;
388                 else if (!(la->la_valid & LA_SIZE) && achange)
389                         /* Attributes were changed by the last writer
390                          * only but no Size-on-MDS update is received.*/
391                         ret = MDT_IOEPOCH_GETATTR;
392         }
393
394         if (achange || ret == MDT_IOEPOCH_GETATTR)
395                 o->mot_flags |= MOF_SOM_CHANGE;
396
397         /* If epoch ends and relable SOM attributes are obtained, update them.
398          * Create SOM ea for new files even if there is no attributes obtained
399          * (0-length file). */
400         if (ret == MDT_IOEPOCH_CLOSED && !opened) {
401                 if (achange || o->mot_flags & MOF_SOM_CREATED) {
402                         LASSERT(achange || !(la->la_valid & LA_SIZE));
403                         rc = mdt_som_attr_set(info, o, o->mot_ioepoch,
404                                               MDT_SOM_ENABLE);
405                         /* Avoid the following setattrs of these attributes,
406                          * e.g. for atime update. */
407                         info->mti_attr.ma_valid = 0;
408                 }
409                 mdt_object_som_enable(o, o->mot_ioepoch);
410         }
411
412         mutex_unlock(&o->mot_ioepoch_mutex);
413         /* If recovery is needed, tell the client to perform GETATTR under
414          * the lock. */
415         if (ret == MDT_IOEPOCH_GETATTR && recovery) {
416                 struct mdt_body *rep;
417                 rep = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
418                 rep->mbo_valid |= OBD_MD_FLGETATTRLOCK;
419         }
420
421         RETURN(rc ? : ret);
422
423 error_up:
424         mutex_unlock(&o->mot_ioepoch_mutex);
425         return rc;
426 }
427
428 /**
429  * Close IOEpoch (opened file or MDS_FMODE_EPOCH state). It happens if:
430  * - a client closes the IOEpoch;
431  * - a client eviction occured.
432  * Return values:
433  * MDT_IOEPOCH_OPENED if the client does not close IOEpoch.
434  * MDT_IOEPOCH_CLOSED if the client closes IOEpoch.
435  * MDT_IOEPOCH_GETATTR if the client closes IOEpoch but another SOM attribute
436  * update is needed.
437  */
438 static int mdt_ioepoch_close(struct mdt_thread_info *info, struct mdt_object *o)
439 {
440         struct ptlrpc_request *req = mdt_info_req(info);
441         ENTRY;
442
443         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
444             !S_ISREG(lu_object_attr(&o->mot_obj)))
445                 RETURN(0);
446
447         LASSERT(o->mot_ioepoch_count);
448         LASSERT(info->mti_ioepoch == NULL ||
449                 info->mti_ioepoch->ioepoch == o->mot_ioepoch);
450
451         /* IOEpoch is closed only if client tells about it or eviction occures.
452          * In the replay case, always close the epoch. */
453         if (req == NULL)
454                 RETURN(mdt_ioepoch_close_on_eviction(info, o));
455         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
456                 RETURN(mdt_ioepoch_close_on_replay(info, o));
457         if (info->mti_ioepoch && (info->mti_ioepoch->flags & MF_EPOCH_CLOSE))
458                 RETURN(mdt_ioepoch_close_reg(info, o));
459         /* IO epoch is not closed. */
460         RETURN(MDT_IOEPOCH_OPENED);
461 }
462
463 /**
464  * Close MDS_FMODE_SOM state, when IOEpoch is already closed and we are waiting
465  * for attribute update. It happens if:
466  * - SOM Attribute Update is obtained;
467  * - the client failed to obtain it and informs MDS about it;
468  * - a client eviction occured.
469  * Apply obtained attributes for the 1st case, wipe out the on-disk SOM
470  * cache otherwise.
471  */
472 int mdt_som_au_close(struct mdt_thread_info *info, struct mdt_object *o)
473 {
474         struct ptlrpc_request   *req = mdt_info_req(info);
475         __u64                    ioepoch = 0;
476         int                      act = MDT_SOM_ENABLE;
477         int                      rc = 0;
478         ENTRY;
479
480         LASSERT(!req || info->mti_ioepoch);
481         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
482             !S_ISREG(lu_object_attr(&o->mot_obj)))
483                 RETURN(0);
484
485         /* No size whereas MF_SOM_CHANGE is set means client failed to
486          * obtain ost attributes, drop the SOM cache on disk if so. */
487         if (!req ||
488             (info->mti_ioepoch &&
489              info->mti_ioepoch->flags & MF_SOM_CHANGE &&
490              !(info->mti_attr.ma_attr.la_valid & LA_SIZE)))
491                 act = MDT_SOM_DISABLE;
492
493         mutex_lock(&o->mot_ioepoch_mutex);
494         /* Mark the object it is the recovery state if we failed to obtain
495          * SOM attributes. */
496         if (act == MDT_SOM_DISABLE)
497                 o->mot_flags |= MOF_SOM_RECOV;
498
499         if (!mdt_ioepoch_opened(o)) {
500                 ioepoch =  info->mti_ioepoch ?
501                         info->mti_ioepoch->ioepoch : o->mot_ioepoch;
502
503                 if (req != NULL
504                     && !(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         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         mutex_lock(&o->mot_ioepoch_mutex);
517         rc = o->mot_writecount;
518         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         mutex_lock(&o->mot_ioepoch_mutex);
527         if (o->mot_writecount < 0)
528                 rc = -ETXTBSY;
529         else
530                 o->mot_writecount++;
531         mutex_unlock(&o->mot_ioepoch_mutex);
532         RETURN(rc);
533 }
534
535 void mdt_write_put(struct mdt_object *o)
536 {
537         ENTRY;
538         mutex_lock(&o->mot_ioepoch_mutex);
539         o->mot_writecount--;
540         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         mutex_lock(&o->mot_ioepoch_mutex);
549         if (o->mot_writecount > 0)
550                 rc = -ETXTBSY;
551         else
552                 o->mot_writecount--;
553         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         mutex_lock(&o->mot_ioepoch_mutex);
561         o->mot_writecount++;
562         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         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_obd_name(mdt), info->mti_transno,
586                                libcfs_nid2str(exp->exp_connection->c_peer.nid),
587                                rc);
588                         spin_unlock(&mdt->mdt_lut.lut_translock);
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         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         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                 mutex_unlock(&ted->ted_lcd_lock);
619                 CDEBUG(D_HA, "%s: transno = "LPU64" < last_transno = "LPU64"\n",
620                        mdt_obd_name(mdt), info->mti_transno,
621                        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         mutex_unlock(&ted->ted_lcd_lock);
648
649         EXIT;
650 }
651
652 void mdt_mfd_set_mode(struct mdt_file_data *mfd, __u64 mode)
653 {
654         LASSERT(mfd != NULL);
655
656         CDEBUG(D_HA, DFID " Change mfd mode "LPO64" -> "LPO64".\n",
657                PFID(mdt_object_fid(mfd->mfd_object)), mfd->mfd_mode, mode);
658
659         mfd->mfd_mode = mode;
660 }
661
662 /**
663  * prep ma_lmm/ma_lmv for md_attr from reply
664  */
665 void mdt_prep_ma_buf_from_rep(struct mdt_thread_info *info,
666                               struct mdt_object *obj,
667                               struct md_attr *ma)
668 {
669         LASSERT(ma->ma_lmv == NULL && ma->ma_lmm == NULL);
670         if (S_ISDIR(obj->mot_header.loh_attr)) {
671                 ma->ma_lmv = req_capsule_server_get(info->mti_pill,
672                                                     &RMF_MDT_MD);
673                 ma->ma_lmv_size = req_capsule_get_size(info->mti_pill,
674                                                        &RMF_MDT_MD,
675                                                        RCL_SERVER);
676                 if (ma->ma_lmv_size > 0)
677                         ma->ma_need |= MA_LMV;
678         } else {
679                 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
680                                                     &RMF_MDT_MD);
681                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
682                                                        &RMF_MDT_MD,
683                                                        RCL_SERVER);
684                 if (ma->ma_lmm_size > 0)
685                         ma->ma_need |= MA_LOV;
686         }
687 }
688
689 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
690                         struct mdt_object *o, __u64 flags, int created,
691                         struct ldlm_reply *rep)
692 {
693         struct ptlrpc_request   *req = mdt_info_req(info);
694         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
695         struct mdt_file_data    *mfd;
696         struct md_attr          *ma  = &info->mti_attr;
697         struct lu_attr          *la  = &ma->ma_attr;
698         struct mdt_body         *repbody;
699         int                      rc = 0, isdir, isreg;
700         ENTRY;
701
702         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
703
704         isreg = S_ISREG(la->la_mode);
705         isdir = S_ISDIR(la->la_mode);
706         if (isreg && !(ma->ma_valid & MA_LOV) && !(flags & MDS_OPEN_RELEASE)) {
707                 /*
708                  * No EA, check whether it is will set regEA and dirEA since in
709                  * above attr get, these size might be zero, so reset it, to
710                  * retrieve the MD after create obj.
711                  */
712                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
713                                                        &RMF_MDT_MD,
714                                                        RCL_SERVER);
715                 /* in replay case, p == NULL */
716                 rc = mdt_create_data(info, p, o);
717                 if (rc)
718                         RETURN(rc);
719
720                 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_DISP_STRIPE)
721                         mdt_set_disposition(info, rep, DISP_OPEN_STRIPE);
722         }
723
724         CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
725                ma->ma_valid, ma->ma_lmm_size);
726
727         if (ma->ma_valid & MA_LOV) {
728                 LASSERT(ma->ma_lmm_size != 0);
729                 repbody->mbo_eadatasize = ma->ma_lmm_size;
730                 if (isdir)
731                         repbody->mbo_valid |= OBD_MD_FLDIREA;
732                 else
733                         repbody->mbo_valid |= OBD_MD_FLEASIZE;
734         }
735
736         if (ma->ma_valid & MA_LMV) {
737                 LASSERT(ma->ma_lmv_size != 0);
738                 repbody->mbo_eadatasize = ma->ma_lmv_size;
739                 LASSERT(isdir);
740                 repbody->mbo_valid |= OBD_MD_FLDIREA | OBD_MD_MEA;
741         }
742
743         if (flags & FMODE_WRITE) {
744                 rc = mdt_write_get(o);
745                 if (rc == 0) {
746                         mdt_ioepoch_open(info, o, created);
747                         repbody->mbo_ioepoch = o->mot_ioepoch;
748                 }
749         } else if (flags & MDS_FMODE_EXEC) {
750                 rc = mdt_write_deny(o);
751         }
752         if (rc)
753                 RETURN(rc);
754
755         rc = mo_open(info->mti_env, mdt_object_child(o),
756                      created ? flags | MDS_OPEN_CREATED : flags);
757         if (rc)
758                 GOTO(err_out, rc);
759
760         mfd = mdt_mfd_new(med);
761         if (mfd == NULL)
762                 GOTO(err_out, rc = -ENOMEM);
763
764         /*
765          * Keep a reference on this object for this open, and is
766          * released by mdt_mfd_close().
767          */
768         mdt_object_get(info->mti_env, o);
769         mfd->mfd_object = o;
770         mfd->mfd_xid = req->rq_xid;
771
772         /*
773          * @flags is always not zero. At least it should be FMODE_READ,
774          * FMODE_WRITE or MDS_FMODE_EXEC.
775          */
776         LASSERT(flags != 0);
777
778         /* Open handling. */
779         mdt_mfd_set_mode(mfd, flags);
780
781         atomic_inc(&o->mot_open_count);
782         if (flags & MDS_OPEN_LEASE)
783                 atomic_inc(&o->mot_lease_count);
784
785         /* replay handle */
786         if (req_is_replay(req)) {
787                 struct mdt_file_data *old_mfd;
788                 /* Check wheather old cookie already exist in
789                  * the list, becasue when do recovery, client
790                  * might be disconnected from server, and
791                  * restart replay, so there maybe some orphan
792                  * mfd here, we should remove them */
793                 LASSERT(info->mti_rr.rr_handle != NULL);
794                 spin_lock(&med->med_open_lock);
795                 old_mfd = mdt_handle2mfd(med, info->mti_rr.rr_handle, true);
796                 if (old_mfd != NULL) {
797                         CDEBUG(D_HA, "delete orphan mfd = %p, fid = "DFID", "
798                                "cookie = "LPX64"\n", mfd,
799                                PFID(mdt_object_fid(mfd->mfd_object)),
800                                info->mti_rr.rr_handle->cookie);
801                         class_handle_unhash(&old_mfd->mfd_handle);
802                         list_del_init(&old_mfd->mfd_list);
803                         spin_unlock(&med->med_open_lock);
804                         /* no attr update for that close */
805                         la->la_valid = 0;
806                         ma->ma_valid |= MA_FLAGS;
807                         ma->ma_attr_flags |= MDS_RECOV_OPEN;
808                         mdt_mfd_close(info, old_mfd);
809                         ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
810                         ma->ma_valid &= ~MA_FLAGS;
811                 } else {
812                         spin_unlock(&med->med_open_lock);
813                         CDEBUG(D_HA, "orphan mfd not found, fid = "DFID", "
814                                "cookie = "LPX64"\n",
815                                PFID(mdt_object_fid(mfd->mfd_object)),
816                                info->mti_rr.rr_handle->cookie);
817                 }
818
819                 CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
820                        info->mti_rr.rr_handle->cookie);
821
822                 mfd->mfd_old_handle.cookie = info->mti_rr.rr_handle->cookie;
823         }
824
825         repbody->mbo_handle.cookie = mfd->mfd_handle.h_cookie;
826
827         if (req->rq_export->exp_disconnected) {
828                 spin_lock(&med->med_open_lock);
829                 class_handle_unhash(&mfd->mfd_handle);
830                 list_del_init(&mfd->mfd_list);
831                 spin_unlock(&med->med_open_lock);
832                 mdt_mfd_close(info, mfd);
833         } else {
834                 spin_lock(&med->med_open_lock);
835                 list_add(&mfd->mfd_list, &med->med_open_head);
836                 spin_unlock(&med->med_open_lock);
837         }
838
839         mdt_empty_transno(info, rc);
840
841         RETURN(rc);
842
843 err_out:
844         if (flags & FMODE_WRITE)
845                 /* XXX We also need to close io epoch here.
846                  * See LU-1220 - green */
847                 mdt_write_put(o);
848         else if (flags & MDS_FMODE_EXEC)
849                 mdt_write_allow(o);
850
851         return rc;
852 }
853
854 int mdt_finish_open(struct mdt_thread_info *info,
855                     struct mdt_object *p, struct mdt_object *o,
856                     __u64 flags, int created, struct ldlm_reply *rep)
857 {
858         struct ptlrpc_request   *req = mdt_info_req(info);
859         struct obd_export       *exp = req->rq_export;
860         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
861         struct md_attr          *ma  = &info->mti_attr;
862         struct lu_attr          *la  = &ma->ma_attr;
863         struct mdt_file_data    *mfd;
864         struct mdt_body         *repbody;
865         int                      rc = 0;
866         int                      isreg, isdir, islnk;
867         struct list_head        *t;
868         ENTRY;
869
870         LASSERT(ma->ma_valid & MA_INODE);
871
872         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
873
874         isreg = S_ISREG(la->la_mode);
875         isdir = S_ISDIR(la->la_mode);
876         islnk = S_ISLNK(la->la_mode);
877         mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
878
879         /* LU-2275, simulate broken behaviour (esp. prevalent in
880          * pre-2.4 servers where a very strange reply is sent on error
881          * that looks like it was actually almost succesful and a failure at the
882          * same time */
883         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_NEGATIVE_POSITIVE)) {
884                 mdt_set_disposition(info, rep, DISP_OPEN_OPEN |
885                                                DISP_LOOKUP_NEG |
886                                                DISP_LOOKUP_POS);
887
888                 if (flags & MDS_OPEN_LOCK)
889                         mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
890
891                 RETURN(-ENOENT);
892         }
893
894         if (exp_connect_rmtclient(exp)) {
895                 void *buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
896
897                 rc = mdt_pack_remote_perm(info, o, buf);
898                 if (rc) {
899                         repbody->mbo_valid &= ~OBD_MD_FLRMTPERM;
900                         repbody->mbo_aclsize = 0;
901                 } else {
902                         repbody->mbo_valid |= OBD_MD_FLRMTPERM;
903                         repbody->mbo_aclsize = sizeof(struct mdt_remote_perm);
904                 }
905         }
906 #ifdef CONFIG_FS_POSIX_ACL
907         else if (exp_connect_flags(exp) & OBD_CONNECT_ACL) {
908                 const struct lu_env *env = info->mti_env;
909                 struct md_object *next = mdt_object_child(o);
910                 struct lu_buf *buf = &info->mti_buf;
911
912                 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
913                 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
914                                                    RCL_SERVER);
915                 if (buf->lb_len > 0) {
916                         rc = mo_xattr_get(env, next, buf,
917                                           XATTR_NAME_ACL_ACCESS);
918                         if (rc < 0) {
919                                 if (rc == -ENODATA) {
920                                         repbody->mbo_aclsize = 0;
921                                         repbody->mbo_valid |= OBD_MD_FLACL;
922                                         rc = 0;
923                                 } else if (rc == -EOPNOTSUPP) {
924                                         rc = 0;
925                                 } else {
926                                         CERROR("got acl size: %d\n", rc);
927                                 }
928                         } else {
929                                 repbody->mbo_aclsize = rc;
930                                 repbody->mbo_valid |= OBD_MD_FLACL;
931                                 rc = 0;
932                         }
933                 }
934         }
935 #endif
936
937         if (info->mti_mdt->mdt_lut.lut_mds_capa &&
938             exp_connect_flags(exp) & OBD_CONNECT_MDS_CAPA) {
939                 struct lustre_capa *capa;
940
941                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
942                 LASSERT(capa);
943                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
944                 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
945                 if (rc)
946                         RETURN(rc);
947                 repbody->mbo_valid |= OBD_MD_FLMDSCAPA;
948         }
949         if (info->mti_mdt->mdt_lut.lut_oss_capa &&
950             exp_connect_flags(exp) & OBD_CONNECT_OSS_CAPA &&
951             S_ISREG(lu_object_attr(&o->mot_obj))) {
952                 struct lustre_capa *capa;
953
954                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
955                 LASSERT(capa);
956                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
957                 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
958                 if (rc)
959                         RETURN(rc);
960                 repbody->mbo_valid |= OBD_MD_FLOSSCAPA;
961         }
962
963         /*
964          * If we are following a symlink, don't open; and do not return open
965          * handle for special nodes as client required.
966          */
967         if (islnk || (!isreg && !isdir &&
968             (exp_connect_flags(req->rq_export) & OBD_CONNECT_NODEVOH))) {
969                 lustre_msg_set_transno(req->rq_repmsg, 0);
970                 RETURN(0);
971         }
972
973         /*
974          * We need to return the existing object's fid back, so it is done here,
975          * after preparing the reply.
976          */
977         if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
978                 RETURN(-EEXIST);
979
980         /* This can't be done earlier, we need to return reply body */
981         if (isdir) {
982                 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
983                         /* We are trying to create or write an existing dir. */
984                         RETURN(-EISDIR);
985                 }
986         } else if (flags & MDS_OPEN_DIRECTORY)
987                 RETURN(-ENOTDIR);
988
989         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
990                                  OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
991                 RETURN(-EAGAIN);
992         }
993
994         mfd = NULL;
995         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
996                 spin_lock(&med->med_open_lock);
997                 list_for_each(t, &med->med_open_head) {
998                         mfd = list_entry(t, struct mdt_file_data, mfd_list);
999                         if (mfd->mfd_xid == req->rq_xid)
1000                                 break;
1001                         mfd = NULL;
1002                 }
1003                 spin_unlock(&med->med_open_lock);
1004
1005                 if (mfd != NULL) {
1006                         repbody->mbo_handle.cookie = mfd->mfd_handle.h_cookie;
1007                         /* set repbody->ea_size for resent case */
1008                         if (ma->ma_valid & MA_LOV) {
1009                                 LASSERT(ma->ma_lmm_size != 0);
1010                                 repbody->mbo_eadatasize = ma->ma_lmm_size;
1011                                 if (isdir)
1012                                         repbody->mbo_valid |= OBD_MD_FLDIREA;
1013                                 else
1014                                         repbody->mbo_valid |= OBD_MD_FLEASIZE;
1015                         }
1016                         mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
1017                         RETURN(0);
1018                 }
1019         }
1020
1021         rc = mdt_mfd_open(info, p, o, flags, created, rep);
1022         if (!rc)
1023                 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
1024
1025         RETURN(rc);
1026 }
1027
1028 extern void mdt_req_from_lcd(struct ptlrpc_request *req,
1029                              struct lsd_client_data *lcd);
1030
1031 void mdt_reconstruct_open(struct mdt_thread_info *info,
1032                           struct mdt_lock_handle *lhc)
1033 {
1034         const struct lu_env *env = info->mti_env;
1035         struct mdt_device       *mdt  = info->mti_mdt;
1036         struct req_capsule      *pill = info->mti_pill;
1037         struct ptlrpc_request   *req  = mdt_info_req(info);
1038         struct tg_export_data   *ted  = &req->rq_export->exp_target_data;
1039         struct lsd_client_data  *lcd  = ted->ted_lcd;
1040         struct md_attr          *ma   = &info->mti_attr;
1041         struct mdt_reint_record *rr   = &info->mti_rr;
1042         __u64                   flags = info->mti_spec.sp_cr_flags;
1043         struct ldlm_reply       *ldlm_rep;
1044         struct mdt_object       *parent;
1045         struct mdt_object       *child;
1046         struct mdt_body         *repbody;
1047         int                      rc;
1048         ENTRY;
1049
1050         LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1051         ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
1052         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1053
1054         ma->ma_need = MA_INODE | MA_HSM;
1055         ma->ma_valid = 0;
1056
1057         mdt_req_from_lcd(req, lcd);
1058         mdt_set_disposition(info, ldlm_rep, lcd->lcd_last_data);
1059
1060         CDEBUG(D_INODE, "This is reconstruct open: disp="LPX64", result=%d\n",
1061                ldlm_rep->lock_policy_res1, req->rq_status);
1062
1063         if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
1064             req->rq_status != 0)
1065                 /* We did not create successfully, return error to client. */
1066                 GOTO(out, rc = req->rq_status);
1067
1068         if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
1069                 struct obd_export *exp = req->rq_export;
1070                 /*
1071                  * We failed after creation, but we do not know in which step
1072                  * we failed. So try to check the child object.
1073                  */
1074                 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1075                 if (IS_ERR(parent)) {
1076                         rc = PTR_ERR(parent);
1077                         LCONSOLE_WARN("Parent "DFID" lookup error %d."
1078                                       " Evicting client %s with export %s.\n",
1079                                       PFID(rr->rr_fid1), rc,
1080                                       obd_uuid2str(&exp->exp_client_uuid),
1081                                       obd_export_nid2str(exp));
1082                         mdt_export_evict(exp);
1083                         RETURN_EXIT;
1084                 }
1085
1086                 child = mdt_object_find(env, mdt, rr->rr_fid2);
1087                 if (IS_ERR(child)) {
1088                         rc = PTR_ERR(child);
1089                         LCONSOLE_WARN("cannot lookup child "DFID": rc = %d; "
1090                                       "evicting client %s with export %s\n",
1091                                       PFID(rr->rr_fid2), rc,
1092                                       obd_uuid2str(&exp->exp_client_uuid),
1093                                       obd_export_nid2str(exp));
1094                         mdt_object_put(env, parent);
1095                         mdt_export_evict(exp);
1096                         RETURN_EXIT;
1097                 }
1098
1099                 if (unlikely(mdt_object_remote(child))) {
1100                         /* the child object was created on remote server */
1101                         if (!mdt_is_dne_client(exp)) {
1102                                 /* Return -EIO for old client */
1103                                 mdt_object_put(env, parent);
1104                                 mdt_object_put(env, child);
1105                                 GOTO(out, rc = -EIO);
1106                         }
1107                         repbody->mbo_fid1 = *rr->rr_fid2;
1108                         repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1109                         rc = 0;
1110                 } else {
1111                         if (mdt_object_exists(child)) {
1112                                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1113                                                  BYPASS_CAPA);
1114                                 mdt_prep_ma_buf_from_rep(info, child, ma);
1115                                 rc = mdt_attr_get_complex(info, child, ma);
1116                                 if (rc == 0)
1117                                         rc = mdt_finish_open(info, parent,
1118                                                              child, flags,
1119                                                              1, ldlm_rep);
1120                         } else {
1121                                 /* the child does not exist, we should do
1122                                  * regular open */
1123                                 mdt_object_put(env, parent);
1124                                 mdt_object_put(env, child);
1125                                 GOTO(regular_open, 0);
1126                         }
1127                 }
1128                 mdt_object_put(env, parent);
1129                 mdt_object_put(env, child);
1130                 GOTO(out, rc);
1131         } else {
1132 regular_open:
1133                 /* We did not try to create, so we are a pure open */
1134                 rc = mdt_reint_open(info, lhc);
1135         }
1136
1137         EXIT;
1138 out:
1139         req->rq_status = rc;
1140         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1141         LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
1142 }
1143
1144 int mdt_open_by_fid(struct mdt_thread_info *info, struct ldlm_reply *rep)
1145 {
1146         __u64                    flags = info->mti_spec.sp_cr_flags;
1147         struct mdt_reint_record *rr = &info->mti_rr;
1148         struct md_attr          *ma = &info->mti_attr;
1149         struct mdt_object       *o;
1150         int                      rc;
1151         ENTRY;
1152
1153         o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1154         if (IS_ERR(o))
1155                 RETURN(rc = PTR_ERR(o));
1156
1157         if (unlikely(mdt_object_remote(o))) {
1158                 /* the child object was created on remote server */
1159                 struct mdt_body *repbody;
1160
1161                 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1162                                                 DISP_LOOKUP_EXECD |
1163                                                 DISP_LOOKUP_POS));
1164                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1165                 repbody->mbo_fid1 = *rr->rr_fid2;
1166                 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1167                 rc = 0;
1168         } else {
1169                 if (mdt_object_exists(o)) {
1170                         mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1171                                                         DISP_LOOKUP_EXECD |
1172                                                         DISP_LOOKUP_POS));
1173                         mdt_prep_ma_buf_from_rep(info, o, ma);
1174                         rc = mdt_attr_get_complex(info, o, ma);
1175                         if (rc == 0)
1176                                 rc = mdt_finish_open(info, NULL, o, flags, 0,
1177                                                      rep);
1178                 } else {
1179                         rc = -ENOENT;
1180                 }
1181         }
1182
1183         mdt_object_put(info->mti_env, o);
1184         RETURN(rc);
1185 }
1186
1187 /* lock object for open */
1188 static int mdt_object_open_lock(struct mdt_thread_info *info,
1189                                 struct mdt_object *obj,
1190                                 struct mdt_lock_handle *lhc,
1191                                 __u64 *ibits)
1192 {
1193         struct md_attr  *ma = &info->mti_attr;
1194         __u64            open_flags = info->mti_spec.sp_cr_flags;
1195         ldlm_mode_t      lm = LCK_CR;
1196         bool             acq_lease = !!(open_flags & MDS_OPEN_LEASE);
1197         bool             try_layout = false;
1198         bool             create_layout = false;
1199         int              rc = 0;
1200         ENTRY;
1201
1202         *ibits = 0;
1203         mdt_lock_handle_init(lhc);
1204
1205         if (req_is_replay(mdt_info_req(info)))
1206                 RETURN(0);
1207
1208         if (S_ISREG(lu_object_attr(&obj->mot_obj))) {
1209                 if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV) &&
1210                     md_should_create(open_flags))
1211                         create_layout = true;
1212                 if (exp_connect_layout(info->mti_exp) && !create_layout &&
1213                     ma->ma_need & MA_LOV)
1214                         try_layout = true;
1215         }
1216
1217         if (acq_lease) {
1218                 /* lease open, acquire write mode of open sem */
1219                 down_write(&obj->mot_open_sem);
1220
1221                 /* Lease exists and ask for new lease */
1222                 if (atomic_read(&obj->mot_lease_count) > 0) {
1223                         /* only exclusive open is supported, so lease
1224                          * are conflicted to each other */
1225                         GOTO(out, rc = -EBUSY);
1226                 }
1227
1228                 /* Lease must be with open lock */
1229                 if (!(open_flags & MDS_OPEN_LOCK)) {
1230                         CERROR("Request lease for file:"DFID ", but open lock "
1231                                 "is missed, open_flags = "LPO64".\n",
1232                                 PFID(mdt_object_fid(obj)), open_flags);
1233                         GOTO(out, rc = -EPROTO);
1234                 }
1235
1236                 /* XXX: only exclusive open is supported. */
1237                 lm = LCK_EX;
1238                 *ibits = MDS_INODELOCK_OPEN;
1239
1240                 /* never grant LCK_EX layout lock to client */
1241                 try_layout = false;
1242         } else { /* normal open */
1243                 /* normal open holds read mode of open sem */
1244                 down_read(&obj->mot_open_sem);
1245
1246                 if (open_flags & MDS_OPEN_LOCK) {
1247                         if (open_flags & FMODE_WRITE)
1248                                 lm = LCK_CW;
1249                         else if (open_flags & MDS_FMODE_EXEC)
1250                                 lm = LCK_PR;
1251                         else
1252                                 lm = LCK_CR;
1253
1254                         *ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN;
1255                 } else if (atomic_read(&obj->mot_lease_count) > 0) {
1256                         if (open_flags & FMODE_WRITE)
1257                                 lm = LCK_CW;
1258                         else
1259                                 lm = LCK_CR;
1260
1261                         /* revoke lease */
1262                         *ibits = MDS_INODELOCK_OPEN;
1263                         try_layout = false;
1264
1265                         lhc = &info->mti_lh[MDT_LH_LOCAL];
1266                 }
1267                 CDEBUG(D_INODE, "normal open:"DFID" lease count: %d, lm: %d\n",
1268                         PFID(mdt_object_fid(obj)),
1269                         atomic_read(&obj->mot_open_count), lm);
1270         }
1271
1272         mdt_lock_reg_init(lhc, lm);
1273
1274         /* one problem to return layout lock on open is that it may result
1275          * in too many layout locks cached on the client side. */
1276         if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
1277                 /* return lookup lock to validate inode at the client side,
1278                  * this is pretty important otherwise mdt will return layout
1279                  * lock for each open.
1280                  * However this is a double-edged sword because changing
1281                  * permission will revoke huge # of LOOKUP locks. */
1282                 *ibits |= MDS_INODELOCK_LAYOUT | MDS_INODELOCK_LOOKUP;
1283                 if (!mdt_object_lock_try(info, obj, lhc, *ibits,
1284                                          MDT_CROSS_LOCK)) {
1285                         *ibits &= ~(MDS_INODELOCK_LAYOUT|MDS_INODELOCK_LOOKUP);
1286                         if (*ibits != 0)
1287                                 rc = mdt_object_lock(info, obj, lhc, *ibits,
1288                                                 MDT_CROSS_LOCK);
1289                 }
1290         } else if (*ibits != 0) {
1291                 rc = mdt_object_lock(info, obj, lhc, *ibits, MDT_CROSS_LOCK);
1292         }
1293
1294         CDEBUG(D_INODE, "Requested bits lock:"DFID ", ibits = "LPX64
1295                 ", open_flags = "LPO64", try_layout = %d, rc = %d\n",
1296                 PFID(mdt_object_fid(obj)), *ibits, open_flags, try_layout, rc);
1297
1298         /* will change layout, revoke layout locks by enqueuing EX lock. */
1299         if (rc == 0 && create_layout) {
1300                 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
1301
1302                 CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
1303                         ", open_flags = "LPO64"\n",
1304                         PFID(mdt_object_fid(obj)), open_flags);
1305
1306                 /* We cannot enqueue another lock for the same resource we
1307                  * already have a lock for, due to mechanics of waiting list
1308                  * iterating in ldlm, see LU-3601.
1309                  * As such we'll drop the open lock we just got above here,
1310                  * it's ok not to have this open lock as it's main purpose is to
1311                  * flush unused cached client open handles. */
1312                 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1313                         mdt_object_unlock(info, obj, lhc, 1);
1314
1315                 LASSERT(!try_layout);
1316                 mdt_lock_handle_init(ll);
1317                 mdt_lock_reg_init(ll, LCK_EX);
1318                 rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT,
1319                                         MDT_LOCAL_LOCK);
1320
1321                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
1322         }
1323
1324         /* Check if there is any other open handles after acquiring
1325          * open lock. At this point, caching open handles have been revoked
1326          * by open lock.
1327          * XXX: Now only exclusive open is supported. Need to check the
1328          * type of open for generic lease support. */
1329         if (rc == 0 && acq_lease) {
1330                 struct ptlrpc_request *req = mdt_info_req(info);
1331                 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
1332                 struct mdt_file_data *mfd;
1333                 bool is_replay_or_resent;
1334                 int open_count = 0;
1335
1336                 /* For lease: application can open a file and then apply lease,
1337                  * @handle contains original open handle in that case.
1338                  * In recovery, open REQ will be replayed and the lease REQ may
1339                  * be resent that means the open handle is already stale, so we
1340                  * need to fix it up here by finding new handle. */
1341                 is_replay_or_resent = req_is_replay(req) ||
1342                         lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT;
1343
1344                 /* if the request is _not_ a replay request, rr_handle
1345                  * may be used to hold an openhandle which is issuing the
1346                  * lease request, so that this openhandle doesn't count. */
1347                 mfd = mdt_handle2mfd(med, info->mti_rr.rr_handle,
1348                                      is_replay_or_resent);
1349                 if (mfd != NULL)
1350                         ++open_count;
1351
1352                 CDEBUG(D_INODE, "acq_lease "DFID": openers: %d, want: %d\n",
1353                         PFID(mdt_object_fid(obj)),
1354                         atomic_read(&obj->mot_open_count), open_count);
1355
1356                 if (atomic_read(&obj->mot_open_count) > open_count)
1357                         GOTO(out, rc = -EBUSY);
1358         }
1359         GOTO(out, rc);
1360
1361 out:
1362         RETURN(rc);
1363 }
1364
1365 static void mdt_object_open_unlock(struct mdt_thread_info *info,
1366                                    struct mdt_object *obj,
1367                                    struct mdt_lock_handle *lhc,
1368                                    __u64 ibits, int rc)
1369 {
1370         __u64 open_flags = info->mti_spec.sp_cr_flags;
1371         struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LOCAL];
1372         ENTRY;
1373
1374         if (req_is_replay(mdt_info_req(info)))
1375                 RETURN_EXIT;
1376
1377         /* Release local lock - the lock put in MDT_LH_LOCAL will never
1378          * return to client side. */
1379         if (lustre_handle_is_used(&ll->mlh_reg_lh))
1380                 mdt_object_unlock(info, obj, ll, 1);
1381
1382         ll = &info->mti_lh[MDT_LH_LAYOUT];
1383         /* Release local layout lock, layout was created */
1384         if (lustre_handle_is_used(&ll->mlh_reg_lh)) {
1385                 LASSERT(!(ibits & MDS_INODELOCK_LAYOUT));
1386                 mdt_object_unlock(info, obj, ll, 1);
1387         }
1388
1389         if (open_flags & MDS_OPEN_LEASE)
1390                 up_write(&obj->mot_open_sem);
1391         else
1392                 up_read(&obj->mot_open_sem);
1393
1394         /* Cross-ref case, the lock should be returned to the client */
1395         if (ibits == 0 || rc == -MDT_EREMOTE_OPEN)
1396                 RETURN_EXIT;
1397
1398         if (!(open_flags & MDS_OPEN_LOCK) && !(ibits & MDS_INODELOCK_LAYOUT)) {
1399                 /* for the open request, the lock will only return to client
1400                  * if open or layout lock is granted. */
1401                 rc = 1;
1402         }
1403
1404         if (rc != 0 || !lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1405                 struct ldlm_reply       *ldlm_rep;
1406
1407                 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1408                 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1409                 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1410                         mdt_object_unlock(info, obj, lhc, 1);
1411         }
1412         RETURN_EXIT;
1413 }
1414
1415 /**
1416  * Check release is permitted for the current HSM flags.
1417  */
1418 static bool mdt_hsm_release_allow(const struct md_attr *ma)
1419 {
1420         if (!(ma->ma_valid & MA_HSM))
1421                 return false;
1422
1423         if (ma->ma_hsm.mh_flags & (HS_DIRTY|HS_NORELEASE|HS_LOST))
1424                 return false;
1425
1426         if (!(ma->ma_hsm.mh_flags & HS_ARCHIVED))
1427                 return false;
1428
1429         return true;
1430 }
1431
1432 int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
1433                          struct mdt_lock_handle *lhc)
1434 {
1435         const struct lu_env     *env   = info->mti_env;
1436         struct mdt_device       *mdt   = info->mti_mdt;
1437         __u64                    flags = info->mti_spec.sp_cr_flags;
1438         struct mdt_reint_record *rr    = &info->mti_rr;
1439         struct md_attr          *ma    = &info->mti_attr;
1440         struct mdt_object       *parent= NULL;
1441         struct mdt_object       *o;
1442         int                      rc;
1443         __u64                    ibits = 0;
1444         ENTRY;
1445
1446         if (md_should_create(flags) && !(flags & MDS_OPEN_HAS_EA)) {
1447                 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1448                         parent = mdt_object_find(env, mdt, rr->rr_fid1);
1449                         if (IS_ERR(parent)) {
1450                                 CDEBUG(D_INODE, "Fail to find parent "DFID
1451                                        " for anonymous created %ld, try to"
1452                                        " use server-side parent.\n",
1453                                        PFID(rr->rr_fid1), PTR_ERR(parent));
1454                                 parent = NULL;
1455                         }
1456                 }
1457                 if (parent == NULL)
1458                         ma->ma_need |= MA_PFID;
1459         }
1460
1461         o = mdt_object_find(env, mdt, rr->rr_fid2);
1462         if (IS_ERR(o))
1463                 GOTO(out_parent_put, rc = PTR_ERR(o));
1464
1465         if (mdt_object_remote(o)) {
1466                 CDEBUG(D_INFO, "%s: "DFID" is on remote MDT.\n",
1467                        mdt_obd_name(info->mti_mdt),
1468                        PFID(rr->rr_fid2));
1469                 GOTO(out, rc = -EREMOTE);
1470         } else if (!mdt_object_exists(o)) {
1471                 mdt_set_disposition(info, rep,
1472                                     DISP_IT_EXECD |
1473                                     DISP_LOOKUP_EXECD |
1474                                     DISP_LOOKUP_NEG);
1475                 GOTO(out, rc = -ENOENT);
1476         }
1477
1478         mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1479
1480         mdt_prep_ma_buf_from_rep(info, o, ma);
1481         if (flags & MDS_OPEN_RELEASE)
1482                 ma->ma_need |= MA_HSM;
1483         rc = mdt_attr_get_complex(info, o, ma);
1484         if (rc)
1485                 GOTO(out, rc);
1486
1487         /* If a release request, check file flags are fine and ask for an
1488          * exclusive open access. */
1489         if (flags & MDS_OPEN_RELEASE && !mdt_hsm_release_allow(ma))
1490                 GOTO(out, rc = -EPERM);
1491
1492         rc = mdt_check_resent_lock(info, o, lhc);
1493         if (rc < 0) {
1494                 GOTO(out, rc);
1495         } else if (rc > 0) {
1496                 rc = mdt_object_open_lock(info, o, lhc, &ibits);
1497                 if (rc)
1498                         GOTO(out_unlock, rc);
1499         }
1500
1501         if (ma->ma_valid & MA_PFID) {
1502                 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1503                 if (IS_ERR(parent)) {
1504                         CDEBUG(D_INODE, "Fail to find parent "DFID
1505                                " for anonymous created %ld, try to"
1506                                " use system default.\n",
1507                                PFID(&ma->ma_pfid), PTR_ERR(parent));
1508                         parent = NULL;
1509                 }
1510         }
1511
1512         rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1513         if (!rc) {
1514                 mdt_set_disposition(info, rep, DISP_LOOKUP_POS);
1515                 if (flags & MDS_OPEN_LOCK)
1516                         mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1517                 if (flags & MDS_OPEN_LEASE)
1518                         mdt_set_disposition(info, rep, DISP_OPEN_LEASE);
1519         }
1520         GOTO(out_unlock, rc);
1521
1522 out_unlock:
1523         mdt_object_open_unlock(info, o, lhc, ibits, rc);
1524 out:
1525         mdt_object_put(env, o);
1526 out_parent_put:
1527         if (parent != NULL)
1528                 mdt_object_put(env, parent);
1529         return rc;
1530 }
1531
1532 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1533 static int mdt_cross_open(struct mdt_thread_info *info,
1534                           const struct lu_fid *parent_fid,
1535                           const struct lu_fid *fid,
1536                           struct ldlm_reply *rep, __u32 flags)
1537 {
1538         struct md_attr    *ma = &info->mti_attr;
1539         struct mdt_object *o;
1540         int                rc;
1541         ENTRY;
1542
1543         o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1544         if (IS_ERR(o))
1545                 RETURN(rc = PTR_ERR(o));
1546
1547         if (mdt_object_remote(o)) {
1548                 /* Something is wrong here, the object is on another MDS! */
1549                 CERROR("%s: "DFID" isn't on this server!: rc = %d\n",
1550                        mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1551                 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1552                                 &o->mot_obj,
1553                                 "Object isn't on this server! FLD error?\n");
1554                 rc = -EFAULT;
1555         } else {
1556                 if (mdt_object_exists(o)) {
1557                         /* Do permission check for cross-open. */
1558                         rc = mo_permission(info->mti_env, NULL,
1559                                            mdt_object_child(o),
1560                                            NULL, flags | MDS_OPEN_CROSS);
1561                         if (rc)
1562                                 goto out;
1563
1564                         mdt_prep_ma_buf_from_rep(info, o, ma);
1565                         mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
1566                         rc = mdt_attr_get_complex(info, o, ma);
1567                         if (rc != 0)
1568                                 GOTO(out, rc);
1569
1570                         rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1571                 } else {
1572                         /*
1573                          * Something is wrong here. lookup was positive but
1574                          * there is no object!
1575                          */
1576                         CERROR("%s: "DFID" doesn't exist!: rc = %d\n",
1577                               mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1578                         rc = -EFAULT;
1579                 }
1580         }
1581 out:
1582         mdt_object_put(info->mti_env, o);
1583         RETURN(rc);
1584 }
1585
1586 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1587 {
1588         struct mdt_device       *mdt = info->mti_mdt;
1589         struct ptlrpc_request   *req = mdt_info_req(info);
1590         struct mdt_object       *parent;
1591         struct mdt_object       *child;
1592         struct mdt_lock_handle  *lh;
1593         struct ldlm_reply       *ldlm_rep;
1594         struct mdt_body         *repbody;
1595         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
1596         struct md_attr          *ma = &info->mti_attr;
1597         __u64                    create_flags = info->mti_spec.sp_cr_flags;
1598         __u64                    ibits = 0;
1599         struct mdt_reint_record *rr = &info->mti_rr;
1600         int                      result, rc;
1601         int                      created = 0;
1602         __u32                    msg_flags;
1603         ENTRY;
1604
1605         OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1606                                (obd_timeout + 1) / 4);
1607
1608         mdt_counter_incr(req, LPROC_MDT_OPEN);
1609         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1610
1611         ma->ma_need = MA_INODE;
1612         ma->ma_valid = 0;
1613
1614         LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1615         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1616
1617         if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1618                 CERROR("file join is not supported anymore.\n");
1619                 GOTO(out, result = err_serious(-EOPNOTSUPP));
1620         }
1621         msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1622
1623         if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1624             info->mti_spec.u.sp_ea.eadata == NULL)
1625                 GOTO(out, result = err_serious(-EINVAL));
1626
1627         CDEBUG(D_INODE, "I am going to open "DFID"/("DNAME"->"DFID") "
1628                "cr_flag="LPO64" mode=0%06o msg_flag=0x%x\n",
1629                PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1630                PFID(rr->rr_fid2), create_flags,
1631                ma->ma_attr.la_mode, msg_flags);
1632
1633         if (info->mti_cross_ref) {
1634                 /* This is cross-ref open */
1635                 mdt_set_disposition(info, ldlm_rep,
1636                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD |
1637                              DISP_LOOKUP_POS));
1638                 result = mdt_cross_open(info, rr->rr_fid2, rr->rr_fid1,
1639                                         ldlm_rep, create_flags);
1640                 GOTO(out, result);
1641         } else if (req_is_replay(req) ||
1642             (req->rq_export->exp_libclient && create_flags & MDS_OPEN_HAS_EA)) {
1643                 /* This is a replay request or from liblustre with ea. */
1644                 result = mdt_open_by_fid(info, ldlm_rep);
1645
1646                 if (result != -ENOENT) {
1647                         if (req->rq_export->exp_libclient &&
1648                             create_flags & MDS_OPEN_HAS_EA)
1649                                 GOTO(out, result = 0);
1650                         GOTO(out, result);
1651                 }
1652                 /* We didn't find the correct object, so we need to re-create it
1653                  * via a regular replay. */
1654                 if (!(create_flags & MDS_OPEN_CREAT)) {
1655                         DEBUG_REQ(D_ERROR, req,
1656                                   "OPEN & CREAT not in open replay/by_fid.");
1657                         GOTO(out, result = -EFAULT);
1658                 }
1659                 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1660         } else if (create_flags & (MDS_OPEN_BY_FID | MDS_OPEN_LOCK)) {
1661                 /*
1662                  * MDS_OPEN_LOCK is checked for backward compatibility with 2.1
1663                  * client.
1664                  */
1665                 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1666                 if (result < 0)
1667                         CDEBUG(D_INFO, "no object for "DFID": %d\n",
1668                                PFID(rr->rr_fid2), result);
1669                 GOTO(out, result);
1670         }
1671
1672         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1673                 GOTO(out, result = err_serious(-ENOMEM));
1674
1675         mdt_set_disposition(info, ldlm_rep,
1676                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1677
1678         if (!lu_name_is_valid(&rr->rr_name))
1679                 GOTO(out, result = -EPROTO);
1680
1681         lh = &info->mti_lh[MDT_LH_PARENT];
1682         mdt_lock_pdo_init(lh,
1683                           (create_flags & MDS_OPEN_CREAT) ? LCK_PW : LCK_PR,
1684                           &rr->rr_name);
1685
1686         parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1687                                       MDS_INODELOCK_UPDATE);
1688         if (IS_ERR(parent))
1689                 GOTO(out, result = PTR_ERR(parent));
1690
1691         /* get and check version of parent */
1692         result = mdt_version_get_check(info, parent, 0);
1693         if (result)
1694                 GOTO(out_parent, result);
1695
1696         fid_zero(child_fid);
1697
1698         result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1699                             &rr->rr_name, child_fid, &info->mti_spec);
1700
1701         LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1702                  "looking for "DFID"/"DNAME", found FID = "DFID"\n",
1703                  PFID(mdt_object_fid(parent)), PNAME(&rr->rr_name),
1704                  PFID(child_fid));
1705
1706         if (result != 0 && result != -ENOENT && result != -ESTALE)
1707                 GOTO(out_parent, result);
1708
1709         if (result == -ENOENT || result == -ESTALE) {
1710                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1711                 if (result == -ESTALE) {
1712                         /*
1713                          * -ESTALE means the parent is a dead(unlinked) dir, so
1714                          * it should return -ENOENT to in accordance with the
1715                          * original mds implementaion.
1716                          */
1717                         GOTO(out_parent, result = -ENOENT);
1718                 }
1719                 if (!(create_flags & MDS_OPEN_CREAT))
1720                         GOTO(out_parent, result);
1721                 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
1722                         GOTO(out_parent, result = -EROFS);
1723                 *child_fid = *info->mti_rr.rr_fid2;
1724                 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1725                          PFID(child_fid));
1726                 /* In the function below, .hs_keycmp resolves to
1727                  * lu_obj_hop_keycmp() */
1728                 /* coverity[overrun-buffer-val] */
1729                 child = mdt_object_new(info->mti_env, mdt, child_fid);
1730         } else {
1731                 /*
1732                  * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1733                  * return FID back in that case.
1734                  */
1735                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1736                 child = mdt_object_find(info->mti_env, mdt, child_fid);
1737         }
1738         if (IS_ERR(child))
1739                 GOTO(out_parent, result = PTR_ERR(child));
1740
1741         /** check version of child  */
1742         rc = mdt_version_get_check(info, child, 1);
1743         if (rc)
1744                 GOTO(out_child, result = rc);
1745
1746         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1747         if (result == -ENOENT) {
1748                 /* Create under OBF and .lustre is not permitted */
1749                 if (!fid_is_md_operative(rr->rr_fid1))
1750                         GOTO(out_child, result = -EPERM);
1751
1752                 /* save versions in reply */
1753                 mdt_version_get_save(info, parent, 0);
1754                 mdt_version_get_save(info, child, 1);
1755
1756                 /* version of child will be changed */
1757                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
1758
1759                 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1760                 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1761
1762                 /* Let lower layers know what is lock mode on directory. */
1763                 info->mti_spec.sp_cr_mode =
1764                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1765
1766                 /*
1767                  * Do not perform lookup sanity check. We know that name does
1768                  * not exist.
1769                  */
1770                 info->mti_spec.sp_cr_lookup = 0;
1771                 info->mti_spec.sp_feat = &dt_directory_features;
1772
1773                 result = mdo_create(info->mti_env,
1774                                     mdt_object_child(parent),
1775                                     &rr->rr_name,
1776                                     mdt_object_child(child),
1777                                     &info->mti_spec,
1778                                     &info->mti_attr);
1779                 if (result == -ERESTART) {
1780                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1781                         GOTO(out_child, result);
1782                 } else {
1783                         mdt_prep_ma_buf_from_rep(info, child, ma);
1784                         /* XXX: we should call this once, see few lines below */
1785                         if (result == 0)
1786                                 result = mdt_attr_get_complex(info, child, ma);
1787
1788                         if (result != 0)
1789                                 GOTO(out_child, result);
1790                 }
1791                 created = 1;
1792         } else {
1793                 /*
1794                  * The object is on remote node, return its FID for remote open.
1795                  */
1796                 if (mdt_object_remote(child)) {
1797                         /*
1798                          * Check if this lock already was sent to client and
1799                          * this is resent case. For resent case do not take lock
1800                          * again, use what is already granted.
1801                          */
1802                         LASSERT(lhc != NULL);
1803
1804                         rc = mdt_check_resent_lock(info, child, lhc);
1805                         if (rc < 0) {
1806                                 GOTO(out_child, result = rc);
1807                         } else if (rc > 0) {
1808                                 mdt_lock_handle_init(lhc);
1809                                 mdt_lock_reg_init(lhc, LCK_PR);
1810
1811                                 rc = mdt_object_lock(info, child, lhc,
1812                                                      MDS_INODELOCK_LOOKUP,
1813                                                      MDT_CROSS_LOCK);
1814                         }
1815                         repbody->mbo_fid1 = *mdt_object_fid(child);
1816                         repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1817                         if (rc != 0)
1818                                 result = rc;
1819                         else
1820                                 result = -MDT_EREMOTE_OPEN;
1821                         GOTO(out_child, result);
1822                 } else if (mdt_object_exists(child)) {
1823                         /* We have to get attr & LOV EA & HSM for this
1824                          * object. */
1825                         mdt_prep_ma_buf_from_rep(info, child, ma);
1826                         ma->ma_need |= MA_HSM;
1827                         result = mdt_attr_get_complex(info, child, ma);
1828                 } else {
1829                         /* Object does not exist. Likely FS corruption. */
1830                         CERROR("%s: name '"DNAME"' present, but FID "
1831                                DFID" is invalid\n", mdt_obd_name(info->mti_mdt),
1832                                PNAME(&rr->rr_name), PFID(child_fid));
1833                         GOTO(out_child, result = -EIO);
1834                 }
1835         }
1836
1837         rc = mdt_check_resent_lock(info, child, lhc);
1838         if (rc < 0) {
1839                 GOTO(out_child, result = rc);
1840         } else if (rc == 0) {
1841                 /* the open lock might already be gotten in
1842                  * ldlm_handle_enqueue() */
1843                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1844                 if (create_flags & MDS_OPEN_LOCK)
1845                         mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1846         } else {
1847                 /* get openlock if this isn't replay and client requested it */
1848                 if (!req_is_replay(req)) {
1849                         rc = mdt_object_open_lock(info, child, lhc, &ibits);
1850                         if (rc != 0)
1851                                 GOTO(out_child_unlock, result = rc);
1852                         else if (create_flags & MDS_OPEN_LOCK)
1853                                 mdt_set_disposition(info, ldlm_rep,
1854                                                     DISP_OPEN_LOCK);
1855                 }
1856         }
1857         /* Try to open it now. */
1858         rc = mdt_finish_open(info, parent, child, create_flags,
1859                              created, ldlm_rep);
1860         if (rc) {
1861                 result = rc;
1862                 /* openlock will be released if mdt_finish_open failed */
1863                 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1864
1865                 if (created && create_flags & MDS_OPEN_VOLATILE) {
1866                         CERROR("%s: cannot open volatile file "DFID", orphan "
1867                                "file will be left in PENDING directory until "
1868                                "next reboot, rc = %d\n", mdt_obd_name(mdt),
1869                                PFID(mdt_object_fid(child)), rc);
1870                         GOTO(out_child_unlock, result);
1871                 }
1872
1873                 if (created) {
1874                         ma->ma_need = 0;
1875                         ma->ma_valid = 0;
1876                         rc = mdo_unlink(info->mti_env,
1877                                         mdt_object_child(parent),
1878                                         mdt_object_child(child),
1879                                         &rr->rr_name,
1880                                         &info->mti_attr, 0);
1881                         if (rc != 0)
1882                                 CERROR("%s: "DFID" cleanup of open: rc = %d\n",
1883                                        mdt_obd_name(info->mti_mdt),
1884                                        PFID(mdt_object_fid(child)), rc);
1885                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1886                 }
1887         }
1888         EXIT;
1889 out_child_unlock:
1890         mdt_object_open_unlock(info, child, lhc, ibits, result);
1891 out_child:
1892         mdt_object_put(info->mti_env, child);
1893 out_parent:
1894         mdt_object_unlock_put(info, parent, lh, result || !created);
1895 out:
1896         if (result)
1897                 lustre_msg_set_transno(req->rq_repmsg, 0);
1898         return result;
1899 }
1900
1901 /**
1902  * Create an orphan object use local root.
1903  */
1904 static struct mdt_object *mdt_orphan_open(struct mdt_thread_info *info,
1905                                           struct mdt_device *mdt,
1906                                           const struct lu_fid *fid,
1907                                           struct md_attr *attr, fmode_t fmode)
1908 {
1909         const struct lu_env *env = info->mti_env;
1910         struct md_op_spec *spec = &info->mti_spec;
1911         struct lu_fid *local_root_fid = &info->mti_tmp_fid1;
1912         struct mdt_object *obj = NULL;
1913         struct mdt_object *local_root;
1914         static const struct lu_name lname = {
1915                 .ln_name = "i_am_nobody",
1916                 .ln_namelen = sizeof("i_am_nobody") - 1,
1917         };
1918         struct lu_ucred *uc;
1919         cfs_cap_t uc_cap_save;
1920         int rc;
1921         ENTRY;
1922
1923         rc = dt_root_get(env, mdt->mdt_bottom, local_root_fid);
1924         if (rc != 0)
1925                 RETURN(ERR_PTR(rc));
1926
1927         local_root = mdt_object_find(env, mdt, local_root_fid);
1928         if (IS_ERR(local_root))
1929                 RETURN(local_root);
1930
1931         obj = mdt_object_new(env, mdt, fid);
1932         if (IS_ERR(obj))
1933                 GOTO(out, rc = PTR_ERR(obj));
1934
1935         spec->sp_cr_lookup = 0;
1936         spec->sp_feat = &dt_directory_features;
1937         spec->sp_cr_mode = MDL_MINMODE; /* no lock */
1938         spec->sp_cr_flags = MDS_OPEN_VOLATILE | fmode;
1939         if (attr->ma_valid & MA_LOV) {
1940                 spec->u.sp_ea.eadata = attr->ma_lmm;
1941                 spec->u.sp_ea.eadatalen = attr->ma_lmm_size;
1942                 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1943         } else {
1944                 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
1945         }
1946
1947         uc = lu_ucred(env);
1948         uc_cap_save = uc->uc_cap;
1949         uc->uc_cap |= 1 << CFS_CAP_DAC_OVERRIDE;
1950         rc = mdo_create(env, mdt_object_child(local_root), &lname,
1951                         mdt_object_child(obj), spec, attr);
1952         uc->uc_cap = uc_cap_save;
1953         if (rc < 0) {
1954                 CERROR("%s: cannot create volatile file "DFID": rc = %d\n",
1955                        mdt_obd_name(mdt), PFID(fid), rc);
1956                 GOTO(out, rc);
1957         }
1958
1959         rc = mo_open(env, mdt_object_child(obj), MDS_OPEN_CREATED);
1960         if (rc < 0)
1961                 CERROR("%s: cannot open volatile file "DFID", orphan "
1962                        "file will be left in PENDING directory until "
1963                        "next reboot, rc = %d\n", mdt_obd_name(mdt),
1964                        PFID(fid), rc);
1965         GOTO(out, rc);
1966
1967 out:
1968         if (rc < 0) {
1969                 if (!IS_ERR(obj))
1970                         mdt_object_put(env, obj);
1971                 obj = ERR_PTR(rc);
1972         }
1973         mdt_object_put(env, local_root);
1974         return obj;
1975 }
1976
1977 static int mdt_hsm_release(struct mdt_thread_info *info, struct mdt_object *o,
1978                            struct md_attr *ma)
1979 {
1980         struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LAYOUT];
1981         struct close_data      *data;
1982         struct ldlm_lock       *lease;
1983         struct mdt_object      *orphan;
1984         struct md_attr         *orp_ma;
1985         struct lu_buf          *buf;
1986         bool                    lease_broken;
1987         int                     rc;
1988         int                     rc2;
1989         ENTRY;
1990
1991         if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
1992                 RETURN(-EROFS);
1993
1994         data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1995         if (data == NULL)
1996                 RETURN(-EPROTO);
1997
1998         lease = ldlm_handle2lock(&data->cd_handle);
1999         if (lease == NULL)
2000                 RETURN(-ESTALE);
2001
2002         /* try to hold open_sem so that nobody else can open the file */
2003         if (!down_write_trylock(&o->mot_open_sem)) {
2004                 ldlm_lock_cancel(lease);
2005                 GOTO(out_reprocess, rc = -EBUSY);
2006         }
2007
2008         /* Check if the lease open lease has already canceled */
2009         lock_res_and_lock(lease);
2010         lease_broken = ldlm_is_cancel(lease);
2011         unlock_res_and_lock(lease);
2012
2013         LDLM_DEBUG(lease, DFID " lease broken? %d\n",
2014                    PFID(mdt_object_fid(o)), lease_broken);
2015
2016         /* Cancel server side lease. Client side counterpart should
2017          * have been cancelled. It's okay to cancel it now as we've
2018          * held mot_open_sem. */
2019         ldlm_lock_cancel(lease);
2020
2021         if (lease_broken) /* don't perform release task */
2022                 GOTO(out_unlock, rc = -ESTALE);
2023
2024         if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
2025                 GOTO(out_unlock, rc = -EINVAL);
2026
2027         /* ma_need was set before but it seems fine to change it in order to
2028          * avoid modifying the one from RPC */
2029         ma->ma_need = MA_HSM;
2030         rc = mdt_attr_get_complex(info, o, ma);
2031         if (rc != 0)
2032                 GOTO(out_unlock, rc);
2033
2034         if (!mdt_hsm_release_allow(ma))
2035                 GOTO(out_unlock, rc = -EPERM);
2036
2037         /* already released? */
2038         if (ma->ma_hsm.mh_flags & HS_RELEASED)
2039                 GOTO(out_unlock, rc = 0);
2040
2041         /* Compare on-disk and packed data_version */
2042         if (data->cd_data_version != ma->ma_hsm.mh_arch_ver) {
2043                 CDEBUG(D_HSM, DFID" data_version mismatches: packed="LPU64
2044                        " and on-disk="LPU64"\n", PFID(mdt_object_fid(o)),
2045                        data->cd_data_version, ma->ma_hsm.mh_arch_ver);
2046                 GOTO(out_unlock, rc = -EPERM);
2047         }
2048
2049         ma->ma_valid = MA_INODE;
2050         ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME | LA_SIZE;
2051         rc = mo_attr_set(info->mti_env, mdt_object_child(o), ma);
2052         if (rc < 0)
2053                 GOTO(out_unlock, rc);
2054
2055         ma->ma_need = MA_INODE | MA_LOV;
2056         rc = mdt_attr_get_complex(info, o, ma);
2057         if (rc < 0)
2058                 GOTO(out_unlock, rc);
2059
2060         if (!(ma->ma_valid & MA_LOV)) {
2061                 /* Even empty file are released */
2062                 memset(ma->ma_lmm, 0, sizeof(*ma->ma_lmm));
2063                 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
2064                 ma->ma_lmm->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
2065                 ma->ma_lmm->lmm_stripe_size = cpu_to_le32(LOV_MIN_STRIPE_SIZE);
2066                 ma->ma_lmm_size = sizeof(*ma->ma_lmm);
2067         } else {
2068                 /* Magic must be LOV_MAGIC_Vx_DEF otherwise LOD will interpret
2069                  * ma_lmm as lov_user_md, then it will be confused by union of
2070                  * layout_gen and stripe_offset. */
2071                 if (le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V1)
2072                         ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
2073                 else if (le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V3)
2074                         ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V3_DEF);
2075                 else
2076                         GOTO(out_unlock, rc = -EINVAL);
2077         }
2078
2079         /* Set file as released */
2080         ma->ma_lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
2081
2082         /* Hopefully it's not used in this call path */
2083         orp_ma = &info->mti_u.som.attr;
2084         orp_ma->ma_attr.la_mode = S_IFREG | S_IWUSR;
2085         orp_ma->ma_attr.la_uid = ma->ma_attr.la_uid;
2086         orp_ma->ma_attr.la_gid = ma->ma_attr.la_gid;
2087         orp_ma->ma_attr.la_valid = LA_MODE | LA_UID | LA_GID;
2088         orp_ma->ma_lmm = ma->ma_lmm;
2089         orp_ma->ma_lmm_size = ma->ma_lmm_size;
2090         orp_ma->ma_valid = MA_INODE | MA_LOV;
2091         orphan = mdt_orphan_open(info, info->mti_mdt, &data->cd_fid, orp_ma,
2092                                  FMODE_WRITE);
2093         if (IS_ERR(orphan)) {
2094                 CERROR("%s: cannot open orphan file "DFID": rc = %ld\n",
2095                        mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid),
2096                        PTR_ERR(orphan));
2097                 GOTO(out_unlock, rc = PTR_ERR(orphan));
2098         }
2099
2100         /* Set up HSM attribute for orphan object */
2101         CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
2102         buf = &info->mti_buf;
2103         buf->lb_buf = info->mti_xattr_buf;
2104         buf->lb_len = sizeof(struct hsm_attrs);
2105         ma->ma_hsm.mh_flags |= HS_RELEASED;
2106         lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
2107         ma->ma_hsm.mh_flags &= ~HS_RELEASED;
2108
2109         mdt_lock_reg_init(lh, LCK_EX);
2110         rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_LAYOUT |
2111                              MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
2112         if (rc != 0)
2113                 GOTO(out_close, rc);
2114
2115         rc = mo_xattr_set(info->mti_env, mdt_object_child(orphan), buf,
2116                           XATTR_NAME_HSM, 0);
2117
2118         if (rc == 0)
2119                 /* Swap layout with orphan object */
2120                 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o),
2121                                      mdt_object_child(orphan),
2122                                      SWAP_LAYOUTS_MDS_HSM);
2123
2124         /* Release exclusive LL */
2125         mdt_object_unlock(info, o, lh, 1);
2126
2127         EXIT;
2128
2129 out_close:
2130         /* Close orphan object anyway */
2131         rc2 = mo_close(info->mti_env, mdt_object_child(orphan), orp_ma,
2132                        FMODE_WRITE);
2133         if (rc2 < 0)
2134                 CERROR("%s: error closing volatile file "DFID": rc = %d\n",
2135                        mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid), rc2);
2136         LU_OBJECT_DEBUG(D_HSM, info->mti_env, &orphan->mot_obj,
2137                         "object closed\n");
2138         mdt_object_put(info->mti_env, orphan);
2139
2140 out_unlock:
2141         up_write(&o->mot_open_sem);
2142
2143         if (rc == 0) { /* already released */
2144                 struct mdt_body *repbody;
2145                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2146                 LASSERT(repbody != NULL);
2147                 repbody->mbo_valid |= OBD_MD_FLRELEASED;
2148         }
2149
2150 out_reprocess:
2151         ldlm_reprocess_all(lease->l_resource);
2152         LDLM_LOCK_PUT(lease);
2153
2154         ma->ma_valid = 0;
2155         ma->ma_need = 0;
2156
2157         return rc;
2158 }
2159
2160 #define MFD_CLOSED(mode) (((mode) & ~(MDS_FMODE_EPOCH | MDS_FMODE_SOM | \
2161                                       MDS_FMODE_TRUNC)) == MDS_FMODE_CLOSED)
2162
2163 static int mdt_mfd_closed(struct mdt_file_data *mfd)
2164 {
2165         return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
2166 }
2167
2168 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
2169 {
2170         struct mdt_object *o = mfd->mfd_object;
2171         struct md_object *next = mdt_object_child(o);
2172         struct md_attr *ma = &info->mti_attr;
2173         int ret = MDT_IOEPOCH_CLOSED;
2174         int rc = 0;
2175         __u64 mode;
2176         ENTRY;
2177
2178         mode = mfd->mfd_mode;
2179
2180         if (ma->ma_attr_flags & MDS_HSM_RELEASE) {
2181                 rc = mdt_hsm_release(info, o, ma);
2182                 if (rc < 0) {
2183                         CDEBUG(D_HSM, "%s: File " DFID " release failed: %d\n",
2184                                 mdt_obd_name(info->mti_mdt),
2185                                 PFID(mdt_object_fid(o)), rc);
2186                         /* continue to close even error occurred. */
2187                 }
2188         }
2189
2190         if ((mode & FMODE_WRITE) || (mode & MDS_FMODE_TRUNC)) {
2191                 mdt_write_put(o);
2192                 ret = mdt_ioepoch_close(info, o);
2193         } else if (mode & MDS_FMODE_EXEC) {
2194                 mdt_write_allow(o);
2195         } else if (mode & MDS_FMODE_EPOCH) {
2196                 ret = mdt_ioepoch_close(info, o);
2197         } else if (mode & MDS_FMODE_SOM) {
2198                 ret = mdt_som_au_close(info, o);
2199         }
2200
2201         /* Update atime on close only. */
2202         if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
2203             && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
2204                 /* Set the atime only. */
2205                 ma->ma_valid = MA_INODE;
2206                 ma->ma_attr.la_valid = LA_ATIME;
2207                 rc = mo_attr_set(info->mti_env, next, ma);
2208         }
2209
2210         /* If file data is modified, add the dirty flag. */
2211         if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
2212                 rc = mdt_add_dirty_flag(info, o, ma);
2213
2214         ma->ma_need |= MA_INODE;
2215         ma->ma_valid &= ~MA_INODE;
2216
2217         if (!MFD_CLOSED(mode))
2218                 rc = mo_close(info->mti_env, next, ma, mode);
2219
2220         if (ret == MDT_IOEPOCH_GETATTR || ret == MDT_IOEPOCH_OPENED) {
2221                 struct mdt_export_data *med;
2222
2223                 /* The IOepoch is still opened or SOM update is needed.
2224                  * Put mfd back into the list. */
2225                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
2226                 mdt_mfd_set_mode(mfd, ret == MDT_IOEPOCH_OPENED ?
2227                                       MDS_FMODE_EPOCH : MDS_FMODE_SOM);
2228
2229                 LASSERT(mdt_info_req(info));
2230                 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
2231                 spin_lock(&med->med_open_lock);
2232                 list_add(&mfd->mfd_list, &med->med_open_head);
2233                 class_handle_hash_back(&mfd->mfd_handle);
2234                 spin_unlock(&med->med_open_lock);
2235
2236                 if (ret == MDT_IOEPOCH_OPENED) {
2237                         ret = 0;
2238                 } else {
2239                         ret = -EAGAIN;
2240                         CDEBUG(D_INODE, "Size-on-MDS attribute update is "
2241                                "needed on "DFID"\n", PFID(mdt_object_fid(o)));
2242                 }
2243         } else {
2244                 /* adjust open and lease count */
2245                 if (mode & MDS_OPEN_LEASE) {
2246                         LASSERT(atomic_read(&o->mot_lease_count) > 0);
2247                         atomic_dec(&o->mot_lease_count);
2248                 }
2249                 LASSERT(atomic_read(&o->mot_open_count) > 0);
2250                 atomic_dec(&o->mot_open_count);
2251
2252                 mdt_mfd_free(mfd);
2253                 mdt_object_put(info->mti_env, o);
2254         }
2255
2256         RETURN(rc ? rc : ret);
2257 }
2258
2259 int mdt_close(struct tgt_session_info *tsi)
2260 {
2261         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
2262         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2263         struct mdt_export_data *med;
2264         struct mdt_file_data   *mfd;
2265         struct mdt_object      *o;
2266         struct md_attr         *ma = &info->mti_attr;
2267         struct mdt_body        *repbody = NULL;
2268         int rc, ret = 0;
2269         ENTRY;
2270
2271         mdt_counter_incr(req, LPROC_MDT_CLOSE);
2272         /* Close may come with the Size-on-MDS update. Unpack it. */
2273         rc = mdt_close_unpack(info);
2274         if (rc)
2275                 GOTO(out, rc = err_serious(rc));
2276
2277         LASSERT(info->mti_ioepoch);
2278
2279         /* These fields are no longer used and are left for compatibility.
2280          * size is always zero */
2281         req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
2282                              0);
2283         req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
2284                              0);
2285         rc = req_capsule_server_pack(info->mti_pill);
2286         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
2287                 mdt_client_compatibility(info);
2288                 if (rc == 0)
2289                         mdt_fix_reply(info);
2290                 mdt_exit_ucred(info);
2291                 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2292         }
2293
2294         /* Continue to close handle even if we can not pack reply */
2295         if (rc == 0) {
2296                 repbody = req_capsule_server_get(info->mti_pill,
2297                                                  &RMF_MDT_BODY);
2298                 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
2299                                                     &RMF_MDT_MD);
2300                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
2301                                                        &RMF_MDT_MD,
2302                                                        RCL_SERVER);
2303                 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
2304                 repbody->mbo_eadatasize = 0;
2305                 repbody->mbo_aclsize = 0;
2306         } else {
2307                 rc = err_serious(rc);
2308         }
2309
2310         med = &req->rq_export->exp_mdt_data;
2311         spin_lock(&med->med_open_lock);
2312         mfd = mdt_handle2mfd(med, &info->mti_ioepoch->handle,
2313                              req_is_replay(req));
2314         if (mdt_mfd_closed(mfd)) {
2315                 spin_unlock(&med->med_open_lock);
2316                 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
2317                        ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
2318                        info->mti_ioepoch->handle.cookie);
2319                 /** not serious error since bug 3633 */
2320                 rc = -ESTALE;
2321         } else {
2322                 class_handle_unhash(&mfd->mfd_handle);
2323                 list_del_init(&mfd->mfd_list);
2324                 spin_unlock(&med->med_open_lock);
2325
2326                 /* Do not lose object before last unlink. */
2327                 o = mfd->mfd_object;
2328                 mdt_object_get(info->mti_env, o);
2329                 ret = mdt_mfd_close(info, mfd);
2330                 if (repbody != NULL)
2331                         rc = mdt_handle_last_unlink(info, o, ma);
2332                 mdt_empty_transno(info, rc);
2333                 mdt_object_put(info->mti_env, o);
2334         }
2335         if (repbody != NULL) {
2336                 mdt_client_compatibility(info);
2337                 rc = mdt_fix_reply(info);
2338         }
2339
2340         mdt_exit_ucred(info);
2341         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
2342                 GOTO(out, rc = err_serious(-ENOMEM));
2343
2344         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
2345                                  OBD_FAIL_MDS_CLOSE_NET_REP))
2346                 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
2347 out:
2348         mdt_thread_info_fini(info);
2349         RETURN(rc ? rc : ret);
2350 }
2351
2352 /**
2353  * DONE_WRITING rpc handler.
2354  *
2355  * As mfd is not kept after replayed CLOSE (see mdt_ioepoch_close_on_replay()),
2356  * only those DONE_WRITING rpc will be replayed which really wrote smth on disk,
2357  * and got a trasid. Waiting for such DONE_WRITING is not reliable, so just
2358  * skip attributes and reconstruct the reply here.
2359  */
2360 int mdt_done_writing(struct tgt_session_info *tsi)
2361 {
2362         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2363         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
2364         struct mdt_body         *repbody = NULL;
2365         struct mdt_export_data  *med;
2366         struct mdt_file_data    *mfd;
2367         int rc;
2368         ENTRY;
2369
2370         rc = req_capsule_server_pack(tsi->tsi_pill);
2371         if (rc)
2372                 GOTO(out, rc = err_serious(rc));
2373
2374         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2375         repbody->mbo_eadatasize = 0;
2376         repbody->mbo_aclsize = 0;
2377
2378         /* Done Writing may come with the Size-on-MDS update. Unpack it. */
2379         rc = mdt_close_unpack(info);
2380         if (rc)
2381                 GOTO(out, rc = err_serious(rc));
2382
2383         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
2384                 mdt_exit_ucred(info);
2385                 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2386         }
2387
2388         med = &info->mti_exp->exp_mdt_data;
2389         spin_lock(&med->med_open_lock);
2390         mfd = mdt_handle2mfd(med, &info->mti_ioepoch->handle,
2391                              req_is_replay(req));
2392         if (mfd == NULL) {
2393                 spin_unlock(&med->med_open_lock);
2394                 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
2395                        ": cookie = "LPX64" ioepoch = "LPU64"\n",
2396                        PFID(info->mti_rr.rr_fid1),
2397                        info->mti_ioepoch->handle.cookie,
2398                        info->mti_ioepoch->ioepoch);
2399                 /* If this is a replay, reconstruct the transno. */
2400                 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2401                         rc = info->mti_ioepoch->flags & MF_SOM_AU ?
2402                              -EAGAIN : 0;
2403                         mdt_empty_transno(info, rc);
2404                 } else
2405                         rc = -ESTALE;
2406                 GOTO(error_ucred, rc);
2407         }
2408
2409         LASSERT(mfd->mfd_mode == MDS_FMODE_EPOCH ||
2410                 mfd->mfd_mode == MDS_FMODE_TRUNC);
2411         class_handle_unhash(&mfd->mfd_handle);
2412         list_del_init(&mfd->mfd_list);
2413         spin_unlock(&med->med_open_lock);
2414
2415         /* Set EPOCH CLOSE flag if not set by client. */
2416         info->mti_ioepoch->flags |= MF_EPOCH_CLOSE;
2417         info->mti_attr.ma_valid = 0;
2418
2419         info->mti_attr.ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
2420         OBD_ALLOC_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
2421         if (info->mti_attr.ma_lmm == NULL)
2422                 GOTO(error_ucred, rc = -ENOMEM);
2423
2424         rc = mdt_mfd_close(info, mfd);
2425
2426         OBD_FREE_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
2427         mdt_empty_transno(info, rc);
2428 error_ucred:
2429         mdt_exit_ucred(info);
2430 out:
2431         mdt_thread_info_fini(info);
2432         RETURN(rc);
2433 }