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