Whamcloud - gitweb
ee1cc2a87eb2b743bac775b1b6eb4d7b93ceebda
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/mdt/mdt_open.c
32  *
33  * Lustre Metadata Target (mdt) open/close file handling
34  *
35  * Author: Huang Hua <huanghua@clusterfs.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_MDS
39
40 #include <lustre_acl.h>
41 #include <lustre_mds.h>
42 #include <lustre_swab.h>
43 #include "mdt_internal.h"
44 #include <lustre_nodemap.h>
45
46 static const char mfd_open_handle_owner[] = "mdt";
47
48 static int mdt_open_by_fid(struct mdt_thread_info *info, struct ldlm_reply *rep,
49                            struct mdt_lock_handle *lhc);
50
51 /* Create a new mdt_file_data struct, initialize it,
52  * and insert it to global hash table */
53 struct mdt_file_data *mdt_mfd_new(const struct mdt_export_data *med)
54 {
55         struct mdt_file_data *mfd;
56
57         ENTRY;
58         OBD_ALLOC_PTR(mfd);
59         if (mfd != NULL) {
60                 refcount_set(&mfd->mfd_open_handle.h_ref, 1);
61                 INIT_HLIST_NODE(&mfd->mfd_open_handle.h_link);
62                 mfd->mfd_owner = med;
63                 INIT_LIST_HEAD(&mfd->mfd_list);
64                 class_handle_hash(&mfd->mfd_open_handle, mfd_open_handle_owner);
65         }
66
67         RETURN(mfd);
68 }
69
70 /*
71  * Find the mfd pointed to by handle in global hash table.
72  * In case of replay the handle is obsoleted
73  * but mfd can be found in mfd list by that handle.
74  * Callers need to be holding med_open_lock.
75  */
76 struct mdt_file_data *mdt_open_handle2mfd(struct mdt_export_data *med,
77                                         const struct lustre_handle *open_handle,
78                                         bool is_replay_or_resent)
79 {
80         struct mdt_file_data   *mfd;
81
82         ENTRY;
83         LASSERT(open_handle != NULL);
84         mfd = class_handle2object(open_handle->cookie, mfd_open_handle_owner);
85         if (mfd)
86                 refcount_dec(&mfd->mfd_open_handle.h_ref);
87
88         /* during dw/setattr replay the mfd can be found by old handle */
89         if ((!mfd || mfd->mfd_owner != med) && is_replay_or_resent) {
90                 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
91                         if (mfd->mfd_open_handle_old.cookie ==
92                             open_handle->cookie)
93                                 RETURN(mfd);
94                 }
95                 mfd = NULL;
96         }
97
98         RETURN(mfd);
99 }
100
101 /* free mfd */
102 void mdt_mfd_free(struct mdt_file_data *mfd)
103 {
104         LASSERT(refcount_read(&mfd->mfd_open_handle.h_ref) == 1);
105         LASSERT(list_empty(&mfd->mfd_list));
106         OBD_FREE_PRE(mfd, sizeof(*mfd), "kfree_rcu");
107         kfree_rcu(mfd, mfd_open_handle.h_rcu);
108 }
109
110 static int mdt_create_data(struct mdt_thread_info *info,
111                            struct mdt_object *p, struct mdt_object *o)
112 {
113         struct md_op_spec *spec = &info->mti_spec;
114         struct md_attr *ma = &info->mti_attr;
115         int rc = 0;
116
117         ENTRY;
118         if (!md_should_create(spec->sp_cr_flags))
119                 RETURN(0);
120
121         ma->ma_need = MA_INODE | MA_LOV;
122         ma->ma_valid = 0;
123         mutex_lock(&o->mot_lov_mutex);
124         if (!o->mot_lov_created) {
125                 rc = mdo_create_data(info->mti_env,
126                                      p ? mdt_object_child(p) : NULL,
127                                      mdt_object_child(o), spec, ma);
128                 if (rc == 0)
129                         rc = mdt_attr_get_complex(info, o, ma);
130
131                 if (rc == 0 && ma->ma_valid & MA_LOV)
132                         o->mot_lov_created = 1;
133         }
134
135         mutex_unlock(&o->mot_lov_mutex);
136         RETURN(rc);
137 }
138
139 int mdt_write_read(struct mdt_object *o)
140 {
141         int rc = 0;
142
143         ENTRY;
144         spin_lock(&o->mot_write_lock);
145         rc = o->mot_write_count;
146         spin_unlock(&o->mot_write_lock);
147         RETURN(rc);
148 }
149
150 int mdt_write_get(struct mdt_object *o)
151 {
152         int rc = 0;
153
154         ENTRY;
155         spin_lock(&o->mot_write_lock);
156         if (o->mot_write_count < 0)
157                 rc = -ETXTBSY;
158         else
159                 o->mot_write_count++;
160         spin_unlock(&o->mot_write_lock);
161
162         RETURN(rc);
163 }
164
165 void mdt_write_put(struct mdt_object *o)
166 {
167         ENTRY;
168         spin_lock(&o->mot_write_lock);
169         o->mot_write_count--;
170         spin_unlock(&o->mot_write_lock);
171         EXIT;
172 }
173
174 static int mdt_write_deny(struct mdt_object *o)
175 {
176         int rc = 0;
177
178         ENTRY;
179         spin_lock(&o->mot_write_lock);
180         if (o->mot_write_count > 0)
181                 rc = -ETXTBSY;
182         else
183                 o->mot_write_count--;
184         spin_unlock(&o->mot_write_lock);
185         RETURN(rc);
186 }
187
188 static void mdt_write_allow(struct mdt_object *o)
189 {
190         ENTRY;
191         spin_lock(&o->mot_write_lock);
192         o->mot_write_count++;
193         spin_unlock(&o->mot_write_lock);
194         EXIT;
195 }
196
197 /* there can be no real transaction so prepare the fake one */
198 static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
199 {
200         struct mdt_device *mdt = info->mti_mdt;
201         struct ptlrpc_request *req = mdt_info_req(info);
202         struct tg_export_data *ted;
203         struct lsd_client_data *lcd;
204
205         ENTRY;
206         if (mdt_rdonly(req->rq_export))
207                 RETURN_EXIT;
208
209         /* transaction has occurred already */
210         if (lustre_msg_get_transno(req->rq_repmsg) != 0)
211                 RETURN_EXIT;
212
213         if (tgt_is_multimodrpcs_client(req->rq_export)) {
214                 struct thandle *th;
215
216                 /* generate an empty transaction to get a transno
217                  * and reply data */
218                 th = dt_trans_create(info->mti_env, mdt->mdt_bottom);
219                 if (!IS_ERR(th)) {
220                         rc = dt_trans_start(info->mti_env, mdt->mdt_bottom, th);
221                         dt_trans_stop(info->mti_env, mdt->mdt_bottom, th);
222                 }
223                 RETURN_EXIT;
224         }
225
226         spin_lock(&mdt->mdt_lut.lut_translock);
227         if (rc != 0) {
228                 if (info->mti_transno != 0) {
229                         struct obd_export *exp = req->rq_export;
230
231                         CERROR("%s: replay trans %llu NID %s: rc = %d\n",
232                                mdt_obd_name(mdt), info->mti_transno,
233                                obd_export_nid2str(exp), rc);
234                         spin_unlock(&mdt->mdt_lut.lut_translock);
235                         RETURN_EXIT;
236                 }
237         } else if (info->mti_transno == 0) {
238                 info->mti_transno = ++mdt->mdt_lut.lut_last_transno;
239         } else {
240                 /* should be replay */
241                 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
242                         mdt->mdt_lut.lut_last_transno = info->mti_transno;
243         }
244         spin_unlock(&mdt->mdt_lut.lut_translock);
245
246         CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
247                info->mti_transno,
248                req->rq_export->exp_obd->obd_last_committed);
249
250         req->rq_transno = info->mti_transno;
251         lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
252
253         /* update lcd in memory only for resent cases */
254         ted = &req->rq_export->exp_target_data;
255         LASSERT(ted);
256         mutex_lock(&ted->ted_lcd_lock);
257         lcd = ted->ted_lcd;
258         if (info->mti_transno < lcd->lcd_last_transno &&
259             info->mti_transno != 0) {
260                 /* This should happen during replay. Do not update
261                  * last rcvd info if replay req transno < last transno,
262                  * otherwise the following resend(after replay) can not
263                  * be checked correctly by xid */
264                 mutex_unlock(&ted->ted_lcd_lock);
265                 CDEBUG(D_HA, "%s: transno = %llu < last_transno = %llu\n",
266                        mdt_obd_name(mdt), info->mti_transno,
267                        lcd->lcd_last_transno);
268                 RETURN_EXIT;
269         }
270
271         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
272                 if (info->mti_transno != 0)
273                         lcd->lcd_last_close_transno = info->mti_transno;
274                 lcd->lcd_last_close_xid = req->rq_xid;
275                 lcd->lcd_last_close_result = rc;
276         } else {
277                 /* VBR: save versions in last_rcvd for reconstruct. */
278                 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
279
280                 if (pre_versions) {
281                         lcd->lcd_pre_versions[0] = pre_versions[0];
282                         lcd->lcd_pre_versions[1] = pre_versions[1];
283                         lcd->lcd_pre_versions[2] = pre_versions[2];
284                         lcd->lcd_pre_versions[3] = pre_versions[3];
285                 }
286                 if (info->mti_transno != 0)
287                         lcd->lcd_last_transno = info->mti_transno;
288
289                 lcd->lcd_last_xid = req->rq_xid;
290                 lcd->lcd_last_result = rc;
291                 lcd->lcd_last_data = info->mti_opdata;
292         }
293         mutex_unlock(&ted->ted_lcd_lock);
294
295         EXIT;
296 }
297
298 void mdt_mfd_set_mode(struct mdt_file_data *mfd, u64 open_flags)
299 {
300         LASSERT(mfd != NULL);
301
302         CDEBUG(D_DENTRY, DFID " Change mfd open_flags %#llo -> %#llo.\n",
303                PFID(mdt_object_fid(mfd->mfd_object)), mfd->mfd_open_flags,
304                open_flags);
305
306         mfd->mfd_open_flags = open_flags;
307 }
308
309 /**
310  * prep ma_lmm/ma_lmv for md_attr from reply
311  */
312 static void mdt_prep_ma_buf_from_rep(struct mdt_thread_info *info,
313                                      struct mdt_object *obj,
314                                      struct md_attr *ma, __u64 open_flags)
315 {
316         struct req_capsule *pill;
317
318         if (ma->ma_lmv || ma->ma_lmm) {
319                 CDEBUG(D_INFO, DFID " %s already set.\n",
320                        PFID(mdt_object_fid(obj)),
321                        ma->ma_lmv ? (ma->ma_lmm ? "ma_lmv and ma_lmm"
322                                                 : "ma_lmv")
323                                   : "ma_lmm");
324                 return;
325         }
326
327         pill = info->mti_pill;
328         if (S_ISDIR(obj->mot_header.loh_attr)) {
329                 ma->ma_lmv = req_capsule_server_get(pill, &RMF_MDT_MD);
330                 ma->ma_lmv_size = req_capsule_get_size(pill, &RMF_MDT_MD,
331                                                        RCL_SERVER);
332                 if (ma->ma_lmv_size > 0)
333                         ma->ma_need |= MA_LMV;
334
335                 if (open_flags & MDS_OPEN_DEFAULT_LMV) {
336                         ma->ma_default_lmv = req_capsule_server_get(pill,
337                                                         &RMF_DEFAULT_MDT_MD);
338                         ma->ma_default_lmv_size = req_capsule_get_size(pill,
339                                                         &RMF_DEFAULT_MDT_MD,
340                                                         RCL_SERVER);
341                         if (ma->ma_default_lmv_size > 0)
342                                 ma->ma_need |= MA_LMV_DEF;
343                 }
344         } else {
345                 ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
346                 ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
347                                                        RCL_SERVER);
348                 if (ma->ma_lmm_size > 0)
349                         ma->ma_need |= MA_LOV;
350         }
351 }
352
353 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
354                         struct mdt_object *o, u64 open_flags, int created,
355                         struct ldlm_reply *rep)
356 {
357         struct ptlrpc_request *req = mdt_info_req(info);
358         struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
359         struct mdt_file_data *mfd;
360         struct md_attr *ma  = &info->mti_attr;
361         struct lu_attr *la  = &ma->ma_attr;
362         struct mdt_body *repbody;
363         bool isdir, isreg;
364         int rc = 0;
365
366         ENTRY;
367         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
368
369         isreg = S_ISREG(la->la_mode);
370         isdir = S_ISDIR(la->la_mode);
371         if (isreg && !(ma->ma_valid & MA_LOV) &&
372             !(open_flags & MDS_OPEN_RELEASE)) {
373                 /*
374                  * No EA, check whether it is will set regEA and dirEA since in
375                  * above attr get, these size might be zero, so reset it, to
376                  * retrieve the MD after create obj.
377                  */
378                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
379                                                        &RMF_MDT_MD,
380                                                        RCL_SERVER);
381                 /* in replay case, p == NULL */
382                 rc = mdt_create_data(info, p, o);
383                 if (rc)
384                         RETURN(rc);
385
386                 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_DISP_STRIPE)
387                         mdt_set_disposition(info, rep, DISP_OPEN_STRIPE);
388         }
389
390         CDEBUG(D_INODE, "after open, ma_valid bit = %#llx lmm_size = %d\n",
391                ma->ma_valid, ma->ma_lmm_size);
392
393         if (ma->ma_valid & MA_LOV) {
394                 LASSERT(ma->ma_lmm_size != 0);
395                 repbody->mbo_eadatasize = ma->ma_lmm_size;
396                 if (isdir)
397                         repbody->mbo_valid |= OBD_MD_FLDIREA;
398                 else
399                         repbody->mbo_valid |= OBD_MD_FLEASIZE;
400         }
401
402         if (ma->ma_valid & MA_LMV) {
403                 LASSERT(ma->ma_lmv_size != 0);
404                 repbody->mbo_eadatasize = ma->ma_lmv_size;
405                 LASSERT(isdir);
406                 repbody->mbo_valid |= OBD_MD_FLDIREA | OBD_MD_MEA;
407         }
408
409         if (ma->ma_valid & MA_LMV_DEF) {
410                 LASSERT(ma->ma_default_lmv_size != 0);
411                 LASSERT(isdir);
412                 repbody->mbo_valid |= OBD_MD_FLDIREA | OBD_MD_DEFAULT_MEA;
413         }
414
415         if (open_flags & MDS_FMODE_WRITE)
416                 rc = mdt_write_get(o);
417         else if (open_flags & MDS_FMODE_EXEC)
418                 rc = mdt_write_deny(o);
419
420         if (rc)
421                 RETURN(rc);
422
423         rc = mo_open(info->mti_env, mdt_object_child(o),
424                      created ? open_flags | MDS_OPEN_CREATED : open_flags,
425                      &info->mti_spec);
426         if (rc != 0) {
427                 /* If we allow the client to chgrp (CFS_SETGRP_PERM), but the
428                  * client does not know which suppgid should be sent to the MDS,
429                  * or some other(s) changed the target file's GID after this RPC
430                  * sent to the MDS with the suppgid as the original GID, then we
431                  * give the client another chance to send the right suppgid. */
432                 if (rc == -EACCES &&
433                     allow_client_chgrp(info, lu_ucred(info->mti_env)))
434                         mdt_set_disposition(info, rep, DISP_OPEN_DENY);
435
436                 GOTO(err_out, rc);
437         }
438
439         mfd = mdt_mfd_new(med);
440         if (mfd == NULL)
441                 GOTO(err_out, rc = -ENOMEM);
442
443         /*
444          * Keep a reference on this object for this open, and is
445          * released by mdt_mfd_close().
446          */
447         mdt_object_get(info->mti_env, o);
448         mfd->mfd_object = o;
449         mfd->mfd_xid = req->rq_xid;
450
451         /*
452          * @open_flags is always not zero. At least it should be FMODE_READ,
453          * FMODE_WRITE or MDS_FMODE_EXEC.
454          */
455         LASSERT(open_flags != 0);
456
457         /* Open handling. */
458         mdt_mfd_set_mode(mfd, open_flags);
459
460         atomic_inc(&o->mot_open_count);
461         if (open_flags & MDS_OPEN_LEASE)
462                 atomic_inc(&o->mot_lease_count);
463
464         /* replay handle */
465         if (req_is_replay(req)) {
466                 struct mdt_file_data *old_mfd;
467                 /* Check wheather old cookie already exist in
468                  * the list, becasue when do recovery, client
469                  * might be disconnected from server, and
470                  * restart replay, so there maybe some orphan
471                  * mfd here, we should remove them */
472                 LASSERT(info->mti_rr.rr_open_handle != NULL);
473                 spin_lock(&med->med_open_lock);
474                 old_mfd = mdt_open_handle2mfd(med, info->mti_rr.rr_open_handle,
475                                               true);
476                 if (old_mfd != NULL) {
477                         CDEBUG(D_HA, "delete orphan mfd = %p, fid = "DFID", "
478                                "cookie = %#llx\n", mfd,
479                                PFID(mdt_object_fid(mfd->mfd_object)),
480                                info->mti_rr.rr_open_handle->cookie);
481                         class_handle_unhash(&old_mfd->mfd_open_handle);
482                         list_del_init(&old_mfd->mfd_list);
483                         spin_unlock(&med->med_open_lock);
484                         /* no attr update for that close */
485                         la->la_valid = 0;
486                         ma->ma_valid |= MA_FLAGS;
487                         ma->ma_attr_flags |= MDS_RECOV_OPEN;
488                         mdt_mfd_close(info, old_mfd);
489                         ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
490                         ma->ma_valid &= ~MA_FLAGS;
491                 } else {
492                         spin_unlock(&med->med_open_lock);
493                         CDEBUG(D_HA, "orphan mfd not found, fid = "DFID", "
494                                "cookie = %#llx\n",
495                                PFID(mdt_object_fid(mfd->mfd_object)),
496                                info->mti_rr.rr_open_handle->cookie);
497                 }
498
499                 CDEBUG(D_HA, "Store old cookie %#llx in new mfd\n",
500                        info->mti_rr.rr_open_handle->cookie);
501
502                 mfd->mfd_open_handle_old = *info->mti_rr.rr_open_handle;
503         }
504
505         repbody->mbo_open_handle.cookie = mfd->mfd_open_handle.h_cookie;
506
507         if (req->rq_export->exp_disconnected) {
508                 spin_lock(&med->med_open_lock);
509                 class_handle_unhash(&mfd->mfd_open_handle);
510                 list_del_init(&mfd->mfd_list);
511                 spin_unlock(&med->med_open_lock);
512                 mdt_mfd_close(info, mfd);
513         } else {
514                 spin_lock(&med->med_open_lock);
515                 list_add_tail(&mfd->mfd_list, &med->med_open_head);
516                 spin_unlock(&med->med_open_lock);
517         }
518
519         mdt_empty_transno(info, rc);
520
521         RETURN(rc);
522
523 err_out:
524         if (open_flags & MDS_FMODE_WRITE)
525                 mdt_write_put(o);
526         else if (open_flags & MDS_FMODE_EXEC)
527                 mdt_write_allow(o);
528
529         return rc;
530 }
531
532 static int mdt_finish_open(struct mdt_thread_info *info,
533                            struct mdt_object *p, struct mdt_object *o,
534                            u64 open_flags,
535                            struct ldlm_reply *rep)
536 {
537         struct ptlrpc_request *req = mdt_info_req(info);
538         struct obd_export *exp = req->rq_export;
539         struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
540         struct md_attr *ma  = &info->mti_attr;
541         struct lu_attr *la  = &ma->ma_attr;
542         struct mdt_file_data *mfd;
543         struct mdt_body *repbody;
544         int created;
545         int rc = 0;
546         int isreg, isdir, islnk;
547         struct list_head *t;
548
549         ENTRY;
550         LASSERT(ma->ma_valid & MA_INODE);
551         created = mdt_get_disposition(rep, DISP_OPEN_CREATE);
552
553         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
554
555         isreg = S_ISREG(la->la_mode);
556         isdir = S_ISDIR(la->la_mode);
557         islnk = S_ISLNK(la->la_mode);
558         mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
559
560         /* compatibility check for 2.10 clients when it tries to open mirrored
561          * files. 2.10 clients don't verify overlapping components so they
562          * would read and write mirrored files just as if they were normal
563          * PFL files, which will cause the problem that sycned mirrors actually
564          * contain different data.
565          * Older clients are not a concern here because they don't even
566          * understand PFL layout. */
567         if (isreg && !exp_connect_flr(exp) && ma->ma_valid & MA_LOV &&
568             mdt_lmm_is_flr(ma->ma_lmm)) {
569                 /* LU-10286: for simplicity clients who don't understand
570                  * mirrored layout(with connect flag OBD_CONNECT2_FLR) won't
571                  * be able to open mirrored files */
572                 RETURN(-EOPNOTSUPP);
573         }
574
575         /* Overstriped files can crash older clients */
576         if (isreg && !exp_connect_overstriping(exp) &&
577             mdt_lmm_is_overstriping(ma->ma_lmm))
578                 RETURN(-EOPNOTSUPP);
579
580         /* LU-2275, simulate broken behaviour (esp. prevalent in
581          * pre-2.4 servers where a very strange reply is sent on error
582          * that looks like it was actually almost successful and a
583          * failure at the same time.) */
584         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_NEGATIVE_POSITIVE)) {
585                 mdt_set_disposition(info, rep, DISP_OPEN_OPEN |
586                                                DISP_LOOKUP_NEG |
587                                                DISP_LOOKUP_POS);
588
589                 if (open_flags & MDS_OPEN_LOCK)
590                         mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
591
592                 RETURN(-ENOENT);
593         }
594
595 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
596         if (exp_connect_flags(exp) & OBD_CONNECT_ACL) {
597                 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
598                 if (IS_ERR(nodemap))
599                         RETURN(PTR_ERR(nodemap));
600
601                 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
602                 nodemap_putref(nodemap);
603                 if (rc)
604                         RETURN(rc);
605         }
606 #endif
607
608         /*
609          * If we are following a symlink, don't open; and do not return open
610          * handle for special nodes as client required.
611          */
612         if (islnk || (!isreg && !isdir &&
613             (exp_connect_flags(req->rq_export) & OBD_CONNECT_NODEVOH))) {
614                 lustre_msg_set_transno(req->rq_repmsg, 0);
615                 RETURN(0);
616         }
617
618         /*
619          * We need to return the existing object's fid back, so it is done here,
620          * after preparing the reply.
621          */
622         if (!created && (open_flags & MDS_OPEN_EXCL) &&
623             (open_flags & MDS_OPEN_CREAT))
624                 RETURN(-EEXIST);
625
626         /* This can't be done earlier, we need to return reply body */
627         if (isdir) {
628                 if (open_flags & (MDS_OPEN_CREAT | MDS_FMODE_WRITE)) {
629                         /* We are trying to create or write an existing dir. */
630                         RETURN(-EISDIR);
631                 }
632         } else if (open_flags & MDS_OPEN_DIRECTORY)
633                 RETURN(-ENOTDIR);
634
635         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
636                                  OBD_FAIL_MDS_LDLM_REPLY_NET | OBD_FAIL_ONCE))
637                 RETURN(-EAGAIN);
638
639         mfd = NULL;
640         if (info->mti_rr.rr_flags & MRF_OPEN_RESEND) {
641                 spin_lock(&med->med_open_lock);
642                 list_for_each(t, &med->med_open_head) {
643                         mfd = list_entry(t, struct mdt_file_data, mfd_list);
644                         if (mfd->mfd_xid == req->rq_xid) {
645                                 repbody->mbo_open_handle.cookie =
646                                                 mfd->mfd_open_handle.h_cookie;
647                                 break;
648                         }
649                         mfd = NULL;
650                 }
651                 spin_unlock(&med->med_open_lock);
652
653                 if (mfd != NULL) {
654                         /* set repbody->ea_size for resent case */
655                         if (ma->ma_valid & MA_LOV) {
656                                 LASSERT(ma->ma_lmm_size != 0);
657                                 repbody->mbo_eadatasize = ma->ma_lmm_size;
658                                 if (isdir)
659                                         repbody->mbo_valid |= OBD_MD_FLDIREA;
660                                 else
661                                         repbody->mbo_valid |= OBD_MD_FLEASIZE;
662                         }
663                         mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
664                         RETURN(0);
665                 }
666                 /* if we have a real resend (not a resend afrer failover), it
667                  * means close is already happend, so lets return error
668                  */
669                 RETURN(-ESTALE);
670         }
671
672         rc = mdt_mfd_open(info, p, o, open_flags, created, rep);
673         if (!rc)
674                 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
675
676         RETURN(rc);
677 }
678
679 void mdt_reconstruct_open(struct mdt_thread_info *info,
680                           struct mdt_lock_handle *lhc)
681 {
682         struct req_capsule *pill = info->mti_pill;
683         struct ptlrpc_request *req = mdt_info_req(info);
684         struct mdt_reint_record *rr = &info->mti_rr;
685         struct md_attr *ma = &info->mti_attr;
686         struct ldlm_reply *ldlm_rep;
687         u64 opdata;
688         int rc;
689
690         ENTRY;
691         LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
692         ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
693
694         ma->ma_need = MA_INODE | MA_HSM;
695         ma->ma_valid = 0;
696         opdata = mdt_req_from_lrd(req, info->mti_reply_data);
697         mdt_set_disposition(info, ldlm_rep, opdata);
698
699         CDEBUG(D_INODE, "This is reconstruct open: disp=%#llx, result=%d\n",
700                ldlm_rep->lock_policy_res1, req->rq_status);
701         if (req->rq_status)
702                 /* We did not create successfully, return error to client. */
703                 GOTO(out, rc = req->rq_status);
704
705         /* tg_reply_data is just memory only  structure, so any non zero fid
706          * means a real resend not a resend after recovery which need to be
707          * handled as regular open
708          */
709         if (likely(!fid_is_zero(&info->mti_reply_data->trd_object))) {
710                 rr->rr_fid2 = &info->mti_reply_data->trd_object;
711                 rr->rr_flags |= MRF_OPEN_RESEND;
712                 rc = mdt_open_by_fid(info, ldlm_rep, lhc);
713                 if (rc)
714                         lustre_msg_set_transno(req->rq_repmsg, 0);
715         } else {
716                 /* We did not try to create, so we are a pure open */
717                 rc = mdt_reint_open(info, lhc);
718         }
719         EXIT;
720 out:
721         req->rq_status = rc;
722         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
723         LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
724 }
725
726 static int mdt_open_by_fid(struct mdt_thread_info *info, struct ldlm_reply *rep,
727                            struct mdt_lock_handle *lhc)
728 {
729         u64 open_flags = info->mti_spec.sp_cr_flags;
730         struct mdt_reint_record *rr = &info->mti_rr;
731         struct md_attr *ma = &info->mti_attr;
732         struct mdt_object *o;
733         int rc;
734
735         ENTRY;
736         o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
737         if (IS_ERR(o))
738                 RETURN(rc = PTR_ERR(o));
739
740         rc = mdt_check_enc(info, o);
741         if (rc)
742                 GOTO(out, rc);
743
744         if (unlikely(mdt_object_remote(o))) {
745                 /* the child object was created on remote server */
746                 struct mdt_body *repbody;
747
748                 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
749                                                 DISP_LOOKUP_EXECD |
750                                                 DISP_LOOKUP_POS));
751                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
752                 repbody->mbo_fid1 = *rr->rr_fid2;
753                 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
754                 rc = 0;
755         } else {
756                 if (mdt_object_exists(o)) {
757                         tgt_open_obj_set(info->mti_env, mdt_obj2dt(o));
758                         mdt_set_disposition(info, rep, (DISP_IT_EXECD |
759                                                         DISP_LOOKUP_EXECD |
760                                                         DISP_LOOKUP_POS));
761                         if ((open_flags & MDS_OPEN_EXCL) &&
762                             (open_flags & MDS_OPEN_CREAT))
763                                 mdt_set_disposition(info, rep,
764                                                     DISP_OPEN_CREATE);
765
766                         mdt_prep_ma_buf_from_rep(info, o, ma, open_flags);
767                         rc = mdt_attr_get_complex(info, o, ma);
768                         if (rc)
769                                 GOTO(out, rc);
770                         rc = mdt_finish_open(info, NULL, o, open_flags, rep);
771                         if (rc)
772                                 GOTO(out, rc);
773                         mdt_pack_size2body(info, rr->rr_fid2, &lhc->mlh_reg_lh);
774                 } else {
775                         rc = -ENOENT;
776                 }
777         }
778
779 out:
780         mdt_object_put(info->mti_env, o);
781         RETURN(rc);
782 }
783
784 /* lock object for open */
785 static int mdt_object_open_lock(struct mdt_thread_info *info,
786                                 struct mdt_object *obj,
787                                 struct mdt_lock_handle *lhc,
788                                 __u64 *ibits)
789 {
790         struct md_attr *ma = &info->mti_attr;
791         __u64 open_flags = info->mti_spec.sp_cr_flags;
792         __u64 trybits = 0;
793         enum ldlm_mode lm = LCK_CR;
794         bool acq_lease = !!(open_flags & MDS_OPEN_LEASE);
795         bool try_layout = false;
796         bool create_layout = false;
797         int rc = 0;
798         __u32 dom_stripe = 0;
799         unsigned int dom_only = 0;
800         unsigned int dom_lock = 0;
801
802         ENTRY;
803         *ibits = 0;
804         if (req_is_replay(mdt_info_req(info)))
805                 RETURN(0);
806
807         if (S_ISREG(lu_object_attr(&obj->mot_obj))) {
808                 if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV) &&
809                     md_should_create(open_flags))
810                         create_layout = true;
811                 if (exp_connect_layout(info->mti_exp) && !create_layout &&
812                     ma->ma_need & MA_LOV)
813                         try_layout = true;
814
815                 /* DoM files can take IO lock at OPEN when it makes sense,
816                  * check if file has DoM stripe and ask for lock if client
817                  * no lock on that resource yet.
818                  */
819                 if (ma->ma_valid & MA_LOV && ma->ma_lmm != NULL)
820                         dom_stripe = mdt_lmm_dom_entry_check(ma->ma_lmm,
821                                                              &dom_only);
822                 /* If only DOM stripe is being used then we can expect IO
823                  * to it after OPEN and will return corresponding DOM ibit
824                  * using default strategy from mdt_opts.mo_dom_lock.
825                  * Otherwise trylock mode is used always and DOM ibit will
826                  * be returned optionally.
827                  */
828                 if (dom_stripe &&
829                     !mdt_dom_client_has_lock(info, mdt_object_fid(obj)))
830                         dom_lock = !dom_only ? TRYLOCK_DOM_ON_OPEN :
831                                    info->mti_mdt->mdt_opts.mo_dom_lock;
832         }
833
834         if (acq_lease) {
835                 /* lease open, acquire write mode of open sem */
836                 down_write(&obj->mot_open_sem);
837
838                 /* Lease exists and ask for new lease */
839                 if (atomic_read(&obj->mot_lease_count) > 0) {
840                         /* only exclusive open is supported, so lease
841                          * are conflicted to each other */
842                         GOTO(out, rc = -EBUSY);
843                 }
844
845                 /* Lease must be with open lock */
846                 if (!(open_flags & MDS_OPEN_LOCK)) {
847                         CERROR("%s: Request lease for file:"DFID ", but open lock "
848                                "is missed, open_flags = %#llo : rc = %d\n",
849                                mdt_obd_name(info->mti_mdt),
850                                PFID(mdt_object_fid(obj)), open_flags, -EPROTO);
851                         GOTO(out, rc = -EPROTO);
852                 }
853
854                 /* should conflict with new opens for write/execute */
855                 lm = LCK_PW;
856                 *ibits = MDS_INODELOCK_OPEN;
857
858                 /* never grant LCK_EX layout lock to client */
859                 try_layout = false;
860         } else { /* normal open */
861                 /* normal open holds read mode of open sem */
862                 down_read(&obj->mot_open_sem);
863
864                 if (open_flags & MDS_OPEN_LOCK) {
865                         if (open_flags & MDS_FMODE_WRITE)
866                                 lm = LCK_CW;
867                         else if (open_flags & MDS_FMODE_EXEC)
868                                 lm = LCK_PR;
869                         else
870                                 lm = LCK_CR;
871
872                         *ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN;
873                 } else if (atomic_read(&obj->mot_lease_count) > 0) {
874                         if (open_flags & MDS_FMODE_WRITE)
875                                 lm = LCK_CW;
876                         else
877                                 lm = LCK_CR;
878
879                         /* revoke lease */
880                         *ibits = MDS_INODELOCK_OPEN;
881                         try_layout = false;
882
883                         lhc = &info->mti_lh[MDT_LH_LOCAL];
884                 } else if (dom_lock) {
885                         lm = (open_flags & MDS_FMODE_WRITE) ? LCK_PW : LCK_PR;
886                         trybits |= MDS_INODELOCK_DOM | MDS_INODELOCK_LAYOUT;
887                 }
888
889                 CDEBUG(D_INODE, "normal open:"DFID" lease count: %d, lm: %d\n",
890                         PFID(mdt_object_fid(obj)),
891                         atomic_read(&obj->mot_lease_count), lm);
892         }
893
894         /* Return lookup lock to validate inode at the client side.
895          * This is pretty important otherwise MDT will return layout
896          * lock for each open.
897          * However this is a double-edged sword because changing
898          * permission will revoke a huge number of LOOKUP locks.
899          */
900         if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
901                 if (!(*ibits & MDS_INODELOCK_LOOKUP))
902                         trybits |= MDS_INODELOCK_LOOKUP;
903                 trybits |= MDS_INODELOCK_LAYOUT;
904         }
905
906         if (*ibits | trybits)
907                 rc = mdt_object_lock_try(info, obj, lhc, ibits, trybits, lm,
908                                          false);
909
910         CDEBUG(D_INODE, "%s: Requested bits lock:"DFID ", ibits = %#llx/%#llx"
911                ", open_flags = %#llo, try_layout = %d : rc = %d\n",
912                mdt_obd_name(info->mti_mdt), PFID(mdt_object_fid(obj)),
913                *ibits, trybits, open_flags, try_layout, rc);
914
915         /* will change layout, revoke layout locks by enqueuing EX lock. */
916         if (rc == 0 && create_layout) {
917                 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
918
919                 CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
920                         ", open_flags = %#llo\n",
921                         PFID(mdt_object_fid(obj)), open_flags);
922
923                 /* We cannot enqueue another lock for the same resource we
924                  * already have a lock for, due to mechanics of waiting list
925                  * iterating in ldlm, see LU-3601.
926                  * As such we'll drop the open lock we just got above here,
927                  * it's ok not to have this open lock as it's main purpose is to
928                  * flush unused cached client open handles. */
929                 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
930                         mdt_object_unlock(info, obj, lhc, 1);
931
932                 LASSERT(!try_layout);
933                 rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT,
934                                      LCK_EX, false);
935
936                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
937         }
938
939         /* Check if there is any other open handles after acquiring
940          * open lock. At this point, caching open handles have been revoked
941          * by open lock.
942          * XXX: Now only exclusive open is supported. Need to check the
943          * type of open for generic lease support. */
944         if (rc == 0 && acq_lease) {
945                 struct ptlrpc_request *req = mdt_info_req(info);
946                 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
947                 struct mdt_file_data *mfd;
948                 bool is_replay_or_resent;
949                 int open_count = 0;
950
951                 /* For lease: application can open a file and then apply lease,
952                  * @handle contains original open handle in that case.
953                  * In recovery, open REQ will be replayed and the lease REQ may
954                  * be resent that means the open handle is already stale, so we
955                  * need to fix it up here by finding new handle. */
956                 is_replay_or_resent = req_is_replay(req) ||
957                         lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT;
958
959                 /* if the request is _not_ a replay request, rr_open_handle
960                  * may be used to hold an open file handle which is issuing the
961                  * lease request, so that this openhandle doesn't count. */
962                 mfd = mdt_open_handle2mfd(med, info->mti_rr.rr_open_handle,
963                                           is_replay_or_resent);
964                 if (mfd != NULL)
965                         ++open_count;
966
967                 CDEBUG(D_INODE, "acq_lease "DFID": openers: %d, want: %d\n",
968                         PFID(mdt_object_fid(obj)),
969                         atomic_read(&obj->mot_open_count), open_count);
970
971                 if (atomic_read(&obj->mot_open_count) > open_count) {
972                         /* fail if anyone *else* has opened file for write */
973                         if (mdt_write_read(obj) > 1)
974                                 GOTO(out, rc = -EBUSY);
975                 }
976         }
977         GOTO(out, rc);
978
979 out:
980         RETURN(rc);
981 }
982
983 static void mdt_object_open_unlock(struct mdt_thread_info *info,
984                                    struct mdt_object *obj,
985                                    struct mdt_lock_handle *lhc,
986                                    __u64 ibits, int rc)
987 {
988         __u64 open_flags = info->mti_spec.sp_cr_flags;
989         struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LOCAL];
990
991         ENTRY;
992         if (req_is_replay(mdt_info_req(info)))
993                 RETURN_EXIT;
994
995         /* Release local lock - the lock put in MDT_LH_LOCAL will never
996          * return to client side. */
997         if (lustre_handle_is_used(&ll->mlh_reg_lh))
998                 mdt_object_unlock(info, obj, ll, 1);
999
1000         ll = &info->mti_lh[MDT_LH_LAYOUT];
1001         /* Release local layout lock, layout was created */
1002         if (lustre_handle_is_used(&ll->mlh_reg_lh)) {
1003                 LASSERT(!(ibits & MDS_INODELOCK_LAYOUT));
1004                 mdt_object_unlock(info, obj, ll, 1);
1005         }
1006
1007         if (open_flags & MDS_OPEN_LEASE)
1008                 up_write(&obj->mot_open_sem);
1009         else
1010                 up_read(&obj->mot_open_sem);
1011
1012         /* Cross-ref case, the lock should be returned to the client */
1013         if (ibits == 0 || rc == -MDT_EREMOTE_OPEN)
1014                 RETURN_EXIT;
1015
1016         if (!(open_flags & MDS_OPEN_LOCK) && !(ibits & MDS_INODELOCK_LAYOUT) &&
1017             !(ibits & MDS_INODELOCK_DOM)) {
1018                 /* for the open request, the lock will only return to client
1019                  * if open or layout lock is granted. */
1020                 rc = 1;
1021         }
1022
1023         if (rc != 0 || !lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1024                 struct ldlm_reply       *ldlm_rep;
1025
1026                 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1027                 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1028                 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1029                         mdt_object_unlock(info, obj, lhc, 1);
1030         }
1031         RETURN_EXIT;
1032 }
1033
1034 /**
1035  * Check release is permitted for the current HSM flags.
1036  */
1037 static bool mdt_hsm_release_allow(const struct md_attr *ma)
1038 {
1039         if (!(ma->ma_valid & MA_HSM))
1040                 return false;
1041
1042         if (ma->ma_hsm.mh_flags & (HS_DIRTY|HS_NORELEASE|HS_LOST))
1043                 return false;
1044
1045         if (!(ma->ma_hsm.mh_flags & HS_ARCHIVED))
1046                 return false;
1047
1048         return true;
1049 }
1050
1051 static int mdt_open_by_fid_lock(struct mdt_thread_info *info,
1052                                 struct ldlm_reply *rep,
1053                                 struct mdt_lock_handle *lhc)
1054 {
1055         const struct lu_env *env = info->mti_env;
1056         struct mdt_device *mdt = info->mti_mdt;
1057         u64 open_flags = info->mti_spec.sp_cr_flags;
1058         struct mdt_reint_record *rr = &info->mti_rr;
1059         struct md_attr *ma = &info->mti_attr;
1060         struct mdt_object *parent = NULL;
1061         struct mdt_object *o;
1062         bool object_locked = false;
1063         u64 ibits = 0;
1064         int rc;
1065
1066         ENTRY;
1067         if (md_should_create(open_flags)) {
1068                 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1069                         parent = mdt_object_find(env, mdt, rr->rr_fid1);
1070                         if (IS_ERR(parent)) {
1071                                 CDEBUG(D_INODE, "Fail to find parent "DFID
1072                                        " for anonymous created %ld, try to"
1073                                        " use server-side parent.\n",
1074                                        PFID(rr->rr_fid1), PTR_ERR(parent));
1075                                 parent = NULL;
1076                         }
1077                 }
1078                 if (parent == NULL)
1079                         ma->ma_need |= MA_PFID;
1080         }
1081
1082         o = mdt_object_find(env, mdt, rr->rr_fid2);
1083         if (IS_ERR(o))
1084                 GOTO(out_parent_put, rc = PTR_ERR(o));
1085
1086         if (mdt_object_remote(o)) {
1087                 CDEBUG(D_INFO, "%s: "DFID" is on remote MDT.\n",
1088                        mdt_obd_name(info->mti_mdt),
1089                        PFID(rr->rr_fid2));
1090                 GOTO(out, rc = -EREMOTE);
1091         } else if (!mdt_object_exists(o)) {
1092                 mdt_set_disposition(info, rep,
1093                                     DISP_IT_EXECD |
1094                                     DISP_LOOKUP_EXECD |
1095                                     DISP_LOOKUP_NEG);
1096                 GOTO(out, rc = -ENOENT);
1097         }
1098
1099         /* do not check enc for directory: always allow open */
1100         if (!S_ISDIR(lu_object_attr(&o->mot_obj))) {
1101                 rc = mdt_check_enc(info, o);
1102                 if (rc)
1103                         GOTO(out, rc);
1104         }
1105
1106         mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1107
1108         mdt_prep_ma_buf_from_rep(info, o, ma, open_flags);
1109         if (open_flags & MDS_OPEN_RELEASE)
1110                 ma->ma_need |= MA_HSM;
1111         rc = mdt_attr_get_complex(info, o, ma);
1112         if (rc)
1113                 GOTO(out, rc);
1114
1115         /* We should not change file's existing LOV EA */
1116         if (S_ISREG(lu_object_attr(&o->mot_obj)) &&
1117             open_flags & MDS_OPEN_HAS_EA && ma->ma_valid & MA_LOV)
1118                 GOTO(out, rc = -EEXIST);
1119
1120         /* If a release request, check file open flags are fine and ask for an
1121          * exclusive open access. */
1122         if (open_flags & MDS_OPEN_RELEASE && !mdt_hsm_release_allow(ma))
1123                 GOTO(out, rc = -EPERM);
1124
1125         rc = mdt_check_resent_lock(info, o, lhc);
1126         if (rc < 0) {
1127                 GOTO(out, rc);
1128         } else if (rc > 0) {
1129                 rc = mdt_object_open_lock(info, o, lhc, &ibits);
1130                 object_locked = true;
1131                 if (rc)
1132                         GOTO(out_unlock, rc);
1133         }
1134
1135         if (ma->ma_valid & MA_PFID) {
1136                 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1137                 if (IS_ERR(parent)) {
1138                         CDEBUG(D_INODE, "Fail to find parent "DFID
1139                                " for anonymous created %ld, try to"
1140                                " use system default.\n",
1141                                PFID(&ma->ma_pfid), PTR_ERR(parent));
1142                         parent = NULL;
1143                 }
1144         }
1145
1146         tgt_open_obj_set(info->mti_env, mdt_obj2dt(o));
1147         rc = mdt_finish_open(info, parent, o, open_flags, rep);
1148         if (!rc) {
1149                 mdt_set_disposition(info, rep, DISP_LOOKUP_POS);
1150                 if (open_flags & MDS_OPEN_LOCK)
1151                         mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1152                 if (open_flags & MDS_OPEN_LEASE)
1153                         mdt_set_disposition(info, rep, DISP_OPEN_LEASE);
1154         }
1155         GOTO(out_unlock, rc);
1156
1157 out_unlock:
1158         if (object_locked)
1159                 mdt_object_open_unlock(info, o, lhc, ibits, rc);
1160 out:
1161         mdt_object_put(env, o);
1162         if (rc == 0)
1163                 mdt_pack_size2body(info, rr->rr_fid2, &lhc->mlh_reg_lh);
1164 out_parent_put:
1165         if (parent != NULL)
1166                 mdt_object_put(env, parent);
1167         return rc;
1168 }
1169
1170 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1171 static int mdt_cross_open(struct mdt_thread_info *info,
1172                           const struct lu_fid *parent_fid,
1173                           const struct lu_fid *fid,
1174                           struct ldlm_reply *rep, u64 open_flags)
1175 {
1176         struct md_attr *ma = &info->mti_attr;
1177         struct mdt_object *o;
1178         int rc;
1179
1180         ENTRY;
1181         o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1182         if (IS_ERR(o))
1183                 RETURN(rc = PTR_ERR(o));
1184
1185         rc = mdt_check_enc(info, o);
1186         if (rc)
1187                 GOTO(out, rc);
1188
1189         if (mdt_object_remote(o)) {
1190                 /* Something is wrong here, the object is on another MDS! */
1191                 CERROR("%s: "DFID" isn't on this server!: rc = %d\n",
1192                        mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1193                 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1194                                 &o->mot_obj,
1195                                 "Object isn't on this server! FLD error?");
1196                 rc = -EFAULT;
1197         } else {
1198                 if (mdt_object_exists(o)) {
1199                         int mask;
1200
1201                         /* Do permission check for cross-open after converting
1202                          * MDS_OPEN_* flags to MAY_* permission mask.
1203                          */
1204                         mask = mds_accmode(open_flags);
1205
1206                         rc = mo_permission(info->mti_env, NULL,
1207                                            mdt_object_child(o), NULL, mask);
1208                         if (rc)
1209                                 goto out;
1210
1211                         mdt_prep_ma_buf_from_rep(info, o, ma, open_flags);
1212                         rc = mdt_attr_get_complex(info, o, ma);
1213                         if (rc != 0)
1214                                 GOTO(out, rc);
1215
1216                         rc = mdt_pack_secctx_in_reply(info, o);
1217                         if (unlikely(rc))
1218                                 GOTO(out, rc);
1219
1220                         rc = mdt_pack_encctx_in_reply(info, o);
1221                         if (unlikely(rc))
1222                                 GOTO(out, rc);
1223
1224                         rc = mdt_finish_open(info, NULL, o, open_flags, rep);
1225                 } else {
1226                         /*
1227                          * Something is wrong here. lookup was positive but
1228                          * there is no object!
1229                          */
1230                         CERROR("%s: "DFID" doesn't exist!: rc = %d\n",
1231                               mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1232                         rc = -EFAULT;
1233                 }
1234         }
1235 out:
1236         mdt_object_put(info->mti_env, o);
1237
1238         RETURN(rc);
1239 }
1240
1241 /*
1242  * find root object and take its xattr lock if it's on remote MDT, later create
1243  * may use fs default striping (which is stored in root xattr).
1244  */
1245 static int mdt_lock_root_xattr(struct mdt_thread_info *info,
1246                                struct mdt_device *mdt)
1247 {
1248         struct mdt_object *md_root = mdt->mdt_md_root;
1249         struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LOCAL];
1250         __u64 ibits = MDS_INODELOCK_XATTR;
1251         int rc;
1252
1253         if (md_root == NULL) {
1254                 lu_root_fid(&info->mti_tmp_fid1);
1255                 md_root = mdt_object_find(info->mti_env, mdt,
1256                                           &info->mti_tmp_fid1);
1257                 if (IS_ERR(md_root))
1258                         return PTR_ERR(md_root);
1259
1260                 spin_lock(&mdt->mdt_lock);
1261                 if (mdt->mdt_md_root != NULL) {
1262                         spin_unlock(&mdt->mdt_lock);
1263
1264                         LASSERTF(mdt->mdt_md_root == md_root,
1265                                  "Different root object ("
1266                                  DFID") instances, %p, %p\n",
1267                                  PFID(&info->mti_tmp_fid1),
1268                                  mdt->mdt_md_root, md_root);
1269                         LASSERT(atomic_read(
1270                                 &md_root->mot_obj.lo_header->loh_ref) > 1);
1271
1272                         mdt_object_put(info->mti_env, md_root);
1273                 } else {
1274                         mdt->mdt_md_root = md_root;
1275                         spin_unlock(&mdt->mdt_lock);
1276                 }
1277         }
1278
1279         if (md_root->mot_cache_attr || !mdt_object_remote(md_root))
1280                 return 0;
1281
1282         mdt_lock_reg_init(lh, LCK_PR);
1283         rc = mdt_object_lock_internal(info, md_root, mdt_object_fid(md_root),
1284                                       lh, &ibits, 0, true, false);
1285         if (rc < 0)
1286                 return rc;
1287
1288         md_root->mot_cache_attr = 1;
1289
1290         /* don't cancel this lock, so that we know the cached xattr is valid. */
1291         ldlm_lock_decref(&lh->mlh_rreg_lh, LCK_PR);
1292         lh->mlh_rreg_lh.cookie = 0ull;
1293
1294         return 0;
1295 }
1296
1297 static inline enum ldlm_mode mdt_open_lock_mode(struct mdt_thread_info *info,
1298                                                 struct mdt_object *p,
1299                                                 struct lu_name *name,
1300                                                 u64 open_flags)
1301 {
1302         int result;
1303         struct lu_fid fid;
1304
1305         /* We don't need to take the DLM lock for a volatile */
1306         if (open_flags & MDS_OPEN_VOLATILE)
1307                 return LCK_NL;
1308
1309         if (!(open_flags & MDS_OPEN_CREAT))
1310                 return LCK_PR;
1311
1312         result = mdo_lookup(info->mti_env, mdt_object_child(p), name, &fid,
1313                             &info->mti_spec);
1314
1315         /* If the file exists we only need a read lock on the parent */
1316         return (result == 0) ? LCK_PR : LCK_PW;
1317 }
1318
1319 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1320 {
1321         struct mdt_device *mdt = info->mti_mdt;
1322         struct ptlrpc_request *req = mdt_info_req(info);
1323         struct mdt_object *parent;
1324         struct mdt_object *child;
1325         struct mdt_lock_handle *lh = NULL;
1326         struct ldlm_reply *ldlm_rep;
1327         struct mdt_body *repbody;
1328         struct lu_fid *child_fid = &info->mti_tmp_fid1;
1329         struct lu_ucred *uc = mdt_ucred(info);
1330         struct md_attr *ma = &info->mti_attr;
1331         u64 open_flags = info->mti_spec.sp_cr_flags;
1332         u64 ibits = 0;
1333         struct mdt_reint_record *rr = &info->mti_rr;
1334         int result, rc;
1335         int created = 0;
1336         int object_locked = 0;
1337         enum ldlm_mode lock_mode;
1338         u32 msg_flags;
1339         ktime_t kstart = ktime_get();
1340
1341         ENTRY;
1342         OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1343                                (obd_timeout + 1) / 4);
1344
1345         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1346
1347         ma->ma_need = MA_INODE;
1348         ma->ma_valid = 0;
1349
1350         LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1351         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1352
1353         if (unlikely(open_flags & MDS_OPEN_JOIN_FILE)) {
1354                 CERROR("file join is not supported anymore.\n");
1355                 GOTO(out, result = err_serious(-EOPNOTSUPP));
1356         }
1357         msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1358
1359         if ((open_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1360             info->mti_spec.u.sp_ea.eadata == NULL)
1361                 GOTO(out, result = err_serious(-EINVAL));
1362
1363         if (open_flags & MDS_FMODE_WRITE &&
1364             exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
1365                 GOTO(out, result = -EROFS);
1366
1367         CDEBUG(D_INODE, "I am going to open "DFID"/("DNAME"->"DFID") "
1368                "cr_flag=%#llo mode=0%06o msg_flag=0x%x\n",
1369                PFID(rr->rr_fid1), PNAME(&rr->rr_name), PFID(rr->rr_fid2),
1370                open_flags, ma->ma_attr.la_mode, msg_flags);
1371
1372         /* Prevent by-fid operation if parent fid is .lustre/fid.
1373          * Also, we want rbac roles to have precedence over any other
1374          * permission or capability checks
1375          */
1376         if (lu_fid_eq(rr->rr_fid1, &LU_OBF_FID) && !uc->uc_rbac_byfid_ops)
1377                 GOTO(out, result = -EPERM);
1378
1379         if (info->mti_cross_ref) {
1380                 /* This is cross-ref open */
1381                 mdt_set_disposition(info, ldlm_rep,
1382                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD |
1383                              DISP_LOOKUP_POS));
1384                 result = mdt_cross_open(info, rr->rr_fid2, rr->rr_fid1,
1385                                         ldlm_rep, open_flags);
1386                 GOTO(out, result);
1387         } else if (req_is_replay(req)) {
1388                 result = mdt_open_by_fid(info, ldlm_rep, lhc);
1389
1390                 if (result != -ENOENT)
1391                         GOTO(out, result);
1392
1393                 /* We didn't find the correct object, so we need to re-create it
1394                  * via a regular replay. */
1395                 if (!(open_flags & MDS_OPEN_CREAT)) {
1396                         DEBUG_REQ(D_ERROR, req,
1397                                   "OPEN & CREAT not in open replay/by_fid");
1398                         GOTO(out, result = -EFAULT);
1399                 }
1400                 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1401         } else if (open_flags & MDS_OPEN_BY_FID) {
1402                 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1403                 if (result < 0)
1404                         CDEBUG(D_INFO, "no object for "DFID": %d\n",
1405                                PFID(rr->rr_fid2), result);
1406                 GOTO(out, result);
1407         }
1408
1409         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1410                 GOTO(out, result = err_serious(-ENOMEM));
1411
1412         mdt_set_disposition(info, ldlm_rep,
1413                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1414
1415         if (!lu_name_is_valid(&rr->rr_name))
1416                 GOTO(out, result = -EPROTO);
1417
1418         result = mdt_lock_root_xattr(info, mdt);
1419         if (result < 0)
1420                 GOTO(out, result);
1421
1422         parent = mdt_object_find(info->mti_env, mdt, rr->rr_fid1);
1423         if (IS_ERR(parent))
1424                 GOTO(out, result = PTR_ERR(parent));
1425
1426         /* get and check version of parent */
1427         result = mdt_version_get_check(info, parent, 0);
1428         if (result) {
1429                 mdt_object_put(info->mti_env, parent);
1430                 GOTO(out, result);
1431         }
1432
1433         if (!uc->uc_rbac_fscrypt_admin &&
1434             parent->mot_obj.lo_header->loh_attr & LOHA_FSCRYPT_MD &&
1435             open_flags & MDS_OPEN_CREAT)
1436                 GOTO(out_parent, result = -EPERM);
1437
1438         result = mdt_check_enc(info, parent);
1439         if (result)
1440                 GOTO(out_parent, result);
1441
1442         fid_zero(child_fid);
1443         result = -ENOENT;
1444         lock_mode = mdt_open_lock_mode(info, parent, &rr->rr_name, open_flags);
1445
1446         OBD_RACE(OBD_FAIL_MDS_REINT_OPEN);
1447 again_pw:
1448         if (lock_mode != LCK_NL) {
1449                 lh = &info->mti_lh[MDT_LH_PARENT];
1450                 result = mdt_parent_lock(info, parent, lh, &rr->rr_name,
1451                                          lock_mode, false);
1452                 if (result != 0)
1453                         GOTO(out_parent, result);
1454
1455                 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1456                                     &rr->rr_name, child_fid, &info->mti_spec);
1457         }
1458
1459         LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1460                  "looking for "DFID"/"DNAME", found FID = "DFID"\n",
1461                  PFID(mdt_object_fid(parent)), PNAME(&rr->rr_name),
1462                  PFID(child_fid));
1463
1464         if (result != 0 && result != -ENOENT)
1465                 GOTO(out_parent_unlock, result);
1466
1467         OBD_RACE(OBD_FAIL_MDS_REINT_OPEN2);
1468
1469         if (result == -ENOENT) {
1470                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1471                 if (!(open_flags & MDS_OPEN_CREAT))
1472                         GOTO(out_parent_unlock, result);
1473                 if (mdt_rdonly(req->rq_export))
1474                         GOTO(out_parent_unlock, result = -EROFS);
1475
1476                 LASSERT(equi(lh == NULL, lock_mode == LCK_NL));
1477
1478                 if (lock_mode == LCK_PR) {
1479                         /* unlink vs create race: get write lock and restart */
1480                         mdt_object_unlock(info, parent, lh, 1);
1481                         mdt_clear_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1482                         lock_mode = LCK_PW;
1483                         goto again_pw;
1484                 }
1485
1486                 *child_fid = *info->mti_rr.rr_fid2;
1487                 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1488                          PFID(child_fid));
1489                 /* In the function below, .hs_keycmp resolves to
1490                  * lu_obj_hop_keycmp() */
1491                 /* coverity[overrun-buffer-val] */
1492                 child = mdt_object_new(info->mti_env, mdt, child_fid);
1493         } else {
1494                 /*
1495                  * Check for O_EXCL is moved to the mdt_finish_open(),
1496                  * we need to return FID back in that case.
1497                  */
1498                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1499                 child = mdt_object_find(info->mti_env, mdt, child_fid);
1500         }
1501         if (IS_ERR(child))
1502                 GOTO(out_parent_unlock, result = PTR_ERR(child));
1503
1504         /** check version of child  */
1505         rc = mdt_version_get_check(info, child, 1);
1506         if (rc)
1507                 GOTO(out_child, result = rc);
1508
1509         tgt_open_obj_set(info->mti_env, mdt_obj2dt(child));
1510
1511         if (result == -ENOENT) {
1512                 /* Create under OBF and .lustre is not permitted */
1513                 if (!fid_is_md_operative(rr->rr_fid1) &&
1514                     (open_flags & MDS_OPEN_VOLATILE) == 0)
1515                         GOTO(out_child, result = -EPERM);
1516
1517                 /* save versions in reply */
1518                 mdt_version_get_save(info, parent, 0);
1519                 mdt_version_get_save(info, child, 1);
1520
1521                 /* version of child will be changed */
1522                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
1523
1524                 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1525                 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1526
1527                 /* Don't do lookup sanity check. We know name doesn't exist. */
1528                 info->mti_spec.sp_cr_lookup = 0;
1529                 info->mti_spec.sp_feat = &dt_directory_features;
1530
1531                 result = mdo_create(info->mti_env, mdt_object_child(parent),
1532                                     &rr->rr_name, mdt_object_child(child),
1533                                     &info->mti_spec, &info->mti_attr);
1534                 if (result == -ERESTART) {
1535                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1536                         GOTO(out_child, result);
1537                 } else {
1538                         mdt_prep_ma_buf_from_rep(info, child, ma, open_flags);
1539                         /* XXX: we should call this once, see few lines below */
1540                         if (result == 0)
1541                                 result = mdt_attr_get_complex(info, child, ma);
1542
1543                         if (result != 0)
1544                                 GOTO(out_child, result);
1545                 }
1546                 created = 1;
1547                 mdt_counter_incr(req, LPROC_MDT_MKNOD,
1548                                  ktime_us_delta(ktime_get(), kstart));
1549         } else {
1550                 /*
1551                  * The object is on remote node, return its FID for remote open.
1552                  */
1553                 if (mdt_object_remote(child)) {
1554                         /*
1555                          * Check if this lock already was sent to client and
1556                          * this is resent case. For resent case do not take lock
1557                          * again, use what is already granted.
1558                          */
1559                         LASSERT(lhc != NULL);
1560
1561                         rc = mdt_check_resent_lock(info, child, lhc);
1562                         if (rc < 0)
1563                                 GOTO(out_child, result = rc);
1564                         else if (rc > 0)
1565                                 rc = mdt_object_lookup_lock(info, NULL, child,
1566                                                             lhc, LCK_PR, false);
1567                         repbody->mbo_fid1 = *mdt_object_fid(child);
1568                         repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1569                         if (rc != 0)
1570                                 result = rc;
1571                         else
1572                                 result = -MDT_EREMOTE_OPEN;
1573                         GOTO(out_child, result);
1574                 } else if (mdt_object_exists(child)) {
1575                         /* Check early for MDS_OPEN_DIRECTORY/O_DIRECTORY to
1576                          * avoid opening regular files from lfs getstripe
1577                          * since doing so breaks the leases used by lfs
1578                          * mirror. See LU-13693. */
1579                         if (open_flags & MDS_OPEN_DIRECTORY &&
1580                             S_ISREG(lu_object_attr(&child->mot_obj)))
1581                                 GOTO(out_child, result = -ENOTDIR);
1582
1583                         /* We have to get attr & LOV EA & HSM for this
1584                          * object. */
1585                         mdt_prep_ma_buf_from_rep(info, child, ma, open_flags);
1586                         ma->ma_need |= MA_HSM;
1587                         result = mdt_attr_get_complex(info, child, ma);
1588                         if (result != 0)
1589                                 GOTO(out_child, result);
1590                 } else {
1591                         /* Object does not exist. Likely FS corruption. */
1592                         CERROR("%s: name '"DNAME"' present, but FID "
1593                                DFID" is invalid\n", mdt_obd_name(info->mti_mdt),
1594                                PNAME(&rr->rr_name), PFID(child_fid));
1595                         GOTO(out_child, result = -EIO);
1596                 }
1597         }
1598
1599         repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1600         repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1601
1602         rc = mdt_pack_secctx_in_reply(info, child);
1603         if (unlikely(rc))
1604                 GOTO(out_child, result = rc);
1605
1606         rc = mdt_pack_encctx_in_reply(info, child);
1607         if (unlikely(rc))
1608                 GOTO(out_child, result = rc);
1609
1610         rc = mdt_check_resent_lock(info, child, lhc);
1611         if (rc < 0) {
1612                 GOTO(out_child, result = rc);
1613         } else if (rc == 0) {
1614                 /* the open lock might already be gotten in
1615                  * ldlm_handle_enqueue() */
1616                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1617                 if (open_flags & MDS_OPEN_LOCK)
1618                         mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1619         } else {
1620                 /* get openlock if this isn't replay and client requested it */
1621                 if (!req_is_replay(req)) {
1622                         rc = mdt_object_open_lock(info, child, lhc, &ibits);
1623                         object_locked = 1;
1624                         if (rc != 0)
1625                                 GOTO(out_child_unlock, result = rc);
1626                         else if (open_flags & MDS_OPEN_LOCK)
1627                                 mdt_set_disposition(info, ldlm_rep,
1628                                                     DISP_OPEN_LOCK);
1629                 }
1630         }
1631         /* Try to open it now. */
1632         rc = mdt_finish_open(info, parent, child, open_flags, ldlm_rep);
1633         if (rc) {
1634                 result = rc;
1635                 /* openlock will be released if mdt_finish_open() failed */
1636                 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1637
1638                 if (created && (open_flags & MDS_OPEN_VOLATILE)) {
1639                         CERROR("%s: cannot open volatile file "DFID", orphan "
1640                                "file will be left in PENDING directory until "
1641                                "next reboot, rc = %d\n", mdt_obd_name(mdt),
1642                                PFID(mdt_object_fid(child)), rc);
1643                         GOTO(out_child_unlock, result);
1644                 }
1645
1646                 if (created) {
1647                         ma->ma_need = 0;
1648                         ma->ma_valid = 0;
1649                         rc = mdo_unlink(info->mti_env,
1650                                         mdt_object_child(parent),
1651                                         mdt_object_child(child),
1652                                         &rr->rr_name,
1653                                         &info->mti_attr, 0);
1654                         if (rc != 0)
1655                                 CERROR("%s: "DFID" cleanup of open: rc = %d\n",
1656                                        mdt_obd_name(info->mti_mdt),
1657                                        PFID(mdt_object_fid(child)), rc);
1658                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1659                 }
1660         }
1661
1662         mdt_counter_incr(req, LPROC_MDT_OPEN,
1663                          ktime_us_delta(ktime_get(), kstart));
1664
1665         EXIT;
1666 out_child_unlock:
1667         if (object_locked)
1668                 mdt_object_open_unlock(info, child, lhc, ibits, result);
1669 out_child:
1670         mdt_object_put(info->mti_env, child);
1671         if (result == 0)
1672                 mdt_pack_size2body(info, child_fid, &lhc->mlh_reg_lh);
1673 out_parent_unlock:
1674         if (lh != NULL)
1675                 mdt_object_unlock(info, parent, lh, result || !created);
1676
1677 out_parent:
1678         mdt_object_put(info->mti_env, parent);
1679 out:
1680         if (result)
1681                 lustre_msg_set_transno(req->rq_repmsg, 0);
1682         return result;
1683 }
1684
1685 /**
1686  * Create an orphan object use local root.
1687  */
1688 static struct mdt_object *mdt_orphan_open(struct mdt_thread_info *info,
1689                                           struct mdt_device *mdt,
1690                                           const struct lu_fid *fid,
1691                                           struct md_attr *attr, fmode_t fmode)
1692 {
1693         const struct lu_env *env = info->mti_env;
1694         struct md_op_spec *spec = &info->mti_spec;
1695         struct lu_fid *local_root_fid = &info->mti_tmp_fid1;
1696         struct mdt_object *obj = NULL;
1697         struct mdt_object *local_root;
1698         static const struct lu_name lname = {
1699                 .ln_name = "i_am_nobody",
1700                 .ln_namelen = sizeof("i_am_nobody") - 1,
1701         };
1702         struct lu_ucred *uc;
1703         kernel_cap_t uc_cap_save;
1704         int rc;
1705
1706         ENTRY;
1707         rc = dt_root_get(env, mdt->mdt_bottom, local_root_fid);
1708         if (rc != 0)
1709                 RETURN(ERR_PTR(rc));
1710
1711         local_root = mdt_object_find(env, mdt, local_root_fid);
1712         if (IS_ERR(local_root))
1713                 RETURN(local_root);
1714
1715         obj = mdt_object_new(env, mdt, fid);
1716         if (IS_ERR(obj))
1717                 GOTO(out, rc = PTR_ERR(obj));
1718
1719         spec->sp_cr_lookup = 0;
1720         spec->sp_feat = &dt_directory_features;
1721         spec->sp_cr_flags = MDS_OPEN_VOLATILE | fmode;
1722         if (attr->ma_valid & MA_LOV) {
1723                 spec->u.sp_ea.eadata = attr->ma_lmm;
1724                 spec->u.sp_ea.eadatalen = attr->ma_lmm_size;
1725                 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1726         } else {
1727                 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
1728         }
1729
1730         uc = lu_ucred(env);
1731         uc_cap_save = uc->uc_cap;
1732         cap_raise(uc->uc_cap, CAP_DAC_OVERRIDE);
1733         rc = mdo_create(env, mdt_object_child(local_root), &lname,
1734                         mdt_object_child(obj), spec, attr);
1735         uc->uc_cap = uc_cap_save;
1736         if (rc < 0) {
1737                 CERROR("%s: cannot create volatile file "DFID": rc = %d\n",
1738                        mdt_obd_name(mdt), PFID(fid), rc);
1739                 GOTO(out, rc);
1740         }
1741
1742         rc = mo_open(env, mdt_object_child(obj), MDS_OPEN_CREATED, spec);
1743         if (rc < 0)
1744                 CERROR("%s: cannot open volatile file "DFID", orphan "
1745                        "file will be left in PENDING directory until "
1746                        "next reboot, rc = %d\n", mdt_obd_name(mdt),
1747                        PFID(fid), rc);
1748         GOTO(out, rc);
1749
1750 out:
1751         if (rc < 0) {
1752                 if (!IS_ERR(obj))
1753                         mdt_object_put(env, obj);
1754                 obj = ERR_PTR(rc);
1755         }
1756         mdt_object_put(env, local_root);
1757         return obj;
1758 }
1759
1760 /* XXX Look into layout in MDT layer. */
1761 static inline int mdt_hsm_set_released(struct lov_mds_md *lmm)
1762 {
1763         struct lov_comp_md_v1 *comp_v1;
1764         struct lov_mds_md *v1;
1765         __u32 off;
1766         int i;
1767
1768         if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_COMP_V1_DEFINED)) {
1769                 comp_v1 = (struct lov_comp_md_v1 *)lmm;
1770
1771                 if (comp_v1->lcm_entry_count == 0)
1772                         return -EINVAL;
1773
1774                 for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
1775                         off = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
1776                         v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
1777                         v1->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
1778                 }
1779         } else {
1780                 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
1781         }
1782         return 0;
1783 }
1784
1785 static inline int mdt_get_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
1786 {
1787         struct lov_comp_md_v1 *comp_v1;
1788
1789         if (le32_to_cpu(lmm->lmm_magic == LOV_MAGIC_COMP_V1)) {
1790                 comp_v1 = (struct lov_comp_md_v1 *)lmm;
1791                 *gen = le32_to_cpu(comp_v1->lcm_layout_gen);
1792         } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
1793                    le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
1794                 *gen = le16_to_cpu(lmm->lmm_layout_gen);
1795         } else {
1796                 return -EINVAL;
1797         }
1798         return 0;
1799 }
1800
1801 static int mdt_hsm_release(struct mdt_thread_info *info, struct mdt_object *o,
1802                            struct md_attr *ma)
1803 {
1804         struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LAYOUT];
1805         struct lu_ucred *uc = mdt_ucred(info);
1806         struct close_data *data;
1807         struct ldlm_lock *lease;
1808         struct mdt_object *orphan;
1809         struct md_attr *orp_ma;
1810         struct lu_buf *buf;
1811         kernel_cap_t cap;
1812         bool lease_broken;
1813         int rc;
1814         int rc2;
1815
1816         ENTRY;
1817         if (mdt_rdonly(info->mti_exp))
1818                 RETURN(-EROFS);
1819
1820         data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1821         if (data == NULL)
1822                 RETURN(-EPROTO);
1823
1824         lease = ldlm_handle2lock(&data->cd_handle);
1825         if (lease == NULL)
1826                 RETURN(-ESTALE);
1827
1828         /* try to hold open_sem so that nobody else can open the file */
1829         if (!down_write_trylock(&o->mot_open_sem)) {
1830                 ldlm_lock_cancel(lease);
1831                 GOTO(out_reprocess, rc = -EBUSY);
1832         }
1833
1834         /* Check if the lease open lease has already canceled */
1835         lock_res_and_lock(lease);
1836         lease_broken = ldlm_is_cancel(lease);
1837         unlock_res_and_lock(lease);
1838
1839         LDLM_DEBUG(lease, DFID " lease broken? %d",
1840                    PFID(mdt_object_fid(o)), lease_broken);
1841
1842         /* Cancel server side lease. Client side counterpart should
1843          * have been cancelled. It's okay to cancel it now as we've
1844          * held mot_open_sem. */
1845         ldlm_lock_cancel(lease);
1846
1847         if (lease_broken) /* don't perform release task */
1848                 GOTO(out_unlock, rc = -ESTALE);
1849
1850         if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
1851                 GOTO(out_unlock, rc = -EINVAL);
1852
1853         /* ma_need was set before but it seems fine to change it in order to
1854          * avoid modifying the one from RPC */
1855         ma->ma_need = MA_HSM;
1856         rc = mdt_attr_get_complex(info, o, ma);
1857         if (rc != 0)
1858                 GOTO(out_unlock, rc);
1859
1860         if (ma->ma_attr_flags & MDS_PCC_ATTACH) {
1861                 if (ma->ma_valid & MA_HSM) {
1862                         if (ma->ma_hsm.mh_flags & HS_RELEASED)
1863                                 GOTO(out_unlock, rc = -EALREADY);
1864
1865                         if (ma->ma_hsm.mh_arch_id != data->cd_archive_id)
1866                                 CDEBUG(D_CACHE,
1867                                        DFID" archive id diff: %llu:%u\n",
1868                                        PFID(mdt_object_fid(o)),
1869                                        ma->ma_hsm.mh_arch_id,
1870                                        data->cd_archive_id);
1871
1872                         if (!(ma->ma_hsm.mh_flags & HS_DIRTY) &&
1873                             ma->ma_hsm.mh_arch_ver == data->cd_data_version) {
1874                                 CDEBUG(D_CACHE,
1875                                        DFID" data version matches: packed=%llu "
1876                                        "and on-disk=%llu\n",
1877                                        PFID(mdt_object_fid(o)),
1878                                        data->cd_data_version,
1879                                        ma->ma_hsm.mh_arch_ver);
1880                                 ma->ma_hsm.mh_flags = HS_ARCHIVED | HS_EXISTS;
1881                         }
1882
1883                         if (ma->ma_hsm.mh_flags & HS_DIRTY)
1884                                 ma->ma_hsm.mh_flags = HS_ARCHIVED | HS_EXISTS;
1885                 } else {
1886                         /* Set up HSM attribte for PCC archived object */
1887                         BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1888                                      sizeof(info->mti_xattr_buf));
1889                         buf = &info->mti_buf;
1890                         buf->lb_buf = info->mti_xattr_buf;
1891                         buf->lb_len = sizeof(struct hsm_attrs);
1892                         memset(&ma->ma_hsm, 0, sizeof(ma->ma_hsm));
1893                         ma->ma_hsm.mh_flags = HS_ARCHIVED | HS_EXISTS;
1894                         ma->ma_hsm.mh_arch_id = data->cd_archive_id;
1895                         ma->ma_hsm.mh_arch_ver = data->cd_data_version;
1896                         lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
1897
1898                         rc = mo_xattr_set(info->mti_env, mdt_object_child(o),
1899                                           buf, XATTR_NAME_HSM, 0);
1900                         if (rc)
1901                                 GOTO(out_unlock, rc);
1902                 }
1903         } else {
1904                 if (!mdt_hsm_release_allow(ma))
1905                         GOTO(out_unlock, rc = -EPERM);
1906
1907                 /* already released? */
1908                 if (ma->ma_hsm.mh_flags & HS_RELEASED)
1909                         GOTO(out_unlock, rc = 0);
1910
1911                 /* Compare on-disk and packed data_version */
1912                 if (data->cd_data_version != ma->ma_hsm.mh_arch_ver) {
1913                         CDEBUG(D_HSM, DFID" data_version mismatches: "
1914                                "packed=%llu and on-disk=%llu\n",
1915                                PFID(mdt_object_fid(o)),
1916                                data->cd_data_version,
1917                                ma->ma_hsm.mh_arch_ver);
1918                         GOTO(out_unlock, rc = -EPERM);
1919                 }
1920         }
1921
1922         ma->ma_valid = MA_INODE;
1923         ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME | LA_SIZE;
1924         rc = mo_attr_set(info->mti_env, mdt_object_child(o), ma);
1925         if (rc < 0)
1926                 GOTO(out_unlock, rc);
1927
1928         mutex_lock(&o->mot_som_mutex);
1929         rc2 = mdt_set_som(info, o, SOM_FL_STRICT, ma->ma_attr.la_size,
1930                            ma->ma_attr.la_blocks);
1931         mutex_unlock(&o->mot_som_mutex);
1932         if (rc2 < 0)
1933                 CDEBUG(D_INODE,
1934                        "%s: File "DFID" SOM update failed: rc = %d\n",
1935                        mdt_obd_name(info->mti_mdt),
1936                        PFID(mdt_object_fid(o)), rc2);
1937
1938
1939         ma->ma_need = MA_INODE | MA_LOV;
1940         rc = mdt_attr_get_complex(info, o, ma);
1941         if (rc < 0)
1942                 GOTO(out_unlock, rc);
1943
1944         if (!(ma->ma_valid & MA_LOV)) {
1945                 /* Even empty file are released */
1946                 memset(ma->ma_lmm, 0, sizeof(*ma->ma_lmm));
1947                 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEFINED);
1948                 ma->ma_lmm->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
1949                 ma->ma_lmm->lmm_stripe_size = cpu_to_le32(LOV_MIN_STRIPE_SIZE);
1950                 ma->ma_lmm_size = sizeof(*ma->ma_lmm);
1951         } else {
1952                 /* Magic must be LOV_MAGIC_*_DEFINED or LOD will interpret
1953                  * ma_lmm as lov_user_md, then it will be confused by union of
1954                  * layout_gen and stripe_offset. */
1955                 if ((le32_to_cpu(ma->ma_lmm->lmm_magic) & LOV_MAGIC_MASK) ==
1956                     LOV_MAGIC_MAGIC)
1957                         ma->ma_lmm->lmm_magic |= cpu_to_le32(LOV_MAGIC_DEFINED);
1958                 else
1959                         GOTO(out_unlock, rc = -EINVAL);
1960         }
1961
1962         /* Set file as released. */
1963         rc = mdt_hsm_set_released(ma->ma_lmm);
1964         if (rc)
1965                 GOTO(out_unlock, rc);
1966
1967         orp_ma = &info->mti_u.hsm.attr;
1968         orp_ma->ma_attr.la_mode = S_IFREG | S_IWUSR;
1969         /* We use root ownership to bypass potential quota
1970          * restrictions on the user and group of the file. */
1971         orp_ma->ma_attr.la_uid = 0;
1972         orp_ma->ma_attr.la_gid = 0;
1973         orp_ma->ma_attr.la_valid = LA_MODE | LA_UID | LA_GID;
1974         orp_ma->ma_lmm = ma->ma_lmm;
1975         orp_ma->ma_lmm_size = ma->ma_lmm_size;
1976         orp_ma->ma_valid = MA_INODE | MA_LOV;
1977         orphan = mdt_orphan_open(info, info->mti_mdt, &data->cd_fid, orp_ma,
1978                                  MDS_FMODE_WRITE);
1979         if (IS_ERR(orphan)) {
1980                 CERROR("%s: cannot open orphan file "DFID": rc = %ld\n",
1981                        mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid),
1982                        PTR_ERR(orphan));
1983                 GOTO(out_unlock, rc = PTR_ERR(orphan));
1984         }
1985
1986         /* Set up HSM attribute for orphan object */
1987         BUILD_BUG_ON(sizeof(struct hsm_attrs) > sizeof(info->mti_xattr_buf));
1988         buf = &info->mti_buf;
1989         buf->lb_buf = info->mti_xattr_buf;
1990         buf->lb_len = sizeof(struct hsm_attrs);
1991         ma->ma_hsm.mh_flags |= HS_RELEASED;
1992         lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
1993         ma->ma_hsm.mh_flags &= ~HS_RELEASED;
1994
1995         rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_LAYOUT |
1996                              MDS_INODELOCK_XATTR, LCK_EX, false);
1997         if (rc != 0)
1998                 GOTO(out_close, rc);
1999
2000         /* The orphan has root ownership so we need to raise
2001          * CAP_FOWNER to set the HSM attributes. */
2002         cap = uc->uc_cap;
2003         cap_raise(uc->uc_cap, CAP_FOWNER);
2004         rc = mo_xattr_set(info->mti_env, mdt_object_child(orphan), buf,
2005                           XATTR_NAME_HSM, 0);
2006         uc->uc_cap = cap;
2007         if (rc != 0)
2008                 GOTO(out_layout_lock, rc);
2009
2010         /* Swap layout with orphan objects. */
2011         rc = mo_swap_layouts(info->mti_env, mdt_object_child(o),
2012                              mdt_object_child(orphan),
2013                              SWAP_LAYOUTS_MDS_HSM);
2014
2015         if (!rc && ma->ma_attr_flags & MDS_PCC_ATTACH) {
2016                 ma->ma_need = MA_LOV;
2017                 rc = mdt_attr_get_complex(info, o, ma);
2018         }
2019
2020         EXIT;
2021
2022 out_layout_lock:
2023         /* Release exclusive LL */
2024         mdt_object_unlock(info, o, lh, 1);
2025 out_close:
2026         /* Close orphan object anyway */
2027         rc2 = mo_close(info->mti_env, mdt_object_child(orphan), orp_ma,
2028                        MDS_FMODE_WRITE);
2029         if (rc2 < 0)
2030                 CERROR("%s: error closing volatile file "DFID": rc = %d\n",
2031                        mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid), rc2);
2032         LU_OBJECT_DEBUG(D_HSM, info->mti_env, &orphan->mot_obj,
2033                         "object closed");
2034         mdt_object_put(info->mti_env, orphan);
2035
2036 out_unlock:
2037         up_write(&o->mot_open_sem);
2038
2039         /* already released */
2040         if (rc == 0) {
2041                 struct mdt_body *repbody;
2042
2043                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2044                 LASSERT(repbody != NULL);
2045                 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2046                 if (ma->ma_attr_flags & MDS_PCC_ATTACH) {
2047                         LASSERT(ma->ma_valid & MA_LOV);
2048                         rc = mdt_get_lmm_gen(ma->ma_lmm,
2049                                              &repbody->mbo_layout_gen);
2050                         if (!rc)
2051                                 repbody->mbo_valid |= OBD_MD_LAYOUT_VERSION;
2052                 }
2053         }
2054
2055 out_reprocess:
2056         ldlm_reprocess_all(lease->l_resource,
2057                            lease->l_policy_data.l_inodebits.bits);
2058         LDLM_LOCK_PUT(lease);
2059
2060         ma->ma_valid = 0;
2061         ma->ma_need = 0;
2062
2063         return rc;
2064 }
2065
2066 int mdt_close_handle_layouts(struct mdt_thread_info *info,
2067                              struct mdt_object *o, struct md_attr *ma)
2068 {
2069         struct mdt_lock_handle *lh1 = &info->mti_lh[MDT_LH_NEW];
2070         struct mdt_lock_handle *lh2 = &info->mti_lh[MDT_LH_OLD];
2071         struct close_data *data;
2072         struct ldlm_lock *lease;
2073         struct mdt_object *o1 = o, *o2 = NULL;
2074         bool lease_broken;
2075         bool swap_objects = false;
2076         int rc;
2077
2078         ENTRY;
2079         if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
2080                 RETURN(-EROFS);
2081
2082         if (!S_ISREG(lu_object_attr(&o1->mot_obj)))
2083                 RETURN(-EINVAL);
2084
2085         data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2086         if (data == NULL)
2087                 RETURN(-EPROTO);
2088
2089         if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
2090                 RETURN(-EINVAL);
2091
2092         rc = lu_fid_cmp(&data->cd_fid, mdt_object_fid(o));
2093         if (rc == 0) {
2094                 /**
2095                  * only MDS_CLOSE_LAYOUT_SPLIT use the same fid to indicate
2096                  * mirror deletion, so we'd zero cd_fid, and keeps o2 be NULL.
2097                  */
2098                 if (!(ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT))
2099                         RETURN(-EINVAL);
2100
2101                 /* zero cd_fid to keeps o2 be NULL */
2102                 fid_zero(&data->cd_fid);
2103         } else if (rc < 0) {
2104                 /* Exchange o1 and o2, to enforce locking order */
2105                 swap_objects = true;
2106         }
2107
2108         lease = ldlm_handle2lock(&data->cd_handle);
2109         if (lease == NULL)
2110                 RETURN(-ESTALE);
2111
2112         if (!fid_is_zero(&data->cd_fid)) {
2113                 o2 = mdt_object_find(info->mti_env, info->mti_mdt,
2114                                      &data->cd_fid);
2115                 if (IS_ERR(o2))
2116                         GOTO(out_lease, rc = PTR_ERR(o2));
2117
2118                 if (!mdt_object_exists(o2))
2119                         GOTO(out_obj, rc = -ENOENT);
2120
2121                 if (!S_ISREG(lu_object_attr(&o2->mot_obj)))
2122                         GOTO(out_obj, rc = -EINVAL);
2123
2124                 if (swap_objects)
2125                         swap(o1, o2);
2126         }
2127
2128         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
2129                            MAY_WRITE);
2130         if (rc < 0)
2131                 GOTO(out_obj, rc);
2132
2133         if (o2) {
2134                 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2),
2135                                    NULL, MAY_WRITE);
2136                 if (rc < 0)
2137                         GOTO(out_obj, rc);
2138         }
2139
2140         /* try to hold open_sem so that nobody else can open the file */
2141         if (!down_write_trylock(&o->mot_open_sem)) {
2142                 ldlm_lock_cancel(lease);
2143                 GOTO(out_obj, rc = -EBUSY);
2144         }
2145
2146         /* Check if the lease open lease has already canceled */
2147         lock_res_and_lock(lease);
2148         lease_broken = ldlm_is_cancel(lease);
2149         unlock_res_and_lock(lease);
2150
2151         LDLM_DEBUG(lease, DFID " lease broken? %d",
2152                    PFID(mdt_object_fid(o)), lease_broken);
2153
2154         /* Cancel server side lease. Client side counterpart should
2155          * have been cancelled. It's okay to cancel it now as we've
2156          * held mot_open_sem. */
2157         ldlm_lock_cancel(lease);
2158
2159         if (lease_broken)
2160                 GOTO(out_unlock_sem, rc = -ESTALE);
2161
2162         rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
2163                              MDS_INODELOCK_XATTR, LCK_EX, false);
2164         if (rc < 0)
2165                 GOTO(out_unlock_sem, rc);
2166
2167         if (o2) {
2168                 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
2169                                      MDS_INODELOCK_XATTR, LCK_EX, false);
2170                 if (rc < 0)
2171                         GOTO(out_unlock1, rc);
2172         }
2173
2174         /* Swap layout with orphan object */
2175         if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SWAP) {
2176                 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
2177                                      mdt_object_child(o2), 0);
2178         } else if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_MERGE ||
2179                    ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT) {
2180                 struct lu_buf *buf = &info->mti_buf;
2181                 struct md_rejig_data mrd;
2182
2183                 if (o2) {
2184                         mrd.mrd_obj = mdt_object_child(o == o1 ? o2 : o1);
2185                 } else {
2186                         if (!(ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT)) {
2187                                 /* paranoid check again */
2188                                 CERROR(DFID
2189                                   ":only mirror split support NULL o2 object\n",
2190                                         PFID(mdt_object_fid(o)));
2191                                 GOTO(out_unlock1, rc = -EINVAL);
2192                         }
2193
2194                         /* set NULL mrd_obj for deleting mirror objects */
2195                         mrd.mrd_obj = NULL;
2196                 }
2197
2198                 if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT) {
2199                         mrd.mrd_mirror_id = data->cd_mirror_id;
2200                         /* set a small enough blocks in the SoM */
2201                         ma->ma_attr.la_blocks >>= 1;
2202                 }
2203
2204                 buf->lb_len = sizeof(mrd);
2205                 buf->lb_buf = &mrd;
2206                 rc = mo_xattr_set(info->mti_env, mdt_object_child(o), buf,
2207                                   XATTR_LUSTRE_LOV,
2208                                   ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT ?
2209                                   LU_XATTR_SPLIT : LU_XATTR_MERGE);
2210                 if (rc == 0 && ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS |
2211                                                        LA_LSIZE | LA_LBLOCKS)) {
2212                         int rc2;
2213                         enum lustre_som_flags lsf;
2214
2215                         if (ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS))
2216                                 lsf = SOM_FL_STRICT;
2217                         else
2218                                 lsf = SOM_FL_LAZY;
2219
2220                         mutex_lock(&o->mot_som_mutex);
2221                         rc2 = mdt_set_som(info, o, lsf,
2222                                           ma->ma_attr.la_size,
2223                                           ma->ma_attr.la_blocks);
2224                         mutex_unlock(&o->mot_som_mutex);
2225                         if (rc2 < 0)
2226                                 CERROR(DFID": Setting i_blocks error: %d, "
2227                                        "i_blocks will be reported wrongly and "
2228                                        "can only be fixed in next resync\n",
2229                                        PFID(mdt_object_fid(o)), rc2);
2230                 }
2231         }
2232         if (rc < 0)
2233                 GOTO(out_unlock2, rc);
2234
2235         EXIT;
2236
2237 out_unlock2:
2238         /* Release exclusive LL */
2239         if (o2)
2240                 mdt_object_unlock(info, o2, lh2, 1);
2241
2242 out_unlock1:
2243         mdt_object_unlock(info, o1, lh1, 1);
2244
2245 out_unlock_sem:
2246         up_write(&o->mot_open_sem);
2247
2248         /* already swapped */
2249         if (rc == 0) {
2250                 struct mdt_body *repbody;
2251
2252                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2253                 LASSERT(repbody != NULL);
2254                 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2255         }
2256
2257 out_obj:
2258         if (o1 != o)
2259                 /* the 2nd object has been used, and swapped to o1 */
2260                 mdt_object_put(info->mti_env, o1);
2261         else if (o2)
2262                 /* the 2nd object has been used, and not swapped */
2263                 mdt_object_put(info->mti_env, o2);
2264
2265         ldlm_reprocess_all(lease->l_resource,
2266                            lease->l_policy_data.l_inodebits.bits);
2267
2268 out_lease:
2269         LDLM_LOCK_PUT(lease);
2270
2271         if (ma != NULL) {
2272                 ma->ma_valid = 0;
2273                 ma->ma_need = 0;
2274         }
2275
2276         return rc;
2277 }
2278
2279 static int mdt_close_resync_done(struct mdt_thread_info *info,
2280                                  struct mdt_object *o, struct md_attr *ma)
2281 {
2282         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LOCAL];
2283         struct close_data *data;
2284         struct ldlm_lock *lease;
2285         struct md_layout_change layout = { 0 };
2286         __u32 *resync_ids = NULL;
2287         size_t resync_count = 0;
2288         bool lease_broken;
2289         int rc;
2290
2291         ENTRY;
2292         if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
2293                 RETURN(-EROFS);
2294
2295         if (!S_ISREG(lu_object_attr(&o->mot_obj)))
2296                 RETURN(-EINVAL);
2297
2298         data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2299         if (data == NULL)
2300                 RETURN(-EPROTO);
2301
2302         if (req_capsule_req_need_swab(info->mti_pill))
2303                 lustre_swab_close_data_resync_done(&data->cd_resync);
2304
2305         if (!fid_is_zero(&data->cd_fid))
2306                 RETURN(-EPROTO);
2307
2308         lease = ldlm_handle2lock(&data->cd_handle);
2309         if (lease == NULL)
2310                 RETURN(-ESTALE);
2311
2312         /* try to hold open_sem so that nobody else can open the file */
2313         if (!down_write_trylock(&o->mot_open_sem)) {
2314                 ldlm_lock_cancel(lease);
2315                 GOTO(out_reprocess, rc = -EBUSY);
2316         }
2317
2318         /* Check if the lease open lease has already canceled */
2319         lock_res_and_lock(lease);
2320         lease_broken = ldlm_is_cancel(lease);
2321         unlock_res_and_lock(lease);
2322
2323         LDLM_DEBUG(lease, DFID " lease broken? %d",
2324                    PFID(mdt_object_fid(o)), lease_broken);
2325
2326         /* Cancel server side lease. Client side counterpart should
2327          * have been cancelled. It's okay to cancel it now as we've
2328          * held mot_open_sem. */
2329         ldlm_lock_cancel(lease);
2330
2331         if (lease_broken) /* don't perform release task */
2332                 GOTO(out_unlock, rc = -ESTALE);
2333
2334         resync_count = data->cd_resync.resync_count;
2335
2336         if (resync_count > INLINE_RESYNC_ARRAY_SIZE) {
2337                 void *data;
2338
2339                 if (!req_capsule_has_field(info->mti_pill, &RMF_U32,
2340                                            RCL_CLIENT))
2341                         GOTO(out_unlock, rc = -EPROTO);
2342
2343                 OBD_ALLOC_PTR_ARRAY(resync_ids, resync_count);
2344                 if (!resync_ids)
2345                         GOTO(out_unlock, rc = -ENOMEM);
2346
2347                 data = req_capsule_client_get(info->mti_pill, &RMF_U32);
2348                 memcpy(resync_ids, data, resync_count * sizeof(__u32));
2349
2350                 layout.mlc_resync_ids = resync_ids;
2351         } else {
2352                 layout.mlc_resync_ids = data->cd_resync.resync_ids_inline;
2353         }
2354
2355         layout.mlc_opc = MD_LAYOUT_RESYNC_DONE;
2356         layout.mlc_resync_count = resync_count;
2357         if (ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS)) {
2358                 layout.mlc_som.lsa_valid = SOM_FL_STRICT;
2359                 layout.mlc_som.lsa_size = ma->ma_attr.la_size;
2360                 layout.mlc_som.lsa_blocks = ma->ma_attr.la_blocks;
2361         }
2362         rc = mdt_layout_change(info, o, lhc, &layout);
2363         if (rc)
2364                 GOTO(out_unlock, rc);
2365
2366         mdt_object_unlock(info, o, lhc, 0);
2367
2368         EXIT;
2369
2370 out_unlock:
2371         up_write(&o->mot_open_sem);
2372
2373         /* already released */
2374         if (rc == 0) {
2375                 struct mdt_body *repbody;
2376
2377                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2378                 LASSERT(repbody != NULL);
2379                 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2380         }
2381
2382         if (resync_ids)
2383                 OBD_FREE_PTR_ARRAY(resync_ids, resync_count);
2384
2385 out_reprocess:
2386         ldlm_reprocess_all(lease->l_resource,
2387                            lease->l_policy_data.l_inodebits.bits);
2388         LDLM_LOCK_PUT(lease);
2389
2390         ma->ma_valid = 0;
2391         ma->ma_need = 0;
2392
2393         return rc;
2394 }
2395
2396 #define MFD_CLOSED(open_flags) ((open_flags) == MDS_FMODE_CLOSED)
2397
2398 static int mdt_mfd_closed(struct mdt_file_data *mfd)
2399 {
2400         return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_open_flags));
2401 }
2402
2403 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
2404 {
2405         struct mdt_object *o = mfd->mfd_object;
2406         struct md_object *next = mdt_object_child(o);
2407         struct md_attr *ma = &info->mti_attr;
2408         struct lu_fid *ofid = &info->mti_tmp_fid1;
2409         int rc = 0;
2410         int rc2;
2411         u64 open_flags;
2412         u64 intent;
2413
2414         ENTRY;
2415         open_flags = mfd->mfd_open_flags;
2416         intent = ma->ma_attr_flags & MDS_CLOSE_INTENT;
2417         *ofid = *mdt_object_fid(o);
2418
2419         /* the below message is checked in replay-single.sh test_46 */
2420         CDEBUG(D_INODE, "%s: %sclosing file handle "DFID" with intent: %llx\n",
2421                mdt_obd_name(info->mti_mdt),
2422                ma->ma_valid & MA_FORCE_LOG ? "force " : "", PFID(ofid), intent);
2423
2424         switch (intent) {
2425         case MDS_HSM_RELEASE: {
2426                 rc = mdt_hsm_release(info, o, ma);
2427                 if (rc < 0) {
2428                         CDEBUG(D_HSM, "%s: File " DFID " release failed: %d\n",
2429                                mdt_obd_name(info->mti_mdt),
2430                                PFID(ofid), rc);
2431                         /* continue to close even error occurred. */
2432                 }
2433                 break;
2434         }
2435         case MDS_CLOSE_LAYOUT_MERGE:
2436         case MDS_CLOSE_LAYOUT_SPLIT:
2437         case MDS_CLOSE_LAYOUT_SWAP: {
2438                 rc = mdt_close_handle_layouts(info, o, ma);
2439                 if (rc < 0) {
2440                         CDEBUG(D_INODE,
2441                                "%s: cannot %s layout of "DFID": rc = %d\n",
2442                                mdt_obd_name(info->mti_mdt),
2443                                intent == MDS_CLOSE_LAYOUT_MERGE ? "merge" :
2444                                intent == MDS_CLOSE_LAYOUT_SPLIT ? "split" :
2445                                "swap",
2446                                PFID(ofid), rc);
2447                         /* continue to close even if error occurred. */
2448                 }
2449                 break;
2450         }
2451         case MDS_CLOSE_RESYNC_DONE:
2452                 rc = mdt_close_resync_done(info, o, ma);
2453                 if (rc < 0) {
2454                         CDEBUG(D_INODE,
2455                                "%s: cannot resync layout of "DFID": rc = %d\n",
2456                                mdt_obd_name(info->mti_mdt),
2457                                PFID(ofid), rc);
2458                         /* continue to close even if error occurred. */
2459                 }
2460                 break;
2461         default:
2462                 /* nothing */
2463                 break;
2464         }
2465
2466         if (S_ISREG(lu_object_attr(&o->mot_obj)) &&
2467             ma->ma_attr.la_valid & (LA_LSIZE | LA_LBLOCKS)) {
2468                 rc2 = mdt_lsom_update(info, o, false);
2469                 if (rc2 < 0) {
2470                         CDEBUG(D_INODE,
2471                                "%s: File " DFID " LSOM failed: rc = %d\n",
2472                                mdt_obd_name(info->mti_mdt),
2473                                PFID(ofid), rc2);
2474                         if (rc == 0)
2475                                 rc = rc2;
2476                         /* continue to close even if error occurred. */
2477                 }
2478         }
2479
2480         if (open_flags & MDS_FMODE_WRITE)
2481                 mdt_write_put(o);
2482         else if (open_flags & MDS_FMODE_EXEC)
2483                 mdt_write_allow(o);
2484
2485         /* Update atime|mtime|ctime on close. */
2486         if ((open_flags & MDS_FMODE_EXEC || open_flags & MDS_FMODE_READ ||
2487              open_flags & MDS_FMODE_WRITE) && (ma->ma_valid & MA_INODE) &&
2488             (ma->ma_attr.la_valid & LA_ATIME ||
2489              ma->ma_attr.la_valid & LA_MTIME ||
2490              ma->ma_attr.la_valid & LA_CTIME)) {
2491                 ma->ma_valid = MA_INODE;
2492                 ma->ma_attr_flags |= MDS_CLOSE_UPDATE_TIMES;
2493                 ma->ma_attr.la_valid &= (LA_ATIME | LA_MTIME | LA_CTIME);
2494
2495                 if (ma->ma_attr.la_valid & LA_MTIME) {
2496                         if (mdt_attr_get_pfid(info, o, &ma->ma_pfid) == 0)
2497                                 ma->ma_valid |= MA_PFID;
2498                 }
2499
2500                 rc2 = mo_attr_set(info->mti_env, next, ma);
2501                 if (rc2 != 0) {
2502                         CDEBUG(D_INODE,
2503                            "%s: File "DFID" set attr (%#llx) failed: rc = %d\n",
2504                                mdt_obd_name(info->mti_mdt), PFID(ofid),
2505                                ma->ma_attr.la_valid, rc2);
2506                         if (rc == 0)
2507                                 rc = rc2;
2508                 }
2509         }
2510
2511         /* If file data is modified, add the dirty flag. */
2512         if (ma->ma_attr_flags & MDS_DATA_MODIFIED) {
2513                 rc2 = mdt_add_dirty_flag(info, o, ma);
2514                 if (rc2 != 0) {
2515                         CDEBUG(D_INODE,
2516                              "%s: File "DFID" add dirty flag failed: rc = %d\n",
2517                                mdt_obd_name(info->mti_mdt), PFID(ofid), rc2);
2518                         if (rc == 0)
2519                                 rc = rc2;
2520                 }
2521         }
2522
2523         ma->ma_need |= MA_INODE;
2524         ma->ma_valid &= ~MA_INODE;
2525
2526         LASSERT(atomic_read(&o->mot_open_count) > 0);
2527         atomic_dec(&o->mot_open_count);
2528         mdt_handle_last_unlink(info, o, ma);
2529
2530         if (!MFD_CLOSED(open_flags)) {
2531                 rc2 = mo_close(info->mti_env, next, ma, open_flags);
2532                 if (rc2 != 0) {
2533                         CDEBUG(D_INODE,
2534                                "%s: File "DFID" close failed: rc = %d\n",
2535                                mdt_obd_name(info->mti_mdt), PFID(ofid), rc2);
2536                         if (rc == 0)
2537                                 rc = rc2;
2538                 }
2539                 if (mdt_dom_check_for_discard(info, o))
2540                         mdt_dom_discard_data(info, o);
2541         }
2542
2543         /* adjust open and lease count */
2544         if (open_flags & MDS_OPEN_LEASE) {
2545                 LASSERT(atomic_read(&o->mot_lease_count) > 0);
2546                 atomic_dec(&o->mot_lease_count);
2547         }
2548
2549         mdt_mfd_free(mfd);
2550         mdt_object_put(info->mti_env, o);
2551
2552         RETURN(rc);
2553 }
2554
2555 int mdt_close_internal(struct mdt_thread_info *info, struct ptlrpc_request *req,
2556                        struct mdt_body *repbody)
2557 {
2558         struct mdt_export_data *med;
2559         struct mdt_file_data   *mfd;
2560         int ret = 0;
2561         int rc = 0;
2562
2563         ENTRY;
2564         med = &req->rq_export->exp_mdt_data;
2565         spin_lock(&med->med_open_lock);
2566         mfd = mdt_open_handle2mfd(med, &info->mti_open_handle,
2567                                   req_is_replay(req));
2568         if (mdt_mfd_closed(mfd)) {
2569                 spin_unlock(&med->med_open_lock);
2570                 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
2571                        ": cookie = %#llx\n", PFID(info->mti_rr.rr_fid1),
2572                        info->mti_open_handle.cookie);
2573                 /** not serious error since bug 3633 */
2574                 rc = -ESTALE;
2575         } else {
2576                 class_handle_unhash(&mfd->mfd_open_handle);
2577                 list_del_init(&mfd->mfd_list);
2578                 spin_unlock(&med->med_open_lock);
2579                 ret = mdt_mfd_close(info, mfd);
2580         }
2581
2582         RETURN(rc ? rc : ret);
2583 }
2584
2585 int mdt_close(struct tgt_session_info *tsi)
2586 {
2587         struct mdt_thread_info *info = tsi2mdt_info(tsi);
2588         struct ptlrpc_request *req = tgt_ses_req(tsi);
2589         struct md_attr *ma = &info->mti_attr;
2590         struct mdt_body *repbody = NULL;
2591         ktime_t kstart = ktime_get();
2592         int rc;
2593         int rc2;
2594
2595         ENTRY;
2596         /* Close may come with the Size-on-MDS update. Unpack it. */
2597         rc = mdt_close_unpack(info);
2598         if (rc)
2599                 GOTO(out, rc = err_serious(rc));
2600
2601         /* These fields are no longer used and are left for compatibility.
2602          * size is always zero */
2603         req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
2604                              0);
2605         req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
2606                              0);
2607         rc = req_capsule_server_pack(info->mti_pill);
2608         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
2609                 mdt_client_compatibility(info);
2610                 if (rc == 0)
2611                         mdt_fix_reply(info);
2612                 mdt_exit_ucred(info);
2613                 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2614         }
2615
2616         /* Continue to close handle even if we can not pack reply */
2617         if (rc == 0) {
2618                 repbody = req_capsule_server_get(info->mti_pill,
2619                                                  &RMF_MDT_BODY);
2620                 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
2621                                                     &RMF_MDT_MD);
2622                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
2623                                                        &RMF_MDT_MD,
2624                                                        RCL_SERVER);
2625                 ma->ma_need = MA_INODE | MA_LOV;
2626                 repbody->mbo_eadatasize = 0;
2627                 repbody->mbo_aclsize = 0;
2628         } else {
2629                 rc = err_serious(rc);
2630         }
2631
2632         rc2 = mdt_close_internal(info, req, repbody);
2633         if (rc2 != -ESTALE)
2634                 mdt_empty_transno(info, rc2);
2635         if (rc2 != 0 && rc == 0)
2636                 rc = rc2;
2637
2638         if (repbody != NULL) {
2639                 mdt_client_compatibility(info);
2640                 rc2 = mdt_fix_reply(info);
2641                 if (rc2 != 0 && rc == 0)
2642                         rc = rc2;
2643         }
2644
2645         mdt_exit_ucred(info);
2646         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
2647                 GOTO(out, rc = err_serious(-ENOMEM));
2648
2649         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
2650                                  OBD_FAIL_MDS_CLOSE_NET_REP))
2651                 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
2652 out:
2653         mdt_thread_info_fini(info);
2654         if (rc == 0)
2655                 mdt_counter_incr(req, LPROC_MDT_CLOSE,
2656                                  ktime_us_delta(ktime_get(), kstart));
2657         RETURN(rc);
2658 }