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