Whamcloud - gitweb
6ac5eeef51e1d6a0bbf4eccc758b39f96cc9e9a6
[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(void)
62 {
63         struct mdt_file_data *mfd;
64         ENTRY;
65
66         OBD_ALLOC_PTR(mfd);
67         if (mfd != NULL) {
68                 CFS_INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
69                 CFS_INIT_LIST_HEAD(&mfd->mfd_list);
70                 class_handle_hash(&mfd->mfd_handle, &mfd_handle_ops);
71         }
72         RETURN(mfd);
73 }
74
75 /*
76  * Find the mfd pointed to by handle in global hash table.
77  * In case of replay the handle is obsoleted
78  * but mfd can be found in mfd list by that handle
79  */
80 struct mdt_file_data *mdt_handle2mfd(struct mdt_thread_info *info,
81                                      const struct lustre_handle *handle)
82 {
83         struct ptlrpc_request *req = mdt_info_req(info);
84         struct mdt_file_data  *mfd;
85         ENTRY;
86
87         LASSERT(handle != NULL);
88         mfd = class_handle2object(handle->cookie);
89         /* during dw/setattr replay the mfd can be found by old handle */
90         if (mfd == NULL && req_is_replay(req)) {
91                 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
92                 cfs_list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
93                         if (mfd->mfd_old_handle.cookie == handle->cookie)
94                                 RETURN (mfd);
95                 }
96                 mfd = NULL;
97         }
98         RETURN (mfd);
99 }
100
101 /* free mfd */
102 void mdt_mfd_free(struct mdt_file_data *mfd)
103 {
104         LASSERT(cfs_list_empty(&mfd->mfd_list));
105         OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
106 }
107
108 static int mdt_create_data(struct mdt_thread_info *info,
109                            struct mdt_object *p, struct mdt_object *o)
110 {
111         struct md_op_spec     *spec = &info->mti_spec;
112         struct md_attr        *ma   = &info->mti_attr;
113         int                    rc   = 0;
114         ENTRY;
115
116         if (!md_should_create(spec->sp_cr_flags))
117                 RETURN(0);
118
119         ma->ma_need = MA_INODE | MA_LOV;
120         ma->ma_valid = 0;
121         mutex_lock(&o->mot_lov_mutex);
122         if (!(o->mot_flags & MOF_LOV_CREATED)) {
123                 if (p != NULL && (fid_is_obf(mdt_object_fid(p)) ||
124                                   fid_is_dot_lustre(mdt_object_fid(p))))
125                         GOTO(unlock, rc = -EPERM);
126
127                 rc = mdo_create_data(info->mti_env,
128                                      p ? mdt_object_child(p) : NULL,
129                                      mdt_object_child(o), spec, ma);
130                 if (rc == 0)
131                         rc = mdt_attr_get_complex(info, o, ma);
132
133                 if (rc == 0 && ma->ma_valid & MA_LOV)
134                         o->mot_flags |= MOF_LOV_CREATED;
135         }
136 unlock:
137         mutex_unlock(&o->mot_lov_mutex);
138         RETURN(rc);
139 }
140
141 static int mdt_ioepoch_opened(struct mdt_object *mo)
142 {
143         return mo->mot_ioepoch_count;
144 }
145
146 int mdt_object_is_som_enabled(struct mdt_object *mo)
147 {
148         return !mo->mot_ioepoch;
149 }
150
151 /**
152  * Re-enable Size-on-MDS.
153  * Call under ->mot_ioepoch_mutex.
154  */
155 static void mdt_object_som_enable(struct mdt_object *mo, __u64 ioepoch)
156 {
157         if (ioepoch == mo->mot_ioepoch) {
158                 LASSERT(!mdt_ioepoch_opened(mo));
159                 mo->mot_ioepoch = 0;
160                 mo->mot_flags = 0;
161         }
162 }
163
164 /**
165  * Open the IOEpoch. It is allowed if @writecount is not negative.
166  * The epoch and writecount handling is performed under the mot_ioepoch_mutex.
167  */
168 int mdt_ioepoch_open(struct mdt_thread_info *info, struct mdt_object *o,
169                      int created)
170 {
171         struct mdt_device *mdt = info->mti_mdt;
172         int cancel = 0;
173         int rc = 0;
174         ENTRY;
175
176         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
177             !S_ISREG(lu_object_attr(&o->mot_obj)))
178                 RETURN(0);
179
180         mutex_lock(&o->mot_ioepoch_mutex);
181         if (mdt_ioepoch_opened(o)) {
182                 /* Epoch continues even if there is no writers yet. */
183                 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
184                        o->mot_ioepoch, PFID(mdt_object_fid(o)));
185         } else {
186                 /* XXX: ->mdt_ioepoch is not initialized at the mount */
187                 spin_lock(&mdt->mdt_ioepoch_lock);
188                 if (mdt->mdt_ioepoch < info->mti_replayepoch)
189                         mdt->mdt_ioepoch = info->mti_replayepoch;
190
191                 if (info->mti_replayepoch)
192                         o->mot_ioepoch = info->mti_replayepoch;
193                 else if (++mdt->mdt_ioepoch == IOEPOCH_INVAL)
194                         o->mot_ioepoch = ++mdt->mdt_ioepoch;
195                 else
196                         o->mot_ioepoch = mdt->mdt_ioepoch;
197
198                 spin_unlock(&mdt->mdt_ioepoch_lock);
199
200                 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
201                        o->mot_ioepoch, PFID(mdt_object_fid(o)));
202                 if (created)
203                         o->mot_flags |= MOF_SOM_CREATED;
204                 cancel = 1;
205         }
206         o->mot_ioepoch_count++;
207         mutex_unlock(&o->mot_ioepoch_mutex);
208
209         /* Cancel Size-on-MDS attributes cached on clients for the open case.
210          * In the truncate case, see mdt_reint_setattr(). */
211         if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
212                 struct mdt_lock_handle  *lh = &info->mti_lh[MDT_LH_CHILD];
213                 mdt_lock_reg_init(lh, LCK_EX);
214                 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
215                                      MDT_LOCAL_LOCK);
216                 if (rc == 0)
217                         mdt_object_unlock(info, o, lh, 1);
218         }
219         RETURN(rc);
220 }
221
222 /**
223  * Update SOM on-disk attributes.
224  * If enabling, write update inodes and lustre-ea with the proper IOEpoch,
225  * mountid and attributes. If disabling, clean SOM xattr.
226  * Call under ->mot_ioepoch_mutex.
227  */
228 static int mdt_som_attr_set(struct mdt_thread_info *info,
229                             struct mdt_object *obj, __u64 ioepoch, bool enable)
230 {
231         struct md_object        *next = mdt_object_child(obj);
232         int                      rc;
233         ENTRY;
234
235         CDEBUG(D_INODE, "Size-on-MDS attribute %s for epoch "LPU64
236                " on "DFID".\n", enable ? "update" : "disabling",
237                ioepoch, PFID(mdt_object_fid(obj)));
238
239         if (enable) {
240                 struct lu_buf           *buf = &info->mti_buf;
241                 struct som_attrs        *attrs;
242                 struct md_attr          *ma = &info->mti_attr;
243                 struct lu_attr          *la = &ma->ma_attr;
244                 struct obd_device       *obd = info->mti_mdt->mdt_lut.lut_obd;
245
246                 attrs = (struct som_attrs *)info->mti_xattr_buf;
247                 CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*attrs));
248
249                 /* pack SOM attributes */
250                 memset(attrs, 0, sizeof(*attrs));
251                 attrs->som_ioepoch = ioepoch;
252                 attrs->som_mountid = obd->u.obt.obt_mount_count;
253                 if ((la->la_valid & LA_SIZE) != 0)
254                         attrs->som_size = la->la_size;
255                 if ((la->la_valid & LA_BLOCKS) != 0)
256                         attrs->som_blocks = la->la_blocks;
257                 lustre_som_swab(attrs);
258
259                 /* update SOM attributes */
260                 buf->lb_buf = attrs;
261                 buf->lb_len = sizeof(*attrs);
262                 rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_SOM, 0);
263         } else {
264                 /* delete SOM attributes */
265                 rc = mo_xattr_del(info->mti_env, next, XATTR_NAME_SOM);
266         }
267
268         RETURN(rc);
269 }
270
271 /** Perform the eviction specific actions on ioepoch close. */
272 static inline int mdt_ioepoch_close_on_eviction(struct mdt_thread_info *info,
273                                                 struct mdt_object *o)
274 {
275         int rc = 0;
276
277         mutex_lock(&o->mot_ioepoch_mutex);
278         CDEBUG(D_INODE, "Eviction. Closing IOepoch "LPU64" on "DFID". "
279                "Count %d\n", o->mot_ioepoch, PFID(mdt_object_fid(o)),
280                o->mot_ioepoch_count);
281         o->mot_ioepoch_count--;
282
283         /* If eviction occured set MOF_SOM_RECOV,
284          * if no other epoch holders, disable SOM on disk. */
285         o->mot_flags |= MOF_SOM_CHANGE | MOF_SOM_RECOV;
286         if (!mdt_ioepoch_opened(o)) {
287                 rc = mdt_som_attr_set(info, o, o->mot_ioepoch, MDT_SOM_DISABLE);
288                 mdt_object_som_enable(o, o->mot_ioepoch);
289         }
290         mutex_unlock(&o->mot_ioepoch_mutex);
291         RETURN(rc);
292 }
293
294 /**
295  * Perform the replay specific actions on ioepoch close.
296  * Skip SOM attribute update if obtained and just forget about the inode state
297  * for the last ioepoch holder. The SOM cache is invalidated on MDS failure.
298  */
299 static inline int mdt_ioepoch_close_on_replay(struct mdt_thread_info *info,
300                                               struct mdt_object *o)
301 {
302         int rc = MDT_IOEPOCH_CLOSED;
303         ENTRY;
304
305         mutex_lock(&o->mot_ioepoch_mutex);
306         CDEBUG(D_INODE, "Replay. Closing epoch "LPU64" on "DFID". Count %d\n",
307                o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
308         o->mot_ioepoch_count--;
309
310         /* Get an info from the replayed request if client is supposed
311          * to send an Attibute Update, reconstruct @rc if so */
312         if (info->mti_ioepoch->flags & MF_SOM_AU)
313                 rc = MDT_IOEPOCH_GETATTR;
314
315         if (!mdt_ioepoch_opened(o))
316                 mdt_object_som_enable(o, info->mti_ioepoch->ioepoch);
317         mutex_unlock(&o->mot_ioepoch_mutex);
318
319         RETURN(rc);
320 }
321
322 /**
323  * Regular file IOepoch close.
324  * Closes the ioepoch, checks the object state, apply obtained attributes and
325  * re-enable SOM on the object, if possible. Also checks if the recovery is
326  * needed and packs OBD_MD_FLGETATTRLOCK flag into the reply to force the client
327  * to obtain SOM attributes under the server-side OST locks.
328  *
329  * Return value:
330  * MDT_IOEPOCH_CLOSED if ioepoch is closed.
331  * MDT_IOEPOCH_GETATTR if ioepoch is closed but another SOM update is needed.
332  */
333 static inline int mdt_ioepoch_close_reg(struct mdt_thread_info *info,
334                                         struct mdt_object *o)
335 {
336         struct md_attr *tmp_ma;
337         struct lu_attr *la;
338         int achange, opened;
339         int recovery = 0;
340         int rc = 0, ret = MDT_IOEPOCH_CLOSED;
341         ENTRY;
342
343         la = &info->mti_attr.ma_attr;
344         achange = (info->mti_ioepoch->flags & MF_SOM_CHANGE);
345
346         mutex_lock(&o->mot_ioepoch_mutex);
347         o->mot_ioepoch_count--;
348
349         tmp_ma = &info->mti_u.som.attr;
350         tmp_ma->ma_lmm = info->mti_attr.ma_lmm;
351         tmp_ma->ma_lmm_size = info->mti_attr.ma_lmm_size;
352         tmp_ma->ma_som = &info->mti_u.som.data;
353         tmp_ma->ma_need = MA_INODE | MA_LOV | MA_SOM;
354         tmp_ma->ma_valid = 0;
355         rc = mdt_attr_get_complex(info, o, tmp_ma);
356         if (rc)
357                 GOTO(error_up, rc);
358
359         /* Check the on-disk SOM state. */
360         if (o->mot_flags & MOF_SOM_RECOV)
361                 recovery = 1;
362         else if (!(o->mot_flags & MOF_SOM_CREATED) &&
363                  !(tmp_ma->ma_valid & MA_SOM))
364                 recovery = 1;
365
366         CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
367                o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
368
369         opened = mdt_ioepoch_opened(o);
370         /**
371          * If IOEpoch is not opened, check if a Size-on-MDS update is needed.
372          * Skip the check for file with no LOV  or for unlink files.
373          */
374         if (!opened && tmp_ma->ma_valid & MA_LOV &&
375             !(tmp_ma->ma_valid & MA_INODE && tmp_ma->ma_attr.la_nlink == 0)) {
376                 if (recovery)
377                         /* If some previous writer was evicted, re-ask the
378                          * client for attributes. Even if attributes are
379                          * provided, we cannot believe in them.
380                          * Another use case is that there is no SOM cache on
381                          * disk -- first access with SOM or there was an MDS
382                          * failure. */
383                         ret = MDT_IOEPOCH_GETATTR;
384                 else if (o->mot_flags & MOF_SOM_CHANGE)
385                         /* Some previous writer changed the attribute.
386                          * Do not believe to the current Size-on-MDS
387                          * update, re-ask client. */
388                         ret = MDT_IOEPOCH_GETATTR;
389                 else if (!(la->la_valid & LA_SIZE) && achange)
390                         /* Attributes were changed by the last writer
391                          * only but no Size-on-MDS update is received.*/
392                         ret = MDT_IOEPOCH_GETATTR;
393         }
394
395         if (achange || ret == MDT_IOEPOCH_GETATTR)
396                 o->mot_flags |= MOF_SOM_CHANGE;
397
398         /* If epoch ends and relable SOM attributes are obtained, update them.
399          * Create SOM ea for new files even if there is no attributes obtained
400          * (0-length file). */
401         if (ret == MDT_IOEPOCH_CLOSED && !opened) {
402                 if (achange || o->mot_flags & MOF_SOM_CREATED) {
403                         LASSERT(achange || !(la->la_valid & LA_SIZE));
404                         rc = mdt_som_attr_set(info, o, o->mot_ioepoch,
405                                               MDT_SOM_ENABLE);
406                         /* Avoid the following setattrs of these attributes,
407                          * e.g. for atime update. */
408                         info->mti_attr.ma_valid = 0;
409                 }
410                 mdt_object_som_enable(o, o->mot_ioepoch);
411         }
412
413         mutex_unlock(&o->mot_ioepoch_mutex);
414         /* If recovery is needed, tell the client to perform GETATTR under
415          * the lock. */
416         if (ret == MDT_IOEPOCH_GETATTR && recovery) {
417                 struct mdt_body *rep;
418                 rep = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
419                 rep->valid |= OBD_MD_FLGETATTRLOCK;
420         }
421
422         RETURN(rc ? : ret);
423
424 error_up:
425         mutex_unlock(&o->mot_ioepoch_mutex);
426         return rc;
427 }
428
429 /**
430  * Close IOEpoch (opened file or MDS_FMODE_EPOCH state). It happens if:
431  * - a client closes the IOEpoch;
432  * - a client eviction occured.
433  * Return values:
434  * MDT_IOEPOCH_OPENED if the client does not close IOEpoch.
435  * MDT_IOEPOCH_CLOSED if the client closes IOEpoch.
436  * MDT_IOEPOCH_GETATTR if the client closes IOEpoch but another SOM attribute
437  * update is needed.
438  */
439 static int mdt_ioepoch_close(struct mdt_thread_info *info, struct mdt_object *o)
440 {
441         struct ptlrpc_request *req = mdt_info_req(info);
442         ENTRY;
443
444         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
445             !S_ISREG(lu_object_attr(&o->mot_obj)))
446                 RETURN(0);
447
448         LASSERT(o->mot_ioepoch_count);
449         LASSERT(info->mti_ioepoch == NULL ||
450                 info->mti_ioepoch->ioepoch == o->mot_ioepoch);
451
452         /* IOEpoch is closed only if client tells about it or eviction occures.
453          * In the replay case, always close the epoch. */
454         if (req == NULL)
455                 RETURN(mdt_ioepoch_close_on_eviction(info, o));
456         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
457                 RETURN(mdt_ioepoch_close_on_replay(info, o));
458         if (info->mti_ioepoch->flags & MF_EPOCH_CLOSE)
459                 RETURN(mdt_ioepoch_close_reg(info, o));
460         /* IO epoch is not closed. */
461         RETURN(MDT_IOEPOCH_OPENED);
462 }
463
464 /**
465  * Close MDS_FMODE_SOM state, when IOEpoch is already closed and we are waiting
466  * for attribute update. It happens if:
467  * - SOM Attribute Update is obtained;
468  * - the client failed to obtain it and informs MDS about it;
469  * - a client eviction occured.
470  * Apply obtained attributes for the 1st case, wipe out the on-disk SOM
471  * cache otherwise.
472  */
473 int mdt_som_au_close(struct mdt_thread_info *info, struct mdt_object *o)
474 {
475         struct ptlrpc_request   *req = mdt_info_req(info);
476         __u64                    ioepoch = 0;
477         int                      act = MDT_SOM_ENABLE;
478         int                      rc = 0;
479         ENTRY;
480
481         LASSERT(!req || info->mti_ioepoch);
482         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
483             !S_ISREG(lu_object_attr(&o->mot_obj)))
484                 RETURN(0);
485
486         /* No size whereas MF_SOM_CHANGE is set means client failed to
487          * obtain ost attributes, drop the SOM cache on disk if so. */
488         if (!req ||
489             (info->mti_ioepoch &&
490              info->mti_ioepoch->flags & MF_SOM_CHANGE &&
491              !(info->mti_attr.ma_attr.la_valid & LA_SIZE)))
492                 act = MDT_SOM_DISABLE;
493
494         mutex_lock(&o->mot_ioepoch_mutex);
495         /* Mark the object it is the recovery state if we failed to obtain
496          * SOM attributes. */
497         if (act == MDT_SOM_DISABLE)
498                 o->mot_flags |= MOF_SOM_RECOV;
499
500         if (!mdt_ioepoch_opened(o)) {
501                 ioepoch =  info->mti_ioepoch ?
502                         info->mti_ioepoch->ioepoch : o->mot_ioepoch;
503
504                 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))
505                         rc = mdt_som_attr_set(info, o, ioepoch, act);
506                 mdt_object_som_enable(o, ioepoch);
507         }
508         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, int mode)
653 {
654         LASSERT(mfd != NULL);
655
656         CDEBUG(D_HA, "Change mfd %p mode 0x%x->0x%x\n",
657                mfd, (unsigned int)mfd->mfd_mode, (unsigned int)mode);
658
659         mfd->mfd_mode = mode;
660 }
661
662 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
663                         struct mdt_object *o, __u64 flags, int created)
664 {
665         struct ptlrpc_request   *req = mdt_info_req(info);
666         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
667         struct mdt_file_data    *mfd;
668         struct md_attr          *ma  = &info->mti_attr;
669         struct lu_attr          *la  = &ma->ma_attr;
670         struct mdt_body         *repbody;
671         int                      rc = 0, isdir, isreg;
672         ENTRY;
673
674         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
675
676         isreg = S_ISREG(la->la_mode);
677         isdir = S_ISDIR(la->la_mode);
678         if (isreg && !(ma->ma_valid & MA_LOV)) {
679                 /*
680                  * No EA, check whether it is will set regEA and dirEA since in
681                  * above attr get, these size might be zero, so reset it, to
682                  * retrieve the MD after create obj.
683                  */
684                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
685                                                        &RMF_MDT_MD,
686                                                        RCL_SERVER);
687                 /* in replay case, p == NULL */
688                 rc = mdt_create_data(info, p, o);
689                 if (rc)
690                         RETURN(rc);
691         }
692
693         CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
694                ma->ma_valid, ma->ma_lmm_size);
695
696         if (ma->ma_valid & MA_LOV) {
697                 LASSERT(ma->ma_lmm_size != 0);
698                 repbody->eadatasize = ma->ma_lmm_size;
699                 if (isdir)
700                         repbody->valid |= OBD_MD_FLDIREA;
701                 else
702                         repbody->valid |= OBD_MD_FLEASIZE;
703         }
704
705         if (flags & FMODE_WRITE) {
706                 rc = mdt_write_get(o);
707                 if (rc == 0) {
708                         mdt_ioepoch_open(info, o, created);
709                         repbody->ioepoch = o->mot_ioepoch;
710                 }
711         } else if (flags & MDS_FMODE_EXEC) {
712                 /* if file is released, we can't deny write because we must
713                  * restore (write) it to access it.*/
714                 if ((ma->ma_valid & MA_HSM) &&
715                     (ma->ma_hsm.mh_flags & HS_RELEASED))
716                         rc = 0;
717                 else
718                         rc = mdt_write_deny(o);
719         }
720         if (rc)
721                 RETURN(rc);
722
723         rc = mo_open(info->mti_env, mdt_object_child(o),
724                      created ? flags | MDS_OPEN_CREATED : flags);
725         if (rc)
726                 GOTO(err_out, rc);
727
728         mfd = mdt_mfd_new();
729         if (mfd != NULL) {
730                 /*
731                  * Keep a reference on this object for this open, and is
732                  * released by mdt_mfd_close().
733                  */
734                 mdt_object_get(info->mti_env, o);
735
736                 /*
737                  * @flags is always not zero. At least it should be FMODE_READ,
738                  * FMODE_WRITE or MDS_FMODE_EXEC.
739                  */
740                 LASSERT(flags != 0);
741
742                 /* Open handling. */
743                 mdt_mfd_set_mode(mfd, flags);
744
745                 mfd->mfd_object = o;
746                 mfd->mfd_xid = req->rq_xid;
747
748                 /* replay handle */
749                 if (req_is_replay(req)) {
750                         struct mdt_file_data *old_mfd;
751                         /* Check wheather old cookie already exist in
752                          * the list, becasue when do recovery, client
753                          * might be disconnected from server, and
754                          * restart replay, so there maybe some orphan
755                          * mfd here, we should remove them */
756                         LASSERT(info->mti_rr.rr_handle != NULL);
757                         old_mfd = mdt_handle2mfd(info, info->mti_rr.rr_handle);
758                         if (old_mfd) {
759                                 CDEBUG(D_HA, "del orph mfd %p fid=("DFID") "
760                                        "cookie=" LPX64"\n", mfd,
761                                        PFID(mdt_object_fid(mfd->mfd_object)),
762                                        info->mti_rr.rr_handle->cookie);
763                                 spin_lock(&med->med_open_lock);
764                                 class_handle_unhash(&old_mfd->mfd_handle);
765                                 cfs_list_del_init(&old_mfd->mfd_list);
766                                 spin_unlock(&med->med_open_lock);
767                                 /* no attr update for that close */
768                                 la->la_valid = 0;
769                                 ma->ma_valid |= MA_FLAGS;
770                                 ma->ma_attr_flags |= MDS_RECOV_OPEN;
771                                 mdt_mfd_close(info, old_mfd);
772                                 ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
773                                 ma->ma_valid &= ~MA_FLAGS;
774                         }
775                         CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
776                                info->mti_rr.rr_handle->cookie);
777                         mfd->mfd_old_handle.cookie =
778                                                 info->mti_rr.rr_handle->cookie;
779                 }
780                 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
781
782                 if (req->rq_export->exp_disconnected) {
783                         spin_lock(&med->med_open_lock);
784                         class_handle_unhash(&mfd->mfd_handle);
785                         cfs_list_del_init(&mfd->mfd_list);
786                         spin_unlock(&med->med_open_lock);
787                         mdt_mfd_close(info, mfd);
788                 } else {
789                         spin_lock(&med->med_open_lock);
790                         cfs_list_add(&mfd->mfd_list, &med->med_open_head);
791                         spin_unlock(&med->med_open_lock);
792                 }
793
794                 mdt_empty_transno(info, rc);
795         } else {
796                 GOTO(err_out, rc = -ENOMEM);
797         }
798
799         RETURN(rc);
800
801 err_out:
802         if (flags & FMODE_WRITE)
803                         /* XXX We also need to close io epoch here.
804                          * See LU-1220 - green */
805                 mdt_write_put(o);
806         else if (flags & FMODE_EXEC)
807                 mdt_write_allow(o);
808         return rc;
809 }
810
811 int mdt_finish_open(struct mdt_thread_info *info,
812                     struct mdt_object *p, struct mdt_object *o,
813                     __u64 flags, int created, struct ldlm_reply *rep)
814 {
815         struct ptlrpc_request   *req = mdt_info_req(info);
816         struct obd_export       *exp = req->rq_export;
817         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
818         struct md_attr          *ma  = &info->mti_attr;
819         struct lu_attr          *la  = &ma->ma_attr;
820         struct mdt_file_data    *mfd;
821         struct mdt_body         *repbody;
822         int                      rc = 0;
823         int                      isreg, isdir, islnk;
824         cfs_list_t              *t;
825         ENTRY;
826
827         LASSERT(ma->ma_valid & MA_INODE);
828
829         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
830
831         isreg = S_ISREG(la->la_mode);
832         isdir = S_ISDIR(la->la_mode);
833         islnk = S_ISLNK(la->la_mode);
834         mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
835
836         /* LU-2275, simulate broken behaviour (esp. prevalent in
837          * pre-2.4 servers where a very strange reply is sent on error
838          * that looks like it was actually almost succesful and a failure at the
839          * same time */
840         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_NEGATIVE_POSITIVE)) {
841                 mdt_set_disposition(info, rep, DISP_OPEN_OPEN |
842                                                DISP_LOOKUP_NEG |
843                                                DISP_LOOKUP_POS);
844
845                 if (flags & MDS_OPEN_LOCK)
846                         mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
847
848                 RETURN(-ENOENT);
849         }
850
851         if (exp_connect_rmtclient(exp)) {
852                 void *buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
853
854                 rc = mdt_pack_remote_perm(info, o, buf);
855                 if (rc) {
856                         repbody->valid &= ~OBD_MD_FLRMTPERM;
857                         repbody->aclsize = 0;
858                 } else {
859                         repbody->valid |= OBD_MD_FLRMTPERM;
860                         repbody->aclsize = sizeof(struct mdt_remote_perm);
861                 }
862         }
863 #ifdef CONFIG_FS_POSIX_ACL
864         else if (exp_connect_flags(exp) & OBD_CONNECT_ACL) {
865                 const struct lu_env *env = info->mti_env;
866                 struct md_object *next = mdt_object_child(o);
867                 struct lu_buf *buf = &info->mti_buf;
868
869                 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
870                 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
871                                                    RCL_SERVER);
872                 if (buf->lb_len > 0) {
873                         rc = mo_xattr_get(env, next, buf,
874                                           XATTR_NAME_ACL_ACCESS);
875                         if (rc < 0) {
876                                 if (rc == -ENODATA) {
877                                         repbody->aclsize = 0;
878                                         repbody->valid |= OBD_MD_FLACL;
879                                         rc = 0;
880                                 } else if (rc == -EOPNOTSUPP) {
881                                         rc = 0;
882                                 } else {
883                                         CERROR("got acl size: %d\n", rc);
884                                 }
885                         } else {
886                                 repbody->aclsize = rc;
887                                 repbody->valid |= OBD_MD_FLACL;
888                                 rc = 0;
889                         }
890                 }
891         }
892 #endif
893
894         if (info->mti_mdt->mdt_opts.mo_mds_capa &&
895             exp_connect_flags(exp) & OBD_CONNECT_MDS_CAPA) {
896                 struct lustre_capa *capa;
897
898                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
899                 LASSERT(capa);
900                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
901                 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
902                 if (rc)
903                         RETURN(rc);
904                 repbody->valid |= OBD_MD_FLMDSCAPA;
905         }
906
907         if (info->mti_mdt->mdt_opts.mo_oss_capa &&
908             exp_connect_flags(exp) & OBD_CONNECT_OSS_CAPA &&
909             S_ISREG(lu_object_attr(&o->mot_obj))) {
910                 struct lustre_capa *capa;
911
912                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
913                 LASSERT(capa);
914                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
915                 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
916                 if (rc)
917                         RETURN(rc);
918                 repbody->valid |= OBD_MD_FLOSSCAPA;
919         }
920
921         /*
922          * If we are following a symlink, don't open; and do not return open
923          * handle for special nodes as client required.
924          */
925         if (islnk || (!isreg && !isdir &&
926             (exp_connect_flags(req->rq_export) & OBD_CONNECT_NODEVOH))) {
927                 lustre_msg_set_transno(req->rq_repmsg, 0);
928                 RETURN(0);
929         }
930
931         /*
932          * We need to return the existing object's fid back, so it is done here,
933          * after preparing the reply.
934          */
935         if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
936                 RETURN(-EEXIST);
937
938         /* This can't be done earlier, we need to return reply body */
939         if (isdir) {
940                 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
941                         /* We are trying to create or write an existing dir. */
942                         RETURN(-EISDIR);
943                 }
944         } else if (flags & MDS_OPEN_DIRECTORY)
945                 RETURN(-ENOTDIR);
946
947         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
948                                  OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
949                 RETURN(-EAGAIN);
950         }
951
952         mfd = NULL;
953         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
954                 spin_lock(&med->med_open_lock);
955                 cfs_list_for_each(t, &med->med_open_head) {
956                         mfd = cfs_list_entry(t, struct mdt_file_data, mfd_list);
957                         if (mfd->mfd_xid == req->rq_xid)
958                                 break;
959                         mfd = NULL;
960                 }
961                 spin_unlock(&med->med_open_lock);
962
963                 if (mfd != NULL) {
964                         repbody->handle.cookie = mfd->mfd_handle.h_cookie;
965                         /*set repbody->ea_size for resent case*/
966                         if (ma->ma_valid & MA_LOV) {
967                                 LASSERT(ma->ma_lmm_size != 0);
968                                 repbody->eadatasize = ma->ma_lmm_size;
969                                 if (isdir)
970                                         repbody->valid |= OBD_MD_FLDIREA;
971                                 else
972                                         repbody->valid |= OBD_MD_FLEASIZE;
973                         }
974                         mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
975                         RETURN(0);
976                 }
977         }
978
979         rc = mdt_mfd_open(info, p, o, flags, created);
980         if (!rc)
981                 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
982
983         RETURN(rc);
984 }
985
986 extern void mdt_req_from_lcd(struct ptlrpc_request *req,
987                              struct lsd_client_data *lcd);
988
989 void mdt_reconstruct_open(struct mdt_thread_info *info,
990                           struct mdt_lock_handle *lhc)
991 {
992         const struct lu_env *env = info->mti_env;
993         struct mdt_device       *mdt  = info->mti_mdt;
994         struct req_capsule      *pill = info->mti_pill;
995         struct ptlrpc_request   *req  = mdt_info_req(info);
996         struct tg_export_data   *ted  = &req->rq_export->exp_target_data;
997         struct lsd_client_data  *lcd  = ted->ted_lcd;
998         struct md_attr          *ma   = &info->mti_attr;
999         struct mdt_reint_record *rr   = &info->mti_rr;
1000         __u32                   flags = info->mti_spec.sp_cr_flags;
1001         struct ldlm_reply       *ldlm_rep;
1002         struct mdt_object       *parent;
1003         struct mdt_object       *child;
1004         struct mdt_body         *repbody;
1005         int                      rc;
1006         ENTRY;
1007
1008         LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1009         ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
1010         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1011
1012         ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
1013         ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
1014                                                RCL_SERVER);
1015         ma->ma_need = MA_INODE | MA_HSM;
1016         if (ma->ma_lmm_size > 0)
1017                 ma->ma_need |= MA_LOV;
1018
1019         ma->ma_valid = 0;
1020
1021         mdt_req_from_lcd(req, lcd);
1022         mdt_set_disposition(info, ldlm_rep, lcd->lcd_last_data);
1023
1024         CDEBUG(D_INODE, "This is reconstruct open: disp="LPX64", result=%d\n",
1025                ldlm_rep->lock_policy_res1, req->rq_status);
1026
1027         if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
1028             req->rq_status != 0)
1029                 /* We did not create successfully, return error to client. */
1030                 GOTO(out, rc = req->rq_status);
1031
1032         if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
1033                 struct obd_export *exp = req->rq_export;
1034                 /*
1035                  * We failed after creation, but we do not know in which step
1036                  * we failed. So try to check the child object.
1037                  */
1038                 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1039                 if (IS_ERR(parent)) {
1040                         rc = PTR_ERR(parent);
1041                         LCONSOLE_WARN("Parent "DFID" lookup error %d."
1042                                       " Evicting client %s with export %s.\n",
1043                                       PFID(rr->rr_fid1), rc,
1044                                       obd_uuid2str(&exp->exp_client_uuid),
1045                                       obd_export_nid2str(exp));
1046                         mdt_export_evict(exp);
1047                         RETURN_EXIT;
1048                 }
1049                 child = mdt_object_find(env, mdt, rr->rr_fid2);
1050                 if (IS_ERR(child)) {
1051                         rc = PTR_ERR(child);
1052                         LCONSOLE_WARN("Child "DFID" lookup error %d."
1053                                       " Evicting client %s with export %s.\n",
1054                                       PFID(mdt_object_fid(child)), rc,
1055                                       obd_uuid2str(&exp->exp_client_uuid),
1056                                       obd_export_nid2str(exp));
1057                         mdt_object_put(env, parent);
1058                         mdt_export_evict(exp);
1059                         RETURN_EXIT;
1060                 }
1061
1062                 if (unlikely(mdt_object_remote(child))) {
1063                         /* the child object was created on remote server */
1064                         if (!mdt_is_dne_client(exp)) {
1065                                 /* Return -EIO for old client */
1066                                 mdt_object_put(env, parent);
1067                                 mdt_object_put(env, child);
1068                                 GOTO(out, rc = -EIO);
1069                         }
1070                         repbody->fid1 = *rr->rr_fid2;
1071                         repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1072                         rc = 0;
1073                 } else {
1074                         if (mdt_object_exists(child)) {
1075                                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1076                                                  BYPASS_CAPA);
1077                                 rc = mdt_attr_get_complex(info, child, ma);
1078                                 if (rc == 0)
1079                                         rc = mdt_finish_open(info, parent,
1080                                                              child, flags,
1081                                                              1, ldlm_rep);
1082                         } else {
1083                                 /* the child does not exist, we should do
1084                                  * regular open */
1085                                 mdt_object_put(env, parent);
1086                                 mdt_object_put(env, child);
1087                                 GOTO(regular_open, 0);
1088                         }
1089                 }
1090                 mdt_object_put(env, parent);
1091                 mdt_object_put(env, child);
1092                 GOTO(out, rc);
1093         } else {
1094 regular_open:
1095                 /* We did not try to create, so we are a pure open */
1096                 rc = mdt_reint_open(info, lhc);
1097         }
1098
1099         EXIT;
1100 out:
1101         req->rq_status = rc;
1102         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1103         LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
1104 }
1105
1106 int mdt_open_by_fid(struct mdt_thread_info* info,
1107                     struct ldlm_reply *rep)
1108 {
1109         __u32                    flags = info->mti_spec.sp_cr_flags;
1110         struct mdt_reint_record *rr = &info->mti_rr;
1111         struct md_attr          *ma = &info->mti_attr;
1112         struct mdt_object       *o;
1113         int                      rc;
1114         ENTRY;
1115
1116         o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1117         if (IS_ERR(o))
1118                 RETURN(rc = PTR_ERR(o));
1119
1120         if (unlikely(mdt_object_remote(o))) {
1121                 /* the child object was created on remote server */
1122                 struct mdt_body *repbody;
1123
1124                 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1125                                                 DISP_LOOKUP_EXECD |
1126                                                 DISP_LOOKUP_POS));
1127                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1128                 repbody->fid1 = *rr->rr_fid2;
1129                 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1130                 rc = 0;
1131         } else {
1132                 if (mdt_object_exists(o)) {
1133                         mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1134                                                         DISP_LOOKUP_EXECD |
1135                                                         DISP_LOOKUP_POS));
1136
1137                         rc = mdt_attr_get_complex(info, o, ma);
1138                         if (rc == 0)
1139                                 rc = mdt_finish_open(info, NULL, o, flags, 0,
1140                                                      rep);
1141                 } else {
1142                         rc = -ENOENT;
1143                 }
1144         }
1145
1146         mdt_object_put(info->mti_env, o);
1147         RETURN(rc);
1148 }
1149
1150 /* lock object for open */
1151 static int mdt_object_open_lock(struct mdt_thread_info *info,
1152                                 struct mdt_object *obj,
1153                                 struct mdt_lock_handle *lhc,
1154                                 __u64 *ibits)
1155 {
1156         struct md_attr  *ma = &info->mti_attr;
1157         __u64            open_flags = info->mti_spec.sp_cr_flags;
1158         ldlm_mode_t      lm = LCK_CR;
1159         bool             try_layout = false;
1160         bool             create_layout = false;
1161         int              rc = 0;
1162         ENTRY;
1163
1164         *ibits = 0;
1165         if (open_flags & MDS_OPEN_LOCK) {
1166                 if (open_flags & FMODE_WRITE)
1167                         lm = LCK_CW;
1168                 /* if file is released, we can't deny write because we must
1169                  * restore (write) it to access it. */
1170                 else if ((open_flags & MDS_FMODE_EXEC) &&
1171                          !((ma->ma_valid & MA_HSM) &&
1172                            (ma->ma_hsm.mh_flags & HS_RELEASED)))
1173                         lm = LCK_PR;
1174                 else
1175                         lm = LCK_CR;
1176
1177                 *ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN;
1178         }
1179
1180         if (S_ISREG(lu_object_attr(&obj->mot_obj))) {
1181                 if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV) &&
1182                     md_should_create(open_flags))
1183                         create_layout = true;
1184                 if (exp_connect_layout(info->mti_exp) && !create_layout &&
1185                     ma->ma_need & MA_LOV)
1186                         try_layout = true;
1187         }
1188
1189         mdt_lock_handle_init(lhc);
1190         mdt_lock_reg_init(lhc, lm);
1191
1192         /* one problem to return layout lock on open is that it may result
1193          * in too many layout locks cached on the client side. */
1194         if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
1195                 /* return lookup lock to validate inode at the client side,
1196                  * this is pretty important otherwise mdt will return layout
1197                  * lock for each open.
1198                  * However this is a double-edged sword because changing
1199                  * permission will revoke huge # of LOOKUP locks. */
1200                 *ibits |= MDS_INODELOCK_LAYOUT | MDS_INODELOCK_LOOKUP;
1201                 if (!mdt_object_lock_try(info, obj, lhc, *ibits,
1202                                          MDT_CROSS_LOCK)) {
1203                         *ibits &= ~(MDS_INODELOCK_LAYOUT|MDS_INODELOCK_LOOKUP);
1204                         if (*ibits != 0)
1205                                 rc = mdt_object_lock(info, obj, lhc, *ibits,
1206                                                 MDT_CROSS_LOCK);
1207                 }
1208         } else if (*ibits != 0) {
1209                 rc = mdt_object_lock(info, obj, lhc, *ibits, MDT_CROSS_LOCK);
1210         }
1211
1212         CDEBUG(D_INODE, "Requested bits lock:"DFID ", ibits = "LPX64
1213                 ", open_flags = "LPO64", try_layout = %d, rc = %d\n",
1214                 PFID(mdt_object_fid(obj)), *ibits, open_flags, try_layout, rc);
1215
1216         /* will change layout, revoke layout locks by enqueuing EX lock. */
1217         if (rc == 0 && create_layout) {
1218                 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
1219
1220                 CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
1221                         ", open_flags = "LPO64"\n",
1222                         PFID(mdt_object_fid(obj)), open_flags);
1223
1224                 LASSERT(!try_layout);
1225                 mdt_lock_handle_init(ll);
1226                 mdt_lock_reg_init(ll, LCK_EX);
1227                 rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT,
1228                                         MDT_LOCAL_LOCK);
1229
1230                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
1231         }
1232
1233         RETURN(rc);
1234 }
1235
1236 static void mdt_object_open_unlock(struct mdt_thread_info *info,
1237                                    struct mdt_object *obj,
1238                                    struct mdt_lock_handle *lhc,
1239                                    __u64 ibits, int rc)
1240 {
1241         __u64 open_flags = info->mti_spec.sp_cr_flags;
1242         struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
1243
1244         /* Release local layout lock - the layout lock put in MDT_LH_LAYOUT
1245          * will never return to client side. */
1246         if (lustre_handle_is_used(&ll->mlh_reg_lh)) {
1247                 LASSERT(!(ibits & MDS_INODELOCK_LAYOUT));
1248                 mdt_object_unlock(info, obj, ll, 1);
1249         }
1250
1251         /* Cross-ref case, the lock should be returned to the client */
1252         if (ibits == 0 || rc == -EREMOTE)
1253                 return;
1254
1255         if (!(open_flags & MDS_OPEN_LOCK) && !(ibits & MDS_INODELOCK_LAYOUT)) {
1256                 /* for the open request, the lock will only return to client
1257                  * if open or layout lock is granted. */
1258                 rc = 1;
1259         }
1260
1261         if (rc != 0) {
1262                 struct ldlm_reply       *ldlm_rep;
1263
1264                 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1265                 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1266                 mdt_object_unlock(info, obj, lhc, 1);
1267         }
1268 }
1269
1270 int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
1271                          struct mdt_lock_handle *lhc)
1272 {
1273         const struct lu_env     *env   = info->mti_env;
1274         struct mdt_device       *mdt   = info->mti_mdt;
1275         __u32                    flags = info->mti_spec.sp_cr_flags;
1276         struct mdt_reint_record *rr    = &info->mti_rr;
1277         struct md_attr          *ma    = &info->mti_attr;
1278         struct mdt_object       *parent= NULL;
1279         struct mdt_object       *o;
1280         int                      rc;
1281         __u64                    ibits = 0;
1282         ENTRY;
1283
1284         if (md_should_create(flags) && !(flags & MDS_OPEN_HAS_EA)) {
1285                 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1286                         parent = mdt_object_find(env, mdt, rr->rr_fid1);
1287                         if (IS_ERR(parent)) {
1288                                 CDEBUG(D_INODE, "Fail to find parent "DFID
1289                                        " for anonymous created %ld, try to"
1290                                        " use server-side parent.\n",
1291                                        PFID(rr->rr_fid1), PTR_ERR(parent));
1292                                 parent = NULL;
1293                         }
1294                 }
1295                 if (parent == NULL)
1296                         ma->ma_need |= MA_PFID;
1297         }
1298
1299         o = mdt_object_find(env, mdt, rr->rr_fid2);
1300         if (IS_ERR(o))
1301                 RETURN(rc = PTR_ERR(o));
1302
1303         if (mdt_object_remote(o)) {
1304                 CDEBUG(D_INFO, "%s: "DFID" is on remote MDT.\n",
1305                        mdt_obd_name(info->mti_mdt),
1306                        PFID(rr->rr_fid2));
1307                 GOTO(out, rc = -EREMOTE);
1308         } else if (!mdt_object_exists(o)) {
1309                 mdt_set_disposition(info, rep,
1310                                     DISP_IT_EXECD |
1311                                     DISP_LOOKUP_EXECD |
1312                                     DISP_LOOKUP_NEG);
1313                 GOTO(out, rc = -ENOENT);
1314         }
1315
1316         mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1317
1318         rc = mdt_attr_get_complex(info, o, ma);
1319         if (rc)
1320                 GOTO(out, rc);
1321
1322         rc = mdt_object_open_lock(info, o, lhc, &ibits);
1323         if (rc)
1324                 GOTO(out, rc);
1325
1326         if (ma->ma_valid & MA_PFID) {
1327                 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1328                 if (IS_ERR(parent)) {
1329                         CDEBUG(D_INODE, "Fail to find parent "DFID
1330                                " for anonymous created %ld, try to"
1331                                " use system default.\n",
1332                                PFID(&ma->ma_pfid), PTR_ERR(parent));
1333                         parent = NULL;
1334                 }
1335         }
1336
1337         rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1338         if (!rc) {
1339                 mdt_set_disposition(info, rep, DISP_LOOKUP_POS);
1340                 if (flags & MDS_OPEN_LOCK)
1341                         mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1342         }
1343         GOTO(out, rc);
1344
1345 out:
1346         mdt_object_open_unlock(info, o, lhc, ibits, rc);
1347         mdt_object_put(env, o);
1348         if (parent != NULL)
1349                 mdt_object_put(env, parent);
1350         return rc;
1351 }
1352
1353 int mdt_pin(struct mdt_thread_info* info)
1354 {
1355         ENTRY;
1356         RETURN(err_serious(-EOPNOTSUPP));
1357 }
1358
1359 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1360 static int mdt_cross_open(struct mdt_thread_info *info,
1361                           const struct lu_fid *parent_fid,
1362                           const struct lu_fid *fid,
1363                           struct ldlm_reply *rep, __u32 flags)
1364 {
1365         struct md_attr    *ma = &info->mti_attr;
1366         struct mdt_object *o;
1367         int                rc;
1368         ENTRY;
1369
1370         o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1371         if (IS_ERR(o))
1372                 RETURN(rc = PTR_ERR(o));
1373
1374         if (mdt_object_remote(o)) {
1375                 /* Something is wrong here, the object is on another MDS! */
1376                 CERROR("%s: "DFID" isn't on this server!: rc = %d\n",
1377                        mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1378                 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1379                                 &o->mot_obj,
1380                                 "Object isn't on this server! FLD error?\n");
1381                 rc = -EFAULT;
1382         } else {
1383                 if (mdt_object_exists(o)) {
1384                         /* Do permission check for cross-open. */
1385                         rc = mo_permission(info->mti_env, NULL,
1386                                            mdt_object_child(o),
1387                                            NULL, flags | MDS_OPEN_CROSS);
1388                         if (rc)
1389                                 goto out;
1390
1391                         mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
1392                         rc = mdt_attr_get_complex(info, o, ma);
1393                         if (rc != 0)
1394                                 GOTO(out, rc);
1395
1396                         /* Do not create lov object if the fid is opened
1397                          * under OBF */
1398                         if (S_ISREG(ma->ma_attr.la_mode) &&
1399                             !(ma->ma_valid & MA_LOV) && (flags & FMODE_WRITE) &&
1400                             fid_is_obf(parent_fid))
1401                                 GOTO(out, rc = -EPERM);
1402
1403                         rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1404                 } else {
1405                         /*
1406                          * Something is wrong here. lookup was positive but
1407                          * there is no object!
1408                          */
1409                         CERROR("%s: "DFID" doesn't exist!: rc = %d\n",
1410                               mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1411                         rc = -EFAULT;
1412                 }
1413         }
1414 out:
1415         mdt_object_put(info->mti_env, o);
1416         RETURN(rc);
1417 }
1418
1419 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1420 {
1421         struct mdt_device       *mdt = info->mti_mdt;
1422         struct ptlrpc_request   *req = mdt_info_req(info);
1423         struct mdt_object       *parent;
1424         struct mdt_object       *child;
1425         struct mdt_lock_handle  *lh;
1426         struct ldlm_reply       *ldlm_rep;
1427         struct mdt_body         *repbody;
1428         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
1429         struct md_attr          *ma = &info->mti_attr;
1430         __u64                    create_flags = info->mti_spec.sp_cr_flags;
1431         __u64                    ibits;
1432         struct mdt_reint_record *rr = &info->mti_rr;
1433         struct lu_name          *lname;
1434         int                      result, rc;
1435         int                      created = 0;
1436         __u32                    msg_flags;
1437         ENTRY;
1438
1439         OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1440                                (obd_timeout + 1) / 4);
1441
1442         mdt_counter_incr(req, LPROC_MDT_OPEN);
1443         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1444
1445         ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
1446         ma->ma_lmm_size = req_capsule_get_size(info->mti_pill, &RMF_MDT_MD,
1447                                                RCL_SERVER);
1448         ma->ma_need = MA_INODE;
1449         if (ma->ma_lmm_size > 0)
1450                 ma->ma_need |= MA_LOV;
1451
1452         ma->ma_valid = 0;
1453
1454         LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1455         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1456
1457         if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1458                 CERROR("file join is not supported anymore.\n");
1459                 GOTO(out, result = err_serious(-EOPNOTSUPP));
1460         }
1461         msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1462
1463         if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1464             info->mti_spec.u.sp_ea.eadata == NULL)
1465                 GOTO(out, result = err_serious(-EINVAL));
1466
1467         CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
1468                "cr_flag="LPO64" mode=0%06o msg_flag=0x%x\n",
1469                PFID(rr->rr_fid1), rr->rr_name,
1470                PFID(rr->rr_fid2), create_flags,
1471                ma->ma_attr.la_mode, msg_flags);
1472         if (info->mti_cross_ref) {
1473                 /* This is cross-ref open */
1474                 mdt_set_disposition(info, ldlm_rep,
1475                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD |
1476                              DISP_LOOKUP_POS));
1477                 result = mdt_cross_open(info, rr->rr_fid2, rr->rr_fid1,
1478                                         ldlm_rep, create_flags);
1479                 GOTO(out, result);
1480         } else if (req_is_replay(req) ||
1481             (req->rq_export->exp_libclient && create_flags & MDS_OPEN_HAS_EA)) {
1482                 /* This is a replay request or from liblustre with ea. */
1483                 result = mdt_open_by_fid(info, ldlm_rep);
1484
1485                 if (result != -ENOENT) {
1486                         if (req->rq_export->exp_libclient &&
1487                             create_flags & MDS_OPEN_HAS_EA)
1488                                 GOTO(out, result = 0);
1489                         GOTO(out, result);
1490                 }
1491                 /* We didn't find the correct object, so we need to re-create it
1492                  * via a regular replay. */
1493                 if (!(create_flags & MDS_OPEN_CREAT)) {
1494                         DEBUG_REQ(D_ERROR, req,
1495                                   "OPEN & CREAT not in open replay/by_fid.");
1496                         GOTO(out, result = -EFAULT);
1497                 }
1498                 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1499         } else if ((rr->rr_namelen == 0 && create_flags & MDS_OPEN_LOCK) ||
1500                    (create_flags & MDS_OPEN_BY_FID)) {
1501                 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1502                 /* If result is 0 then open by FID has found the file
1503                  * and there is nothing left for us to do here.  More
1504                  * generally if it is anything other than -ENOENT or
1505                  * -EREMOTE then we return that now.  If -ENOENT and
1506                  * MDS_OPEN_CREAT is set then we must create the file
1507                  * below.  If -EREMOTE then we need to return a LOOKUP
1508                  * lock to the client, which we do below.  Hence this
1509                  * odd looking condition.  See LU-2523. */
1510                 if (!(result == -ENOENT && (create_flags & MDS_OPEN_CREAT)) &&
1511                     result != -EREMOTE)
1512                         GOTO(out, result);
1513
1514                 if (unlikely(rr->rr_namelen == 0))
1515                         GOTO(out, result = -EINVAL);
1516
1517                 CDEBUG(D_INFO, "No object(2), continue as regular open.\n");
1518         }
1519
1520         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1521                 GOTO(out, result = err_serious(-ENOMEM));
1522
1523         mdt_set_disposition(info, ldlm_rep,
1524                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1525
1526         lh = &info->mti_lh[MDT_LH_PARENT];
1527         mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
1528                           LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
1529
1530         parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1531                                       MDS_INODELOCK_UPDATE);
1532         if (IS_ERR(parent))
1533                 GOTO(out, result = PTR_ERR(parent));
1534
1535         /* get and check version of parent */
1536         result = mdt_version_get_check(info, parent, 0);
1537         if (result)
1538                 GOTO(out_parent, result);
1539
1540         fid_zero(child_fid);
1541
1542         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1543         result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1544                             lname, child_fid, &info->mti_spec);
1545         LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1546                  "looking for "DFID"/%s, result fid="DFID"\n",
1547                  PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
1548
1549         if (result != 0 && result != -ENOENT && result != -ESTALE)
1550                 GOTO(out_parent, result);
1551
1552         if (result == -ENOENT || result == -ESTALE) {
1553                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1554                 if (result == -ESTALE) {
1555                         /*
1556                          * -ESTALE means the parent is a dead(unlinked) dir, so
1557                          * it should return -ENOENT to in accordance with the
1558                          * original mds implementaion.
1559                          */
1560                         GOTO(out_parent, result = -ENOENT);
1561                 }
1562                 if (!(create_flags & MDS_OPEN_CREAT))
1563                         GOTO(out_parent, result);
1564                 *child_fid = *info->mti_rr.rr_fid2;
1565                 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1566                          PFID(child_fid));
1567                 /* In the function below, .hs_keycmp resolves to
1568                  * lu_obj_hop_keycmp() */
1569                 /* coverity[overrun-buffer-val] */
1570                 child = mdt_object_new(info->mti_env, mdt, child_fid);
1571         } else {
1572                 /*
1573                  * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1574                  * return FID back in that case.
1575                  */
1576                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1577                 child = mdt_object_find(info->mti_env, mdt, child_fid);
1578         }
1579         if (IS_ERR(child))
1580                 GOTO(out_parent, result = PTR_ERR(child));
1581
1582         /** check version of child  */
1583         rc = mdt_version_get_check(info, child, 1);
1584         if (rc)
1585                 GOTO(out_child, result = rc);
1586
1587         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1588         if (result == -ENOENT) {
1589                 /* Create under OBF and .lustre is not permitted */
1590                 if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1))
1591                         GOTO(out_child, result = -EPERM);
1592
1593                 /* save versions in reply */
1594                 mdt_version_get_save(info, parent, 0);
1595                 mdt_version_get_save(info, child, 1);
1596
1597                 /* version of child will be changed */
1598                 info->mti_mos = child;
1599
1600                 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1601                 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1602
1603                 /* Let lower layers know what is lock mode on directory. */
1604                 info->mti_spec.sp_cr_mode =
1605                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1606
1607                 /*
1608                  * Do not perform lookup sanity check. We know that name does
1609                  * not exist.
1610                  */
1611                 info->mti_spec.sp_cr_lookup = 0;
1612                 info->mti_spec.sp_feat = &dt_directory_features;
1613
1614                 result = mdo_create(info->mti_env,
1615                                     mdt_object_child(parent),
1616                                     lname,
1617                                     mdt_object_child(child),
1618                                     &info->mti_spec,
1619                                     &info->mti_attr);
1620                 if (result == -ERESTART) {
1621                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1622                         GOTO(out_child, result);
1623                 } else {
1624
1625                         /* XXX: we should call this once, see few lines below */
1626                         if (result == 0)
1627                                 result = mdt_attr_get_complex(info, child, ma);
1628
1629                         if (result != 0)
1630                                 GOTO(out_child, result);
1631                 }
1632                 created = 1;
1633         } else {
1634                 /*
1635                  * The object is on remote node, return its FID for remote open.
1636                  */
1637                 if (mdt_object_remote(child)) {
1638                         /*
1639                          * Check if this lock already was sent to client and
1640                          * this is resent case. For resent case do not take lock
1641                          * again, use what is already granted.
1642                          */
1643                         LASSERT(lhc != NULL);
1644
1645                         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1646                                 struct ldlm_lock *lock;
1647
1648                                 LASSERT(msg_flags & MSG_RESENT);
1649
1650                                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1651                                 if (!lock) {
1652                                         CERROR("Invalid lock handle "LPX64"\n",
1653                                                lhc->mlh_reg_lh.cookie);
1654                                         LBUG();
1655                                 }
1656                                 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1657                                                         &lock->l_resource->lr_name));
1658                                 LDLM_LOCK_PUT(lock);
1659                                 rc = 0;
1660                         } else {
1661                                 mdt_lock_handle_init(lhc);
1662                                 mdt_lock_reg_init(lhc, LCK_PR);
1663
1664                                 rc = mdt_object_lock(info, child, lhc,
1665                                                      MDS_INODELOCK_LOOKUP,
1666                                                      MDT_CROSS_LOCK);
1667                         }
1668                         repbody->fid1 = *mdt_object_fid(child);
1669                         repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1670                         if (rc != 0)
1671                                 result = rc;
1672                         else
1673                                 result = -EREMOTE;
1674                         GOTO(out_child, result);
1675                 } else {
1676                         if (mdt_object_exists(child)) {
1677                                 /* We have to get attr & LOV EA & HSM for this
1678                                  * object */
1679                                 ma->ma_need |= MA_HSM;
1680                                 result = mdt_attr_get_complex(info, child, ma);
1681                         } else {
1682                                 /*object non-exist!!!*/
1683                                 LBUG();
1684                         }
1685                 }
1686         }
1687
1688         LASSERT(!lustre_handle_is_used(&lhc->mlh_reg_lh));
1689
1690         /* get openlock if this is not replay and if a client requested it */
1691         if (!req_is_replay(req)) {
1692                 rc = mdt_object_open_lock(info, child, lhc, &ibits);
1693                 if (rc != 0)
1694                         GOTO(out_child, result = rc);
1695                 else if (create_flags & MDS_OPEN_LOCK)
1696                         mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1697         }
1698
1699         /* Try to open it now. */
1700         rc = mdt_finish_open(info, parent, child, create_flags,
1701                              created, ldlm_rep);
1702         if (rc) {
1703                 result = rc;
1704                 /* openlock will be released if mdt_finish_open failed */
1705                 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1706                 if (created) {
1707                         ma->ma_need = 0;
1708                         ma->ma_valid = 0;
1709                         ma->ma_cookie_size = 0;
1710                         rc = mdo_unlink(info->mti_env,
1711                                         mdt_object_child(parent),
1712                                         mdt_object_child(child),
1713                                         lname,
1714                                         &info->mti_attr, 0);
1715                         if (rc != 0)
1716                                 CERROR("%s: "DFID" cleanup of open: rc = %d\n",
1717                                        mdt_obd_name(info->mti_mdt),
1718                                        PFID(mdt_object_fid(child)), rc);
1719                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1720                 }
1721         }
1722         EXIT;
1723 out_child:
1724         mdt_object_open_unlock(info, child, lhc, ibits, result);
1725         mdt_object_put(info->mti_env, child);
1726 out_parent:
1727         mdt_object_unlock_put(info, parent, lh, result || !created);
1728 out:
1729         if (result)
1730                 lustre_msg_set_transno(req->rq_repmsg, 0);
1731         return result;
1732 }
1733
1734 #define MFD_CLOSED(mode) (((mode) & ~(MDS_FMODE_EPOCH | MDS_FMODE_SOM | \
1735                                       MDS_FMODE_TRUNC)) == MDS_FMODE_CLOSED)
1736
1737 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1738 {
1739         return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1740 }
1741
1742 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1743 {
1744         struct mdt_object *o = mfd->mfd_object;
1745         struct md_object *next = mdt_object_child(o);
1746         struct md_attr *ma = &info->mti_attr;
1747         int ret = MDT_IOEPOCH_CLOSED;
1748         int rc = 0;
1749         int mode;
1750         ENTRY;
1751
1752         mode = mfd->mfd_mode;
1753
1754         if ((mode & FMODE_WRITE) || (mode & MDS_FMODE_TRUNC)) {
1755                 mdt_write_put(o);
1756                 ret = mdt_ioepoch_close(info, o);
1757         } else if (mode & MDS_FMODE_EXEC) {
1758                 mdt_write_allow(o);
1759         } else if (mode & MDS_FMODE_EPOCH) {
1760                 ret = mdt_ioepoch_close(info, o);
1761         } else if (mode & MDS_FMODE_SOM) {
1762                 ret = mdt_som_au_close(info, o);
1763         }
1764
1765         /* Update atime on close only. */
1766         if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1767             && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1768                 /* Set the atime only. */
1769                 ma->ma_valid = MA_INODE;
1770                 ma->ma_attr.la_valid = LA_ATIME;
1771                 rc = mo_attr_set(info->mti_env, next, ma);
1772         }
1773
1774         /* If file data is modified, add the dirty flag.
1775          *
1776          * If MDS_CLOSE_CLEANUP is set, this file is being closed due to an
1777          * eviction, file could have been modified and now dirty
1778          * regarding to HSM archive, check this!
1779          * The logic here is to mark a file dirty if there's a chance it was
1780          * dirtied before the client was evicted, so that we don't have to wait
1781          * for a release attempt before finding out the file was actually dirty
1782          * and fail the release. Aggressively marking it dirty here will cause
1783          * the policy engine to attempt to re-archive it; when rearchiving, we
1784          * can compare the current version to the LMA data_version and make the
1785          * archive request into a noop if it's not actually dirty.
1786          */
1787         if ((ma->ma_attr_flags & MDS_DATA_MODIFIED) ||
1788             ((ma->ma_attr_flags & MDS_CLOSE_CLEANUP) &&
1789              (mode & (FMODE_WRITE|MDS_FMODE_TRUNC))))
1790                 rc = mdt_add_dirty_flag(info, o, ma);
1791
1792         ma->ma_need |= MA_INODE;
1793         ma->ma_valid &= ~MA_INODE;
1794
1795         if (!MFD_CLOSED(mode))
1796                 rc = mo_close(info->mti_env, next, ma, mode);
1797
1798         if (ret == MDT_IOEPOCH_GETATTR || ret == MDT_IOEPOCH_OPENED) {
1799                 struct mdt_export_data *med;
1800
1801                 /* The IOepoch is still opened or SOM update is needed.
1802                  * Put mfd back into the list. */
1803                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1804                 mdt_mfd_set_mode(mfd, ret == MDT_IOEPOCH_OPENED ?
1805                                       MDS_FMODE_EPOCH : MDS_FMODE_SOM);
1806
1807                 LASSERT(mdt_info_req(info));
1808                 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1809                 spin_lock(&med->med_open_lock);
1810                 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
1811                 class_handle_hash_back(&mfd->mfd_handle);
1812                 spin_unlock(&med->med_open_lock);
1813
1814                 if (ret == MDT_IOEPOCH_OPENED) {
1815                         ret = 0;
1816                 } else {
1817                         ret = -EAGAIN;
1818                         CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1819                                "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1820                 }
1821         } else {
1822                 mdt_mfd_free(mfd);
1823                 mdt_object_put(info->mti_env, o);
1824         }
1825
1826         RETURN(rc ? rc : ret);
1827 }
1828
1829 int mdt_close(struct mdt_thread_info *info)
1830 {
1831         struct mdt_export_data *med;
1832         struct mdt_file_data   *mfd;
1833         struct mdt_object      *o;
1834         struct md_attr         *ma = &info->mti_attr;
1835         struct mdt_body        *repbody = NULL;
1836         struct ptlrpc_request  *req = mdt_info_req(info);
1837         int rc, ret = 0;
1838         ENTRY;
1839
1840         mdt_counter_incr(req, LPROC_MDT_CLOSE);
1841         /* Close may come with the Size-on-MDS update. Unpack it. */
1842         rc = mdt_close_unpack(info);
1843         if (rc)
1844                 RETURN(err_serious(rc));
1845
1846         LASSERT(info->mti_ioepoch);
1847
1848         req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1849                              info->mti_mdt->mdt_max_mdsize);
1850         req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1851                              info->mti_mdt->mdt_max_cookiesize);
1852         rc = req_capsule_server_pack(info->mti_pill);
1853         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1854                 mdt_client_compatibility(info);
1855                 if (rc == 0)
1856                         mdt_fix_reply(info);
1857                 mdt_exit_ucred(info);
1858                 RETURN(lustre_msg_get_status(req->rq_repmsg));
1859         }
1860
1861         /* Continue to close handle even if we can not pack reply */
1862         if (rc == 0) {
1863                 repbody = req_capsule_server_get(info->mti_pill,
1864                                                  &RMF_MDT_BODY);
1865                 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
1866                                                     &RMF_MDT_MD);
1867                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
1868                                                        &RMF_MDT_MD,
1869                                                        RCL_SERVER);
1870                 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
1871                                                        &RMF_LOGCOOKIES);
1872                 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
1873                                                           &RMF_LOGCOOKIES,
1874                                                           RCL_SERVER);
1875                 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1876                 repbody->eadatasize = 0;
1877                 repbody->aclsize = 0;
1878         } else {
1879                 rc = err_serious(rc);
1880         }
1881
1882         med = &req->rq_export->exp_mdt_data;
1883         spin_lock(&med->med_open_lock);
1884         mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1885         if (mdt_mfd_closed(mfd)) {
1886                 spin_unlock(&med->med_open_lock);
1887                 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1888                        ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1889                        info->mti_ioepoch->handle.cookie);
1890                 /** not serious error since bug 3633 */
1891                 rc = -ESTALE;
1892         } else {
1893                 class_handle_unhash(&mfd->mfd_handle);
1894                 cfs_list_del_init(&mfd->mfd_list);
1895                 spin_unlock(&med->med_open_lock);
1896
1897                 /* Do not lose object before last unlink. */
1898                 o = mfd->mfd_object;
1899                 mdt_object_get(info->mti_env, o);
1900                 ret = mdt_mfd_close(info, mfd);
1901                 if (repbody != NULL)
1902                         rc = mdt_handle_last_unlink(info, o, ma);
1903                 mdt_empty_transno(info, rc);
1904                 mdt_object_put(info->mti_env, o);
1905         }
1906         if (repbody != NULL) {
1907                 mdt_client_compatibility(info);
1908                 rc = mdt_fix_reply(info);
1909         }
1910
1911         mdt_exit_ucred(info);
1912         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1913                 RETURN(err_serious(-ENOMEM));
1914
1915         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
1916                                  OBD_FAIL_MDS_CLOSE_NET_REP))
1917                 info->mti_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
1918         RETURN(rc ? rc : ret);
1919 }
1920
1921 /**
1922  * DONE_WRITING rpc handler.
1923  *
1924  * As mfd is not kept after replayed CLOSE (see mdt_ioepoch_close_on_replay()),
1925  * only those DONE_WRITING rpc will be replayed which really wrote smth on disk,
1926  * and got a trasid. Waiting for such DONE_WRITING is not reliable, so just
1927  * skip attributes and reconstruct the reply here.
1928  */
1929 int mdt_done_writing(struct mdt_thread_info *info)
1930 {
1931         struct ptlrpc_request   *req = mdt_info_req(info);
1932         struct mdt_body         *repbody = NULL;
1933         struct mdt_export_data  *med;
1934         struct mdt_file_data    *mfd;
1935         int rc;
1936         ENTRY;
1937
1938         rc = req_capsule_server_pack(info->mti_pill);
1939         if (rc)
1940                 RETURN(err_serious(rc));
1941
1942         repbody = req_capsule_server_get(info->mti_pill,
1943                                          &RMF_MDT_BODY);
1944         repbody->eadatasize = 0;
1945         repbody->aclsize = 0;
1946
1947         /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1948         rc = mdt_close_unpack(info);
1949         if (rc)
1950                 RETURN(err_serious(rc));
1951
1952         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1953                 mdt_exit_ucred(info);
1954                 RETURN(lustre_msg_get_status(req->rq_repmsg));
1955         }
1956
1957         med = &info->mti_exp->exp_mdt_data;
1958         spin_lock(&med->med_open_lock);
1959         mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1960         if (mfd == NULL) {
1961                 spin_unlock(&med->med_open_lock);
1962                 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1963                        ": cookie = "LPX64" ioepoch = "LPU64"\n",
1964                        PFID(info->mti_rr.rr_fid1),
1965                        info->mti_ioepoch->handle.cookie,
1966                        info->mti_ioepoch->ioepoch);
1967                 /* If this is a replay, reconstruct the transno. */
1968                 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1969                         rc = info->mti_ioepoch->flags & MF_SOM_AU ?
1970                              -EAGAIN : 0;
1971                         mdt_empty_transno(info, rc);
1972                 } else
1973                         rc = -ESTALE;
1974                 GOTO(error_ucred, rc);
1975         }
1976
1977         LASSERT(mfd->mfd_mode == MDS_FMODE_EPOCH ||
1978                 mfd->mfd_mode == MDS_FMODE_TRUNC);
1979         class_handle_unhash(&mfd->mfd_handle);
1980         cfs_list_del_init(&mfd->mfd_list);
1981         spin_unlock(&med->med_open_lock);
1982
1983         /* Set EPOCH CLOSE flag if not set by client. */
1984         info->mti_ioepoch->flags |= MF_EPOCH_CLOSE;
1985         info->mti_attr.ma_valid = 0;
1986
1987         info->mti_attr.ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
1988         OBD_ALLOC_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1989         if (info->mti_attr.ma_lmm == NULL)
1990                 GOTO(error_ucred, rc = -ENOMEM);
1991
1992         rc = mdt_mfd_close(info, mfd);
1993
1994         OBD_FREE_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1995         mdt_empty_transno(info, rc);
1996 error_ucred:
1997         mdt_exit_ucred(info);
1998         RETURN(rc);
1999 }