Whamcloud - gitweb
LU-3030 build: Update Master Copyrights pre 2.4 split
[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.mo_lu)))
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.mo_lu)))
446                 RETURN(0);
447
448         LASSERT(o->mot_ioepoch_count);
449         LASSERT(info->mti_ioepoch == NULL ||
450                 info->mti_ioepoch->ioepoch == o->mot_ioepoch);
451
452         /* IOEpoch is closed only if client tells about it or eviction occures.
453          * In the replay case, always close the epoch. */
454         if (req == NULL)
455                 RETURN(mdt_ioepoch_close_on_eviction(info, o));
456         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
457                 RETURN(mdt_ioepoch_close_on_replay(info, o));
458         if (info->mti_ioepoch->flags & MF_EPOCH_CLOSE)
459                 RETURN(mdt_ioepoch_close_reg(info, o));
460         /* IO epoch is not closed. */
461         RETURN(MDT_IOEPOCH_OPENED);
462 }
463
464 /**
465  * Close MDS_FMODE_SOM state, when IOEpoch is already closed and we are waiting
466  * for attribute update. It happens if:
467  * - SOM Attribute Update is obtained;
468  * - the client failed to obtain it and informs MDS about it;
469  * - a client eviction occured.
470  * Apply obtained attributes for the 1st case, wipe out the on-disk SOM
471  * cache otherwise.
472  */
473 int mdt_som_au_close(struct mdt_thread_info *info, struct mdt_object *o)
474 {
475         struct ptlrpc_request *req = mdt_info_req(info);
476         __u64 ioepoch = 0;
477         int act = MDT_SOM_ENABLE;
478         int rc = 0;
479         ENTRY;
480
481         LASSERT(!req || info->mti_ioepoch);
482         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
483             !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
484                 RETURN(0);
485
486         /* No size whereas MF_SOM_CHANGE is set means client failed to
487          * obtain ost attributes, drop the SOM cache on disk if so. */
488         if (!req ||
489             (info->mti_ioepoch &&
490              info->mti_ioepoch->flags & MF_SOM_CHANGE &&
491              !(info->mti_attr.ma_attr.la_valid & LA_SIZE)))
492                 act = MDT_SOM_DISABLE;
493
494         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                         RETURN_EXIT;
589                 }
590         } else if (info->mti_transno == 0) {
591                 info->mti_transno = ++mdt->mdt_lut.lut_last_transno;
592         } else {
593                 /* should be replay */
594                 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
595                         mdt->mdt_lut.lut_last_transno = info->mti_transno;
596         }
597         spin_unlock(&mdt->mdt_lut.lut_translock);
598
599         CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
600                info->mti_transno,
601                req->rq_export->exp_obd->obd_last_committed);
602
603         req->rq_transno = info->mti_transno;
604         lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
605
606         /* update lcd in memory only for resent cases */
607         ted = &req->rq_export->exp_target_data;
608         LASSERT(ted);
609         mutex_lock(&ted->ted_lcd_lock);
610         lcd = ted->ted_lcd;
611         if (info->mti_transno < lcd->lcd_last_transno &&
612             info->mti_transno != 0) {
613                 /* This should happen during replay. Do not update
614                  * last rcvd info if replay req transno < last transno,
615                  * otherwise the following resend(after replay) can not
616                  * be checked correctly by xid */
617                 mutex_unlock(&ted->ted_lcd_lock);
618                 CDEBUG(D_HA, "%s: transno = "LPU64" < last_transno = "LPU64"\n",
619                        mdt_obd_name(mdt), info->mti_transno,
620                        lcd->lcd_last_transno);
621                 RETURN_EXIT;
622         }
623
624         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
625             lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
626                 if (info->mti_transno != 0)
627                         lcd->lcd_last_close_transno = info->mti_transno;
628                 lcd->lcd_last_close_xid = req->rq_xid;
629                 lcd->lcd_last_close_result = rc;
630         } else {
631                 /* VBR: save versions in last_rcvd for reconstruct. */
632                 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
633                 if (pre_versions) {
634                         lcd->lcd_pre_versions[0] = pre_versions[0];
635                         lcd->lcd_pre_versions[1] = pre_versions[1];
636                         lcd->lcd_pre_versions[2] = pre_versions[2];
637                         lcd->lcd_pre_versions[3] = pre_versions[3];
638                 }
639                 if (info->mti_transno != 0)
640                         lcd->lcd_last_transno = info->mti_transno;
641
642                 lcd->lcd_last_xid = req->rq_xid;
643                 lcd->lcd_last_result = rc;
644                 lcd->lcd_last_data = info->mti_opdata;
645         }
646         mutex_unlock(&ted->ted_lcd_lock);
647
648         EXIT;
649 }
650
651 void mdt_mfd_set_mode(struct mdt_file_data *mfd, int mode)
652 {
653         LASSERT(mfd != NULL);
654
655         CDEBUG(D_HA, "Change mfd %p mode 0x%x->0x%x\n",
656                mfd, (unsigned int)mfd->mfd_mode, (unsigned int)mode);
657
658         mfd->mfd_mode = mode;
659 }
660
661 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
662                         struct mdt_object *o, __u64 flags, int created)
663 {
664         struct ptlrpc_request   *req = mdt_info_req(info);
665         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
666         struct mdt_file_data    *mfd;
667         struct md_attr          *ma  = &info->mti_attr;
668         struct lu_attr          *la  = &ma->ma_attr;
669         struct mdt_body         *repbody;
670         int                      rc = 0, isdir, isreg;
671         ENTRY;
672
673         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
674
675         isreg = S_ISREG(la->la_mode);
676         isdir = S_ISDIR(la->la_mode);
677         if (isreg && !(ma->ma_valid & MA_LOV)) {
678                 /*
679                  * No EA, check whether it is will set regEA and dirEA since in
680                  * above attr get, these size might be zero, so reset it, to
681                  * retrieve the MD after create obj.
682                  */
683                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
684                                                        &RMF_MDT_MD,
685                                                        RCL_SERVER);
686                 /* in replay case, p == NULL */
687                 rc = mdt_create_data(info, p, o);
688                 if (rc)
689                         RETURN(rc);
690         }
691
692         CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
693                ma->ma_valid, ma->ma_lmm_size);
694
695         if (ma->ma_valid & MA_LOV) {
696                 LASSERT(ma->ma_lmm_size != 0);
697                 repbody->eadatasize = ma->ma_lmm_size;
698                 if (isdir)
699                         repbody->valid |= OBD_MD_FLDIREA;
700                 else
701                         repbody->valid |= OBD_MD_FLEASIZE;
702         }
703
704         if (flags & FMODE_WRITE) {
705                 rc = mdt_write_get(o);
706                 if (rc == 0) {
707                         mdt_ioepoch_open(info, o, created);
708                         repbody->ioepoch = o->mot_ioepoch;
709                 }
710         } else if (flags & MDS_FMODE_EXEC) {
711                 /* if file is released, we can't deny write because we must
712                  * restore (write) it to access it.*/
713                 if ((ma->ma_valid & MA_HSM) &&
714                     (ma->ma_hsm.mh_flags & HS_RELEASED))
715                         rc = 0;
716                 else
717                         rc = mdt_write_deny(o);
718         }
719         if (rc)
720                 RETURN(rc);
721
722         rc = mo_open(info->mti_env, mdt_object_child(o),
723                      created ? flags | MDS_OPEN_CREATED : flags);
724         if (rc)
725                 GOTO(err_out, rc);
726
727         mfd = mdt_mfd_new();
728         if (mfd != NULL) {
729                 /*
730                  * Keep a reference on this object for this open, and is
731                  * released by mdt_mfd_close().
732                  */
733                 mdt_object_get(info->mti_env, o);
734
735                 /*
736                  * @flags is always not zero. At least it should be FMODE_READ,
737                  * FMODE_WRITE or MDS_FMODE_EXEC.
738                  */
739                 LASSERT(flags != 0);
740
741                 /* Open handling. */
742                 mdt_mfd_set_mode(mfd, flags);
743
744                 mfd->mfd_object = o;
745                 mfd->mfd_xid = req->rq_xid;
746
747                 /* replay handle */
748                 if (req_is_replay(req)) {
749                         struct mdt_file_data *old_mfd;
750                         /* Check wheather old cookie already exist in
751                          * the list, becasue when do recovery, client
752                          * might be disconnected from server, and
753                          * restart replay, so there maybe some orphan
754                          * mfd here, we should remove them */
755                         LASSERT(info->mti_rr.rr_handle != NULL);
756                         old_mfd = mdt_handle2mfd(info, info->mti_rr.rr_handle);
757                         if (old_mfd) {
758                                 CDEBUG(D_HA, "del orph mfd %p fid=("DFID") "
759                                        "cookie=" LPX64"\n", mfd,
760                                        PFID(mdt_object_fid(mfd->mfd_object)),
761                                        info->mti_rr.rr_handle->cookie);
762                                 spin_lock(&med->med_open_lock);
763                                 class_handle_unhash(&old_mfd->mfd_handle);
764                                 cfs_list_del_init(&old_mfd->mfd_list);
765                                 spin_unlock(&med->med_open_lock);
766                                 /* no attr update for that close */
767                                 la->la_valid = 0;
768                                 ma->ma_valid |= MA_FLAGS;
769                                 ma->ma_attr_flags |= MDS_RECOV_OPEN;
770                                 mdt_mfd_close(info, old_mfd);
771                                 ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
772                                 ma->ma_valid &= ~MA_FLAGS;
773                         }
774                         CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
775                                info->mti_rr.rr_handle->cookie);
776                         mfd->mfd_old_handle.cookie =
777                                                 info->mti_rr.rr_handle->cookie;
778                 }
779                 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
780
781                 if (req->rq_export->exp_disconnected) {
782                         spin_lock(&med->med_open_lock);
783                         class_handle_unhash(&mfd->mfd_handle);
784                         cfs_list_del_init(&mfd->mfd_list);
785                         spin_unlock(&med->med_open_lock);
786                         mdt_mfd_close(info, mfd);
787                 } else {
788                         spin_lock(&med->med_open_lock);
789                         cfs_list_add(&mfd->mfd_list, &med->med_open_head);
790                         spin_unlock(&med->med_open_lock);
791                 }
792
793                 mdt_empty_transno(info, rc);
794         } else {
795                 GOTO(err_out, rc = -ENOMEM);
796         }
797
798         RETURN(rc);
799
800 err_out:
801         if (flags & FMODE_WRITE)
802                         /* XXX We also need to close io epoch here.
803                          * See LU-1220 - green */
804                 mdt_write_put(o);
805         else if (flags & FMODE_EXEC)
806                 mdt_write_allow(o);
807         return rc;
808 }
809
810 int mdt_finish_open(struct mdt_thread_info *info,
811                     struct mdt_object *p, struct mdt_object *o,
812                     __u64 flags, int created, struct ldlm_reply *rep)
813 {
814         struct ptlrpc_request   *req = mdt_info_req(info);
815         struct obd_export       *exp = req->rq_export;
816         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
817         struct md_attr          *ma  = &info->mti_attr;
818         struct lu_attr          *la  = &ma->ma_attr;
819         struct mdt_file_data    *mfd;
820         struct mdt_body         *repbody;
821         int                      rc = 0;
822         int                      isreg, isdir, islnk;
823         cfs_list_t              *t;
824         ENTRY;
825
826         LASSERT(ma->ma_valid & MA_INODE);
827
828         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
829
830         isreg = S_ISREG(la->la_mode);
831         isdir = S_ISDIR(la->la_mode);
832         islnk = S_ISLNK(la->la_mode);
833         mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
834
835         /* LU-2275, simulate broken behaviour (esp. prevalent in
836          * pre-2.4 servers where a very strange reply is sent on error
837          * that looks like it was actually almost succesful and a failure at the
838          * same time */
839         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_NEGATIVE_POSITIVE)) {
840                 mdt_set_disposition(info, rep, DISP_OPEN_OPEN |
841                                                DISP_LOOKUP_NEG |
842                                                DISP_LOOKUP_POS);
843
844                 if (flags & MDS_OPEN_LOCK)
845                         mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
846
847                 RETURN(-ENOENT);
848         }
849
850         if (exp_connect_rmtclient(exp)) {
851                 void *buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
852
853                 rc = mdt_pack_remote_perm(info, o, buf);
854                 if (rc) {
855                         repbody->valid &= ~OBD_MD_FLRMTPERM;
856                         repbody->aclsize = 0;
857                 } else {
858                         repbody->valid |= OBD_MD_FLRMTPERM;
859                         repbody->aclsize = sizeof(struct mdt_remote_perm);
860                 }
861         }
862 #ifdef CONFIG_FS_POSIX_ACL
863         else if (exp_connect_flags(exp) & OBD_CONNECT_ACL) {
864                 const struct lu_env *env = info->mti_env;
865                 struct md_object *next = mdt_object_child(o);
866                 struct lu_buf *buf = &info->mti_buf;
867
868                 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
869                 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
870                                                    RCL_SERVER);
871                 if (buf->lb_len > 0) {
872                         rc = mo_xattr_get(env, next, buf,
873                                           XATTR_NAME_ACL_ACCESS);
874                         if (rc < 0) {
875                                 if (rc == -ENODATA) {
876                                         repbody->aclsize = 0;
877                                         repbody->valid |= OBD_MD_FLACL;
878                                         rc = 0;
879                                 } else if (rc == -EOPNOTSUPP) {
880                                         rc = 0;
881                                 } else {
882                                         CERROR("got acl size: %d\n", rc);
883                                 }
884                         } else {
885                                 repbody->aclsize = rc;
886                                 repbody->valid |= OBD_MD_FLACL;
887                                 rc = 0;
888                         }
889                 }
890         }
891 #endif
892
893         if (info->mti_mdt->mdt_opts.mo_mds_capa &&
894             exp_connect_flags(exp) & OBD_CONNECT_MDS_CAPA) {
895                 struct lustre_capa *capa;
896
897                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
898                 LASSERT(capa);
899                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
900                 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
901                 if (rc)
902                         RETURN(rc);
903                 repbody->valid |= OBD_MD_FLMDSCAPA;
904         }
905         if (info->mti_mdt->mdt_opts.mo_oss_capa &&
906             exp_connect_flags(exp) & OBD_CONNECT_OSS_CAPA &&
907             S_ISREG(lu_object_attr(&o->mot_obj.mo_lu))) {
908                 struct lustre_capa *capa;
909
910                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
911                 LASSERT(capa);
912                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
913                 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
914                 if (rc)
915                         RETURN(rc);
916                 repbody->valid |= OBD_MD_FLOSSCAPA;
917         }
918
919         /*
920          * If we are following a symlink, don't open; and do not return open
921          * handle for special nodes as client required.
922          */
923         if (islnk || (!isreg && !isdir &&
924             (exp_connect_flags(req->rq_export) & OBD_CONNECT_NODEVOH))) {
925                 lustre_msg_set_transno(req->rq_repmsg, 0);
926                 RETURN(0);
927         }
928
929         /*
930          * We need to return the existing object's fid back, so it is done here,
931          * after preparing the reply.
932          */
933         if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
934                 RETURN(-EEXIST);
935
936         /* This can't be done earlier, we need to return reply body */
937         if (isdir) {
938                 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
939                         /* We are trying to create or write an existing dir. */
940                         RETURN(-EISDIR);
941                 }
942         } else if (flags & MDS_OPEN_DIRECTORY)
943                 RETURN(-ENOTDIR);
944
945         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
946                                  OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
947                 RETURN(-EAGAIN);
948         }
949
950         mfd = NULL;
951         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
952                 spin_lock(&med->med_open_lock);
953                 cfs_list_for_each(t, &med->med_open_head) {
954                         mfd = cfs_list_entry(t, struct mdt_file_data, mfd_list);
955                         if (mfd->mfd_xid == req->rq_xid)
956                                 break;
957                         mfd = NULL;
958                 }
959                 spin_unlock(&med->med_open_lock);
960
961                 if (mfd != NULL) {
962                         repbody->handle.cookie = mfd->mfd_handle.h_cookie;
963                         /*set repbody->ea_size for resent case*/
964                         if (ma->ma_valid & MA_LOV) {
965                                 LASSERT(ma->ma_lmm_size != 0);
966                                 repbody->eadatasize = ma->ma_lmm_size;
967                                 if (isdir)
968                                         repbody->valid |= OBD_MD_FLDIREA;
969                                 else
970                                         repbody->valid |= OBD_MD_FLEASIZE;
971                         }
972                         mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
973                         RETURN(0);
974                 }
975         }
976
977         rc = mdt_mfd_open(info, p, o, flags, created);
978         if (!rc)
979                 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
980
981         RETURN(rc);
982 }
983
984 extern void mdt_req_from_lcd(struct ptlrpc_request *req,
985                              struct lsd_client_data *lcd);
986
987 void mdt_reconstruct_open(struct mdt_thread_info *info,
988                           struct mdt_lock_handle *lhc)
989 {
990         const struct lu_env *env = info->mti_env;
991         struct mdt_device       *mdt  = info->mti_mdt;
992         struct req_capsule      *pill = info->mti_pill;
993         struct ptlrpc_request   *req  = mdt_info_req(info);
994         struct tg_export_data   *ted  = &req->rq_export->exp_target_data;
995         struct lsd_client_data  *lcd  = ted->ted_lcd;
996         struct md_attr          *ma   = &info->mti_attr;
997         struct mdt_reint_record *rr   = &info->mti_rr;
998         __u32                   flags = info->mti_spec.sp_cr_flags;
999         struct ldlm_reply       *ldlm_rep;
1000         struct mdt_object       *parent;
1001         struct mdt_object       *child;
1002         struct mdt_body         *repbody;
1003         int                      rc;
1004         ENTRY;
1005
1006         LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1007         ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
1008         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1009
1010         ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
1011         ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
1012                                                RCL_SERVER);
1013         ma->ma_need = MA_INODE | MA_HSM;
1014         if (ma->ma_lmm_size > 0)
1015                 ma->ma_need |= MA_LOV;
1016
1017         ma->ma_valid = 0;
1018
1019         mdt_req_from_lcd(req, lcd);
1020         mdt_set_disposition(info, ldlm_rep, lcd->lcd_last_data);
1021
1022         CDEBUG(D_INODE, "This is reconstruct open: disp="LPX64", result=%d\n",
1023                ldlm_rep->lock_policy_res1, req->rq_status);
1024
1025         if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
1026             req->rq_status != 0)
1027                 /* We did not create successfully, return error to client. */
1028                 GOTO(out, rc = req->rq_status);
1029
1030         if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
1031                 struct obd_export *exp = req->rq_export;
1032                 /*
1033                  * We failed after creation, but we do not know in which step
1034                  * we failed. So try to check the child object.
1035                  */
1036                 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1037                 if (IS_ERR(parent)) {
1038                         rc = PTR_ERR(parent);
1039                         LCONSOLE_WARN("Parent "DFID" lookup error %d."
1040                                       " Evicting client %s with export %s.\n",
1041                                       PFID(rr->rr_fid1), rc,
1042                                       obd_uuid2str(&exp->exp_client_uuid),
1043                                       obd_export_nid2str(exp));
1044                         mdt_export_evict(exp);
1045                         RETURN_EXIT;
1046                 }
1047                 child = mdt_object_find(env, mdt, rr->rr_fid2);
1048                 if (IS_ERR(child)) {
1049                         rc = PTR_ERR(child);
1050                         LCONSOLE_WARN("Child "DFID" lookup error %d."
1051                                       " Evicting client %s with export %s.\n",
1052                                       PFID(mdt_object_fid(child)), rc,
1053                                       obd_uuid2str(&exp->exp_client_uuid),
1054                                       obd_export_nid2str(exp));
1055                         mdt_object_put(env, parent);
1056                         mdt_export_evict(exp);
1057                         RETURN_EXIT;
1058                 }
1059
1060                 if (unlikely(mdt_object_remote(child))) {
1061                         /* the child object was created on remote server */
1062                         if (!mdt_is_dne_client(exp)) {
1063                                 /* Return -EIO for old client */
1064                                 mdt_object_put(env, parent);
1065                                 mdt_object_put(env, child);
1066                                 GOTO(out, rc = -EIO);
1067                         }
1068                         repbody->fid1 = *rr->rr_fid2;
1069                         repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1070                         rc = 0;
1071                 } else {
1072                         if (mdt_object_exists(child)) {
1073                                 mdt_set_capainfo(info, 1, rr->rr_fid2,
1074                                                  BYPASS_CAPA);
1075                                 rc = mdt_attr_get_complex(info, child, ma);
1076                                 if (rc == 0)
1077                                         rc = mdt_finish_open(info, parent,
1078                                                              child, flags,
1079                                                              1, ldlm_rep);
1080                         } else {
1081                                 /* the child does not exist, we should do
1082                                  * regular open */
1083                                 mdt_object_put(env, parent);
1084                                 mdt_object_put(env, child);
1085                                 GOTO(regular_open, 0);
1086                         }
1087                 }
1088                 mdt_object_put(env, parent);
1089                 mdt_object_put(env, child);
1090                 GOTO(out, rc);
1091         } else {
1092 regular_open:
1093                 /* We did not try to create, so we are a pure open */
1094                 rc = mdt_reint_open(info, lhc);
1095         }
1096
1097         EXIT;
1098 out:
1099         req->rq_status = rc;
1100         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1101         LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
1102 }
1103
1104 int mdt_open_by_fid(struct mdt_thread_info* info,
1105                     struct ldlm_reply *rep)
1106 {
1107         __u32                    flags = info->mti_spec.sp_cr_flags;
1108         struct mdt_reint_record *rr = &info->mti_rr;
1109         struct md_attr          *ma = &info->mti_attr;
1110         struct mdt_object       *o;
1111         int                      rc;
1112         ENTRY;
1113
1114         o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1115         if (IS_ERR(o))
1116                 RETURN(rc = PTR_ERR(o));
1117
1118         if (unlikely(mdt_object_remote(o))) {
1119                 /* the child object was created on remote server */
1120                 struct mdt_body *repbody;
1121
1122                 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1123                                                 DISP_LOOKUP_EXECD |
1124                                                 DISP_LOOKUP_POS));
1125                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1126                 repbody->fid1 = *rr->rr_fid2;
1127                 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1128                 rc = 0;
1129         } else {
1130                 if (mdt_object_exists(o)) {
1131                         mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1132                                                         DISP_LOOKUP_EXECD |
1133                                                         DISP_LOOKUP_POS));
1134
1135                         rc = mdt_attr_get_complex(info, o, ma);
1136                         if (rc == 0)
1137                                 rc = mdt_finish_open(info, NULL, o, flags, 0,
1138                                                      rep);
1139                 } else {
1140                         rc = -ENOENT;
1141                 }
1142         }
1143
1144         mdt_object_put(info->mti_env, o);
1145         RETURN(rc);
1146 }
1147
1148 /* lock object for open */
1149 static int mdt_object_open_lock(struct mdt_thread_info *info,
1150                                 struct mdt_object *obj,
1151                                 struct mdt_lock_handle *lhc,
1152                                 __u64 *ibits)
1153 {
1154         struct md_attr  *ma = &info->mti_attr;
1155         __u64            open_flags = info->mti_spec.sp_cr_flags;
1156         ldlm_mode_t      lm = LCK_CR;
1157         bool             try_layout = false;
1158         bool             create_layout = false;
1159         int              rc = 0;
1160         ENTRY;
1161
1162         *ibits = 0;
1163         if (open_flags & MDS_OPEN_LOCK) {
1164                 if (open_flags & FMODE_WRITE)
1165                         lm = LCK_CW;
1166                 /* if file is released, we can't deny write because we must
1167                  * restore (write) it to access it. */
1168                 else if ((open_flags & MDS_FMODE_EXEC) &&
1169                          !((ma->ma_valid & MA_HSM) &&
1170                            (ma->ma_hsm.mh_flags & HS_RELEASED)))
1171                         lm = LCK_PR;
1172                 else
1173                         lm = LCK_CR;
1174
1175                 *ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN;
1176         }
1177
1178         if (S_ISREG(lu_object_attr(&obj->mot_obj.mo_lu))) {
1179                 if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV) &&
1180                     md_should_create(open_flags))
1181                         create_layout = true;
1182                 if (exp_connect_layout(info->mti_exp) && !create_layout &&
1183                     ma->ma_need & MA_LOV)
1184                         try_layout = true;
1185         }
1186
1187         mdt_lock_handle_init(lhc);
1188         mdt_lock_reg_init(lhc, lm);
1189
1190         /* one problem to return layout lock on open is that it may result
1191          * in too many layout locks cached on the client side. */
1192         if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
1193                 /* return lookup lock to validate inode at the client side,
1194                  * this is pretty important otherwise mdt will return layout
1195                  * lock for each open.
1196                  * However this is a double-edged sword because changing
1197                  * permission will revoke huge # of LOOKUP locks. */
1198                 *ibits |= MDS_INODELOCK_LAYOUT | MDS_INODELOCK_LOOKUP;
1199                 if (!mdt_object_lock_try(info, obj, lhc, *ibits,
1200                                          MDT_CROSS_LOCK)) {
1201                         *ibits &= ~(MDS_INODELOCK_LAYOUT|MDS_INODELOCK_LOOKUP);
1202                         if (*ibits != 0)
1203                                 rc = mdt_object_lock(info, obj, lhc, *ibits,
1204                                                 MDT_CROSS_LOCK);
1205                 }
1206         } else if (*ibits != 0) {
1207                 rc = mdt_object_lock(info, obj, lhc, *ibits, MDT_CROSS_LOCK);
1208         }
1209
1210         CDEBUG(D_INODE, "Requested bits lock:"DFID ", ibits = "LPX64
1211                 ", open_flags = "LPO64", try_layout = %d, rc = %d\n",
1212                 PFID(mdt_object_fid(obj)), *ibits, open_flags, try_layout, rc);
1213
1214         /* will change layout, revoke layout locks by enqueuing EX lock. */
1215         if (rc == 0 && create_layout) {
1216                 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
1217
1218                 CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
1219                         ", open_flags = "LPO64"\n",
1220                         PFID(mdt_object_fid(obj)), open_flags);
1221
1222                 LASSERT(!try_layout);
1223                 mdt_lock_handle_init(ll);
1224                 mdt_lock_reg_init(ll, LCK_EX);
1225                 rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT,
1226                                         MDT_LOCAL_LOCK);
1227
1228                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
1229         }
1230
1231         RETURN(rc);
1232 }
1233
1234 static void mdt_object_open_unlock(struct mdt_thread_info *info,
1235                                    struct mdt_object *obj,
1236                                    struct mdt_lock_handle *lhc,
1237                                    __u64 ibits, int rc)
1238 {
1239         __u64 open_flags = info->mti_spec.sp_cr_flags;
1240         struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
1241
1242         /* Release local layout lock - the layout lock put in MDT_LH_LAYOUT
1243          * will never return to client side. */
1244         if (lustre_handle_is_used(&ll->mlh_reg_lh)) {
1245                 LASSERT(!(ibits & MDS_INODELOCK_LAYOUT));
1246                 mdt_object_unlock(info, obj, ll, 1);
1247         }
1248
1249         /* Cross-ref case, the lock should be returned to the client */
1250         if (ibits == 0 || rc == -EREMOTE)
1251                 return;
1252
1253         if (!(open_flags & MDS_OPEN_LOCK) && !(ibits & MDS_INODELOCK_LAYOUT)) {
1254                 /* for the open request, the lock will only return to client
1255                  * if open or layout lock is granted. */
1256                 rc = 1;
1257         }
1258
1259         if (rc != 0) {
1260                 struct ldlm_reply       *ldlm_rep;
1261
1262                 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1263                 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1264                 mdt_object_unlock(info, obj, lhc, 1);
1265         }
1266 }
1267
1268 int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
1269                          struct mdt_lock_handle *lhc)
1270 {
1271         const struct lu_env     *env   = info->mti_env;
1272         struct mdt_device       *mdt   = info->mti_mdt;
1273         __u32                    flags = info->mti_spec.sp_cr_flags;
1274         struct mdt_reint_record *rr    = &info->mti_rr;
1275         struct md_attr          *ma    = &info->mti_attr;
1276         struct mdt_object       *parent= NULL;
1277         struct mdt_object       *o;
1278         int                      rc;
1279         __u64                    ibits = 0;
1280         ENTRY;
1281
1282         if (md_should_create(flags) && !(flags & MDS_OPEN_HAS_EA)) {
1283                 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1284                         parent = mdt_object_find(env, mdt, rr->rr_fid1);
1285                         if (IS_ERR(parent)) {
1286                                 CDEBUG(D_INODE, "Fail to find parent "DFID
1287                                        " for anonymous created %ld, try to"
1288                                        " use server-side parent.\n",
1289                                        PFID(rr->rr_fid1), PTR_ERR(parent));
1290                                 parent = NULL;
1291                         }
1292                 }
1293                 if (parent == NULL)
1294                         ma->ma_need |= MA_PFID;
1295         }
1296
1297         o = mdt_object_find(env, mdt, rr->rr_fid2);
1298         if (IS_ERR(o))
1299                 RETURN(rc = PTR_ERR(o));
1300
1301         if (mdt_object_remote(o)) {
1302                 CDEBUG(D_INFO, "%s: "DFID" is on remote MDT.\n",
1303                        mdt_obd_name(info->mti_mdt),
1304                        PFID(rr->rr_fid2));
1305                 GOTO(out, rc = -EREMOTE);
1306         } else if (!mdt_object_exists(o)) {
1307                 mdt_set_disposition(info, rep,
1308                                     DISP_IT_EXECD |
1309                                     DISP_LOOKUP_EXECD |
1310                                     DISP_LOOKUP_NEG);
1311                 GOTO(out, rc = -ENOENT);
1312         }
1313
1314         mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1315
1316         rc = mdt_attr_get_complex(info, o, ma);
1317         if (rc)
1318                 GOTO(out, rc);
1319
1320         rc = mdt_object_open_lock(info, o, lhc, &ibits);
1321         if (rc)
1322                 GOTO(out, rc);
1323
1324         if (ma->ma_valid & MA_PFID) {
1325                 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1326                 if (IS_ERR(parent)) {
1327                         CDEBUG(D_INODE, "Fail to find parent "DFID
1328                                " for anonymous created %ld, try to"
1329                                " use system default.\n",
1330                                PFID(&ma->ma_pfid), PTR_ERR(parent));
1331                         parent = NULL;
1332                 }
1333         }
1334
1335         rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1336         if (!rc) {
1337                 mdt_set_disposition(info, rep, DISP_LOOKUP_POS);
1338                 if (flags & MDS_OPEN_LOCK)
1339                         mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1340         }
1341         GOTO(out, rc);
1342
1343 out:
1344         mdt_object_open_unlock(info, o, lhc, ibits, rc);
1345         mdt_object_put(env, o);
1346         if (parent != NULL)
1347                 mdt_object_put(env, parent);
1348         return rc;
1349 }
1350
1351 int mdt_pin(struct mdt_thread_info* info)
1352 {
1353         ENTRY;
1354         RETURN(err_serious(-EOPNOTSUPP));
1355 }
1356
1357 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1358 static int mdt_cross_open(struct mdt_thread_info *info,
1359                           const struct lu_fid *parent_fid,
1360                           const struct lu_fid *fid,
1361                           struct ldlm_reply *rep, __u32 flags)
1362 {
1363         struct md_attr    *ma = &info->mti_attr;
1364         struct mdt_object *o;
1365         int                rc;
1366         ENTRY;
1367
1368         o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1369         if (IS_ERR(o))
1370                 RETURN(rc = PTR_ERR(o));
1371
1372         if (mdt_object_remote(o)) {
1373                 /* Something is wrong here, the object is on another MDS! */
1374                 CERROR("%s: "DFID" isn't on this server!: rc = %d\n",
1375                        mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1376                 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1377                                 &o->mot_obj.mo_lu,
1378                                 "Object isn't on this server! FLD error?\n");
1379                 rc = -EFAULT;
1380         } else {
1381                 if (mdt_object_exists(o)) {
1382                         /* Do permission check for cross-open. */
1383                         rc = mo_permission(info->mti_env, NULL,
1384                                            mdt_object_child(o),
1385                                            NULL, flags | MDS_OPEN_CROSS);
1386                         if (rc)
1387                                 goto out;
1388
1389                         mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
1390                         rc = mdt_attr_get_complex(info, o, ma);
1391                         if (rc != 0)
1392                                 GOTO(out, rc);
1393
1394                         /* Do not create lov object if the fid is opened
1395                          * under OBF */
1396                         if (S_ISREG(ma->ma_attr.la_mode) &&
1397                             !(ma->ma_valid & MA_LOV) && (flags & FMODE_WRITE) &&
1398                             fid_is_obf(parent_fid))
1399                                 GOTO(out, rc = -EPERM);
1400
1401                         rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1402                 } else {
1403                         /*
1404                          * Something is wrong here. lookup was positive but
1405                          * there is no object!
1406                          */
1407                         CERROR("%s: "DFID" doesn't exist!: rc = %d\n",
1408                               mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1409                         rc = -EFAULT;
1410                 }
1411         }
1412 out:
1413         mdt_object_put(info->mti_env, o);
1414         RETURN(rc);
1415 }
1416
1417 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1418 {
1419         struct mdt_device       *mdt = info->mti_mdt;
1420         struct ptlrpc_request   *req = mdt_info_req(info);
1421         struct mdt_object       *parent;
1422         struct mdt_object       *child;
1423         struct mdt_lock_handle  *lh;
1424         struct ldlm_reply       *ldlm_rep;
1425         struct mdt_body         *repbody;
1426         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
1427         struct md_attr          *ma = &info->mti_attr;
1428         __u64                    create_flags = info->mti_spec.sp_cr_flags;
1429         __u64                    ibits;
1430         struct mdt_reint_record *rr = &info->mti_rr;
1431         struct lu_name          *lname;
1432         int                      result, rc;
1433         int                      created = 0;
1434         __u32                    msg_flags;
1435         ENTRY;
1436
1437         OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1438                                (obd_timeout + 1) / 4);
1439
1440         mdt_counter_incr(req, LPROC_MDT_OPEN);
1441         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1442
1443         ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
1444         ma->ma_lmm_size = req_capsule_get_size(info->mti_pill, &RMF_MDT_MD,
1445                                                RCL_SERVER);
1446         ma->ma_need = MA_INODE;
1447         if (ma->ma_lmm_size > 0)
1448                 ma->ma_need |= MA_LOV;
1449
1450         ma->ma_valid = 0;
1451
1452         LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1453         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1454
1455         if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1456                 CERROR("file join is not supported anymore.\n");
1457                 GOTO(out, result = err_serious(-EOPNOTSUPP));
1458         }
1459         msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1460
1461         if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1462             info->mti_spec.u.sp_ea.eadata == NULL)
1463                 GOTO(out, result = err_serious(-EINVAL));
1464
1465         CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
1466                "cr_flag="LPO64" mode=0%06o msg_flag=0x%x\n",
1467                PFID(rr->rr_fid1), rr->rr_name,
1468                PFID(rr->rr_fid2), create_flags,
1469                ma->ma_attr.la_mode, msg_flags);
1470         if (info->mti_cross_ref) {
1471                 /* This is cross-ref open */
1472                 mdt_set_disposition(info, ldlm_rep,
1473                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD |
1474                              DISP_LOOKUP_POS));
1475                 result = mdt_cross_open(info, rr->rr_fid2, rr->rr_fid1,
1476                                         ldlm_rep, create_flags);
1477                 GOTO(out, result);
1478         } else if (req_is_replay(req) ||
1479             (req->rq_export->exp_libclient && create_flags & MDS_OPEN_HAS_EA)) {
1480                 /* This is a replay request or from liblustre with ea. */
1481                 result = mdt_open_by_fid(info, ldlm_rep);
1482
1483                 if (result != -ENOENT) {
1484                         if (req->rq_export->exp_libclient &&
1485                             create_flags & MDS_OPEN_HAS_EA)
1486                                 GOTO(out, result = 0);
1487                         GOTO(out, result);
1488                 }
1489                 /* We didn't find the correct object, so we need to re-create it
1490                  * via a regular replay. */
1491                 if (!(create_flags & MDS_OPEN_CREAT)) {
1492                         DEBUG_REQ(D_ERROR, req,
1493                                   "OPEN & CREAT not in open replay/by_fid.");
1494                         GOTO(out, result = -EFAULT);
1495                 }
1496                 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1497         } else if ((rr->rr_namelen == 0 && create_flags & MDS_OPEN_LOCK) ||
1498                    (create_flags & MDS_OPEN_BY_FID)) {
1499                 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1500                 /* If result is 0 then open by FID has found the file
1501                  * and there is nothing left for us to do here.  More
1502                  * generally if it is anything other than -ENOENT or
1503                  * -EREMOTE then we return that now.  If -ENOENT and
1504                  * MDS_OPEN_CREAT is set then we must create the file
1505                  * below.  If -EREMOTE then we need to return a LOOKUP
1506                  * lock to the client, which we do below.  Hence this
1507                  * odd looking condition.  See LU-2523. */
1508                 if (!(result == -ENOENT && (create_flags & MDS_OPEN_CREAT)) &&
1509                     result != -EREMOTE)
1510                         GOTO(out, result);
1511
1512                 if (unlikely(rr->rr_namelen == 0))
1513                         GOTO(out, result = -EINVAL);
1514
1515                 CDEBUG(D_INFO, "No object(2), continue as regular open.\n");
1516         }
1517
1518         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1519                 GOTO(out, result = err_serious(-ENOMEM));
1520
1521         mdt_set_disposition(info, ldlm_rep,
1522                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1523
1524         lh = &info->mti_lh[MDT_LH_PARENT];
1525         mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
1526                           LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
1527
1528         parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1529                                       MDS_INODELOCK_UPDATE);
1530         if (IS_ERR(parent))
1531                 GOTO(out, result = PTR_ERR(parent));
1532
1533         /* get and check version of parent */
1534         result = mdt_version_get_check(info, parent, 0);
1535         if (result)
1536                 GOTO(out_parent, result);
1537
1538         fid_zero(child_fid);
1539
1540         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1541         result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1542                             lname, child_fid, &info->mti_spec);
1543         LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1544                  "looking for "DFID"/%s, result fid="DFID"\n",
1545                  PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
1546
1547         if (result != 0 && result != -ENOENT && result != -ESTALE)
1548                 GOTO(out_parent, result);
1549
1550         if (result == -ENOENT || result == -ESTALE) {
1551                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1552                 if (result == -ESTALE) {
1553                         /*
1554                          * -ESTALE means the parent is a dead(unlinked) dir, so
1555                          * it should return -ENOENT to in accordance with the
1556                          * original mds implementaion.
1557                          */
1558                         GOTO(out_parent, result = -ENOENT);
1559                 }
1560                 if (!(create_flags & MDS_OPEN_CREAT))
1561                         GOTO(out_parent, result);
1562                 *child_fid = *info->mti_rr.rr_fid2;
1563                 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1564                          PFID(child_fid));
1565                 /* In the function below, .hs_keycmp resolves to
1566                  * lu_obj_hop_keycmp() */
1567                 /* coverity[overrun-buffer-val] */
1568                 child = mdt_object_new(info->mti_env, mdt, child_fid);
1569         } else {
1570                 /*
1571                  * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1572                  * return FID back in that case.
1573                  */
1574                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1575                 child = mdt_object_find(info->mti_env, mdt, child_fid);
1576         }
1577         if (IS_ERR(child))
1578                 GOTO(out_parent, result = PTR_ERR(child));
1579
1580         /** check version of child  */
1581         rc = mdt_version_get_check(info, child, 1);
1582         if (rc)
1583                 GOTO(out_child, result = rc);
1584
1585         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1586         if (result == -ENOENT) {
1587                 /* Create under OBF and .lustre is not permitted */
1588                 if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1))
1589                         GOTO(out_child, result = -EPERM);
1590
1591                 /* save versions in reply */
1592                 mdt_version_get_save(info, parent, 0);
1593                 mdt_version_get_save(info, child, 1);
1594
1595                 /* version of child will be changed */
1596                 info->mti_mos = child;
1597
1598                 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1599                 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1600
1601                 /* Let lower layers know what is lock mode on directory. */
1602                 info->mti_spec.sp_cr_mode =
1603                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1604
1605                 /*
1606                  * Do not perform lookup sanity check. We know that name does
1607                  * not exist.
1608                  */
1609                 info->mti_spec.sp_cr_lookup = 0;
1610                 info->mti_spec.sp_feat = &dt_directory_features;
1611
1612                 result = mdo_create(info->mti_env,
1613                                     mdt_object_child(parent),
1614                                     lname,
1615                                     mdt_object_child(child),
1616                                     &info->mti_spec,
1617                                     &info->mti_attr);
1618                 if (result == -ERESTART) {
1619                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1620                         GOTO(out_child, result);
1621                 } else {
1622
1623                         /* XXX: we should call this once, see few lines below */
1624                         if (result == 0)
1625                                 result = mdt_attr_get_complex(info, child, ma);
1626
1627                         if (result != 0)
1628                                 GOTO(out_child, result);
1629                 }
1630                 created = 1;
1631         } else {
1632                 /*
1633                  * The object is on remote node, return its FID for remote open.
1634                  */
1635                 if (mdt_object_remote(child)) {
1636                         /*
1637                          * Check if this lock already was sent to client and
1638                          * this is resent case. For resent case do not take lock
1639                          * again, use what is already granted.
1640                          */
1641                         LASSERT(lhc != NULL);
1642
1643                         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1644                                 struct ldlm_lock *lock;
1645
1646                                 LASSERT(msg_flags & MSG_RESENT);
1647
1648                                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1649                                 if (!lock) {
1650                                         CERROR("Invalid lock handle "LPX64"\n",
1651                                                lhc->mlh_reg_lh.cookie);
1652                                         LBUG();
1653                                 }
1654                                 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1655                                                         &lock->l_resource->lr_name));
1656                                 LDLM_LOCK_PUT(lock);
1657                                 rc = 0;
1658                         } else {
1659                                 mdt_lock_handle_init(lhc);
1660                                 mdt_lock_reg_init(lhc, LCK_PR);
1661
1662                                 rc = mdt_object_lock(info, child, lhc,
1663                                                      MDS_INODELOCK_LOOKUP,
1664                                                      MDT_CROSS_LOCK);
1665                         }
1666                         repbody->fid1 = *mdt_object_fid(child);
1667                         repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1668                         if (rc != 0)
1669                                 result = rc;
1670                         else
1671                                 result = -EREMOTE;
1672                         GOTO(out_child, result);
1673                 } else {
1674                         if (mdt_object_exists(child)) {
1675                                 /* We have to get attr & LOV EA & HSM for this
1676                                  * object */
1677                                 ma->ma_need |= MA_HSM;
1678                                 result = mdt_attr_get_complex(info, child, ma);
1679                         } else {
1680                                 /*object non-exist!!!*/
1681                                 LBUG();
1682                         }
1683                 }
1684         }
1685
1686         LASSERT(!lustre_handle_is_used(&lhc->mlh_reg_lh));
1687
1688         /* get openlock if this is not replay and if a client requested it */
1689         if (!req_is_replay(req)) {
1690                 rc = mdt_object_open_lock(info, child, lhc, &ibits);
1691                 if (rc != 0)
1692                         GOTO(out_child, result = rc);
1693                 else if (create_flags & MDS_OPEN_LOCK)
1694                         mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1695         }
1696
1697         /* Try to open it now. */
1698         rc = mdt_finish_open(info, parent, child, create_flags,
1699                              created, ldlm_rep);
1700         if (rc) {
1701                 result = rc;
1702                 /* openlock will be released if mdt_finish_open failed */
1703                 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1704                 if (created) {
1705                         ma->ma_need = 0;
1706                         ma->ma_valid = 0;
1707                         ma->ma_cookie_size = 0;
1708                         rc = mdo_unlink(info->mti_env,
1709                                         mdt_object_child(parent),
1710                                         mdt_object_child(child),
1711                                         lname,
1712                                         &info->mti_attr, 0);
1713                         if (rc != 0)
1714                                 CERROR("%s: "DFID" cleanup of open: rc = %d\n",
1715                                        mdt_obd_name(info->mti_mdt),
1716                                        PFID(mdt_object_fid(child)), rc);
1717                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1718                 }
1719         }
1720         EXIT;
1721 out_child:
1722         mdt_object_open_unlock(info, child, lhc, ibits, result);
1723         mdt_object_put(info->mti_env, child);
1724 out_parent:
1725         mdt_object_unlock_put(info, parent, lh, result || !created);
1726 out:
1727         if (result)
1728                 lustre_msg_set_transno(req->rq_repmsg, 0);
1729         return result;
1730 }
1731
1732 #define MFD_CLOSED(mode) (((mode) & ~(MDS_FMODE_EPOCH | MDS_FMODE_SOM | \
1733                                       MDS_FMODE_TRUNC)) == MDS_FMODE_CLOSED)
1734
1735 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1736 {
1737         return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1738 }
1739
1740 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1741 {
1742         struct mdt_object *o = mfd->mfd_object;
1743         struct md_object *next = mdt_object_child(o);
1744         struct md_attr *ma = &info->mti_attr;
1745         int ret = MDT_IOEPOCH_CLOSED;
1746         int rc = 0;
1747         int mode;
1748         ENTRY;
1749
1750         mode = mfd->mfd_mode;
1751
1752         if ((mode & FMODE_WRITE) || (mode & MDS_FMODE_TRUNC)) {
1753                 mdt_write_put(o);
1754                 ret = mdt_ioepoch_close(info, o);
1755         } else if (mode & MDS_FMODE_EXEC) {
1756                 mdt_write_allow(o);
1757         } else if (mode & MDS_FMODE_EPOCH) {
1758                 ret = mdt_ioepoch_close(info, o);
1759         } else if (mode & MDS_FMODE_SOM) {
1760                 ret = mdt_som_au_close(info, o);
1761         }
1762
1763         /* Update atime on close only. */
1764         if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1765             && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1766                 /* Set the atime only. */
1767                 ma->ma_valid = MA_INODE;
1768                 ma->ma_attr.la_valid = LA_ATIME;
1769                 rc = mo_attr_set(info->mti_env, next, ma);
1770         }
1771
1772         /* If file data is modified, add the dirty flag.
1773          *
1774          * If MDS_CLOSE_CLEANUP is set, this file is being closed due to an
1775          * eviction, file could have been modified and now dirty
1776          * regarding to HSM archive, check this!
1777          * The logic here is to mark a file dirty if there's a chance it was
1778          * dirtied before the client was evicted, so that we don't have to wait
1779          * for a release attempt before finding out the file was actually dirty
1780          * and fail the release. Aggressively marking it dirty here will cause
1781          * the policy engine to attempt to re-archive it; when rearchiving, we
1782          * can compare the current version to the LMA data_version and make the
1783          * archive request into a noop if it's not actually dirty.
1784          */
1785         if ((ma->ma_attr_flags & MDS_DATA_MODIFIED) ||
1786             ((ma->ma_attr_flags & MDS_CLOSE_CLEANUP) &&
1787              (mode & (FMODE_WRITE|MDS_FMODE_TRUNC))))
1788                 rc = mdt_add_dirty_flag(info, o, ma);
1789
1790         ma->ma_need |= MA_INODE;
1791         ma->ma_valid &= ~MA_INODE;
1792
1793         if (!MFD_CLOSED(mode))
1794                 rc = mo_close(info->mti_env, next, ma, mode);
1795
1796         if (ret == MDT_IOEPOCH_GETATTR || ret == MDT_IOEPOCH_OPENED) {
1797                 struct mdt_export_data *med;
1798
1799                 /* The IOepoch is still opened or SOM update is needed.
1800                  * Put mfd back into the list. */
1801                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1802                 mdt_mfd_set_mode(mfd, ret == MDT_IOEPOCH_OPENED ?
1803                                       MDS_FMODE_EPOCH : MDS_FMODE_SOM);
1804
1805                 LASSERT(mdt_info_req(info));
1806                 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1807                 spin_lock(&med->med_open_lock);
1808                 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
1809                 class_handle_hash_back(&mfd->mfd_handle);
1810                 spin_unlock(&med->med_open_lock);
1811
1812                 if (ret == MDT_IOEPOCH_OPENED) {
1813                         ret = 0;
1814                 } else {
1815                         ret = -EAGAIN;
1816                         CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1817                                "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1818                 }
1819         } else {
1820                 mdt_mfd_free(mfd);
1821                 mdt_object_put(info->mti_env, o);
1822         }
1823
1824         RETURN(rc ? rc : ret);
1825 }
1826
1827 int mdt_close(struct mdt_thread_info *info)
1828 {
1829         struct mdt_export_data *med;
1830         struct mdt_file_data   *mfd;
1831         struct mdt_object      *o;
1832         struct md_attr         *ma = &info->mti_attr;
1833         struct mdt_body        *repbody = NULL;
1834         struct ptlrpc_request  *req = mdt_info_req(info);
1835         int rc, ret = 0;
1836         ENTRY;
1837
1838         mdt_counter_incr(req, LPROC_MDT_CLOSE);
1839         /* Close may come with the Size-on-MDS update. Unpack it. */
1840         rc = mdt_close_unpack(info);
1841         if (rc)
1842                 RETURN(err_serious(rc));
1843
1844         LASSERT(info->mti_ioepoch);
1845
1846         req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1847                              info->mti_mdt->mdt_max_mdsize);
1848         req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1849                              info->mti_mdt->mdt_max_cookiesize);
1850         rc = req_capsule_server_pack(info->mti_pill);
1851         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1852                 mdt_client_compatibility(info);
1853                 if (rc == 0)
1854                         mdt_fix_reply(info);
1855                 mdt_exit_ucred(info);
1856                 RETURN(lustre_msg_get_status(req->rq_repmsg));
1857         }
1858
1859         /* Continue to close handle even if we can not pack reply */
1860         if (rc == 0) {
1861                 repbody = req_capsule_server_get(info->mti_pill,
1862                                                  &RMF_MDT_BODY);
1863                 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
1864                                                     &RMF_MDT_MD);
1865                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
1866                                                        &RMF_MDT_MD,
1867                                                        RCL_SERVER);
1868                 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
1869                                                        &RMF_LOGCOOKIES);
1870                 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
1871                                                           &RMF_LOGCOOKIES,
1872                                                           RCL_SERVER);
1873                 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1874                 repbody->eadatasize = 0;
1875                 repbody->aclsize = 0;
1876         } else {
1877                 rc = err_serious(rc);
1878         }
1879
1880         med = &req->rq_export->exp_mdt_data;
1881         spin_lock(&med->med_open_lock);
1882         mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1883         if (mdt_mfd_closed(mfd)) {
1884                 spin_unlock(&med->med_open_lock);
1885                 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1886                        ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1887                        info->mti_ioepoch->handle.cookie);
1888                 /** not serious error since bug 3633 */
1889                 rc = -ESTALE;
1890         } else {
1891                 class_handle_unhash(&mfd->mfd_handle);
1892                 cfs_list_del_init(&mfd->mfd_list);
1893                 spin_unlock(&med->med_open_lock);
1894
1895                 /* Do not lose object before last unlink. */
1896                 o = mfd->mfd_object;
1897                 mdt_object_get(info->mti_env, o);
1898                 ret = mdt_mfd_close(info, mfd);
1899                 if (repbody != NULL)
1900                         rc = mdt_handle_last_unlink(info, o, ma);
1901                 mdt_empty_transno(info, rc);
1902                 mdt_object_put(info->mti_env, o);
1903         }
1904         if (repbody != NULL) {
1905                 mdt_client_compatibility(info);
1906                 rc = mdt_fix_reply(info);
1907         }
1908
1909         mdt_exit_ucred(info);
1910         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1911                 RETURN(err_serious(-ENOMEM));
1912
1913         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
1914                                  OBD_FAIL_MDS_CLOSE_NET_REP))
1915                 info->mti_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
1916         RETURN(rc ? rc : ret);
1917 }
1918
1919 /**
1920  * DONE_WRITING rpc handler.
1921  *
1922  * As mfd is not kept after replayed CLOSE (see mdt_ioepoch_close_on_replay()),
1923  * only those DONE_WRITING rpc will be replayed which really wrote smth on disk,
1924  * and got a trasid. Waiting for such DONE_WRITING is not reliable, so just
1925  * skip attributes and reconstruct the reply here.
1926  */
1927 int mdt_done_writing(struct mdt_thread_info *info)
1928 {
1929         struct ptlrpc_request   *req = mdt_info_req(info);
1930         struct mdt_body         *repbody = NULL;
1931         struct mdt_export_data  *med;
1932         struct mdt_file_data    *mfd;
1933         int rc;
1934         ENTRY;
1935
1936         rc = req_capsule_server_pack(info->mti_pill);
1937         if (rc)
1938                 RETURN(err_serious(rc));
1939
1940         repbody = req_capsule_server_get(info->mti_pill,
1941                                          &RMF_MDT_BODY);
1942         repbody->eadatasize = 0;
1943         repbody->aclsize = 0;
1944
1945         /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1946         rc = mdt_close_unpack(info);
1947         if (rc)
1948                 RETURN(err_serious(rc));
1949
1950         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1951                 mdt_exit_ucred(info);
1952                 RETURN(lustre_msg_get_status(req->rq_repmsg));
1953         }
1954
1955         med = &info->mti_exp->exp_mdt_data;
1956         spin_lock(&med->med_open_lock);
1957         mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1958         if (mfd == NULL) {
1959                 spin_unlock(&med->med_open_lock);
1960                 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1961                        ": cookie = "LPX64" ioepoch = "LPU64"\n",
1962                        PFID(info->mti_rr.rr_fid1),
1963                        info->mti_ioepoch->handle.cookie,
1964                        info->mti_ioepoch->ioepoch);
1965                 /* If this is a replay, reconstruct the transno. */
1966                 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1967                         rc = info->mti_ioepoch->flags & MF_SOM_AU ?
1968                              -EAGAIN : 0;
1969                         mdt_empty_transno(info, rc);
1970                 } else
1971                         rc = -ESTALE;
1972                 GOTO(error_ucred, rc);
1973         }
1974
1975         LASSERT(mfd->mfd_mode == MDS_FMODE_EPOCH ||
1976                 mfd->mfd_mode == MDS_FMODE_TRUNC);
1977         class_handle_unhash(&mfd->mfd_handle);
1978         cfs_list_del_init(&mfd->mfd_list);
1979         spin_unlock(&med->med_open_lock);
1980
1981         /* Set EPOCH CLOSE flag if not set by client. */
1982         info->mti_ioepoch->flags |= MF_EPOCH_CLOSE;
1983         info->mti_attr.ma_valid = 0;
1984
1985         info->mti_attr.ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
1986         OBD_ALLOC_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1987         if (info->mti_attr.ma_lmm == NULL)
1988                 GOTO(error_ucred, rc = -ENOMEM);
1989
1990         rc = mdt_mfd_close(info, mfd);
1991
1992         OBD_FREE_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1993         mdt_empty_transno(info, rc);
1994 error_ucred:
1995         mdt_exit_ucred(info);
1996         RETURN(rc);
1997 }