Whamcloud - gitweb
LU-15176 sec: present .fscrypt in subdir mount
[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  */
31
32 #include <linux/fs.h>
33 #include <linux/sched.h>
34 #include <linux/mm.h>
35 #include <linux/file.h>
36 #include <linux/quotaops.h>
37 #include <linux/highmem.h>
38 #include <linux/pagemap.h>
39 #include <linux/user_namespace.h>
40 #include <linux/uidgid.h>
41
42 #define DEBUG_SUBSYSTEM S_LLITE
43
44 #include <obd_support.h>
45 #include <lustre_fid.h>
46 #include <lustre_dlm.h>
47 #include "llite_internal.h"
48
49 #ifndef HAVE_USER_NAMESPACE_ARG
50 #define ll_create_nd(ns, dir, de, mode, ex)     ll_create_nd(dir, de, mode, ex)
51 #define ll_mkdir(ns, dir, dch, mode)            ll_mkdir(dir, dch, mode)
52 #define ll_mknod(ns, dir, dch, mode, rd)        ll_mknod(dir, dch, mode, rd)
53 #ifdef HAVE_IOPS_RENAME_WITH_FLAGS
54 #define ll_rename(ns, src, sdc, tgt, tdc, fl)   ll_rename(src, sdc, tgt, tdc, fl)
55 #else
56 #define ll_rename(ns, src, sdc, tgt, tdc)       ll_rename(src, sdc, tgt, tdc)
57 #endif /* HAVE_IOPS_RENAME_WITH_FLAGS */
58 #define ll_symlink(nd, dir, dch, old)           ll_symlink(dir, dch, old)
59 #endif
60
61 static int ll_create_it(struct inode *dir, struct dentry *dentry,
62                         struct lookup_intent *it,
63                         void *secctx, __u32 secctxlen, bool encrypt,
64                         void *encctx, __u32 encctxlen, unsigned int open_flags);
65
66 /* called from iget5_locked->find_inode() under inode_lock spinlock */
67 static int ll_test_inode(struct inode *inode, void *opaque)
68 {
69         struct ll_inode_info    *lli = ll_i2info(inode);
70         struct lustre_md        *md = opaque;
71
72         if (unlikely(!(md->body->mbo_valid & OBD_MD_FLID))) {
73                 CERROR("MDS body missing FID\n");
74                 return 0;
75         }
76
77         if (!lu_fid_eq(&lli->lli_fid, &md->body->mbo_fid1))
78                 return 0;
79
80         return 1;
81 }
82
83 static int ll_set_inode(struct inode *inode, void *opaque)
84 {
85         struct ll_inode_info *lli = ll_i2info(inode);
86         struct mdt_body *body = ((struct lustre_md *)opaque)->body;
87
88         if (unlikely(!(body->mbo_valid & OBD_MD_FLID))) {
89                 CERROR("MDS body missing FID\n");
90                 return -EINVAL;
91         }
92
93         lli->lli_fid = body->mbo_fid1;
94         if (unlikely(!(body->mbo_valid & OBD_MD_FLTYPE))) {
95                 CERROR("Can not initialize inode "DFID" without object type: "
96                        "valid = %#llx\n",
97                        PFID(&lli->lli_fid), body->mbo_valid);
98                 return -EINVAL;
99         }
100
101         inode->i_mode = (inode->i_mode & ~S_IFMT) | (body->mbo_mode & S_IFMT);
102         if (unlikely(inode->i_mode == 0)) {
103                 CERROR("Invalid inode "DFID" type\n", PFID(&lli->lli_fid));
104                 return -EINVAL;
105         }
106
107         ll_lli_init(lli);
108
109         return 0;
110 }
111
112
113 /**
114  * Get an inode by inode number(@hash), which is already instantiated by
115  * the intent lookup).
116  */
117 struct inode *ll_iget(struct super_block *sb, ino_t hash,
118                       struct lustre_md *md)
119 {
120         struct inode    *inode;
121         int             rc = 0;
122
123         ENTRY;
124
125         LASSERT(hash != 0);
126         inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
127         if (inode == NULL)
128                 RETURN(ERR_PTR(-ENOMEM));
129
130         if (inode->i_state & I_NEW) {
131                 rc = ll_read_inode2(inode, md);
132                 if (rc == 0 && S_ISREG(inode->i_mode) &&
133                     ll_i2info(inode)->lli_clob == NULL)
134                         rc = cl_file_inode_init(inode, md);
135
136                 if (rc != 0) {
137                         /* Let's clear directory lsm here, otherwise
138                          * make_bad_inode() will reset the inode mode
139                          * to regular, then ll_clear_inode will not
140                          * be able to clear lsm_md */
141                         if (S_ISDIR(inode->i_mode))
142                                 ll_dir_clear_lsm_md(inode);
143                         make_bad_inode(inode);
144                         unlock_new_inode(inode);
145                         iput(inode);
146                         inode = ERR_PTR(rc);
147                 } else {
148                         inode_has_no_xattr(inode);
149                         unlock_new_inode(inode);
150                 }
151         } else if (is_bad_inode(inode)) {
152                 iput(inode);
153                 inode = ERR_PTR(-ESTALE);
154         } else if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
155                 rc = ll_update_inode(inode, md);
156                 CDEBUG(D_VFSTRACE, "got inode: "DFID"(%p): rc = %d\n",
157                        PFID(&md->body->mbo_fid1), inode, rc);
158                 if (rc != 0) {
159                         if (S_ISDIR(inode->i_mode))
160                                 ll_dir_clear_lsm_md(inode);
161                         iput(inode);
162                         inode = ERR_PTR(rc);
163                 }
164         }
165
166         RETURN(inode);
167 }
168
169 /* mark negative sub file dentries invalid and prune unused dentries */
170 static void ll_prune_negative_children(struct inode *dir)
171 {
172         struct dentry *dentry;
173         struct dentry *child;
174
175         ENTRY;
176
177 restart:
178         spin_lock(&dir->i_lock);
179         hlist_for_each_entry(dentry, &dir->i_dentry, d_alias) {
180                 spin_lock(&dentry->d_lock);
181                 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
182                         if (child->d_inode)
183                                 continue;
184
185                         spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
186                         if (lld_is_init(child))
187                                 ll_d2d(child)->lld_invalid = 1;
188                         if (!ll_d_count(child)) {
189                                 dget_dlock(child);
190                                 __d_drop(child);
191                                 spin_unlock(&child->d_lock);
192                                 spin_unlock(&dentry->d_lock);
193                                 spin_unlock(&dir->i_lock);
194
195                                 CDEBUG(D_DENTRY, "prune negative dentry %pd\n",
196                                        child);
197
198                                 dput(child);
199                                 goto restart;
200                         }
201                         spin_unlock(&child->d_lock);
202                 }
203                 spin_unlock(&dentry->d_lock);
204         }
205         spin_unlock(&dir->i_lock);
206
207         EXIT;
208 }
209
210 int ll_test_inode_by_fid(struct inode *inode, void *opaque)
211 {
212         return lu_fid_eq(&ll_i2info(inode)->lli_fid, opaque);
213 }
214
215 static int ll_dom_lock_cancel(struct inode *inode, struct ldlm_lock *lock)
216 {
217         struct lu_env *env;
218         struct ll_inode_info *lli = ll_i2info(inode);
219         __u16 refcheck;
220         int rc;
221         ENTRY;
222
223         env = cl_env_get(&refcheck);
224         if (IS_ERR(env))
225                 RETURN(PTR_ERR(env));
226
227         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_REPLAY_PAUSE, cfs_fail_val);
228
229         /* reach MDC layer to flush data under  the DoM ldlm lock */
230         rc = cl_object_flush(env, lli->lli_clob, lock);
231         if (rc == -ENODATA) {
232                 CDEBUG(D_INODE, "inode "DFID" layout has no DoM stripe\n",
233                        PFID(ll_inode2fid(inode)));
234                 /* most likely result of layout change, do nothing */
235                 rc = 0;
236         }
237
238         cl_env_put(env, &refcheck);
239         RETURN(rc);
240 }
241
242 static void ll_lock_cancel_bits(struct ldlm_lock *lock, __u64 to_cancel)
243 {
244         struct inode *inode = ll_inode_from_resource_lock(lock);
245         struct ll_inode_info *lli;
246         __u64 bits = to_cancel;
247         int rc;
248
249         ENTRY;
250
251         if (!inode) {
252                 /* That means the inode is evicted most likely and may cause
253                  * the skipping of lock cleanups below, so print the message
254                  * about that in log.
255                  */
256                 if (lock->l_resource->lr_lvb_inode)
257                         LDLM_DEBUG(lock,
258                                    "can't take inode for the lock (%sevicted)\n",
259                                    lock->l_resource->lr_lvb_inode->i_state &
260                                    I_FREEING ? "" : "not ");
261                 RETURN_EXIT;
262         }
263
264         if (!fid_res_name_eq(ll_inode2fid(inode),
265                              &lock->l_resource->lr_name)) {
266                 LDLM_ERROR(lock, "data mismatch with object "DFID"(%p)",
267                            PFID(ll_inode2fid(inode)), inode);
268                 LBUG();
269         }
270
271         if (bits & MDS_INODELOCK_XATTR) {
272                 ll_xattr_cache_empty(inode);
273                 bits &= ~MDS_INODELOCK_XATTR;
274         }
275
276         /* For OPEN locks we differentiate between lock modes
277          * LCK_CR, LCK_CW, LCK_PR - bug 22891 */
278         if (bits & MDS_INODELOCK_OPEN)
279                 ll_have_md_lock(inode, &bits, lock->l_req_mode);
280
281         if (bits & MDS_INODELOCK_OPEN) {
282                 fmode_t fmode;
283
284                 switch (lock->l_req_mode) {
285                 case LCK_CW:
286                         fmode = FMODE_WRITE;
287                         break;
288                 case LCK_PR:
289                         fmode = FMODE_EXEC;
290                         break;
291                 case LCK_CR:
292                         fmode = FMODE_READ;
293                         break;
294                 default:
295                         LDLM_ERROR(lock, "bad lock mode for OPEN lock");
296                         LBUG();
297                 }
298
299                 ll_md_real_close(inode, fmode);
300
301                 bits &= ~MDS_INODELOCK_OPEN;
302         }
303
304         if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
305                     MDS_INODELOCK_LAYOUT | MDS_INODELOCK_PERM |
306                     MDS_INODELOCK_DOM))
307                 ll_have_md_lock(inode, &bits, LCK_MINMODE);
308
309         if (bits & MDS_INODELOCK_DOM) {
310                 rc =  ll_dom_lock_cancel(inode, lock);
311                 if (rc < 0)
312                         CDEBUG(D_INODE, "cannot flush DoM data "
313                                DFID": rc = %d\n",
314                                PFID(ll_inode2fid(inode)), rc);
315         }
316
317         if (bits & MDS_INODELOCK_LAYOUT) {
318                 struct cl_object_conf conf = {
319                         .coc_opc = OBJECT_CONF_INVALIDATE,
320                         .coc_inode = inode,
321                 };
322
323                 rc = ll_layout_conf(inode, &conf);
324                 if (rc < 0)
325                         CDEBUG(D_INODE, "cannot invalidate layout of "
326                                DFID": rc = %d\n",
327                                PFID(ll_inode2fid(inode)), rc);
328         }
329
330         lli = ll_i2info(inode);
331
332         if (bits & MDS_INODELOCK_UPDATE)
333                 set_bit(LLIF_UPDATE_ATIME, &lli->lli_flags);
334
335         if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
336                 CDEBUG(D_INODE, "invalidating inode "DFID" lli = %p, "
337                        "pfid  = "DFID"\n", PFID(ll_inode2fid(inode)),
338                        lli, PFID(&lli->lli_pfid));
339                 truncate_inode_pages(inode->i_mapping, 0);
340
341                 if (unlikely(!fid_is_zero(&lli->lli_pfid))) {
342                         struct inode *master_inode = NULL;
343                         unsigned long hash;
344
345                         /* This is slave inode, since all of the child dentry
346                          * is connected on the master inode, so we have to
347                          * invalidate the negative children on master inode */
348                         CDEBUG(D_INODE, "Invalidate s"DFID" m"DFID"\n",
349                                PFID(ll_inode2fid(inode)), PFID(&lli->lli_pfid));
350
351                         hash = cl_fid_build_ino(&lli->lli_pfid,
352                                         ll_need_32bit_api(ll_i2sbi(inode)));
353
354                         /* Do not lookup the inode with ilookup5, otherwise
355                          * it will cause dead lock,
356                          * 1. Client1 send chmod req to the MDT0, then on MDT0,
357                          * it enqueues master and all of its slaves lock,
358                          * (mdt_attr_set() -> mdt_lock_slaves()), after gets
359                          * master and stripe0 lock, it will send the enqueue
360                          * req (for stripe1) to MDT1, then MDT1 finds the lock
361                          * has been granted to client2. Then MDT1 sends blocking
362                          * ast to client2.
363                          * 2. At the same time, client2 tries to unlink
364                          * the striped dir (rm -rf striped_dir), and during
365                          * lookup, it will hold the master inode of the striped
366                          * directory, whose inode state is NEW, then tries to
367                          * revalidate all of its slaves, (ll_prep_inode()->
368                          * ll_iget()->ll_read_inode2()-> ll_update_inode().).
369                          * And it will be blocked on the server side because
370                          * of 1.
371                          * 3. Then the client get the blocking_ast req, cancel
372                          * the lock, but being blocked if using ->ilookup5()),
373                          * because master inode state is NEW. */
374                         master_inode = ilookup5_nowait(inode->i_sb, hash,
375                                                         ll_test_inode_by_fid,
376                                                         (void *)&lli->lli_pfid);
377                         if (master_inode) {
378                                 ll_prune_negative_children(master_inode);
379                                 iput(master_inode);
380                         }
381                 } else {
382                         ll_prune_negative_children(inode);
383                 }
384         }
385
386         if ((bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM)) &&
387             !is_root_inode(inode))
388                 ll_prune_aliases(inode);
389
390         if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM))
391                 forget_all_cached_acls(inode);
392
393         iput(inode);
394         RETURN_EXIT;
395 }
396
397 /* Check if the given lock may be downgraded instead of canceling and
398  * that convert is really needed. */
399 int ll_md_need_convert(struct ldlm_lock *lock)
400 {
401         struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
402         struct inode *inode;
403         __u64 wanted = lock->l_policy_data.l_inodebits.cancel_bits;
404         __u64 bits = lock->l_policy_data.l_inodebits.bits & ~wanted;
405         enum ldlm_mode mode = LCK_MINMODE;
406
407         if (!lock->l_conn_export ||
408             !exp_connect_lock_convert(lock->l_conn_export))
409                 return 0;
410
411         if (!wanted || !bits || ldlm_is_cancel(lock))
412                 return 0;
413
414         /* do not convert locks other than DOM for now */
415         if (!((bits | wanted) & MDS_INODELOCK_DOM))
416                 return 0;
417
418         /* We may have already remaining bits in some other lock so
419          * lock convert will leave us just extra lock for the same bit.
420          * Check if client has other lock with the same bits and the same
421          * or lower mode and don't convert if any.
422          */
423         switch (lock->l_req_mode) {
424         case LCK_PR:
425                 mode = LCK_PR;
426                 fallthrough;
427         case LCK_PW:
428                 mode |= LCK_CR;
429                 break;
430         case LCK_CW:
431                 mode = LCK_CW;
432                 fallthrough;
433         case LCK_CR:
434                 mode |= LCK_CR;
435                 break;
436         default:
437                 /* do not convert other modes */
438                 return 0;
439         }
440
441         /* is lock is too old to be converted? */
442         lock_res_and_lock(lock);
443         if (ktime_after(ktime_get(),
444                         ktime_add(lock->l_last_used, ns->ns_dirty_age_limit))) {
445                 unlock_res_and_lock(lock);
446                 return 0;
447         }
448         unlock_res_and_lock(lock);
449
450         inode = ll_inode_from_resource_lock(lock);
451         ll_have_md_lock(inode, &bits, mode);
452         iput(inode);
453         return !!(bits);
454 }
455
456 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *ld,
457                        void *data, int flag)
458 {
459         struct lustre_handle lockh;
460         int rc;
461
462         ENTRY;
463
464         switch (flag) {
465         case LDLM_CB_BLOCKING:
466         {
467                 __u64 cancel_flags = LCF_ASYNC;
468
469                 /* if lock convert is not needed then still have to
470                  * pass lock via ldlm_cli_convert() to keep all states
471                  * correct, set cancel_bits to full lock bits to cause
472                  * full cancel to happen.
473                  */
474                 if (!ll_md_need_convert(lock)) {
475                         lock_res_and_lock(lock);
476                         lock->l_policy_data.l_inodebits.cancel_bits =
477                                         lock->l_policy_data.l_inodebits.bits;
478                         unlock_res_and_lock(lock);
479                 }
480                 rc = ldlm_cli_convert(lock, cancel_flags);
481                 if (!rc)
482                         RETURN(0);
483                 /* continue with cancel otherwise */
484                 ldlm_lock2handle(lock, &lockh);
485                 rc = ldlm_cli_cancel(&lockh, cancel_flags);
486                 if (rc < 0) {
487                         CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
488                         RETURN(rc);
489                 }
490                 break;
491         }
492         case LDLM_CB_CANCELING:
493         {
494                 __u64 to_cancel = lock->l_policy_data.l_inodebits.bits;
495
496                 /* Nothing to do for non-granted locks */
497                 if (!ldlm_is_granted(lock))
498                         break;
499
500                 /* If 'ld' is supplied then bits to be cancelled are passed
501                  * implicitly by lock converting and cancel_bits from 'ld'
502                  * should be used. Otherwise full cancel is being performed
503                  * and lock inodebits are used.
504                  *
505                  * Note: we cannot rely on cancel_bits in lock itself at this
506                  * moment because they can be changed by concurrent thread,
507                  * so ldlm_cli_inodebits_convert() pass cancel bits implicitly
508                  * in 'ld' parameter.
509                  */
510                 if (ld) {
511                         /* partial bits cancel allowed only during convert */
512                         LASSERT(ldlm_is_converting(lock));
513                         /* mask cancel bits by lock bits so only no any unused
514                          * bits are passed to ll_lock_cancel_bits()
515                          */
516                         to_cancel &= ld->l_policy_data.l_inodebits.cancel_bits;
517                 }
518                 ll_lock_cancel_bits(lock, to_cancel);
519                 break;
520         }
521         default:
522                 LBUG();
523         }
524
525         RETURN(0);
526 }
527
528 __u32 ll_i2suppgid(struct inode *i)
529 {
530         if (in_group_p(i->i_gid))
531                 return (__u32)from_kgid(&init_user_ns, i->i_gid);
532         else
533                 return (__u32) __kgid_val(INVALID_GID);
534 }
535
536 /* Pack the required supplementary groups into the supplied groups array.
537  * If we don't need to use the groups from the target inode(s) then we
538  * instead pack one or more groups from the user's supplementary group
539  * array in case it might be useful.  Not needed if doing an MDS-side upcall. */
540 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
541 {
542         LASSERT(i1 != NULL);
543         LASSERT(suppgids != NULL);
544
545         suppgids[0] = ll_i2suppgid(i1);
546
547         if (i2)
548                 suppgids[1] = ll_i2suppgid(i2);
549         else
550                 suppgids[1] = -1;
551 }
552
553 /*
554  * try to reuse three types of dentry:
555  * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
556  *    by concurrent .revalidate).
557  * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
558  *    be cleared by others calling d_lustre_revalidate).
559  * 3. DISCONNECTED alias.
560  */
561 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
562 {
563         struct dentry *alias, *discon_alias, *invalid_alias;
564
565         if (hlist_empty(&inode->i_dentry))
566                 return NULL;
567
568         discon_alias = invalid_alias = NULL;
569
570         spin_lock(&inode->i_lock);
571         hlist_for_each_entry(alias, &inode->i_dentry, d_alias) {
572                 LASSERT(alias != dentry);
573
574                 spin_lock(&alias->d_lock);
575                 if ((alias->d_flags & DCACHE_DISCONNECTED) &&
576                     S_ISDIR(inode->i_mode))
577                         /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
578                         discon_alias = alias;
579                 else if (alias->d_parent == dentry->d_parent             &&
580                          alias->d_name.hash == dentry->d_name.hash       &&
581                          alias->d_name.len == dentry->d_name.len         &&
582                          memcmp(alias->d_name.name, dentry->d_name.name,
583                                 dentry->d_name.len) == 0)
584                         invalid_alias = alias;
585                 spin_unlock(&alias->d_lock);
586
587                 if (invalid_alias)
588                         break;
589         }
590         alias = invalid_alias ?: discon_alias ?: NULL;
591         if (alias) {
592                 spin_lock(&alias->d_lock);
593                 dget_dlock(alias);
594                 spin_unlock(&alias->d_lock);
595         }
596         spin_unlock(&inode->i_lock);
597
598         return alias;
599 }
600
601 /*
602  * Similar to d_splice_alias(), but lustre treats invalid alias
603  * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
604  */
605 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
606 {
607         struct dentry *new;
608
609         if (inode) {
610                 new = ll_find_alias(inode, de);
611                 if (new) {
612                         if (!ll_d_setup(new, true))
613                                 return ERR_PTR(-ENOMEM);
614                         d_move(new, de);
615                         iput(inode);
616                         CDEBUG(D_DENTRY,
617                                "Reuse dentry %p inode %p refc %d flags %#x\n",
618                               new, new->d_inode, ll_d_count(new), new->d_flags);
619                         return new;
620                 }
621         }
622         if (!ll_d_setup(de, false))
623                 return ERR_PTR(-ENOMEM);
624         d_add(de, inode);
625
626         /* this needs only to be done for foreign symlink dirs as
627          * DCACHE_SYMLINK_TYPE is already set by d_flags_for_inode()
628          * kernel routine for files with symlink ops (ie, real symlink)
629          */
630         if (inode && S_ISDIR(inode->i_mode) &&
631             ll_sbi_has_foreign_symlink(ll_i2sbi(inode)) &&
632 #ifdef HAVE_IOP_GET_LINK
633             inode->i_op->get_link) {
634 #else
635             inode->i_op->follow_link) {
636 #endif
637                 CDEBUG(D_INFO, "%s: inode "DFID": faking foreign dir as a symlink\n",
638                        ll_i2sbi(inode)->ll_fsname, PFID(ll_inode2fid(inode)));
639                 spin_lock(&de->d_lock);
640                 /* like d_flags_for_inode() already does for files */
641                 de->d_flags = (de->d_flags & ~DCACHE_ENTRY_TYPE) |
642                               DCACHE_SYMLINK_TYPE;
643                 spin_unlock(&de->d_lock);
644         }
645
646         CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
647                de, de->d_inode, ll_d_count(de), de->d_flags);
648         return de;
649 }
650
651 static int ll_lookup_it_finish(struct ptlrpc_request *request,
652                                struct lookup_intent *it,
653                                struct inode *parent, struct dentry **de,
654                                void *secctx, __u32 secctxlen,
655                                void *encctx, __u32 encctxlen,
656                                ktime_t kstart, bool encrypt)
657 {
658         struct inode             *inode = NULL;
659         __u64                     bits = 0;
660         int                       rc;
661         struct dentry *alias;
662         ENTRY;
663
664         /* NB 1 request reference will be taken away by ll_intent_lock()
665          * when I return */
666         CDEBUG(D_DENTRY, "it %p it_disposition %x\n", it,
667                it->it_disposition);
668         if (!it_disposition(it, DISP_LOOKUP_NEG)) {
669                 struct req_capsule *pill = &request->rq_pill;
670                 struct mdt_body *body = req_capsule_server_get(pill,
671                                                                &RMF_MDT_BODY);
672
673                 rc = ll_prep_inode(&inode, &request->rq_pill, (*de)->d_sb, it);
674                 if (rc)
675                         RETURN(rc);
676
677                 /* If encryption context was returned by MDT, put it in
678                  * inode now to save an extra getxattr and avoid deadlock.
679                  */
680                 if (body->mbo_valid & OBD_MD_ENCCTX) {
681                         encctx = req_capsule_server_get(pill, &RMF_FILE_ENCCTX);
682                         encctxlen = req_capsule_get_size(pill,
683                                                          &RMF_FILE_ENCCTX,
684                                                          RCL_SERVER);
685
686                         if (encctxlen) {
687                                 CDEBUG(D_SEC,
688                                        "server returned encryption ctx for "DFID"\n",
689                                        PFID(ll_inode2fid(inode)));
690                                 rc = ll_xattr_cache_insert(inode,
691                                                LL_XATTR_NAME_ENCRYPTION_CONTEXT,
692                                                            encctx, encctxlen);
693                                 if (rc)
694                                         CWARN("%s: cannot set enc ctx for "DFID": rc = %d\n",
695                                               ll_i2sbi(inode)->ll_fsname,
696                                               PFID(ll_inode2fid(inode)), rc);
697                                 else if (encrypt) {
698                                         rc = llcrypt_get_encryption_info(inode);
699                                         if (rc)
700                                                 CDEBUG(D_SEC,
701                                                  "cannot get enc info for "DFID": rc = %d\n",
702                                                  PFID(ll_inode2fid(inode)), rc);
703                                 }
704                         }
705                 }
706
707                 ll_set_lock_data(ll_i2sbi(parent)->ll_md_exp, inode, it, &bits);
708                 /* OPEN can return data if lock has DoM+LAYOUT bits set */
709                 if (it->it_op & IT_OPEN &&
710                     bits & MDS_INODELOCK_DOM && bits & MDS_INODELOCK_LAYOUT)
711                         ll_dom_finish_open(inode, request);
712
713                 /* We used to query real size from OSTs here, but actually
714                  * this is not needed. For stat() calls size would be updated
715                  * from subsequent do_revalidate()->ll_inode_revalidate_it() in
716                  * 2.4 and
717                  * vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
718                  * Everybody else who needs correct file size would call
719                  * ll_glimpse_size or some equivalent themselves anyway.
720                  * Also see bug 7198.
721                  */
722
723                 /* If security context was returned by MDT, put it in
724                  * inode now to save an extra getxattr from security hooks,
725                  * and avoid deadlock.
726                  */
727                 if (body->mbo_valid & OBD_MD_SECCTX) {
728                         secctx = req_capsule_server_get(pill, &RMF_FILE_SECCTX);
729                         secctxlen = req_capsule_get_size(pill,
730                                                            &RMF_FILE_SECCTX,
731                                                            RCL_SERVER);
732
733                         if (secctxlen)
734                                 CDEBUG(D_SEC, "server returned security context"
735                                        " for "DFID"\n",
736                                        PFID(ll_inode2fid(inode)));
737                 }
738
739                 if (secctx != NULL && secctxlen != 0) {
740                         /* no need to protect selinux_inode_setsecurity() by
741                          * inode_lock. Taking it would lead to a client deadlock
742                          * LU-13617
743                          */
744                         rc = security_inode_notifysecctx(inode, secctx,
745                                                          secctxlen);
746                         if (rc)
747                                 CWARN("%s: cannot set security context for "DFID": rc = %d\n",
748                                       ll_i2sbi(inode)->ll_fsname,
749                                       PFID(ll_inode2fid(inode)),
750                                       rc);
751                 }
752         }
753
754         /* Only hash *de if it is unhashed (new dentry).
755          * Atoimc_open may passin hashed dentries for open.
756          */
757         alias = ll_splice_alias(inode, *de);
758         if (IS_ERR(alias))
759                 GOTO(out, rc = PTR_ERR(alias));
760
761         *de = alias;
762
763         if (!it_disposition(it, DISP_LOOKUP_NEG)) {
764                 /* we have lookup look - unhide dentry */
765                 if (bits & MDS_INODELOCK_LOOKUP) {
766                         d_lustre_revalidate(*de);
767                         ll_update_dir_depth(parent, (*de)->d_inode);
768                 }
769
770                 if (encrypt) {
771                         rc = llcrypt_get_encryption_info(inode);
772                         if (rc)
773                                 GOTO(out, rc);
774                         if (!llcrypt_has_encryption_key(inode))
775                                 GOTO(out, rc = -ENOKEY);
776                 }
777         } else if (!it_disposition(it, DISP_OPEN_CREATE)) {
778                 /*
779                  * If file was created on the server, the dentry is revalidated
780                  * in ll_create_it if the lock allows for it.
781                  */
782                 /* Check that parent has UPDATE lock. */
783                 struct lookup_intent parent_it = {
784                                         .it_op = IT_GETATTR,
785                                         .it_lock_handle = 0 };
786                 struct lu_fid   fid = ll_i2info(parent)->lli_fid;
787
788                 /* If it is striped directory, get the real stripe parent */
789                 if (unlikely(ll_dir_striped(parent))) {
790                         rc = md_get_fid_from_lsm(ll_i2mdexp(parent),
791                                                  ll_i2info(parent)->lli_lsm_md,
792                                                  (*de)->d_name.name,
793                                                  (*de)->d_name.len, &fid);
794                         if (rc != 0)
795                                 GOTO(out, rc);
796                 }
797
798                 if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it, &fid,
799                                        NULL)) {
800                         d_lustre_revalidate(*de);
801                         ll_intent_release(&parent_it);
802                 }
803         }
804
805         if (it_disposition(it, DISP_OPEN_CREATE)) {
806                 ll_stats_ops_tally(ll_i2sbi(parent), LPROC_LL_MKNOD,
807                                    ktime_us_delta(ktime_get(), kstart));
808         }
809
810         GOTO(out, rc = 0);
811
812 out:
813         if (rc != 0 && it->it_op & IT_OPEN) {
814                 ll_intent_drop_lock(it);
815                 ll_open_cleanup((*de)->d_sb, &request->rq_pill);
816         }
817
818         return rc;
819 }
820
821 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
822                                    struct lookup_intent *it,
823                                    void **secctx, __u32 *secctxlen,
824                                    struct pcc_create_attach *pca,
825                                    bool encrypt,
826                                    void **encctx, __u32 *encctxlen)
827 {
828         ktime_t kstart = ktime_get();
829         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
830         struct dentry *save = dentry, *retval;
831         struct ptlrpc_request *req = NULL;
832         struct md_op_data *op_data = NULL;
833         struct lov_user_md *lum = NULL;
834         __u32 opc;
835         int rc;
836         char secctx_name[XATTR_NAME_MAX + 1];
837         struct llcrypt_name fname;
838         struct lu_fid fid;
839         ENTRY;
840
841         if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
842                 RETURN(ERR_PTR(-ENAMETOOLONG));
843
844         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p), intent=%s\n",
845                dentry, PFID(ll_inode2fid(parent)), parent, LL_IT2STR(it));
846
847         if (d_mountpoint(dentry))
848                 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
849
850         if (it == NULL || it->it_op == IT_GETXATTR)
851                 it = &lookup_it;
852
853         if (it->it_op == IT_GETATTR && dentry_may_statahead(parent, dentry)) {
854                 rc = ll_revalidate_statahead(parent, &dentry, 0);
855                 if (rc == 1)
856                         RETURN(dentry == save ? NULL : dentry);
857         }
858
859         if (it->it_op & IT_OPEN && it->it_flags & FMODE_WRITE &&
860             dentry->d_sb->s_flags & SB_RDONLY)
861                 RETURN(ERR_PTR(-EROFS));
862
863         if (it->it_op & IT_CREAT)
864                 opc = LUSTRE_OPC_CREATE;
865         else
866                 opc = LUSTRE_OPC_LOOKUP;
867
868         /* Here we should be calling llcrypt_prepare_lookup(). But it installs a
869          * custom ->d_revalidate() method, so we lose ll_d_ops.
870          * To workaround this, call ll_setup_filename() and do the rest
871          * manually. Also make a copy of llcrypt_d_revalidate() (unfortunately
872          * not exported function) and call it from ll_revalidate_dentry(), to
873          * ensure we do not cache stale dentries after a key has been added.
874          */
875         rc = ll_setup_filename(parent, &dentry->d_name, 1, &fname, &fid);
876         if ((!rc || rc == -ENOENT) && fname.is_ciphertext_name) {
877                 spin_lock(&dentry->d_lock);
878                 dentry->d_flags |= DCACHE_ENCRYPTED_NAME;
879                 spin_unlock(&dentry->d_lock);
880         }
881         if (rc == -ENOENT)
882                 RETURN(NULL);
883         if (rc)
884                 RETURN(ERR_PTR(rc));
885
886         op_data = ll_prep_md_op_data(NULL, parent, NULL, fname.disk_name.name,
887                                      fname.disk_name.len, 0, opc, NULL);
888         if (IS_ERR(op_data)) {
889                 llcrypt_free_filename(&fname);
890                 RETURN(ERR_CAST(op_data));
891         }
892         if (!fid_is_zero(&fid)) {
893                 op_data->op_fid2 = fid;
894                 op_data->op_bias = MDS_FID_OP;
895                 if (it->it_op & IT_OPEN)
896                         it->it_flags |= MDS_OPEN_BY_FID;
897         }
898
899         /* enforce umask if acl disabled or MDS doesn't support umask */
900         if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
901                 it->it_create_mode &= ~current_umask();
902
903         if (it->it_op & IT_CREAT &&
904             test_bit(LL_SBI_FILE_SECCTX, ll_i2sbi(parent)->ll_flags)) {
905                 rc = ll_dentry_init_security(dentry, it->it_create_mode,
906                                              &dentry->d_name,
907                                              &op_data->op_file_secctx_name,
908                                              &op_data->op_file_secctx,
909                                              &op_data->op_file_secctx_size);
910                 if (rc < 0)
911                         GOTO(out, retval = ERR_PTR(rc));
912                 if (secctx != NULL)
913                         *secctx = op_data->op_file_secctx;
914                 if (secctxlen != NULL)
915                         *secctxlen = op_data->op_file_secctx_size;
916         } else {
917                 if (secctx != NULL)
918                         *secctx = NULL;
919                 if (secctxlen != NULL)
920                         *secctxlen = 0;
921         }
922         if (it->it_op & IT_CREAT && encrypt) {
923                 if (unlikely(filename_is_volatile(dentry->d_name.name,
924                                                   dentry->d_name.len, NULL))) {
925                         /* get encryption context from reference file */
926                         int ctx_size = LLCRYPT_ENC_CTX_SIZE;
927                         struct lustre_sb_info *lsi;
928                         struct file *ref_file;
929                         struct inode *ref_inode;
930                         void *ctx;
931
932                         rc = volatile_ref_file(dentry->d_name.name,
933                                                dentry->d_name.len,
934                                                &ref_file);
935                         if (rc)
936                                 GOTO(out, retval = ERR_PTR(rc));
937
938                         ref_inode = file_inode(ref_file);
939                         if (!ref_inode) {
940                                 fput(ref_file);
941                                 GOTO(inherit, rc = -EINVAL);
942                         }
943
944                         lsi = s2lsi(ref_inode->i_sb);
945
946 getctx:
947                         OBD_ALLOC(ctx, ctx_size);
948                         if (!ctx)
949                                 GOTO(out, retval = ERR_PTR(-ENOMEM));
950
951 #ifdef CONFIG_LL_ENCRYPTION
952                         rc = lsi->lsi_cop->get_context(ref_inode,
953                                                        ctx, ctx_size);
954 #elif defined(HAVE_LUSTRE_CRYPTO)
955                         rc = ref_inode->i_sb->s_cop->get_context(ref_inode,
956                                                                  ctx, ctx_size);
957 #else
958                         rc = -ENODATA;
959 #endif
960                         if (rc == -ERANGE) {
961                                 OBD_FREE(ctx, ctx_size);
962                                 ctx_size *= 2;
963                                 goto getctx;
964                         }
965                         fput(ref_file);
966                         if (rc < 0) {
967                                 OBD_FREE(ctx, ctx_size);
968                                 GOTO(inherit, rc);
969                         }
970
971                         op_data->op_file_encctx_size = rc;
972                         if (rc == ctx_size) {
973                                 op_data->op_file_encctx = ctx;
974                         } else {
975                                 OBD_ALLOC(op_data->op_file_encctx,
976                                           op_data->op_file_encctx_size);
977                                 if (!op_data->op_file_encctx) {
978                                         OBD_FREE(ctx, ctx_size);
979                                         GOTO(out, retval = ERR_PTR(-ENOMEM));
980                                 }
981                                 memcpy(op_data->op_file_encctx, ctx,
982                                        op_data->op_file_encctx_size);
983                                 OBD_FREE(ctx, ctx_size);
984                         }
985                 } else {
986 inherit:
987                         rc = llcrypt_inherit_context(parent, NULL, op_data,
988                                                      false);
989                         if (rc)
990                                 GOTO(out, retval = ERR_PTR(rc));
991                 }
992                 if (encctx != NULL)
993                         *encctx = op_data->op_file_encctx;
994                 if (encctxlen != NULL)
995                         *encctxlen = op_data->op_file_encctx_size;
996         } else {
997                 if (encctx != NULL)
998                         *encctx = NULL;
999                 if (encctxlen != NULL)
1000                         *encctxlen = 0;
1001         }
1002
1003         /* ask for security context upon intent */
1004         if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_OPEN)) {
1005                 /* get name of security xattr to request to server */
1006                 rc = ll_listsecurity(parent, secctx_name,
1007                                      sizeof(secctx_name));
1008                 if (rc < 0) {
1009                         CDEBUG(D_SEC, "cannot get security xattr name for "
1010                                DFID": rc = %d\n",
1011                                PFID(ll_inode2fid(parent)), rc);
1012                 } else if (rc > 0) {
1013                         op_data->op_file_secctx_name = secctx_name;
1014                         op_data->op_file_secctx_name_size = rc;
1015                         CDEBUG(D_SEC, "'%.*s' is security xattr for "DFID"\n",
1016                                rc, secctx_name, PFID(ll_inode2fid(parent)));
1017                 }
1018         }
1019
1020         if (pca && pca->pca_dataset) {
1021                 OBD_ALLOC_PTR(lum);
1022                 if (lum == NULL)
1023                         GOTO(out, retval = ERR_PTR(-ENOMEM));
1024
1025                 lum->lmm_magic = LOV_USER_MAGIC_V1;
1026                 lum->lmm_pattern = LOV_PATTERN_F_RELEASED | LOV_PATTERN_RAID0;
1027                 op_data->op_data = lum;
1028                 op_data->op_data_size = sizeof(*lum);
1029                 op_data->op_archive_id = pca->pca_dataset->pccd_rwid;
1030                 it->it_flags |= MDS_OPEN_PCC;
1031         }
1032
1033         rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
1034                             &ll_md_blocking_ast, 0);
1035         /* If the MDS allows the client to chgrp (CFS_SETGRP_PERM), but the
1036          * client does not know which suppgid should be sent to the MDS, or
1037          * some other(s) changed the target file's GID after this RPC sent
1038          * to the MDS with the suppgid as the original GID, then we should
1039          * try again with right suppgid. */
1040         if (rc == -EACCES && it->it_op & IT_OPEN &&
1041             it_disposition(it, DISP_OPEN_DENY)) {
1042                 struct mdt_body *body;
1043
1044                 LASSERT(req != NULL);
1045
1046                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1047                 if (op_data->op_suppgids[0] == body->mbo_gid ||
1048                     op_data->op_suppgids[1] == body->mbo_gid ||
1049                     !in_group_p(make_kgid(&init_user_ns, body->mbo_gid)))
1050                         GOTO(out, retval = ERR_PTR(-EACCES));
1051
1052                 fid_zero(&op_data->op_fid2);
1053                 op_data->op_suppgids[1] = body->mbo_gid;
1054                 ptlrpc_req_finished(req);
1055                 req = NULL;
1056                 ll_intent_release(it);
1057                 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
1058                                     &ll_md_blocking_ast, 0);
1059         }
1060
1061         if (rc < 0)
1062                 GOTO(out, retval = ERR_PTR(rc));
1063
1064         if (pca && pca->pca_dataset) {
1065                 rc = pcc_inode_create(parent->i_sb, pca->pca_dataset,
1066                                       &op_data->op_fid2,
1067                                       &pca->pca_dentry);
1068                 if (rc)
1069                         GOTO(out, retval = ERR_PTR(rc));
1070         }
1071
1072         /* dir layout may change */
1073         ll_unlock_md_op_lsm(op_data);
1074         rc = ll_lookup_it_finish(req, it, parent, &dentry,
1075                                  secctx != NULL ? *secctx : NULL,
1076                                  secctxlen != NULL ? *secctxlen : 0,
1077                                  encctx != NULL ? *encctx : NULL,
1078                                  encctxlen != NULL ? *encctxlen : 0,
1079                                  kstart, encrypt);
1080         if (rc != 0) {
1081                 ll_intent_release(it);
1082                 GOTO(out, retval = ERR_PTR(rc));
1083         }
1084
1085         if ((it->it_op & IT_OPEN) && dentry->d_inode &&
1086             !S_ISREG(dentry->d_inode->i_mode) &&
1087             !S_ISDIR(dentry->d_inode->i_mode)) {
1088                 ll_release_openhandle(dentry, it);
1089         }
1090         ll_lookup_finish_locks(it, dentry);
1091
1092         GOTO(out, retval = (dentry == save) ? NULL : dentry);
1093
1094 out:
1095         if (op_data != NULL && !IS_ERR(op_data)) {
1096                 if (secctx != NULL && secctxlen != NULL) {
1097                         /* caller needs sec ctx info, so reset it in op_data to
1098                          * prevent it from being freed */
1099                         op_data->op_file_secctx = NULL;
1100                         op_data->op_file_secctx_size = 0;
1101                 }
1102                 if (encctx != NULL && encctxlen != NULL &&
1103                     it->it_op & IT_CREAT && encrypt) {
1104                         /* caller needs enc ctx info, so reset it in op_data to
1105                          * prevent it from being freed
1106                          */
1107                         op_data->op_file_encctx = NULL;
1108                         op_data->op_file_encctx_size = 0;
1109                 }
1110                 llcrypt_free_filename(&fname);
1111                 ll_finish_md_op_data(op_data);
1112         }
1113
1114         if (lum != NULL)
1115                 OBD_FREE_PTR(lum);
1116
1117         ptlrpc_req_finished(req);
1118         return retval;
1119 }
1120
1121 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
1122                                    unsigned int flags)
1123 {
1124         struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
1125         struct dentry *de;
1126
1127         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p), flags=%u\n",
1128                dentry, PFID(ll_inode2fid(parent)), parent, flags);
1129
1130         /*
1131          * Optimize away (CREATE && !OPEN). Let .create handle the race.
1132          * but only if we have write permissions there, otherwise we need
1133          * to proceed with lookup. LU-4185
1134          */
1135         if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN) &&
1136             (inode_permission(&init_user_ns,
1137                               parent, MAY_WRITE | MAY_EXEC) == 0))
1138                 return NULL;
1139
1140         if (flags & (LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE))
1141                 itp = NULL;
1142         else
1143                 itp = &it;
1144         de = ll_lookup_it(parent, dentry, itp, NULL, NULL, NULL, false,
1145                           NULL, NULL);
1146
1147         if (itp != NULL)
1148                 ll_intent_release(itp);
1149
1150         return de;
1151 }
1152
1153 #ifdef FMODE_CREATED /* added in Linux v4.18-rc1-20-g73a09dd */
1154 # define ll_is_opened(o, f)             ((f)->f_mode & FMODE_OPENED)
1155 # define ll_finish_open(f, d, o)        finish_open((f), (d), NULL)
1156 # define ll_last_arg
1157 # define ll_set_created(o, f)                                           \
1158 do {                                                                    \
1159         (f)->f_mode |= FMODE_CREATED;                                   \
1160 } while (0)
1161
1162 #else
1163 # define ll_is_opened(o, f)             (*(o))
1164 # define ll_finish_open(f, d, o)        finish_open((f), (d), NULL, (o))
1165 # define ll_last_arg                    , int *opened
1166 # define ll_set_created(o, f)                                           \
1167 do {                                                                    \
1168         *(o) |= FILE_CREATED;                                           \
1169 } while (0)
1170
1171 #endif
1172
1173 /*
1174  * For cached negative dentry and new dentry, handle lookup/create/open
1175  * together.
1176  */
1177 static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
1178                           struct file *file, unsigned open_flags,
1179                           umode_t mode ll_last_arg)
1180 {
1181         struct lookup_intent *it;
1182         struct dentry *de;
1183         long long lookup_flags = LOOKUP_OPEN;
1184         void *secctx = NULL;
1185         __u32 secctxlen = 0;
1186         void *encctx = NULL;
1187         __u32 encctxlen = 0;
1188         struct ll_sb_info *sbi = NULL;
1189         struct pcc_create_attach pca = { NULL, NULL };
1190         bool encrypt = false;
1191         int rc = 0;
1192         ENTRY;
1193
1194         CDEBUG(D_VFSTRACE,
1195                "VFS Op:name=%pd, dir="DFID"(%p), file %p, open_flags %x, mode %x opened %d\n",
1196                dentry, PFID(ll_inode2fid(dir)), dir, file, open_flags, mode,
1197                ll_is_opened(opened, file));
1198
1199         /* Only negative dentries enter here */
1200         LASSERT(dentry->d_inode == NULL);
1201
1202         if (!d_unhashed(dentry)) {
1203                 /* A valid negative dentry that just passed revalidation,
1204                  * there's little point to try and open it server-side,
1205                  * even though there's a minuscule chance it might succeed.
1206                  * Either way it's a valid race to just return -ENOENT here.
1207                  */
1208                 if (!(open_flags & O_CREAT))
1209                         return -ENOENT;
1210
1211                 /* Otherwise we just unhash it to be rehashed afresh via
1212                  * lookup if necessary
1213                  */
1214                 d_drop(dentry);
1215         }
1216
1217         OBD_ALLOC(it, sizeof(*it));
1218         if (!it)
1219                 RETURN(-ENOMEM);
1220
1221         it->it_op = IT_OPEN;
1222         if (open_flags & O_CREAT) {
1223                 it->it_op |= IT_CREAT;
1224                 lookup_flags |= LOOKUP_CREATE;
1225                 sbi = ll_i2sbi(dir);
1226                 /* Volatile file is used for HSM restore, so do not use PCC */
1227                 if (!filename_is_volatile(dentry->d_name.name,
1228                                           dentry->d_name.len, NULL)) {
1229                         struct pcc_matcher item;
1230                         struct pcc_dataset *dataset;
1231
1232                         item.pm_uid = from_kuid(&init_user_ns, current_uid());
1233                         item.pm_gid = from_kgid(&init_user_ns, current_gid());
1234                         item.pm_projid = ll_i2info(dir)->lli_projid;
1235                         item.pm_name = &dentry->d_name;
1236                         dataset = pcc_dataset_match_get(&sbi->ll_pcc_super,
1237                                                         &item);
1238                         pca.pca_dataset = dataset;
1239                 }
1240         }
1241         it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
1242         it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
1243         it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
1244
1245         if (ll_sbi_has_encrypt(ll_i2sbi(dir)) && IS_ENCRYPTED(dir)) {
1246                 /* in case of create, this is going to be a regular file because
1247                  * we set S_IFREG bit on it->it_create_mode above
1248                  */
1249                 rc = llcrypt_get_encryption_info(dir);
1250                 if (rc)
1251                         GOTO(out_release, rc);
1252                 if (open_flags & O_CREAT) {
1253                         /* For migration or mirroring without enc key, we still
1254                          * need to be able to create a volatile file.
1255                          */
1256                         if (!llcrypt_has_encryption_key(dir) &&
1257                             (!filename_is_volatile(dentry->d_name.name,
1258                                                    dentry->d_name.len, NULL) ||
1259                             (open_flags & O_FILE_ENC) != O_FILE_ENC ||
1260                             !(open_flags & O_DIRECT)))
1261                                 GOTO(out_release, rc = -ENOKEY);
1262                         encrypt = true;
1263                 }
1264         }
1265
1266         OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE2, cfs_fail_val);
1267
1268         /* We can only arrive at this path when we have no inode, so
1269          * we only need to request open lock if it was requested
1270          * for every open
1271          */
1272         if (ll_i2sbi(dir)->ll_oc_thrsh_count == 1 &&
1273             exp_connect_flags2(ll_i2mdexp(dir)) &
1274             OBD_CONNECT2_ATOMIC_OPEN_LOCK)
1275                 it->it_flags |= MDS_OPEN_LOCK;
1276
1277         /* Dentry added to dcache tree in ll_lookup_it */
1278         de = ll_lookup_it(dir, dentry, it, &secctx, &secctxlen, &pca, encrypt,
1279                           &encctx, &encctxlen);
1280         if (IS_ERR(de))
1281                 rc = PTR_ERR(de);
1282         else if (de != NULL)
1283                 dentry = de;
1284
1285         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
1286
1287         if (!rc) {
1288                 if (it_disposition(it, DISP_OPEN_CREATE)) {
1289                         /* Dentry instantiated in ll_create_it. */
1290                         rc = ll_create_it(dir, dentry, it, secctx, secctxlen,
1291                                           encrypt, encctx, encctxlen,
1292                                           open_flags);
1293                         ll_security_release_secctx(secctx, secctxlen);
1294                         llcrypt_free_ctx(encctx, encctxlen);
1295                         if (rc) {
1296                                 /* We dget in ll_splice_alias. */
1297                                 if (de != NULL)
1298                                         dput(de);
1299                                 goto out_release;
1300                         }
1301
1302                         rc = pcc_inode_create_fini(dentry->d_inode, &pca);
1303                         if (rc) {
1304                                 if (de != NULL)
1305                                         dput(de);
1306                                 GOTO(out_release, rc);
1307                         }
1308
1309                         ll_set_created(opened, file);
1310                 } else {
1311                         /* Open the file with O_CREAT, but the file already
1312                          * existed on MDT. This may happend in the case that
1313                          * the LOOKUP ibits lock is revoked and the
1314                          * corresponding dentry cache is deleted.
1315                          * i.e. In the current Lustre, the truncate operation
1316                          * will revoke the LOOKUP ibits lock, and the file
1317                          * dentry cache will be invalidated. The following open
1318                          * with O_CREAT flag will call into ->atomic_open, the
1319                          * file was wrongly though as newly created file and
1320                          * try to auto cache the file. So after client knows it
1321                          * is not a DISP_OPEN_CREATE, it should cleanup the
1322                          * already created PCC copy.
1323                          */
1324                         pcc_create_attach_cleanup(dir->i_sb, &pca);
1325
1326                         if (open_flags & O_CREAT && encrypt &&
1327                             dentry->d_inode) {
1328                                 rc = ll_set_encflags(dentry->d_inode, encctx,
1329                                                      encctxlen, true);
1330                                 llcrypt_free_ctx(encctx, encctxlen);
1331                                 if (rc)
1332                                         GOTO(out_release, rc);
1333                         }
1334                 }
1335
1336                 /* check also if a foreign file is openable */
1337                 if (dentry->d_inode && it_disposition(it, DISP_OPEN_OPEN) &&
1338                     ll_foreign_is_openable(dentry, open_flags)) {
1339                         /* Open dentry. */
1340                         if (S_ISFIFO(dentry->d_inode->i_mode)) {
1341                                 /* We cannot call open here as it might
1342                                  * deadlock. This case is unreachable in
1343                                  * practice because of OBD_CONNECT_NODEVOH. */
1344                                 rc = finish_no_open(file, de);
1345                         } else {
1346                                 file->private_data = it;
1347                                 rc = ll_finish_open(file, dentry, opened);
1348                                 /* We dget in ll_splice_alias. finish_open takes
1349                                  * care of dget for fd open.
1350                                  */
1351                                 if (de != NULL)
1352                                         dput(de);
1353                         }
1354                 } else {
1355                         rc = finish_no_open(file, de);
1356                 }
1357         } else {
1358                 pcc_create_attach_cleanup(dir->i_sb, &pca);
1359         }
1360
1361 out_release:
1362         ll_intent_release(it);
1363         OBD_FREE(it, sizeof(*it));
1364
1365         RETURN(rc);
1366 }
1367
1368 /* We depend on "mode" being set with the proper file type/umask by now */
1369 static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
1370 {
1371         struct inode *inode = NULL;
1372         struct ptlrpc_request *request = NULL;
1373         struct ll_sb_info *sbi = ll_i2sbi(dir);
1374         int rc;
1375         ENTRY;
1376
1377         LASSERT(it && it->it_disposition);
1378
1379         LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
1380         request = it->it_request;
1381         it_clear_disposition(it, DISP_ENQ_CREATE_REF);
1382         rc = ll_prep_inode(&inode, &request->rq_pill, dir->i_sb, it);
1383         if (rc)
1384                 GOTO(out, inode = ERR_PTR(rc));
1385
1386         /* Pause to allow for a race with concurrent access by fid */
1387         OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_NODE_PAUSE, cfs_fail_val);
1388
1389         /* We asked for a lock on the directory, but were granted a
1390          * lock on the inode.  Since we finally have an inode pointer,
1391          * stuff it in the lock. */
1392         CDEBUG(D_DLMTRACE, "setting l_ast_data to inode "DFID"(%p)\n",
1393                PFID(ll_inode2fid(inode)), inode);
1394         ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
1395         EXIT;
1396  out:
1397         ptlrpc_req_finished(request);
1398         return inode;
1399 }
1400
1401 /*
1402  * By the time this is called, we already have created the directory cache
1403  * entry for the new file, but it is so far negative - it has no inode.
1404  *
1405  * We defer creating the OBD object(s) until open, to keep the intent and
1406  * non-intent code paths similar, and also because we do not have the MDS
1407  * inode number before calling ll_create_node() (which is needed for LOV),
1408  * so we would need to do yet another RPC to the MDS to store the LOV EA
1409  * data on the MDS.  If needed, we would pass the PACKED lmm as data and
1410  * lmm_size in datalen (the MDS still has code which will handle that).
1411  *
1412  * If the create succeeds, we fill in the inode information
1413  * with d_instantiate().
1414  */
1415 static int ll_create_it(struct inode *dir, struct dentry *dentry,
1416                         struct lookup_intent *it,
1417                         void *secctx, __u32 secctxlen, bool encrypt,
1418                         void *encctx, __u32 encctxlen, unsigned int open_flags)
1419 {
1420         struct inode *inode;
1421         __u64 bits = 0;
1422         int rc = 0;
1423         ENTRY;
1424
1425         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p), intent=%s\n",
1426                dentry, PFID(ll_inode2fid(dir)), dir, LL_IT2STR(it));
1427
1428         rc = it_open_error(DISP_OPEN_CREATE, it);
1429         if (rc)
1430                 RETURN(rc);
1431
1432         inode = ll_create_node(dir, it);
1433         if (IS_ERR(inode))
1434                 RETURN(PTR_ERR(inode));
1435
1436         if (test_bit(LL_SBI_FILE_SECCTX, ll_i2sbi(inode)->ll_flags) &&
1437             secctx) {
1438                 /* must be done before d_instantiate, because it calls
1439                  * security_d_instantiate, which means a getxattr if security
1440                  * context is not set yet */
1441                 /* no need to protect selinux_inode_setsecurity() by
1442                  * inode_lock. Taking it would lead to a client deadlock
1443                  * LU-13617
1444                  */
1445                 rc = security_inode_notifysecctx(inode, secctx, secctxlen);
1446                 if (rc)
1447                         RETURN(rc);
1448         }
1449
1450         d_instantiate(dentry, inode);
1451
1452         if (encrypt) {
1453                 bool preload = true;
1454
1455                 /* For migration or mirroring without enc key, we
1456                  * create a volatile file without enc context.
1457                  */
1458                 if (!llcrypt_has_encryption_key(dir) &&
1459                     filename_is_volatile(dentry->d_name.name,
1460                                          dentry->d_name.len, NULL) &&
1461                     (open_flags & O_FILE_ENC) == O_FILE_ENC &&
1462                     open_flags & O_DIRECT)
1463                         preload = false;
1464                 rc = ll_set_encflags(inode, encctx, encctxlen, preload);
1465                 if (rc)
1466                         RETURN(rc);
1467         }
1468
1469         if (!test_bit(LL_SBI_FILE_SECCTX, ll_i2sbi(inode)->ll_flags)) {
1470                 rc = ll_inode_init_security(dentry, inode, dir);
1471                 if (rc)
1472                         RETURN(rc);
1473         }
1474
1475         ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, inode, it, &bits);
1476         if (bits & MDS_INODELOCK_LOOKUP) {
1477                 d_lustre_revalidate(dentry);
1478                 ll_update_dir_depth(dir, inode);
1479         }
1480
1481         RETURN(0);
1482 }
1483
1484 void ll_update_times(struct ptlrpc_request *request, struct inode *inode)
1485 {
1486         struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
1487                                                        &RMF_MDT_BODY);
1488
1489         LASSERT(body);
1490         if (body->mbo_valid & OBD_MD_FLMTIME &&
1491             body->mbo_mtime > inode->i_mtime.tv_sec) {
1492                 CDEBUG(D_INODE,
1493                        "setting fid " DFID " mtime from %lld to %llu\n",
1494                        PFID(ll_inode2fid(inode)),
1495                        (s64)inode->i_mtime.tv_sec, body->mbo_mtime);
1496                 inode->i_mtime.tv_sec = body->mbo_mtime;
1497         }
1498
1499         if (body->mbo_valid & OBD_MD_FLCTIME &&
1500             body->mbo_ctime > inode->i_ctime.tv_sec)
1501                 inode->i_ctime.tv_sec = body->mbo_ctime;
1502 }
1503
1504 /* once default LMV (space balanced) is set on ROOT, it should take effect if
1505  * default LMV is not set on parent directory.
1506  */
1507 static void ll_qos_mkdir_prep(struct md_op_data *op_data, struct inode *dir)
1508 {
1509         struct inode *root = dir->i_sb->s_root->d_inode;
1510         struct ll_inode_info *rlli = ll_i2info(root);
1511         struct ll_inode_info *lli = ll_i2info(dir);
1512         struct lmv_stripe_md *lsm;
1513
1514         op_data->op_dir_depth = lli->lli_dir_depth;
1515
1516         /* parent directory is striped */
1517         if (unlikely(lli->lli_lsm_md))
1518                 return;
1519
1520         /* default LMV set on parent directory */
1521         if (unlikely(lli->lli_default_lsm_md))
1522                 return;
1523
1524         /* parent is ROOT */
1525         if (unlikely(dir == root))
1526                 return;
1527
1528         /* default LMV not set on ROOT */
1529         if (!rlli->lli_default_lsm_md)
1530                 return;
1531
1532         down_read(&rlli->lli_lsm_sem);
1533         lsm = rlli->lli_default_lsm_md;
1534         if (!lsm)
1535                 goto unlock;
1536
1537         /* not space balanced */
1538         if (lsm->lsm_md_master_mdt_index != LMV_OFFSET_DEFAULT)
1539                 goto unlock;
1540
1541         if (lsm->lsm_md_max_inherit != LMV_INHERIT_NONE &&
1542             (lsm->lsm_md_max_inherit == LMV_INHERIT_UNLIMITED ||
1543              lsm->lsm_md_max_inherit >= lli->lli_dir_depth)) {
1544                 op_data->op_flags |= MF_QOS_MKDIR;
1545                 if (lsm->lsm_md_max_inherit_rr != LMV_INHERIT_RR_NONE &&
1546                     (lsm->lsm_md_max_inherit_rr == LMV_INHERIT_RR_UNLIMITED ||
1547                      lsm->lsm_md_max_inherit_rr >= lli->lli_dir_depth))
1548                         op_data->op_flags |= MF_RR_MKDIR;
1549                 CDEBUG(D_INODE, DFID" requests qos mkdir %#x\n",
1550                        PFID(&lli->lli_fid), op_data->op_flags);
1551         }
1552 unlock:
1553         up_read(&rlli->lli_lsm_sem);
1554 }
1555
1556 static int ll_new_node(struct inode *dir, struct dentry *dchild,
1557                        const char *tgt, umode_t mode, __u64 rdev, __u32 opc)
1558 {
1559         struct qstr *name = &dchild->d_name;
1560         struct ptlrpc_request *request = NULL;
1561         struct md_op_data *op_data = NULL;
1562         struct inode *inode = NULL;
1563         struct ll_sb_info *sbi = ll_i2sbi(dir);
1564         struct llcrypt_str *disk_link = NULL;
1565         bool encrypt = false;
1566         int err;
1567
1568         ENTRY;
1569         if (unlikely(tgt != NULL)) {
1570                 disk_link = (struct llcrypt_str *)rdev;
1571                 rdev = 0;
1572                 if (!disk_link)
1573                         RETURN(-EINVAL);
1574         }
1575
1576 again:
1577         op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
1578                                      name->len, 0, opc, NULL);
1579         if (IS_ERR(op_data))
1580                 GOTO(err_exit, err = PTR_ERR(op_data));
1581
1582         if (S_ISDIR(mode))
1583                 ll_qos_mkdir_prep(op_data, dir);
1584
1585         if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags)) {
1586                 err = ll_dentry_init_security(dchild, mode, &dchild->d_name,
1587                                               &op_data->op_file_secctx_name,
1588                                               &op_data->op_file_secctx,
1589                                               &op_data->op_file_secctx_size);
1590                 if (err < 0)
1591                         GOTO(err_exit, err);
1592         }
1593
1594         if (ll_sbi_has_encrypt(sbi) &&
1595             ((IS_ENCRYPTED(dir) &&
1596             (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) ||
1597             (unlikely(llcrypt_dummy_context_enabled(dir)) && S_ISDIR(mode)))) {
1598                 err = llcrypt_get_encryption_info(dir);
1599                 if (err)
1600                         GOTO(err_exit, err);
1601                 if (!llcrypt_has_encryption_key(dir))
1602                         GOTO(err_exit, err = -ENOKEY);
1603                 encrypt = true;
1604         }
1605
1606         if (encrypt) {
1607                 err = llcrypt_inherit_context(dir, NULL, op_data, false);
1608                 if (err)
1609                         GOTO(err_exit, err);
1610
1611                 if (S_ISLNK(mode)) {
1612                         /* llcrypt needs inode to encrypt target name, so create
1613                          * a fake inode and associate encryption context got
1614                          * from llcrypt_inherit_context.
1615                          */
1616                         struct inode *fakeinode =
1617                                 dchild->d_sb->s_op->alloc_inode(dchild->d_sb);
1618
1619                         if (!fakeinode)
1620                                 GOTO(err_exit, err = -ENOMEM);
1621                         fakeinode->i_sb = dchild->d_sb;
1622                         fakeinode->i_mode |= S_IFLNK;
1623 #ifdef IOP_XATTR
1624                         fakeinode->i_opflags |= IOP_XATTR;
1625 #endif
1626                         ll_lli_init(ll_i2info(fakeinode));
1627                         err = ll_set_encflags(fakeinode,
1628                                               op_data->op_file_encctx,
1629                                               op_data->op_file_encctx_size,
1630                                               true);
1631                         if (!err)
1632                                 err = __llcrypt_encrypt_symlink(fakeinode, tgt,
1633                                                                 strlen(tgt),
1634                                                                 disk_link);
1635
1636                         ll_xattr_cache_destroy(fakeinode);
1637                         llcrypt_put_encryption_info(fakeinode);
1638                         dchild->d_sb->s_op->destroy_inode(fakeinode);
1639                         if (err)
1640                                 GOTO(err_exit, err);
1641                 }
1642         }
1643
1644         err = md_create(sbi->ll_md_exp, op_data, tgt ? disk_link->name : NULL,
1645                         tgt ? disk_link->len : 0, mode,
1646                         from_kuid(&init_user_ns, current_fsuid()),
1647                         from_kgid(&init_user_ns, current_fsgid()),
1648                         current_cap(), rdev, &request);
1649 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 58, 0)
1650         /*
1651          * server < 2.12.58 doesn't pack default LMV in intent_getattr reply,
1652          * fetch default LMV here.
1653          */
1654         if (unlikely(err == -EREMOTE)) {
1655                 struct ll_inode_info    *lli = ll_i2info(dir);
1656                 struct lmv_user_md      *lum;
1657                 int                     lumsize;
1658                 int                     err2;
1659
1660                 ptlrpc_req_finished(request);
1661                 request = NULL;
1662                 ll_finish_md_op_data(op_data);
1663                 op_data = NULL;
1664
1665                 err2 = ll_dir_getstripe(dir, (void **)&lum, &lumsize, &request,
1666                                         OBD_MD_DEFAULT_MEA);
1667                 if (err2 == 0) {
1668                         struct lustre_md md = { NULL };
1669
1670                         md.body = req_capsule_server_get(&request->rq_pill,
1671                                                          &RMF_MDT_BODY);
1672                         if (!md.body)
1673                                 GOTO(err_exit, err = -EPROTO);
1674
1675                         OBD_ALLOC_PTR(md.default_lmv);
1676                         if (!md.default_lmv)
1677                                 GOTO(err_exit, err = -ENOMEM);
1678
1679                         md.default_lmv->lsm_md_magic = lum->lum_magic;
1680                         md.default_lmv->lsm_md_stripe_count =
1681                                 lum->lum_stripe_count;
1682                         md.default_lmv->lsm_md_master_mdt_index =
1683                                 lum->lum_stripe_offset;
1684                         md.default_lmv->lsm_md_hash_type = lum->lum_hash_type;
1685                         md.default_lmv->lsm_md_max_inherit =
1686                                 lum->lum_max_inherit;
1687                         md.default_lmv->lsm_md_max_inherit_rr =
1688                                 lum->lum_max_inherit_rr;
1689
1690                         err = ll_update_inode(dir, &md);
1691                         md_free_lustre_md(sbi->ll_md_exp, &md);
1692                         if (err)
1693                                 GOTO(err_exit, err);
1694                 } else if (err2 == -ENODATA && lli->lli_default_lsm_md) {
1695                         /*
1696                          * If there are no default stripe EA on the MDT, but the
1697                          * client has default stripe, then it probably means
1698                          * default stripe EA has just been deleted.
1699                          */
1700                         down_write(&lli->lli_lsm_sem);
1701                         if (lli->lli_default_lsm_md)
1702                                 OBD_FREE_PTR(lli->lli_default_lsm_md);
1703                         lli->lli_default_lsm_md = NULL;
1704                         up_write(&lli->lli_lsm_sem);
1705                 } else {
1706                         GOTO(err_exit, err);
1707                 }
1708
1709                 ptlrpc_req_finished(request);
1710                 request = NULL;
1711                 goto again;
1712         }
1713 #endif
1714
1715         if (err < 0)
1716                 GOTO(err_exit, err);
1717
1718         ll_update_times(request, dir);
1719
1720         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_NEWNODE_PAUSE, cfs_fail_val);
1721
1722         err = ll_prep_inode(&inode, &request->rq_pill, dchild->d_sb, NULL);
1723         if (err)
1724                 GOTO(err_exit, err);
1725
1726         if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags)) {
1727                 /* must be done before d_instantiate, because it calls
1728                  * security_d_instantiate, which means a getxattr if security
1729                  * context is not set yet */
1730                 /* no need to protect selinux_inode_setsecurity() by
1731                  * inode_lock. Taking it would lead to a client deadlock
1732                  * LU-13617
1733                  */
1734                 err = security_inode_notifysecctx(inode,
1735                                                   op_data->op_file_secctx,
1736                                                   op_data->op_file_secctx_size);
1737                 if (err)
1738                         GOTO(err_exit, err);
1739         }
1740
1741         d_instantiate(dchild, inode);
1742
1743         if (encrypt) {
1744                 err = ll_set_encflags(inode, op_data->op_file_encctx,
1745                                       op_data->op_file_encctx_size, true);
1746                 if (err)
1747                         GOTO(err_exit, err);
1748
1749                 if (S_ISLNK(mode)) {
1750                         struct ll_inode_info *lli = ll_i2info(inode);
1751
1752                         /* Cache the plaintext symlink target
1753                          * for later use by get_link()
1754                          */
1755                         OBD_ALLOC(lli->lli_symlink_name, strlen(tgt) + 1);
1756                         /* do not return an error if we cannot
1757                          * cache the symlink locally
1758                          */
1759                         if (lli->lli_symlink_name)
1760                                 memcpy(lli->lli_symlink_name,
1761                                        tgt, strlen(tgt) + 1);
1762                 }
1763         }
1764
1765         if (!test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags)) {
1766                 err = ll_inode_init_security(dchild, inode, dir);
1767                 if (err)
1768                         GOTO(err_exit, err);
1769         }
1770
1771         EXIT;
1772 err_exit:
1773         if (request != NULL)
1774                 ptlrpc_req_finished(request);
1775
1776         if (!IS_ERR_OR_NULL(op_data))
1777                 ll_finish_md_op_data(op_data);
1778
1779         RETURN(err);
1780 }
1781
1782 static int ll_mknod(struct user_namespace *mnt_userns, struct inode *dir,
1783                     struct dentry *dchild, umode_t mode, dev_t rdev)
1784 {
1785         ktime_t kstart = ktime_get();
1786         int err;
1787         ENTRY;
1788
1789         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p) mode %o dev %x\n",
1790                dchild, PFID(ll_inode2fid(dir)), dir, mode, rdev);
1791
1792         if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1793                 mode &= ~current_umask();
1794
1795         switch (mode & S_IFMT) {
1796         case 0:
1797                 mode |= S_IFREG;
1798                 fallthrough;
1799         case S_IFREG:
1800         case S_IFCHR:
1801         case S_IFBLK:
1802         case S_IFIFO:
1803         case S_IFSOCK:
1804                 err = ll_new_node(dir, dchild, NULL, mode, old_encode_dev(rdev),
1805                                   LUSTRE_OPC_MKNOD);
1806                 break;
1807         case S_IFDIR:
1808                 err = -EPERM;
1809                 break;
1810         default:
1811                 err = -EINVAL;
1812         }
1813
1814         if (!err)
1815                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD,
1816                                    ktime_us_delta(ktime_get(), kstart));
1817
1818         RETURN(err);
1819 }
1820
1821 /*
1822  * Plain create. Intent create is handled in atomic_open.
1823  */
1824 static int ll_create_nd(struct user_namespace *mnt_userns,
1825                         struct inode *dir, struct dentry *dentry,
1826                         umode_t mode, bool want_excl)
1827 {
1828         ktime_t kstart = ktime_get();
1829         int rc;
1830
1831         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
1832
1833         CDEBUG(D_VFSTRACE,
1834                "VFS Op:name=%pd, dir="DFID"(%p), flags=%u, excl=%d\n",
1835                dentry, PFID(ll_inode2fid(dir)), dir, mode, want_excl);
1836
1837         /* Using mknod(2) to create a regular file is designed to not recognize
1838          * volatile file name, so we use ll_mknod() here. */
1839         rc = ll_mknod(mnt_userns, dir, dentry, mode, 0);
1840
1841         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, unhashed %d\n",
1842                dentry, d_unhashed(dentry));
1843
1844         if (!rc)
1845                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE,
1846                                    ktime_us_delta(ktime_get(), kstart));
1847
1848         return rc;
1849 }
1850
1851 static int ll_symlink(struct user_namespace *mnt_userns, struct inode *dir,
1852                       struct dentry *dchild, const char *oldpath)
1853 {
1854         ktime_t kstart = ktime_get();
1855         int len = strlen(oldpath);
1856         struct llcrypt_str disk_link;
1857         int err;
1858         ENTRY;
1859
1860         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p), target=%.*s\n",
1861                dchild, PFID(ll_inode2fid(dir)), dir, 3000, oldpath);
1862
1863         err = llcrypt_prepare_symlink(dir, oldpath, len, dir->i_sb->s_blocksize,
1864                                       &disk_link);
1865         if (err)
1866                 RETURN(err);
1867
1868         err = ll_new_node(dir, dchild, oldpath, S_IFLNK | S_IRWXUGO,
1869                           (__u64)&disk_link, LUSTRE_OPC_SYMLINK);
1870
1871         if (disk_link.name != (unsigned char *)oldpath)
1872                 kfree(disk_link.name);
1873
1874         if (!err)
1875                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK,
1876                                    ktime_us_delta(ktime_get(), kstart));
1877
1878         RETURN(err);
1879 }
1880
1881 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1882                    struct dentry *new_dentry)
1883 {
1884         struct inode *src = old_dentry->d_inode;
1885         struct qstr *name = &new_dentry->d_name;
1886         struct ll_sb_info *sbi = ll_i2sbi(dir);
1887         struct ptlrpc_request *request = NULL;
1888         struct md_op_data *op_data;
1889         ktime_t kstart = ktime_get();
1890         int err;
1891
1892         ENTRY;
1893         CDEBUG(D_VFSTRACE,
1894                "VFS Op: inode="DFID"(%p), dir="DFID"(%p), target=%pd\n",
1895                PFID(ll_inode2fid(src)), src,
1896                PFID(ll_inode2fid(dir)), dir, new_dentry);
1897
1898         err = llcrypt_prepare_link(old_dentry, dir, new_dentry);
1899         if (err)
1900                 RETURN(err);
1901
1902         op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len,
1903                                      0, LUSTRE_OPC_ANY, NULL);
1904         if (IS_ERR(op_data))
1905                 RETURN(PTR_ERR(op_data));
1906
1907         err = md_link(sbi->ll_md_exp, op_data, &request);
1908         ll_finish_md_op_data(op_data);
1909         if (err)
1910                 GOTO(out, err);
1911
1912         ll_update_times(request, dir);
1913         ll_stats_ops_tally(sbi, LPROC_LL_LINK,
1914                            ktime_us_delta(ktime_get(), kstart));
1915         EXIT;
1916 out:
1917         ptlrpc_req_finished(request);
1918         RETURN(err);
1919 }
1920
1921 static int ll_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
1922                     struct dentry *dchild, umode_t mode)
1923 {
1924         ktime_t kstart = ktime_get();
1925         int err;
1926         ENTRY;
1927
1928         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p)\n",
1929                dchild, PFID(ll_inode2fid(dir)), dir);
1930
1931         if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1932                 mode &= ~current_umask();
1933
1934         mode = (mode & (S_IRWXUGO|S_ISVTX)) | S_IFDIR;
1935
1936         err = ll_new_node(dir, dchild, NULL, mode, 0, LUSTRE_OPC_MKDIR);
1937         if (err == 0)
1938                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR,
1939                                    ktime_us_delta(ktime_get(), kstart));
1940
1941         RETURN(err);
1942 }
1943
1944 static int ll_rmdir(struct inode *dir, struct dentry *dchild)
1945 {
1946         struct qstr *name = &dchild->d_name;
1947         struct ptlrpc_request *request = NULL;
1948         struct md_op_data *op_data;
1949         ktime_t kstart = ktime_get();
1950         int rc;
1951
1952         ENTRY;
1953
1954         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p)\n",
1955                dchild, PFID(ll_inode2fid(dir)), dir);
1956
1957         if (unlikely(d_mountpoint(dchild)))
1958                 RETURN(-EBUSY);
1959
1960         /* some foreign dir may not be allowed to be removed */
1961         if (!ll_foreign_is_removable(dchild, false))
1962                 RETURN(-EPERM);
1963
1964         op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
1965                                      S_IFDIR, LUSTRE_OPC_ANY, NULL);
1966         if (IS_ERR(op_data))
1967                 RETURN(PTR_ERR(op_data));
1968
1969         if (dchild->d_inode != NULL)
1970                 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1971
1972         if (fid_is_zero(&op_data->op_fid2))
1973                 op_data->op_fid2 = op_data->op_fid3;
1974         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1975         ll_finish_md_op_data(op_data);
1976         if (!rc) {
1977                 struct mdt_body *body;
1978
1979                 ll_update_times(request, dir);
1980                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR,
1981                                    ktime_us_delta(ktime_get(), kstart));
1982
1983                 /*
1984                  * The server puts attributes in on the last unlink, use them
1985                  * to update the link count so the inode can be freed
1986                  * immediately.
1987                  */
1988                 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
1989                 if (body->mbo_valid & OBD_MD_FLNLINK) {
1990                         spin_lock(&dchild->d_inode->i_lock);
1991                         set_nlink(dchild->d_inode, body->mbo_nlink);
1992                         spin_unlock(&dchild->d_inode->i_lock);
1993                 }
1994         }
1995
1996         ptlrpc_req_finished(request);
1997
1998         RETURN(rc);
1999 }
2000
2001 /**
2002  * Remove dir entry
2003  **/
2004 int ll_rmdir_entry(struct inode *dir, char *name, int namelen)
2005 {
2006         struct ptlrpc_request *request = NULL;
2007         struct md_op_data *op_data;
2008         ktime_t kstart = ktime_get();
2009         int rc;
2010         ENTRY;
2011
2012         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
2013                namelen, name, PFID(ll_inode2fid(dir)), dir);
2014
2015         op_data = ll_prep_md_op_data(NULL, dir, NULL, name, strlen(name),
2016                                      S_IFDIR, LUSTRE_OPC_ANY, NULL);
2017         if (IS_ERR(op_data))
2018                 RETURN(PTR_ERR(op_data));
2019         op_data->op_cli_flags |= CLI_RM_ENTRY;
2020         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
2021         ll_finish_md_op_data(op_data);
2022         if (!rc)
2023                 ll_update_times(request, dir);
2024
2025         ptlrpc_req_finished(request);
2026         if (!rc)
2027                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR,
2028                                    ktime_us_delta(ktime_get(), kstart));
2029         RETURN(rc);
2030 }
2031
2032 static int ll_unlink(struct inode *dir, struct dentry *dchild)
2033 {
2034         struct qstr *name = &dchild->d_name;
2035         struct ptlrpc_request *request = NULL;
2036         struct md_op_data *op_data;
2037         struct mdt_body *body;
2038         ktime_t kstart = ktime_get();
2039         int rc;
2040
2041         ENTRY;
2042
2043         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p)\n",
2044                dchild, PFID(ll_inode2fid(dir)), dir);
2045
2046         /*
2047          * XXX: unlink bind mountpoint maybe call to here,
2048          * just check it as vfs_unlink does.
2049          */
2050         if (unlikely(d_mountpoint(dchild)))
2051                 RETURN(-EBUSY);
2052
2053         /* some foreign file/dir may not be allowed to be unlinked */
2054         if (!ll_foreign_is_removable(dchild, false))
2055                 RETURN(-EPERM);
2056
2057         op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0,
2058                                      LUSTRE_OPC_ANY, NULL);
2059         if (IS_ERR(op_data))
2060                 RETURN(PTR_ERR(op_data));
2061
2062         op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
2063         /* notify lower layer if inode has dirty pages */
2064         if (S_ISREG(dchild->d_inode->i_mode) &&
2065             ll_i2info(dchild->d_inode)->lli_clob &&
2066             dirty_cnt(dchild->d_inode))
2067                 op_data->op_cli_flags |= CLI_DIRTY_DATA;
2068         if (fid_is_zero(&op_data->op_fid2))
2069                 op_data->op_fid2 = op_data->op_fid3;
2070         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
2071         ll_finish_md_op_data(op_data);
2072         if (rc)
2073                 GOTO(out, rc);
2074
2075         /*
2076          * The server puts attributes in on the last unlink, use them to update
2077          * the link count so the inode can be freed immediately.
2078          */
2079         body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
2080         if (body->mbo_valid & OBD_MD_FLNLINK) {
2081                 spin_lock(&dchild->d_inode->i_lock);
2082                 set_nlink(dchild->d_inode, body->mbo_nlink);
2083                 spin_unlock(&dchild->d_inode->i_lock);
2084         }
2085
2086         ll_update_times(request, dir);
2087
2088 out:
2089         ptlrpc_req_finished(request);
2090         if (!rc)
2091                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK,
2092                                    ktime_us_delta(ktime_get(), kstart));
2093         RETURN(rc);
2094 }
2095
2096 static int ll_rename(struct user_namespace *mnt_userns,
2097                      struct inode *src, struct dentry *src_dchild,
2098                      struct inode *tgt, struct dentry *tgt_dchild
2099 #if defined(HAVE_USER_NAMESPACE_ARG) || defined(HAVE_IOPS_RENAME_WITH_FLAGS)
2100                      , unsigned int flags
2101 #endif
2102                      )
2103 {
2104         struct ptlrpc_request *request = NULL;
2105         struct ll_sb_info *sbi = ll_i2sbi(src);
2106         struct md_op_data *op_data;
2107         ktime_t kstart = ktime_get();
2108         umode_t mode = 0;
2109         struct llcrypt_name foldname, fnewname;
2110         int err;
2111         ENTRY;
2112
2113 #if defined(HAVE_USER_NAMESPACE_ARG) || defined(HAVE_IOPS_RENAME_WITH_FLAGS)
2114         if (flags)
2115                 return -EINVAL;
2116 #endif
2117
2118         CDEBUG(D_VFSTRACE,
2119                "VFS Op:oldname=%pd, src_dir="DFID"(%p), newname=%pd, tgt_dir="DFID"(%p)\n",
2120                src_dchild, PFID(ll_inode2fid(src)), src,
2121                tgt_dchild, PFID(ll_inode2fid(tgt)), tgt);
2122
2123         if (unlikely(d_mountpoint(src_dchild) || d_mountpoint(tgt_dchild)))
2124                 RETURN(-EBUSY);
2125
2126 #if defined(HAVE_USER_NAMESPACE_ARG) || defined(HAVE_IOPS_RENAME_WITH_FLAGS)
2127         err = llcrypt_prepare_rename(src, src_dchild, tgt, tgt_dchild, flags);
2128 #else
2129         err = llcrypt_prepare_rename(src, src_dchild, tgt, tgt_dchild, 0);
2130 #endif
2131         if (err)
2132                 RETURN(err);
2133         /* we prevent an encrypted file from being renamed
2134          * into an unencrypted dir
2135          */
2136         if (IS_ENCRYPTED(src) && !IS_ENCRYPTED(tgt))
2137                 RETURN(-EXDEV);
2138
2139         if (src_dchild->d_inode)
2140                 mode = src_dchild->d_inode->i_mode;
2141
2142         if (tgt_dchild->d_inode)
2143                 mode = tgt_dchild->d_inode->i_mode;
2144
2145         op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, mode,
2146                                      LUSTRE_OPC_ANY, NULL);
2147         if (IS_ERR(op_data))
2148                 RETURN(PTR_ERR(op_data));
2149
2150         /* If the client is using a subdir mount and does a rename to what it
2151          * sees as /.fscrypt, interpret it as the .fscrypt dir at fs root.
2152          */
2153         if (unlikely(is_root_inode(tgt) && !fid_is_root(ll_inode2fid(tgt)) &&
2154                      tgt_dchild->d_name.len == strlen(dot_fscrypt_name) &&
2155                      strncmp(tgt_dchild->d_name.name, dot_fscrypt_name,
2156                              tgt_dchild->d_name.len) == 0))
2157                 lu_root_fid(&op_data->op_fid2);
2158
2159         if (src_dchild->d_inode)
2160                 op_data->op_fid3 = *ll_inode2fid(src_dchild->d_inode);
2161
2162         if (tgt_dchild->d_inode)
2163                 op_data->op_fid4 = *ll_inode2fid(tgt_dchild->d_inode);
2164
2165         err = ll_setup_filename(src, &src_dchild->d_name, 1, &foldname, NULL);
2166         if (err)
2167                 RETURN(err);
2168         err = ll_setup_filename(tgt, &tgt_dchild->d_name, 1, &fnewname, NULL);
2169         if (err) {
2170                 llcrypt_free_filename(&foldname);
2171                 RETURN(err);
2172         }
2173         err = md_rename(sbi->ll_md_exp, op_data,
2174                         foldname.disk_name.name, foldname.disk_name.len,
2175                         fnewname.disk_name.name, fnewname.disk_name.len,
2176                         &request);
2177         llcrypt_free_filename(&foldname);
2178         llcrypt_free_filename(&fnewname);
2179         ll_finish_md_op_data(op_data);
2180         if (!err) {
2181                 ll_update_times(request, src);
2182                 ll_update_times(request, tgt);
2183         }
2184
2185         ptlrpc_req_finished(request);
2186
2187         if (!err) {
2188                 d_move(src_dchild, tgt_dchild);
2189                 ll_stats_ops_tally(sbi, LPROC_LL_RENAME,
2190                                    ktime_us_delta(ktime_get(), kstart));
2191         }
2192
2193         RETURN(err);
2194 }
2195
2196 const struct inode_operations ll_dir_inode_operations = {
2197         .mknod          = ll_mknod,
2198         .atomic_open    = ll_atomic_open,
2199         .lookup         = ll_lookup_nd,
2200         .create         = ll_create_nd,
2201         /* We need all these non-raw things for NFSD, to not patch it. */
2202         .unlink         = ll_unlink,
2203         .mkdir          = ll_mkdir,
2204         .rmdir          = ll_rmdir,
2205         .symlink        = ll_symlink,
2206         .link           = ll_link,
2207         .rename         = ll_rename,
2208         .setattr        = ll_setattr,
2209         .getattr        = ll_getattr,
2210         .permission     = ll_inode_permission,
2211 #ifdef HAVE_IOP_XATTR
2212         .setxattr       = ll_setxattr,
2213         .getxattr       = ll_getxattr,
2214         .removexattr    = ll_removexattr,
2215 #endif
2216         .listxattr      = ll_listxattr,
2217         .get_acl        = ll_get_acl,
2218 #ifdef HAVE_IOP_SET_ACL
2219         .set_acl        = ll_set_acl,
2220 #endif
2221 };
2222
2223 const struct inode_operations ll_special_inode_operations = {
2224         .setattr        = ll_setattr,
2225         .getattr        = ll_getattr,
2226         .permission     = ll_inode_permission,
2227 #ifdef HAVE_IOP_XATTR
2228         .setxattr       = ll_setxattr,
2229         .getxattr       = ll_getxattr,
2230         .removexattr    = ll_removexattr,
2231 #endif
2232         .listxattr      = ll_listxattr,
2233         .get_acl        = ll_get_acl,
2234 #ifdef HAVE_IOP_SET_ACL
2235         .set_acl        = ll_set_acl,
2236 #endif
2237 };