Whamcloud - gitweb
8118a921ae7979101ee9d205085bad0bdc4340f7
[fs/lustre-release.git] / lustre / llite / namei.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) 2002, 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  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #include <linux/fs.h>
34 #include <linux/sched.h>
35 #include <linux/mm.h>
36 #include <linux/quotaops.h>
37 #include <linux/highmem.h>
38 #include <linux/pagemap.h>
39 #include <linux/security.h>
40 #include <linux/user_namespace.h>
41 #ifdef HAVE_UIDGID_HEADER
42 # include <linux/uidgid.h>
43 #endif
44
45 #define DEBUG_SUBSYSTEM S_LLITE
46
47 #include <obd_support.h>
48 #include <lustre_fid.h>
49 #include <lustre_dlm.h>
50 #include "llite_internal.h"
51
52 static int ll_create_it(struct inode *dir, struct dentry *dentry,
53                         struct lookup_intent *it,
54                         void *secctx, __u32 secctxlen);
55
56 /* called from iget5_locked->find_inode() under inode_lock spinlock */
57 static int ll_test_inode(struct inode *inode, void *opaque)
58 {
59         struct ll_inode_info    *lli = ll_i2info(inode);
60         struct lustre_md        *md = opaque;
61
62         if (unlikely(!(md->body->mbo_valid & OBD_MD_FLID))) {
63                 CERROR("MDS body missing FID\n");
64                 return 0;
65         }
66
67         if (!lu_fid_eq(&lli->lli_fid, &md->body->mbo_fid1))
68                 return 0;
69
70         return 1;
71 }
72
73 static int ll_set_inode(struct inode *inode, void *opaque)
74 {
75         struct ll_inode_info *lli = ll_i2info(inode);
76         struct mdt_body *body = ((struct lustre_md *)opaque)->body;
77
78         if (unlikely(!(body->mbo_valid & OBD_MD_FLID))) {
79                 CERROR("MDS body missing FID\n");
80                 return -EINVAL;
81         }
82
83         lli->lli_fid = body->mbo_fid1;
84         if (unlikely(!(body->mbo_valid & OBD_MD_FLTYPE))) {
85                 CERROR("Can not initialize inode "DFID" without object type: "
86                        "valid = %#llx\n",
87                        PFID(&lli->lli_fid), body->mbo_valid);
88                 return -EINVAL;
89         }
90
91         inode->i_mode = (inode->i_mode & ~S_IFMT) | (body->mbo_mode & S_IFMT);
92         if (unlikely(inode->i_mode == 0)) {
93                 CERROR("Invalid inode "DFID" type\n", PFID(&lli->lli_fid));
94                 return -EINVAL;
95         }
96
97         ll_lli_init(lli);
98
99         return 0;
100 }
101
102
103 /**
104  * Get an inode by inode number(@hash), which is already instantiated by
105  * the intent lookup).
106  */
107 struct inode *ll_iget(struct super_block *sb, ino_t hash,
108                       struct lustre_md *md)
109 {
110         struct inode    *inode;
111         int             rc = 0;
112
113         ENTRY;
114
115         LASSERT(hash != 0);
116         inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
117         if (inode == NULL)
118                 RETURN(ERR_PTR(-ENOMEM));
119
120         if (inode->i_state & I_NEW) {
121                 rc = ll_read_inode2(inode, md);
122                 if (rc == 0 && S_ISREG(inode->i_mode) &&
123                     ll_i2info(inode)->lli_clob == NULL)
124                         rc = cl_file_inode_init(inode, md);
125
126                 if (rc != 0) {
127                         /* Let's clear directory lsm here, otherwise
128                          * make_bad_inode() will reset the inode mode
129                          * to regular, then ll_clear_inode will not
130                          * be able to clear lsm_md */
131                         if (S_ISDIR(inode->i_mode))
132                                 ll_dir_clear_lsm_md(inode);
133                         make_bad_inode(inode);
134                         unlock_new_inode(inode);
135                         iput(inode);
136                         inode = ERR_PTR(rc);
137                 } else {
138                         inode_has_no_xattr(inode);
139                         unlock_new_inode(inode);
140                 }
141         } else if (is_bad_inode(inode)) {
142                 iput(inode);
143                 inode = ERR_PTR(-ESTALE);
144         } else if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
145                 rc = ll_update_inode(inode, md);
146                 CDEBUG(D_VFSTRACE, "got inode: "DFID"(%p): rc = %d\n",
147                        PFID(&md->body->mbo_fid1), inode, rc);
148                 if (rc != 0) {
149                         if (S_ISDIR(inode->i_mode))
150                                 ll_dir_clear_lsm_md(inode);
151                         iput(inode);
152                         inode = ERR_PTR(rc);
153                 }
154         }
155
156         RETURN(inode);
157 }
158
159 static void ll_invalidate_negative_children(struct inode *dir)
160 {
161         struct dentry *dentry, *tmp_subdir;
162         DECLARE_LL_D_HLIST_NODE_PTR(p);
163
164         ll_lock_dcache(dir);
165         ll_d_hlist_for_each_entry(dentry, p, &dir->i_dentry) {
166                 spin_lock(&dentry->d_lock);
167                 if (!list_empty(&dentry->d_subdirs)) {
168                         struct dentry *child;
169
170                         list_for_each_entry_safe(child, tmp_subdir,
171                                                  &dentry->d_subdirs,
172                                                  d_child) {
173                                 if (child->d_inode == NULL)
174                                         d_lustre_invalidate(child, 1);
175                         }
176                 }
177                 spin_unlock(&dentry->d_lock);
178         }
179         ll_unlock_dcache(dir);
180 }
181
182 int ll_test_inode_by_fid(struct inode *inode, void *opaque)
183 {
184         return lu_fid_eq(&ll_i2info(inode)->lli_fid, opaque);
185 }
186
187 int ll_dom_lock_cancel(struct inode *inode, struct ldlm_lock *lock)
188 {
189         struct lu_env *env;
190         struct ll_inode_info *lli = ll_i2info(inode);
191         struct cl_layout clt = { .cl_layout_gen = 0, };
192         int rc;
193         __u16 refcheck;
194
195
196         ENTRY;
197
198         if (!lli->lli_clob)
199                 RETURN(0);
200
201         env = cl_env_get(&refcheck);
202         if (IS_ERR(env))
203                 RETURN(PTR_ERR(env));
204
205         rc = cl_object_layout_get(env, lli->lli_clob, &clt);
206         if (rc) {
207                 CDEBUG(D_INODE, "Cannot get layout for "DFID"\n",
208                        PFID(ll_inode2fid(inode)));
209                 rc = -ENODATA;
210         } else if (clt.cl_size == 0 || clt.cl_dom_comp_size == 0) {
211                 CDEBUG(D_INODE, "DOM lock without DOM layout for "DFID"\n",
212                        PFID(ll_inode2fid(inode)));
213         } else {
214                 enum cl_fsync_mode mode;
215                 loff_t end = clt.cl_dom_comp_size - 1;
216
217                 mode = ldlm_is_discard_data(lock) ?
218                                         CL_FSYNC_DISCARD : CL_FSYNC_LOCAL;
219                 rc = cl_sync_file_range(inode, 0, end, mode, 1);
220                 truncate_inode_pages_range(inode->i_mapping, 0, end);
221         }
222         cl_env_put(env, &refcheck);
223         RETURN(rc);
224 }
225
226 void ll_lock_cancel_bits(struct ldlm_lock *lock, __u64 to_cancel)
227 {
228         struct inode *inode = ll_inode_from_resource_lock(lock);
229         __u64 bits = to_cancel;
230         int rc;
231
232         if (inode == NULL)
233                 return;
234
235         if (!fid_res_name_eq(ll_inode2fid(inode),
236                              &lock->l_resource->lr_name)) {
237                 LDLM_ERROR(lock, "data mismatch with object "DFID"(%p)",
238                            PFID(ll_inode2fid(inode)), inode);
239                 LBUG();
240         }
241
242         if (bits & MDS_INODELOCK_XATTR) {
243                 if (S_ISDIR(inode->i_mode))
244                         ll_i2info(inode)->lli_def_stripe_offset = -1;
245                 ll_xattr_cache_destroy(inode);
246                 bits &= ~MDS_INODELOCK_XATTR;
247         }
248
249         /* For OPEN locks we differentiate between lock modes
250          * LCK_CR, LCK_CW, LCK_PR - bug 22891 */
251         if (bits & MDS_INODELOCK_OPEN)
252                 ll_have_md_lock(inode, &bits, lock->l_req_mode);
253
254         if (bits & MDS_INODELOCK_OPEN) {
255                 fmode_t fmode;
256
257                 switch (lock->l_req_mode) {
258                 case LCK_CW:
259                         fmode = FMODE_WRITE;
260                         break;
261                 case LCK_PR:
262                         fmode = FMODE_EXEC;
263                         break;
264                 case LCK_CR:
265                         fmode = FMODE_READ;
266                         break;
267                 default:
268                         LDLM_ERROR(lock, "bad lock mode for OPEN lock");
269                         LBUG();
270                 }
271
272                 ll_md_real_close(inode, fmode);
273
274                 bits &= ~MDS_INODELOCK_OPEN;
275         }
276
277         if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
278                     MDS_INODELOCK_LAYOUT | MDS_INODELOCK_PERM |
279                     MDS_INODELOCK_DOM))
280                 ll_have_md_lock(inode, &bits, LCK_MINMODE);
281
282         if (bits & MDS_INODELOCK_DOM) {
283                 rc =  ll_dom_lock_cancel(inode, lock);
284                 if (rc < 0)
285                         CDEBUG(D_INODE, "cannot flush DoM data "
286                                DFID": rc = %d\n",
287                                PFID(ll_inode2fid(inode)), rc);
288                 lock_res_and_lock(lock);
289                 ldlm_set_kms_ignore(lock);
290                 unlock_res_and_lock(lock);
291         }
292
293         if (bits & MDS_INODELOCK_LAYOUT) {
294                 struct cl_object_conf conf = {
295                         .coc_opc = OBJECT_CONF_INVALIDATE,
296                         .coc_inode = inode,
297                 };
298
299                 rc = ll_layout_conf(inode, &conf);
300                 if (rc < 0)
301                         CDEBUG(D_INODE, "cannot invalidate layout of "
302                                DFID": rc = %d\n",
303                                PFID(ll_inode2fid(inode)), rc);
304         }
305
306         if (bits & MDS_INODELOCK_UPDATE) {
307                 struct ll_inode_info *lli = ll_i2info(inode);
308
309                 lli->lli_update_atime = 1;
310         }
311
312         if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
313                 struct ll_inode_info *lli = ll_i2info(inode);
314
315                 CDEBUG(D_INODE, "invalidating inode "DFID" lli = %p, "
316                        "pfid  = "DFID"\n", PFID(ll_inode2fid(inode)),
317                        lli, PFID(&lli->lli_pfid));
318                 truncate_inode_pages(inode->i_mapping, 0);
319
320                 if (unlikely(!fid_is_zero(&lli->lli_pfid))) {
321                         struct inode *master_inode = NULL;
322                         unsigned long hash;
323
324                         /* This is slave inode, since all of the child dentry
325                          * is connected on the master inode, so we have to
326                          * invalidate the negative children on master inode */
327                         CDEBUG(D_INODE, "Invalidate s"DFID" m"DFID"\n",
328                                PFID(ll_inode2fid(inode)), PFID(&lli->lli_pfid));
329
330                         hash = cl_fid_build_ino(&lli->lli_pfid,
331                                         ll_need_32bit_api(ll_i2sbi(inode)));
332
333                         /* Do not lookup the inode with ilookup5, otherwise
334                          * it will cause dead lock,
335                          * 1. Client1 send chmod req to the MDT0, then on MDT0,
336                          * it enqueues master and all of its slaves lock,
337                          * (mdt_attr_set() -> mdt_lock_slaves()), after gets
338                          * master and stripe0 lock, it will send the enqueue
339                          * req (for stripe1) to MDT1, then MDT1 finds the lock
340                          * has been granted to client2. Then MDT1 sends blocking
341                          * ast to client2.
342                          * 2. At the same time, client2 tries to unlink
343                          * the striped dir (rm -rf striped_dir), and during
344                          * lookup, it will hold the master inode of the striped
345                          * directory, whose inode state is NEW, then tries to
346                          * revalidate all of its slaves, (ll_prep_inode()->
347                          * ll_iget()->ll_read_inode2()-> ll_update_inode().).
348                          * And it will be blocked on the server side because
349                          * of 1.
350                          * 3. Then the client get the blocking_ast req, cancel
351                          * the lock, but being blocked if using ->ilookup5()),
352                          * because master inode state is NEW. */
353                         master_inode = ilookup5_nowait(inode->i_sb, hash,
354                                                         ll_test_inode_by_fid,
355                                                         (void *)&lli->lli_pfid);
356                         if (master_inode) {
357                                 ll_invalidate_negative_children(master_inode);
358                                 iput(master_inode);
359                         }
360                 } else {
361                         ll_invalidate_negative_children(inode);
362                 }
363         }
364
365         if ((bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM)) &&
366             inode->i_sb->s_root != NULL &&
367             inode != inode->i_sb->s_root->d_inode)
368                 ll_invalidate_aliases(inode);
369
370         iput(inode);
371 }
372
373 /* Check if the given lock may be downgraded instead of canceling and
374  * that convert is really needed. */
375 int ll_md_need_convert(struct ldlm_lock *lock)
376 {
377         struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
378         struct inode *inode;
379         __u64 wanted = lock->l_policy_data.l_inodebits.cancel_bits;
380         __u64 bits = lock->l_policy_data.l_inodebits.bits & ~wanted;
381         enum ldlm_mode mode = LCK_MINMODE;
382
383         if (!lock->l_conn_export ||
384             !exp_connect_lock_convert(lock->l_conn_export))
385                 return 0;
386
387         if (!wanted || !bits || ldlm_is_cancel(lock))
388                 return 0;
389
390         /* do not convert locks other than DOM for now */
391         if (!((bits | wanted) & MDS_INODELOCK_DOM))
392                 return 0;
393
394         /* We may have already remaining bits in some other lock so
395          * lock convert will leave us just extra lock for the same bit.
396          * Check if client has other lock with the same bits and the same
397          * or lower mode and don't convert if any.
398          */
399         switch (lock->l_req_mode) {
400         case LCK_PR:
401                 mode = LCK_PR;
402         case LCK_PW:
403                 mode |= LCK_CR;
404                 break;
405         case LCK_CW:
406                 mode = LCK_CW;
407         case LCK_CR:
408                 mode |= LCK_CR;
409                 break;
410         default:
411                 /* do not convert other modes */
412                 return 0;
413         }
414
415         /* is lock is too old to be converted? */
416         lock_res_and_lock(lock);
417         if (ktime_after(ktime_get(),
418                         ktime_add(lock->l_last_used,
419                                   ktime_set(ns->ns_dirty_age_limit, 0)))) {
420                 unlock_res_and_lock(lock);
421                 return 0;
422         }
423         unlock_res_and_lock(lock);
424
425         inode = ll_inode_from_resource_lock(lock);
426         ll_have_md_lock(inode, &bits, mode);
427         iput(inode);
428         return !!(bits);
429 }
430
431 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
432                        void *data, int flag)
433 {
434         struct lustre_handle lockh;
435         __u64 bits = lock->l_policy_data.l_inodebits.bits;
436         int rc;
437
438         ENTRY;
439
440         switch (flag) {
441         case LDLM_CB_BLOCKING:
442         {
443                 __u64 cancel_flags = LCF_ASYNC;
444
445                 if (ll_md_need_convert(lock)) {
446                         cancel_flags |= LCF_CONVERT;
447                         /* For lock convert some cancel actions may require
448                          * this lock with non-dropped canceled bits, e.g. page
449                          * flush for DOM lock. So call ll_lock_cancel_bits()
450                          * here while canceled bits are still set.
451                          */
452                         bits = lock->l_policy_data.l_inodebits.cancel_bits;
453                         if (bits & MDS_INODELOCK_DOM)
454                                 ll_lock_cancel_bits(lock, MDS_INODELOCK_DOM);
455                 }
456                 ldlm_lock2handle(lock, &lockh);
457                 rc = ldlm_cli_cancel(&lockh, cancel_flags);
458                 if (rc < 0) {
459                         CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
460                         RETURN(rc);
461                 }
462                 break;
463         }
464         case LDLM_CB_CANCELING:
465                 if (ldlm_is_converting(lock)) {
466                         /* this is called on already converted lock, so
467                          * ibits has remained bits only and cancel_bits
468                          * are bits that were dropped.
469                          * Note that DOM lock is handled prior lock convert
470                          * and is excluded here.
471                          */
472                         bits = lock->l_policy_data.l_inodebits.cancel_bits &
473                                 ~MDS_INODELOCK_DOM;
474                 } else {
475                         LASSERT(ldlm_is_canceling(lock));
476                 }
477                 ll_lock_cancel_bits(lock, bits);
478                 break;
479         default:
480                 LBUG();
481         }
482
483         RETURN(0);
484 }
485
486 __u32 ll_i2suppgid(struct inode *i)
487 {
488         if (in_group_p(i->i_gid))
489                 return (__u32)from_kgid(&init_user_ns, i->i_gid);
490         else
491                 return (__u32) __kgid_val(INVALID_GID);
492 }
493
494 /* Pack the required supplementary groups into the supplied groups array.
495  * If we don't need to use the groups from the target inode(s) then we
496  * instead pack one or more groups from the user's supplementary group
497  * array in case it might be useful.  Not needed if doing an MDS-side upcall. */
498 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
499 {
500         LASSERT(i1 != NULL);
501         LASSERT(suppgids != NULL);
502
503         suppgids[0] = ll_i2suppgid(i1);
504
505         if (i2)
506                 suppgids[1] = ll_i2suppgid(i2);
507         else
508                 suppgids[1] = -1;
509 }
510
511 /*
512  * try to reuse three types of dentry:
513  * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
514  *    by concurrent .revalidate).
515  * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
516  *    be cleared by others calling d_lustre_revalidate).
517  * 3. DISCONNECTED alias.
518  */
519 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
520 {
521         struct dentry *alias, *discon_alias, *invalid_alias;
522         DECLARE_LL_D_HLIST_NODE_PTR(p);
523
524         if (ll_d_hlist_empty(&inode->i_dentry))
525                 return NULL;
526
527         discon_alias = invalid_alias = NULL;
528
529         ll_lock_dcache(inode);
530         ll_d_hlist_for_each_entry(alias, p, &inode->i_dentry) {
531                 LASSERT(alias != dentry);
532
533                 spin_lock(&alias->d_lock);
534                 if ((alias->d_flags & DCACHE_DISCONNECTED) &&
535                     S_ISDIR(inode->i_mode))
536                         /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
537                         discon_alias = alias;
538                 else if (alias->d_parent == dentry->d_parent             &&
539                          alias->d_name.hash == dentry->d_name.hash       &&
540                          alias->d_name.len == dentry->d_name.len         &&
541                          memcmp(alias->d_name.name, dentry->d_name.name,
542                                 dentry->d_name.len) == 0)
543                         invalid_alias = alias;
544                 spin_unlock(&alias->d_lock);
545
546                 if (invalid_alias)
547                         break;
548         }
549         alias = invalid_alias ?: discon_alias ?: NULL;
550         if (alias) {
551                 spin_lock(&alias->d_lock);
552                 dget_dlock(alias);
553                 spin_unlock(&alias->d_lock);
554         }
555         ll_unlock_dcache(inode);
556
557         return alias;
558 }
559
560 /*
561  * Similar to d_splice_alias(), but lustre treats invalid alias
562  * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
563  */
564 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
565 {
566         struct dentry *new;
567         int rc;
568
569         if (inode) {
570                 new = ll_find_alias(inode, de);
571                 if (new) {
572                         rc = ll_d_init(new);
573                         if (rc < 0) {
574                                 dput(new);
575                                 return ERR_PTR(rc);
576                         }
577                         d_move(new, de);
578                         iput(inode);
579                         CDEBUG(D_DENTRY,
580                                "Reuse dentry %p inode %p refc %d flags %#x\n",
581                               new, new->d_inode, ll_d_count(new), new->d_flags);
582                         return new;
583                 }
584         }
585         rc = ll_d_init(de);
586         if (rc < 0)
587                 return ERR_PTR(rc);
588         d_add(de, inode);
589         CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
590                de, de->d_inode, ll_d_count(de), de->d_flags);
591         return de;
592 }
593
594 static int ll_lookup_it_finish(struct ptlrpc_request *request,
595                                struct lookup_intent *it,
596                                struct inode *parent, struct dentry **de)
597 {
598         struct inode             *inode = NULL;
599         __u64                     bits = 0;
600         int                       rc;
601         struct dentry *alias;
602         ENTRY;
603
604         /* NB 1 request reference will be taken away by ll_intent_lock()
605          * when I return */
606         CDEBUG(D_DENTRY, "it %p it_disposition %x\n", it,
607                it->it_disposition);
608         if (!it_disposition(it, DISP_LOOKUP_NEG)) {
609                 rc = ll_prep_inode(&inode, request, (*de)->d_sb, it);
610                 if (rc)
611                         RETURN(rc);
612
613                 ll_set_lock_data(ll_i2sbi(parent)->ll_md_exp, inode, it, &bits);
614
615                 /* We used to query real size from OSTs here, but actually
616                    this is not needed. For stat() calls size would be updated
617                    from subsequent do_revalidate()->ll_inode_revalidate_it() in
618                    2.4 and
619                    vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
620                    Everybody else who needs correct file size would call
621                    ll_glimpse_size or some equivalent themselves anyway.
622                    Also see bug 7198. */
623         }
624
625         /* Only hash *de if it is unhashed (new dentry).
626          * Atoimc_open may passin hashed dentries for open.
627          */
628         alias = ll_splice_alias(inode, *de);
629         if (IS_ERR(alias))
630                 GOTO(out, rc = PTR_ERR(alias));
631
632         *de = alias;
633
634         if (!it_disposition(it, DISP_LOOKUP_NEG)) {
635                 /* we have lookup look - unhide dentry */
636                 if (bits & MDS_INODELOCK_LOOKUP)
637                         d_lustre_revalidate(*de);
638         } else if (!it_disposition(it, DISP_OPEN_CREATE)) {
639                 /* If file created on server, don't depend on parent UPDATE
640                  * lock to unhide it. It is left hidden and next lookup can
641                  * find it in ll_splice_alias.
642                  */
643                 /* Check that parent has UPDATE lock. */
644                 struct lookup_intent parent_it = {
645                                         .it_op = IT_GETATTR,
646                                         .it_lock_handle = 0 };
647                 struct lu_fid   fid = ll_i2info(parent)->lli_fid;
648
649                 /* If it is striped directory, get the real stripe parent */
650                 if (unlikely(ll_i2info(parent)->lli_lsm_md != NULL)) {
651                         rc = md_get_fid_from_lsm(ll_i2mdexp(parent),
652                                                  ll_i2info(parent)->lli_lsm_md,
653                                                  (*de)->d_name.name,
654                                                  (*de)->d_name.len, &fid);
655                         if (rc != 0)
656                                 GOTO(out, rc);
657                 }
658
659                 if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it, &fid,
660                                        NULL)) {
661                         d_lustre_revalidate(*de);
662                         ll_intent_release(&parent_it);
663                 }
664         }
665
666         GOTO(out, rc = 0);
667
668 out:
669         if (rc != 0 && it->it_op & IT_OPEN)
670                 ll_open_cleanup((*de)->d_sb, request);
671
672         return rc;
673 }
674
675 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
676                                    struct lookup_intent *it,
677                                    void **secctx, __u32 *secctxlen)
678 {
679         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
680         struct dentry *save = dentry, *retval;
681         struct ptlrpc_request *req = NULL;
682         struct md_op_data *op_data = NULL;
683         __u32 opc;
684         int rc;
685         ENTRY;
686
687         if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
688                 RETURN(ERR_PTR(-ENAMETOOLONG));
689
690         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), intent=%s\n",
691                dentry->d_name.len, dentry->d_name.name,
692                PFID(ll_inode2fid(parent)), parent, LL_IT2STR(it));
693
694         if (d_mountpoint(dentry))
695                 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
696
697         if (it == NULL || it->it_op == IT_GETXATTR)
698                 it = &lookup_it;
699
700         if (it->it_op == IT_GETATTR && dentry_may_statahead(parent, dentry)) {
701                 rc = ll_statahead(parent, &dentry, 0);
702                 if (rc == 1)
703                         RETURN(dentry == save ? NULL : dentry);
704         }
705
706         if (it->it_op & IT_OPEN && it->it_flags & FMODE_WRITE &&
707             dentry->d_sb->s_flags & MS_RDONLY)
708                 RETURN(ERR_PTR(-EROFS));
709
710         if (it->it_op & IT_CREAT)
711                 opc = LUSTRE_OPC_CREATE;
712         else
713                 opc = LUSTRE_OPC_ANY;
714
715         op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
716                                      dentry->d_name.len, 0, opc, NULL);
717         if (IS_ERR(op_data))
718                 GOTO(out, retval = ERR_CAST(op_data));
719
720         /* enforce umask if acl disabled or MDS doesn't support umask */
721         if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
722                 it->it_create_mode &= ~current_umask();
723
724         if (it->it_op & IT_CREAT &&
725             ll_i2sbi(parent)->ll_flags & LL_SBI_FILE_SECCTX) {
726                 rc = ll_dentry_init_security(dentry, it->it_create_mode,
727                                              &dentry->d_name,
728                                              &op_data->op_file_secctx_name,
729                                              &op_data->op_file_secctx,
730                                              &op_data->op_file_secctx_size);
731                 if (rc < 0)
732                         GOTO(out, retval = ERR_PTR(rc));
733                 if (secctx != NULL)
734                         *secctx = op_data->op_file_secctx;
735                 if (secctxlen != NULL)
736                         *secctxlen = op_data->op_file_secctx_size;
737         }
738
739         rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
740                             &ll_md_blocking_ast, 0);
741         /* If the MDS allows the client to chgrp (CFS_SETGRP_PERM), but the
742          * client does not know which suppgid should be sent to the MDS, or
743          * some other(s) changed the target file's GID after this RPC sent
744          * to the MDS with the suppgid as the original GID, then we should
745          * try again with right suppgid. */
746         if (rc == -EACCES && it->it_op & IT_OPEN &&
747             it_disposition(it, DISP_OPEN_DENY)) {
748                 struct mdt_body *body;
749
750                 LASSERT(req != NULL);
751
752                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
753                 if (op_data->op_suppgids[0] == body->mbo_gid ||
754                     op_data->op_suppgids[1] == body->mbo_gid ||
755                     !in_group_p(make_kgid(&init_user_ns, body->mbo_gid)))
756                         GOTO(out, retval = ERR_PTR(-EACCES));
757
758                 fid_zero(&op_data->op_fid2);
759                 op_data->op_suppgids[1] = body->mbo_gid;
760                 ptlrpc_req_finished(req);
761                 req = NULL;
762                 ll_intent_release(it);
763                 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
764                                     &ll_md_blocking_ast, 0);
765         }
766
767         if (rc < 0)
768                 GOTO(out, retval = ERR_PTR(rc));
769
770         rc = ll_lookup_it_finish(req, it, parent, &dentry);
771         if (rc != 0) {
772                 ll_intent_release(it);
773                 GOTO(out, retval = ERR_PTR(rc));
774         }
775
776         if ((it->it_op & IT_OPEN) && dentry->d_inode &&
777             !S_ISREG(dentry->d_inode->i_mode) &&
778             !S_ISDIR(dentry->d_inode->i_mode)) {
779                 ll_release_openhandle(dentry, it);
780         }
781         ll_lookup_finish_locks(it, dentry);
782
783         GOTO(out, retval = (dentry == save) ? NULL : dentry);
784
785 out:
786         if (op_data != NULL && !IS_ERR(op_data)) {
787                 if (secctx != NULL && secctxlen != NULL) {
788                         /* caller needs sec ctx info, so reset it in op_data to
789                          * prevent it from being freed */
790                         op_data->op_file_secctx = NULL;
791                         op_data->op_file_secctx_size = 0;
792                 }
793                 ll_finish_md_op_data(op_data);
794         }
795
796         ptlrpc_req_finished(req);
797         return retval;
798 }
799
800 #ifdef HAVE_IOP_ATOMIC_OPEN
801 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
802                                    unsigned int flags)
803 {
804         struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
805         struct dentry *de;
806
807         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), flags=%u\n",
808                dentry->d_name.len, dentry->d_name.name,
809                PFID(ll_inode2fid(parent)), parent, flags);
810
811         /*
812          * Optimize away (CREATE && !OPEN). Let .create handle the race.
813          * but only if we have write permissions there, otherwise we need
814          * to proceed with lookup. LU-4185
815          */
816         if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN) &&
817             (inode_permission(parent, MAY_WRITE | MAY_EXEC) == 0))
818                 return NULL;
819
820         if (flags & (LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE))
821                 itp = NULL;
822         else
823                 itp = &it;
824         de = ll_lookup_it(parent, dentry, itp, NULL, NULL);
825
826         if (itp != NULL)
827                 ll_intent_release(itp);
828
829         return de;
830 }
831
832 /*
833  * For cached negative dentry and new dentry, handle lookup/create/open
834  * together.
835  */
836 static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
837                           struct file *file, unsigned open_flags,
838                           umode_t mode, int *opened)
839 {
840         struct lookup_intent *it;
841         struct dentry *de;
842         long long lookup_flags = LOOKUP_OPEN;
843         void *secctx = NULL;
844         __u32 secctxlen = 0;
845         int rc = 0;
846         ENTRY;
847
848         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), file %p,"
849                            "open_flags %x, mode %x opened %d\n",
850                dentry->d_name.len, dentry->d_name.name,
851                PFID(ll_inode2fid(dir)), dir, file, open_flags, mode, *opened);
852
853         /* Only negative dentries enter here */
854         LASSERT(dentry->d_inode == NULL);
855
856         if (!d_unhashed(dentry)) {
857                 /* A valid negative dentry that just passed revalidation,
858                  * there's little point to try and open it server-side,
859                  * even though there's a minuscule chance it might succeed.
860                  * Either way it's a valid race to just return -ENOENT here.
861                  */
862                 if (!(open_flags & O_CREAT))
863                         return -ENOENT;
864
865                 /* Otherwise we just unhash it to be rehashed afresh via
866                  * lookup if necessary
867                  */
868                 d_drop(dentry);
869         }
870
871         OBD_ALLOC(it, sizeof(*it));
872         if (!it)
873                 RETURN(-ENOMEM);
874
875         it->it_op = IT_OPEN;
876         if (open_flags & O_CREAT) {
877                 it->it_op |= IT_CREAT;
878                 lookup_flags |= LOOKUP_CREATE;
879         }
880         it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
881         it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
882         it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
883
884         /* Dentry added to dcache tree in ll_lookup_it */
885         de = ll_lookup_it(dir, dentry, it, &secctx, &secctxlen);
886         if (IS_ERR(de))
887                 rc = PTR_ERR(de);
888         else if (de != NULL)
889                 dentry = de;
890
891         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
892
893         if (!rc) {
894                 if (it_disposition(it, DISP_OPEN_CREATE)) {
895                         /* Dentry instantiated in ll_create_it. */
896                         rc = ll_create_it(dir, dentry, it, secctx, secctxlen);
897                         security_release_secctx(secctx, secctxlen);
898                         if (rc) {
899                                 /* We dget in ll_splice_alias. */
900                                 if (de != NULL)
901                                         dput(de);
902                                 goto out_release;
903                         }
904
905                         *opened |= FILE_CREATED;
906                 }
907                 if (dentry->d_inode && it_disposition(it, DISP_OPEN_OPEN)) {
908                         /* Open dentry. */
909                         if (S_ISFIFO(dentry->d_inode->i_mode)) {
910                                 /* We cannot call open here as it might
911                                  * deadlock. This case is unreachable in
912                                  * practice because of OBD_CONNECT_NODEVOH. */
913                                 rc = finish_no_open(file, de);
914                         } else {
915                                 file->private_data = it;
916                                 rc = finish_open(file, dentry, NULL, opened);
917                                 /* We dget in ll_splice_alias. finish_open takes
918                                  * care of dget for fd open.
919                                  */
920                                 if (de != NULL)
921                                         dput(de);
922                         }
923                 } else {
924                         rc = finish_no_open(file, de);
925                 }
926         }
927
928 out_release:
929         ll_intent_release(it);
930         OBD_FREE(it, sizeof(*it));
931
932         RETURN(rc);
933 }
934
935 #else /* !HAVE_IOP_ATOMIC_OPEN */
936 static struct lookup_intent *
937 ll_convert_intent(struct open_intent *oit, int lookup_flags, bool is_readonly)
938 {
939         struct lookup_intent *it;
940
941         OBD_ALLOC_PTR(it);
942         if (!it)
943                 return ERR_PTR(-ENOMEM);
944
945         if (lookup_flags & LOOKUP_OPEN) {
946                 it->it_op = IT_OPEN;
947                 /* Avoid file creation for ro bind mount point(is_readonly) */
948                 if ((lookup_flags & LOOKUP_CREATE) && !is_readonly)
949                         it->it_op |= IT_CREAT;
950                 it->it_create_mode = (oit->create_mode & S_IALLUGO) | S_IFREG;
951                 it->it_flags = ll_namei_to_lookup_intent_flag(oit->flags &
952                                                 ~(is_readonly ? O_CREAT : 0));
953                 it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
954         } else {
955                 it->it_op = IT_GETATTR;
956         }
957
958         return it;
959 }
960
961 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
962                                    struct nameidata *nd)
963 {
964         struct dentry *de;
965         ENTRY;
966
967         if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
968                 struct lookup_intent *it;
969
970                 if (ll_d2d(dentry) && ll_d2d(dentry)->lld_it) {
971                         it = ll_d2d(dentry)->lld_it;
972                         ll_d2d(dentry)->lld_it = NULL;
973                 } else {
974                         /*
975                          * Optimize away (CREATE && !OPEN). Let .create handle
976                          * the race. But only if we have write permissions
977                          * there, otherwise we need to proceed with lookup.
978                          * LU-4185
979                          */
980                         if ((nd->flags & LOOKUP_CREATE) &&
981                             !(nd->flags & LOOKUP_OPEN) &&
982                             (inode_permission(parent,
983                                               MAY_WRITE | MAY_EXEC) == 0))
984                                 RETURN(NULL);
985
986                         it = ll_convert_intent(&nd->intent.open, nd->flags,
987                                 (nd->path.mnt->mnt_flags & MNT_READONLY) ||
988                                 (nd->path.mnt->mnt_sb->s_flags & MS_RDONLY));
989                         if (IS_ERR(it))
990                                 RETURN((struct dentry *)it);
991                 }
992
993                 de = ll_lookup_it(parent, dentry, it, NULL, NULL);
994                 if (de)
995                         dentry = de;
996                 if ((nd->flags & LOOKUP_OPEN) && !IS_ERR(dentry)) { /* Open */
997                         if (dentry->d_inode &&
998                             it_disposition(it, DISP_OPEN_OPEN)) { /* nocreate */
999                                 if (S_ISFIFO(dentry->d_inode->i_mode)) {
1000                                         /* We cannot call open here as it might
1001                                          * deadlock. This case is unreachable in
1002                                          * practice because of
1003                                          * OBD_CONNECT_NODEVOH. */
1004                                 } else {
1005                                         struct file *filp;
1006
1007                                         nd->intent.open.file->private_data = it;
1008                                         filp = lookup_instantiate_filp(nd,
1009                                                                        dentry,
1010                                                                        NULL);
1011                                         if (IS_ERR(filp)) {
1012                                                 if (de)
1013                                                         dput(de);
1014                                                 de = (struct dentry *)filp;
1015                                         }
1016                                 }
1017                         } else if (it_disposition(it, DISP_OPEN_CREATE)) {
1018                                 /* XXX This can only reliably work on assumption
1019                                  * that there are NO hashed negative dentries.*/
1020                                 ll_d2d(dentry)->lld_it = it;
1021                                 it = NULL; /* Will be freed in ll_create_nd */
1022                                 /* We absolutely depend on ll_create_nd to be
1023                                  * called to not leak this intent and possible
1024                                  * data attached to it */
1025                         }
1026                 }
1027
1028                 if (it) {
1029                         ll_intent_release(it);
1030                         OBD_FREE(it, sizeof(*it));
1031                 }
1032         } else {
1033                 de = ll_lookup_it(parent, dentry, NULL, NULL, NULL);
1034         }
1035
1036         RETURN(de);
1037 }
1038 #endif /* HAVE_IOP_ATOMIC_OPEN */
1039
1040 /* We depend on "mode" being set with the proper file type/umask by now */
1041 static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
1042 {
1043         struct inode *inode = NULL;
1044         struct ptlrpc_request *request = NULL;
1045         struct ll_sb_info *sbi = ll_i2sbi(dir);
1046         int rc;
1047         ENTRY;
1048
1049         LASSERT(it && it->it_disposition);
1050
1051         LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
1052         request = it->it_request;
1053         it_clear_disposition(it, DISP_ENQ_CREATE_REF);
1054         rc = ll_prep_inode(&inode, request, dir->i_sb, it);
1055         if (rc)
1056                 GOTO(out, inode = ERR_PTR(rc));
1057
1058         /* Pause to allow for a race with concurrent access by fid */
1059         OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_NODE_PAUSE, cfs_fail_val);
1060
1061         /* We asked for a lock on the directory, but were granted a
1062          * lock on the inode.  Since we finally have an inode pointer,
1063          * stuff it in the lock. */
1064         CDEBUG(D_DLMTRACE, "setting l_ast_data to inode "DFID"(%p)\n",
1065                PFID(ll_inode2fid(inode)), inode);
1066         ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
1067         EXIT;
1068  out:
1069         ptlrpc_req_finished(request);
1070         return inode;
1071 }
1072
1073 /*
1074  * By the time this is called, we already have created the directory cache
1075  * entry for the new file, but it is so far negative - it has no inode.
1076  *
1077  * We defer creating the OBD object(s) until open, to keep the intent and
1078  * non-intent code paths similar, and also because we do not have the MDS
1079  * inode number before calling ll_create_node() (which is needed for LOV),
1080  * so we would need to do yet another RPC to the MDS to store the LOV EA
1081  * data on the MDS.  If needed, we would pass the PACKED lmm as data and
1082  * lmm_size in datalen (the MDS still has code which will handle that).
1083  *
1084  * If the create succeeds, we fill in the inode information
1085  * with d_instantiate().
1086  */
1087 static int ll_create_it(struct inode *dir, struct dentry *dentry,
1088                         struct lookup_intent *it,
1089                         void *secctx, __u32 secctxlen)
1090 {
1091         struct inode *inode;
1092         int rc = 0;
1093         ENTRY;
1094
1095         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), intent=%s\n",
1096                dentry->d_name.len, dentry->d_name.name,
1097                PFID(ll_inode2fid(dir)), dir, LL_IT2STR(it));
1098
1099         rc = it_open_error(DISP_OPEN_CREATE, it);
1100         if (rc)
1101                 RETURN(rc);
1102
1103         inode = ll_create_node(dir, it);
1104         if (IS_ERR(inode))
1105                 RETURN(PTR_ERR(inode));
1106
1107         if ((ll_i2sbi(inode)->ll_flags & LL_SBI_FILE_SECCTX) &&
1108             secctx != NULL) {
1109                 inode_lock(inode);
1110                 /* must be done before d_instantiate, because it calls
1111                  * security_d_instantiate, which means a getxattr if security
1112                  * context is not set yet */
1113                 rc = security_inode_notifysecctx(inode, secctx, secctxlen);
1114                 inode_unlock(inode);
1115                 if (rc)
1116                         RETURN(rc);
1117         }
1118
1119         d_instantiate(dentry, inode);
1120
1121         if (!(ll_i2sbi(inode)->ll_flags & LL_SBI_FILE_SECCTX)) {
1122                 rc = ll_inode_init_security(dentry, inode, dir);
1123                 if (rc)
1124                         RETURN(rc);
1125         }
1126
1127         RETURN(0);
1128 }
1129
1130 void ll_update_times(struct ptlrpc_request *request, struct inode *inode)
1131 {
1132         struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
1133                                                        &RMF_MDT_BODY);
1134
1135         LASSERT(body);
1136         if (body->mbo_valid & OBD_MD_FLMTIME &&
1137             body->mbo_mtime > LTIME_S(inode->i_mtime)) {
1138                 CDEBUG(D_INODE, "setting fid "DFID" mtime from %lu to %llu"
1139                        "\n", PFID(ll_inode2fid(inode)),
1140                        LTIME_S(inode->i_mtime), body->mbo_mtime);
1141                 LTIME_S(inode->i_mtime) = body->mbo_mtime;
1142         }
1143
1144         if (body->mbo_valid & OBD_MD_FLCTIME &&
1145             body->mbo_ctime > LTIME_S(inode->i_ctime))
1146                 LTIME_S(inode->i_ctime) = body->mbo_ctime;
1147 }
1148
1149 static int ll_new_node(struct inode *dir, struct dentry *dchild,
1150                        const char *tgt, umode_t mode, int rdev, __u32 opc)
1151 {
1152         struct qstr *name = &dchild->d_name;
1153         struct ptlrpc_request *request = NULL;
1154         struct md_op_data *op_data;
1155         struct inode *inode = NULL;
1156         struct ll_sb_info *sbi = ll_i2sbi(dir);
1157         int tgt_len = 0;
1158         int err;
1159
1160         ENTRY;
1161         if (unlikely(tgt != NULL))
1162                 tgt_len = strlen(tgt) + 1;
1163
1164 again:
1165         op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
1166                                      name->len, 0, opc, NULL);
1167         if (IS_ERR(op_data))
1168                 GOTO(err_exit, err = PTR_ERR(op_data));
1169
1170         if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
1171                 err = ll_dentry_init_security(dchild, mode, &dchild->d_name,
1172                                               &op_data->op_file_secctx_name,
1173                                               &op_data->op_file_secctx,
1174                                               &op_data->op_file_secctx_size);
1175                 if (err < 0)
1176                         GOTO(err_exit, err);
1177         }
1178
1179         err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
1180                         from_kuid(&init_user_ns, current_fsuid()),
1181                         from_kgid(&init_user_ns, current_fsgid()),
1182                         cfs_curproc_cap_pack(), rdev, &request);
1183         if (err < 0 && err != -EREMOTE)
1184                 GOTO(err_exit, err);
1185
1186         /* If the client doesn't know where to create a subdirectory (or
1187          * in case of a race that sends the RPC to the wrong MDS), the
1188          * MDS will return -EREMOTE and the client will fetch the layout
1189          * of the directory, then create the directory on the right MDT. */
1190         if (unlikely(err == -EREMOTE)) {
1191                 struct ll_inode_info    *lli = ll_i2info(dir);
1192                 struct lmv_user_md      *lum;
1193                 int                     lumsize;
1194                 int                     err2;
1195
1196                 ptlrpc_req_finished(request);
1197                 request = NULL;
1198
1199                 err2 = ll_dir_getstripe(dir, (void **)&lum, &lumsize, &request,
1200                                         OBD_MD_DEFAULT_MEA);
1201                 if (err2 == 0) {
1202                         /* Update stripe_offset and retry */
1203                         lli->lli_def_stripe_offset = lum->lum_stripe_offset;
1204                 } else if (err2 == -ENODATA &&
1205                            lli->lli_def_stripe_offset != -1) {
1206                         /* If there are no default stripe EA on the MDT, but the
1207                          * client has default stripe, then it probably means
1208                          * default stripe EA has just been deleted. */
1209                         lli->lli_def_stripe_offset = -1;
1210                 } else {
1211                         GOTO(err_exit, err);
1212                 }
1213
1214                 ptlrpc_req_finished(request);
1215                 request = NULL;
1216                 ll_finish_md_op_data(op_data);
1217                 goto again;
1218         }
1219
1220         ll_update_times(request, dir);
1221
1222         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_NEWNODE_PAUSE, cfs_fail_val);
1223
1224         err = ll_prep_inode(&inode, request, dchild->d_sb, NULL);
1225         if (err)
1226                 GOTO(err_exit, err);
1227
1228         if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
1229                 inode_lock(inode);
1230                 /* must be done before d_instantiate, because it calls
1231                  * security_d_instantiate, which means a getxattr if security
1232                  * context is not set yet */
1233                 err = security_inode_notifysecctx(inode,
1234                                                   op_data->op_file_secctx,
1235                                                   op_data->op_file_secctx_size);
1236                 inode_unlock(inode);
1237                 if (err)
1238                         GOTO(err_exit, err);
1239         }
1240
1241         d_instantiate(dchild, inode);
1242
1243         if (!(sbi->ll_flags & LL_SBI_FILE_SECCTX)) {
1244                 err = ll_inode_init_security(dchild, inode, dir);
1245                 if (err)
1246                         GOTO(err_exit, err);
1247         }
1248
1249         EXIT;
1250 err_exit:
1251         if (request != NULL)
1252                 ptlrpc_req_finished(request);
1253
1254         if (!IS_ERR_OR_NULL(op_data))
1255                 ll_finish_md_op_data(op_data);
1256
1257         return err;
1258 }
1259
1260 static int ll_mknod(struct inode *dir, struct dentry *dchild, ll_umode_t mode,
1261                     dev_t rdev)
1262 {
1263         struct qstr *name = &dchild->d_name;
1264         int err;
1265         ENTRY;
1266
1267         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p) mode %o dev %x\n",
1268                name->len, name->name, PFID(ll_inode2fid(dir)), dir,
1269                mode, rdev);
1270
1271         if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1272                 mode &= ~current_umask();
1273
1274         switch (mode & S_IFMT) {
1275         case 0:
1276                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1277         case S_IFREG:
1278         case S_IFCHR:
1279         case S_IFBLK:
1280         case S_IFIFO:
1281         case S_IFSOCK:
1282                 err = ll_new_node(dir, dchild, NULL, mode, old_encode_dev(rdev),
1283                                   LUSTRE_OPC_MKNOD);
1284                 break;
1285         case S_IFDIR:
1286                 err = -EPERM;
1287                 break;
1288         default:
1289                 err = -EINVAL;
1290         }
1291
1292         if (!err)
1293                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
1294
1295         RETURN(err);
1296 }
1297
1298 #ifdef HAVE_IOP_ATOMIC_OPEN
1299 /*
1300  * Plain create. Intent create is handled in atomic_open.
1301  */
1302 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1303                         umode_t mode, bool want_excl)
1304 {
1305         int rc;
1306
1307         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
1308
1309         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), "
1310                            "flags=%u, excl=%d\n", dentry->d_name.len,
1311                dentry->d_name.name, PFID(ll_inode2fid(dir)),
1312                dir, mode, want_excl);
1313
1314         /* Using mknod(2) to create a regular file is designed to not recognize
1315          * volatile file name, so we use ll_mknod() here. */
1316         rc = ll_mknod(dir, dentry, mode, 0);
1317
1318         ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1319
1320         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, unhashed %d\n",
1321                dentry->d_name.len, dentry->d_name.name, d_unhashed(dentry));
1322
1323         return rc;
1324 }
1325 #else /* !HAVE_IOP_ATOMIC_OPEN */
1326 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1327                         ll_umode_t mode, struct nameidata *nd)
1328 {
1329         struct ll_dentry_data *lld = ll_d2d(dentry);
1330         struct lookup_intent *it = NULL;
1331         int rc;
1332
1333         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
1334
1335         if (lld != NULL)
1336                 it = lld->lld_it;
1337
1338         if (!it) {
1339                 /* LU-8559: use LUSTRE_OPC_CREATE for non atomic open case
1340                  * so that volatile file name is recoginized.
1341                  * Mknod(2), however, is designed to not recognize volatile
1342                  * file name to avoid inode leak under orphan directory until
1343                  * MDT reboot */
1344                 return ll_new_node(dir, dentry, NULL, mode, 0,
1345                                    LUSTRE_OPC_CREATE);
1346         }
1347
1348         lld->lld_it = NULL;
1349
1350         /* Was there an error? Propagate it! */
1351         if (it->it_status) {
1352                 rc = it->it_status;
1353                 goto out;
1354         }
1355
1356         rc = ll_create_it(dir, dentry, it, NULL, 0);
1357         if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
1358                 struct file *filp;
1359
1360                 nd->intent.open.file->private_data = it;
1361                 filp = lookup_instantiate_filp(nd, dentry, NULL);
1362                 if (IS_ERR(filp))
1363                         rc = PTR_ERR(filp);
1364         }
1365
1366 out:
1367         ll_intent_release(it);
1368         OBD_FREE(it, sizeof(*it));
1369
1370         if (!rc)
1371                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1372
1373         return rc;
1374 }
1375 #endif /* HAVE_IOP_ATOMIC_OPEN */
1376
1377 static int ll_symlink(struct inode *dir, struct dentry *dchild,
1378                       const char *oldpath)
1379 {
1380         struct qstr *name = &dchild->d_name;
1381         int err;
1382         ENTRY;
1383
1384         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), target=%.*s\n",
1385                name->len, name->name, PFID(ll_inode2fid(dir)),
1386                dir, 3000, oldpath);
1387
1388         err = ll_new_node(dir, dchild, oldpath, S_IFLNK | S_IRWXUGO, 0,
1389                           LUSTRE_OPC_SYMLINK);
1390
1391         if (!err)
1392                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
1393
1394         RETURN(err);
1395 }
1396
1397 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1398                    struct dentry *new_dentry)
1399 {
1400         struct inode *src = old_dentry->d_inode;
1401         struct qstr *name = &new_dentry->d_name;
1402         struct ll_sb_info *sbi = ll_i2sbi(dir);
1403         struct ptlrpc_request *request = NULL;
1404         struct md_op_data *op_data;
1405         int err;
1406
1407         ENTRY;
1408         CDEBUG(D_VFSTRACE, "VFS Op: inode="DFID"(%p), dir="DFID"(%p), "
1409                "target=%.*s\n", PFID(ll_inode2fid(src)), src,
1410                PFID(ll_inode2fid(dir)), dir, name->len, name->name);
1411
1412         op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len,
1413                                      0, LUSTRE_OPC_ANY, NULL);
1414         if (IS_ERR(op_data))
1415                 RETURN(PTR_ERR(op_data));
1416
1417         err = md_link(sbi->ll_md_exp, op_data, &request);
1418         ll_finish_md_op_data(op_data);
1419         if (err)
1420                 GOTO(out, err);
1421
1422         ll_update_times(request, dir);
1423         ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
1424         EXIT;
1425 out:
1426         ptlrpc_req_finished(request);
1427         RETURN(err);
1428 }
1429
1430 static int ll_mkdir(struct inode *dir, struct dentry *dchild, ll_umode_t mode)
1431 {
1432         struct qstr *name = &dchild->d_name;
1433         int err;
1434         ENTRY;
1435
1436         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1437                name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1438
1439         if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1440                 mode &= ~current_umask();
1441
1442         mode = (mode & (S_IRWXUGO|S_ISVTX)) | S_IFDIR;
1443
1444         err = ll_new_node(dir, dchild, NULL, mode, 0, LUSTRE_OPC_MKDIR);
1445         if (err == 0)
1446                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
1447
1448         RETURN(err);
1449 }
1450
1451 static int ll_rmdir(struct inode *dir, struct dentry *dchild)
1452 {
1453         struct qstr *name = &dchild->d_name;
1454         struct ptlrpc_request *request = NULL;
1455         struct md_op_data *op_data;
1456         int rc;
1457         ENTRY;
1458
1459         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1460                name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1461
1462         if (unlikely(d_mountpoint(dchild)))
1463                 RETURN(-EBUSY);
1464
1465         op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
1466                                      S_IFDIR, LUSTRE_OPC_ANY, NULL);
1467         if (IS_ERR(op_data))
1468                 RETURN(PTR_ERR(op_data));
1469
1470         if (dchild->d_inode != NULL)
1471                 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1472
1473         op_data->op_fid2 = op_data->op_fid3;
1474         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1475         ll_finish_md_op_data(op_data);
1476         if (rc == 0) {
1477                 ll_update_times(request, dir);
1478                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1479         }
1480
1481         ptlrpc_req_finished(request);
1482         RETURN(rc);
1483 }
1484
1485 /**
1486  * Remove dir entry
1487  **/
1488 int ll_rmdir_entry(struct inode *dir, char *name, int namelen)
1489 {
1490         struct ptlrpc_request *request = NULL;
1491         struct md_op_data *op_data;
1492         int rc;
1493         ENTRY;
1494
1495         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1496                namelen, name, PFID(ll_inode2fid(dir)), dir);
1497
1498         op_data = ll_prep_md_op_data(NULL, dir, NULL, name, strlen(name),
1499                                      S_IFDIR, LUSTRE_OPC_ANY, NULL);
1500         if (IS_ERR(op_data))
1501                 RETURN(PTR_ERR(op_data));
1502         op_data->op_cli_flags |= CLI_RM_ENTRY;
1503         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1504         ll_finish_md_op_data(op_data);
1505         if (rc == 0) {
1506                 ll_update_times(request, dir);
1507                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1508         }
1509
1510         ptlrpc_req_finished(request);
1511         RETURN(rc);
1512 }
1513
1514 static int ll_unlink(struct inode *dir, struct dentry *dchild)
1515 {
1516         struct qstr *name = &dchild->d_name;
1517         struct ptlrpc_request *request = NULL;
1518         struct md_op_data *op_data;
1519         struct mdt_body *body;
1520         int rc;
1521         ENTRY;
1522         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1523                name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1524
1525         /*
1526          * XXX: unlink bind mountpoint maybe call to here,
1527          * just check it as vfs_unlink does.
1528          */
1529         if (unlikely(d_mountpoint(dchild)))
1530                 RETURN(-EBUSY);
1531
1532         op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0,
1533                                      LUSTRE_OPC_ANY, NULL);
1534         if (IS_ERR(op_data))
1535                 RETURN(PTR_ERR(op_data));
1536
1537         op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1538
1539         op_data->op_fid2 = op_data->op_fid3;
1540         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1541         ll_finish_md_op_data(op_data);
1542         if (rc)
1543                 GOTO(out, rc);
1544
1545         /*
1546          * The server puts attributes in on the last unlink, use them to update
1547          * the link count so the inode can be freed immediately.
1548          */
1549         body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
1550         if (body->mbo_valid & OBD_MD_FLNLINK)
1551                 set_nlink(dchild->d_inode, body->mbo_nlink);
1552
1553         ll_update_times(request, dir);
1554         ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
1555
1556 out:
1557         ptlrpc_req_finished(request);
1558         RETURN(rc);
1559 }
1560
1561 static int ll_rename(struct inode *src, struct dentry *src_dchild,
1562                      struct inode *tgt, struct dentry *tgt_dchild
1563 #ifdef HAVE_IOPS_RENAME_WITH_FLAGS
1564                      , unsigned int flags
1565 #endif
1566                      )
1567 {
1568         struct qstr *src_name = &src_dchild->d_name;
1569         struct qstr *tgt_name = &tgt_dchild->d_name;
1570         struct ptlrpc_request *request = NULL;
1571         struct ll_sb_info *sbi = ll_i2sbi(src);
1572         struct md_op_data *op_data;
1573         int err;
1574         ENTRY;
1575
1576 #ifdef HAVE_IOPS_RENAME_WITH_FLAGS
1577         if (flags)
1578                 return -EINVAL;
1579 #endif
1580
1581         CDEBUG(D_VFSTRACE, "VFS Op:oldname=%.*s, src_dir="DFID
1582                "(%p), newname=%.*s, tgt_dir="DFID"(%p)\n",
1583                src_name->len, src_name->name,
1584                PFID(ll_inode2fid(src)), src, tgt_name->len,
1585                tgt_name->name, PFID(ll_inode2fid(tgt)), tgt);
1586
1587         if (unlikely(d_mountpoint(src_dchild) || d_mountpoint(tgt_dchild)))
1588                 RETURN(-EBUSY);
1589
1590         op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0,
1591                                      LUSTRE_OPC_ANY, NULL);
1592         if (IS_ERR(op_data))
1593                 RETURN(PTR_ERR(op_data));
1594
1595         if (src_dchild->d_inode != NULL)
1596                 op_data->op_fid3 = *ll_inode2fid(src_dchild->d_inode);
1597
1598         if (tgt_dchild->d_inode != NULL)
1599                 op_data->op_fid4 = *ll_inode2fid(tgt_dchild->d_inode);
1600
1601         err = md_rename(sbi->ll_md_exp, op_data,
1602                         src_name->name, src_name->len,
1603                         tgt_name->name, tgt_name->len, &request);
1604         ll_finish_md_op_data(op_data);
1605         if (!err) {
1606                 ll_update_times(request, src);
1607                 ll_update_times(request, tgt);
1608                 ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1609         }
1610
1611         ptlrpc_req_finished(request);
1612
1613         if (err == 0)
1614                 d_move(src_dchild, tgt_dchild);
1615
1616         RETURN(err);
1617 }
1618
1619 const struct inode_operations ll_dir_inode_operations = {
1620         .mknod          = ll_mknod,
1621 #ifdef HAVE_IOP_ATOMIC_OPEN
1622         .atomic_open    = ll_atomic_open,
1623 #endif
1624         .lookup         = ll_lookup_nd,
1625         .create         = ll_create_nd,
1626         /* We need all these non-raw things for NFSD, to not patch it. */
1627         .unlink         = ll_unlink,
1628         .mkdir          = ll_mkdir,
1629         .rmdir          = ll_rmdir,
1630         .symlink        = ll_symlink,
1631         .link           = ll_link,
1632         .rename         = ll_rename,
1633         .setattr        = ll_setattr,
1634         .getattr        = ll_getattr,
1635         .permission     = ll_inode_permission,
1636 #ifdef HAVE_IOP_XATTR
1637         .setxattr       = ll_setxattr,
1638         .getxattr       = ll_getxattr,
1639         .removexattr    = ll_removexattr,
1640 #endif
1641         .listxattr      = ll_listxattr,
1642 #ifdef HAVE_IOP_GET_ACL
1643         .get_acl        = ll_get_acl,
1644 #endif
1645 #ifdef HAVE_IOP_SET_ACL
1646         .set_acl        = ll_set_acl,
1647 #endif
1648 };
1649
1650 const struct inode_operations ll_special_inode_operations = {
1651         .setattr        = ll_setattr,
1652         .getattr        = ll_getattr,
1653         .permission     = ll_inode_permission,
1654 #ifdef HAVE_IOP_XATTR
1655         .setxattr       = ll_setxattr,
1656         .getxattr       = ll_getxattr,
1657         .removexattr    = ll_removexattr,
1658 #endif
1659         .listxattr      = ll_listxattr,
1660 #ifdef HAVE_IOP_GET_ACL
1661         .get_acl        = ll_get_acl,
1662 #endif
1663 #ifdef HAVE_IOP_SET_ACL
1664         .set_acl        = ll_set_acl,
1665 #endif
1666 };