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