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