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