4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
33 #include <linux/sched.h>
35 #include <linux/quotaops.h>
36 #include <linux/highmem.h>
37 #include <linux/pagemap.h>
38 #include <linux/user_namespace.h>
39 #include <linux/uidgid.h>
41 #define DEBUG_SUBSYSTEM S_LLITE
43 #include <obd_support.h>
44 #include <lustre_fid.h>
45 #include <lustre_dlm.h>
46 #include "llite_internal.h"
48 static int ll_create_it(struct inode *dir, struct dentry *dentry,
49 struct lookup_intent *it,
50 void *secctx, __u32 secctxlen, bool encrypt,
51 void *encctx, __u32 encctxlen);
53 /* called from iget5_locked->find_inode() under inode_lock spinlock */
54 static int ll_test_inode(struct inode *inode, void *opaque)
56 struct ll_inode_info *lli = ll_i2info(inode);
57 struct lustre_md *md = opaque;
59 if (unlikely(!(md->body->mbo_valid & OBD_MD_FLID))) {
60 CERROR("MDS body missing FID\n");
64 if (!lu_fid_eq(&lli->lli_fid, &md->body->mbo_fid1))
70 static int ll_set_inode(struct inode *inode, void *opaque)
72 struct ll_inode_info *lli = ll_i2info(inode);
73 struct mdt_body *body = ((struct lustre_md *)opaque)->body;
75 if (unlikely(!(body->mbo_valid & OBD_MD_FLID))) {
76 CERROR("MDS body missing FID\n");
80 lli->lli_fid = body->mbo_fid1;
81 if (unlikely(!(body->mbo_valid & OBD_MD_FLTYPE))) {
82 CERROR("Can not initialize inode "DFID" without object type: "
84 PFID(&lli->lli_fid), body->mbo_valid);
88 inode->i_mode = (inode->i_mode & ~S_IFMT) | (body->mbo_mode & S_IFMT);
89 if (unlikely(inode->i_mode == 0)) {
90 CERROR("Invalid inode "DFID" type\n", PFID(&lli->lli_fid));
101 * Get an inode by inode number(@hash), which is already instantiated by
102 * the intent lookup).
104 struct inode *ll_iget(struct super_block *sb, ino_t hash,
105 struct lustre_md *md)
113 inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
115 RETURN(ERR_PTR(-ENOMEM));
117 if (inode->i_state & I_NEW) {
118 rc = ll_read_inode2(inode, md);
119 if (rc == 0 && S_ISREG(inode->i_mode) &&
120 ll_i2info(inode)->lli_clob == NULL)
121 rc = cl_file_inode_init(inode, md);
124 /* Let's clear directory lsm here, otherwise
125 * make_bad_inode() will reset the inode mode
126 * to regular, then ll_clear_inode will not
127 * be able to clear lsm_md */
128 if (S_ISDIR(inode->i_mode))
129 ll_dir_clear_lsm_md(inode);
130 make_bad_inode(inode);
131 unlock_new_inode(inode);
135 inode_has_no_xattr(inode);
136 unlock_new_inode(inode);
138 } else if (is_bad_inode(inode)) {
140 inode = ERR_PTR(-ESTALE);
141 } else if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
142 rc = ll_update_inode(inode, md);
143 CDEBUG(D_VFSTRACE, "got inode: "DFID"(%p): rc = %d\n",
144 PFID(&md->body->mbo_fid1), inode, rc);
146 if (S_ISDIR(inode->i_mode))
147 ll_dir_clear_lsm_md(inode);
156 /* mark negative sub file dentries invalid and prune unused dentries */
157 static void ll_prune_negative_children(struct inode *dir)
159 struct dentry *dentry;
160 struct dentry *child;
165 spin_lock(&dir->i_lock);
166 hlist_for_each_entry(dentry, &dir->i_dentry, d_alias) {
167 spin_lock(&dentry->d_lock);
168 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
172 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
173 __d_lustre_invalidate(child);
174 if (!ll_d_count(child)) {
177 spin_unlock(&child->d_lock);
178 spin_unlock(&dentry->d_lock);
179 spin_unlock(&dir->i_lock);
181 CDEBUG(D_DENTRY, "prune negative dentry %pd\n",
187 spin_unlock(&child->d_lock);
189 spin_unlock(&dentry->d_lock);
191 spin_unlock(&dir->i_lock);
196 int ll_test_inode_by_fid(struct inode *inode, void *opaque)
198 return lu_fid_eq(&ll_i2info(inode)->lli_fid, opaque);
201 static int ll_dom_lock_cancel(struct inode *inode, struct ldlm_lock *lock)
204 struct ll_inode_info *lli = ll_i2info(inode);
209 env = cl_env_get(&refcheck);
211 RETURN(PTR_ERR(env));
213 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_REPLAY_PAUSE, cfs_fail_val);
215 /* reach MDC layer to flush data under the DoM ldlm lock */
216 rc = cl_object_flush(env, lli->lli_clob, lock);
217 if (rc == -ENODATA) {
218 CDEBUG(D_INODE, "inode "DFID" layout has no DoM stripe\n",
219 PFID(ll_inode2fid(inode)));
220 /* most likely result of layout change, do nothing */
224 cl_env_put(env, &refcheck);
228 static void ll_lock_cancel_bits(struct ldlm_lock *lock, __u64 to_cancel)
230 struct inode *inode = ll_inode_from_resource_lock(lock);
231 struct ll_inode_info *lli;
232 __u64 bits = to_cancel;
238 /* That means the inode is evicted most likely and may cause
239 * the skipping of lock cleanups below, so print the message
242 if (lock->l_resource->lr_lvb_inode)
244 "can't take inode for the lock (%sevicted)\n",
245 lock->l_resource->lr_lvb_inode->i_state &
246 I_FREEING ? "" : "not ");
250 if (!fid_res_name_eq(ll_inode2fid(inode),
251 &lock->l_resource->lr_name)) {
252 LDLM_ERROR(lock, "data mismatch with object "DFID"(%p)",
253 PFID(ll_inode2fid(inode)), inode);
257 if (bits & MDS_INODELOCK_XATTR) {
258 ll_xattr_cache_destroy(inode);
259 bits &= ~MDS_INODELOCK_XATTR;
262 /* For OPEN locks we differentiate between lock modes
263 * LCK_CR, LCK_CW, LCK_PR - bug 22891 */
264 if (bits & MDS_INODELOCK_OPEN)
265 ll_have_md_lock(inode, &bits, lock->l_req_mode);
267 if (bits & MDS_INODELOCK_OPEN) {
270 switch (lock->l_req_mode) {
281 LDLM_ERROR(lock, "bad lock mode for OPEN lock");
285 ll_md_real_close(inode, fmode);
287 bits &= ~MDS_INODELOCK_OPEN;
290 if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
291 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_PERM |
293 ll_have_md_lock(inode, &bits, LCK_MINMODE);
295 if (bits & MDS_INODELOCK_DOM) {
296 rc = ll_dom_lock_cancel(inode, lock);
298 CDEBUG(D_INODE, "cannot flush DoM data "
300 PFID(ll_inode2fid(inode)), rc);
303 if (bits & MDS_INODELOCK_LAYOUT) {
304 struct cl_object_conf conf = {
305 .coc_opc = OBJECT_CONF_INVALIDATE,
309 rc = ll_layout_conf(inode, &conf);
311 CDEBUG(D_INODE, "cannot invalidate layout of "
313 PFID(ll_inode2fid(inode)), rc);
316 lli = ll_i2info(inode);
318 if (bits & MDS_INODELOCK_UPDATE)
319 set_bit(LLIF_UPDATE_ATIME, &lli->lli_flags);
321 if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
322 CDEBUG(D_INODE, "invalidating inode "DFID" lli = %p, "
323 "pfid = "DFID"\n", PFID(ll_inode2fid(inode)),
324 lli, PFID(&lli->lli_pfid));
325 truncate_inode_pages(inode->i_mapping, 0);
327 if (unlikely(!fid_is_zero(&lli->lli_pfid))) {
328 struct inode *master_inode = NULL;
331 /* This is slave inode, since all of the child dentry
332 * is connected on the master inode, so we have to
333 * invalidate the negative children on master inode */
334 CDEBUG(D_INODE, "Invalidate s"DFID" m"DFID"\n",
335 PFID(ll_inode2fid(inode)), PFID(&lli->lli_pfid));
337 hash = cl_fid_build_ino(&lli->lli_pfid,
338 ll_need_32bit_api(ll_i2sbi(inode)));
340 /* Do not lookup the inode with ilookup5, otherwise
341 * it will cause dead lock,
342 * 1. Client1 send chmod req to the MDT0, then on MDT0,
343 * it enqueues master and all of its slaves lock,
344 * (mdt_attr_set() -> mdt_lock_slaves()), after gets
345 * master and stripe0 lock, it will send the enqueue
346 * req (for stripe1) to MDT1, then MDT1 finds the lock
347 * has been granted to client2. Then MDT1 sends blocking
349 * 2. At the same time, client2 tries to unlink
350 * the striped dir (rm -rf striped_dir), and during
351 * lookup, it will hold the master inode of the striped
352 * directory, whose inode state is NEW, then tries to
353 * revalidate all of its slaves, (ll_prep_inode()->
354 * ll_iget()->ll_read_inode2()-> ll_update_inode().).
355 * And it will be blocked on the server side because
357 * 3. Then the client get the blocking_ast req, cancel
358 * the lock, but being blocked if using ->ilookup5()),
359 * because master inode state is NEW. */
360 master_inode = ilookup5_nowait(inode->i_sb, hash,
361 ll_test_inode_by_fid,
362 (void *)&lli->lli_pfid);
364 ll_prune_negative_children(master_inode);
368 ll_prune_negative_children(inode);
372 if ((bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM)) &&
373 !is_root_inode(inode))
374 ll_prune_aliases(inode);
376 if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM))
377 forget_all_cached_acls(inode);
383 /* Check if the given lock may be downgraded instead of canceling and
384 * that convert is really needed. */
385 int ll_md_need_convert(struct ldlm_lock *lock)
387 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
389 __u64 wanted = lock->l_policy_data.l_inodebits.cancel_bits;
390 __u64 bits = lock->l_policy_data.l_inodebits.bits & ~wanted;
391 enum ldlm_mode mode = LCK_MINMODE;
393 if (!lock->l_conn_export ||
394 !exp_connect_lock_convert(lock->l_conn_export))
397 if (!wanted || !bits || ldlm_is_cancel(lock))
400 /* do not convert locks other than DOM for now */
401 if (!((bits | wanted) & MDS_INODELOCK_DOM))
404 /* We may have already remaining bits in some other lock so
405 * lock convert will leave us just extra lock for the same bit.
406 * Check if client has other lock with the same bits and the same
407 * or lower mode and don't convert if any.
409 switch (lock->l_req_mode) {
423 /* do not convert other modes */
427 /* is lock is too old to be converted? */
428 lock_res_and_lock(lock);
429 if (ktime_after(ktime_get(),
430 ktime_add(lock->l_last_used, ns->ns_dirty_age_limit))) {
431 unlock_res_and_lock(lock);
434 unlock_res_and_lock(lock);
436 inode = ll_inode_from_resource_lock(lock);
437 ll_have_md_lock(inode, &bits, mode);
442 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *ld,
443 void *data, int flag)
445 struct lustre_handle lockh;
451 case LDLM_CB_BLOCKING:
453 __u64 cancel_flags = LCF_ASYNC;
455 /* if lock convert is not needed then still have to
456 * pass lock via ldlm_cli_convert() to keep all states
457 * correct, set cancel_bits to full lock bits to cause
458 * full cancel to happen.
460 if (!ll_md_need_convert(lock)) {
461 lock_res_and_lock(lock);
462 lock->l_policy_data.l_inodebits.cancel_bits =
463 lock->l_policy_data.l_inodebits.bits;
464 unlock_res_and_lock(lock);
466 rc = ldlm_cli_convert(lock, cancel_flags);
469 /* continue with cancel otherwise */
470 ldlm_lock2handle(lock, &lockh);
471 rc = ldlm_cli_cancel(&lockh, cancel_flags);
473 CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
478 case LDLM_CB_CANCELING:
480 __u64 to_cancel = lock->l_policy_data.l_inodebits.bits;
482 /* Nothing to do for non-granted locks */
483 if (!ldlm_is_granted(lock))
486 /* If 'ld' is supplied then bits to be cancelled are passed
487 * implicitly by lock converting and cancel_bits from 'ld'
488 * should be used. Otherwise full cancel is being performed
489 * and lock inodebits are used.
491 * Note: we cannot rely on cancel_bits in lock itself at this
492 * moment because they can be changed by concurrent thread,
493 * so ldlm_cli_inodebits_convert() pass cancel bits implicitly
497 /* partial bits cancel allowed only during convert */
498 LASSERT(ldlm_is_converting(lock));
499 /* mask cancel bits by lock bits so only no any unused
500 * bits are passed to ll_lock_cancel_bits()
502 to_cancel &= ld->l_policy_data.l_inodebits.cancel_bits;
504 ll_lock_cancel_bits(lock, to_cancel);
514 __u32 ll_i2suppgid(struct inode *i)
516 if (in_group_p(i->i_gid))
517 return (__u32)from_kgid(&init_user_ns, i->i_gid);
519 return (__u32) __kgid_val(INVALID_GID);
522 /* Pack the required supplementary groups into the supplied groups array.
523 * If we don't need to use the groups from the target inode(s) then we
524 * instead pack one or more groups from the user's supplementary group
525 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
526 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
529 LASSERT(suppgids != NULL);
531 suppgids[0] = ll_i2suppgid(i1);
534 suppgids[1] = ll_i2suppgid(i2);
540 * try to reuse three types of dentry:
541 * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
542 * by concurrent .revalidate).
543 * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
544 * be cleared by others calling d_lustre_revalidate).
545 * 3. DISCONNECTED alias.
547 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
549 struct dentry *alias, *discon_alias, *invalid_alias;
551 if (hlist_empty(&inode->i_dentry))
554 discon_alias = invalid_alias = NULL;
556 spin_lock(&inode->i_lock);
557 hlist_for_each_entry(alias, &inode->i_dentry, d_alias) {
558 LASSERT(alias != dentry);
560 spin_lock(&alias->d_lock);
561 if ((alias->d_flags & DCACHE_DISCONNECTED) &&
562 S_ISDIR(inode->i_mode))
563 /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
564 discon_alias = alias;
565 else if (alias->d_parent == dentry->d_parent &&
566 alias->d_name.hash == dentry->d_name.hash &&
567 alias->d_name.len == dentry->d_name.len &&
568 memcmp(alias->d_name.name, dentry->d_name.name,
569 dentry->d_name.len) == 0)
570 invalid_alias = alias;
571 spin_unlock(&alias->d_lock);
576 alias = invalid_alias ?: discon_alias ?: NULL;
578 spin_lock(&alias->d_lock);
580 spin_unlock(&alias->d_lock);
582 spin_unlock(&inode->i_lock);
588 * Similar to d_splice_alias(), but lustre treats invalid alias
589 * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
591 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
597 new = ll_find_alias(inode, de);
607 "Reuse dentry %p inode %p refc %d flags %#x\n",
608 new, new->d_inode, ll_d_count(new), new->d_flags);
617 /* this needs only to be done for foreign symlink dirs as
618 * DCACHE_SYMLINK_TYPE is already set by d_flags_for_inode()
619 * kernel routine for files with symlink ops (ie, real symlink)
621 if (inode && S_ISDIR(inode->i_mode) &&
622 ll_sbi_has_foreign_symlink(ll_i2sbi(inode)) &&
623 #ifdef HAVE_IOP_GET_LINK
624 inode->i_op->get_link) {
626 inode->i_op->follow_link) {
628 CDEBUG(D_INFO, "%s: inode "DFID": faking foreign dir as a symlink\n",
629 ll_i2sbi(inode)->ll_fsname, PFID(ll_inode2fid(inode)));
630 spin_lock(&de->d_lock);
631 /* like d_flags_for_inode() already does for files */
632 de->d_flags = (de->d_flags & ~DCACHE_ENTRY_TYPE) |
634 spin_unlock(&de->d_lock);
637 CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
638 de, de->d_inode, ll_d_count(de), de->d_flags);
642 static int ll_lookup_it_finish(struct ptlrpc_request *request,
643 struct lookup_intent *it,
644 struct inode *parent, struct dentry **de,
645 void *secctx, __u32 secctxlen,
646 void *encctx, __u32 encctxlen,
647 ktime_t kstart, bool encrypt)
649 struct inode *inode = NULL;
652 struct dentry *alias;
655 /* NB 1 request reference will be taken away by ll_intent_lock()
657 CDEBUG(D_DENTRY, "it %p it_disposition %x\n", it,
659 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
660 struct req_capsule *pill = &request->rq_pill;
661 struct mdt_body *body = req_capsule_server_get(pill,
664 rc = ll_prep_inode(&inode, &request->rq_pill, (*de)->d_sb, it);
668 /* If encryption context was returned by MDT, put it in
669 * inode now to save an extra getxattr and avoid deadlock.
671 if (body->mbo_valid & OBD_MD_ENCCTX) {
672 encctx = req_capsule_server_get(pill, &RMF_FILE_ENCCTX);
673 encctxlen = req_capsule_get_size(pill,
679 "server returned encryption ctx for "DFID"\n",
680 PFID(ll_inode2fid(inode)));
681 rc = ll_xattr_cache_insert(inode,
682 LL_XATTR_NAME_ENCRYPTION_CONTEXT,
685 CWARN("%s: cannot set enc ctx for "DFID": rc = %d\n",
686 ll_i2sbi(inode)->ll_fsname,
687 PFID(ll_inode2fid(inode)), rc);
689 rc = llcrypt_get_encryption_info(inode);
692 "cannot get enc info for "DFID": rc = %d\n",
693 PFID(ll_inode2fid(inode)), rc);
698 ll_set_lock_data(ll_i2sbi(parent)->ll_md_exp, inode, it, &bits);
699 /* OPEN can return data if lock has DoM+LAYOUT bits set */
700 if (it->it_op & IT_OPEN &&
701 bits & MDS_INODELOCK_DOM && bits & MDS_INODELOCK_LAYOUT)
702 ll_dom_finish_open(inode, request);
704 /* We used to query real size from OSTs here, but actually
705 * this is not needed. For stat() calls size would be updated
706 * from subsequent do_revalidate()->ll_inode_revalidate_it() in
708 * vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
709 * Everybody else who needs correct file size would call
710 * ll_glimpse_size or some equivalent themselves anyway.
714 /* If security context was returned by MDT, put it in
715 * inode now to save an extra getxattr from security hooks,
716 * and avoid deadlock.
718 if (body->mbo_valid & OBD_MD_SECCTX) {
719 secctx = req_capsule_server_get(pill, &RMF_FILE_SECCTX);
720 secctxlen = req_capsule_get_size(pill,
725 CDEBUG(D_SEC, "server returned security context"
727 PFID(ll_inode2fid(inode)));
730 if (secctx != NULL && secctxlen != 0) {
731 /* no need to protect selinux_inode_setsecurity() by
732 * inode_lock. Taking it would lead to a client deadlock
735 rc = security_inode_notifysecctx(inode, secctx,
738 CWARN("%s: cannot set security context for "DFID": rc = %d\n",
739 ll_i2sbi(inode)->ll_fsname,
740 PFID(ll_inode2fid(inode)),
745 /* Only hash *de if it is unhashed (new dentry).
746 * Atoimc_open may passin hashed dentries for open.
748 alias = ll_splice_alias(inode, *de);
750 GOTO(out, rc = PTR_ERR(alias));
754 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
755 /* we have lookup look - unhide dentry */
756 if (bits & MDS_INODELOCK_LOOKUP)
757 d_lustre_revalidate(*de);
760 rc = llcrypt_get_encryption_info(inode);
763 if (!llcrypt_has_encryption_key(inode))
764 GOTO(out, rc = -ENOKEY);
766 } else if (!it_disposition(it, DISP_OPEN_CREATE)) {
768 * If file was created on the server, the dentry is revalidated
769 * in ll_create_it if the lock allows for it.
771 /* Check that parent has UPDATE lock. */
772 struct lookup_intent parent_it = {
774 .it_lock_handle = 0 };
775 struct lu_fid fid = ll_i2info(parent)->lli_fid;
777 /* If it is striped directory, get the real stripe parent */
778 if (unlikely(ll_dir_striped(parent))) {
779 rc = md_get_fid_from_lsm(ll_i2mdexp(parent),
780 ll_i2info(parent)->lli_lsm_md,
782 (*de)->d_name.len, &fid);
787 if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it, &fid,
789 d_lustre_revalidate(*de);
790 ll_intent_release(&parent_it);
794 if (it_disposition(it, DISP_OPEN_CREATE)) {
795 ll_stats_ops_tally(ll_i2sbi(parent), LPROC_LL_MKNOD,
796 ktime_us_delta(ktime_get(), kstart));
802 if (rc != 0 && it->it_op & IT_OPEN) {
803 ll_intent_drop_lock(it);
804 ll_open_cleanup((*de)->d_sb, &request->rq_pill);
810 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
811 struct lookup_intent *it,
812 void **secctx, __u32 *secctxlen,
813 struct pcc_create_attach *pca,
815 void **encctx, __u32 *encctxlen)
817 ktime_t kstart = ktime_get();
818 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
819 struct dentry *save = dentry, *retval;
820 struct ptlrpc_request *req = NULL;
821 struct md_op_data *op_data = NULL;
822 struct lov_user_md *lum = NULL;
825 char secctx_name[XATTR_NAME_MAX + 1];
829 if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
830 RETURN(ERR_PTR(-ENAMETOOLONG));
832 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p), intent=%s\n",
833 dentry, PFID(ll_inode2fid(parent)), parent, LL_IT2STR(it));
835 if (d_mountpoint(dentry))
836 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
838 if (it == NULL || it->it_op == IT_GETXATTR)
841 if (it->it_op == IT_GETATTR && dentry_may_statahead(parent, dentry)) {
842 rc = ll_revalidate_statahead(parent, &dentry, 0);
844 RETURN(dentry == save ? NULL : dentry);
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));
851 if (it->it_op & IT_CREAT)
852 opc = LUSTRE_OPC_CREATE;
854 opc = LUSTRE_OPC_ANY;
856 op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
857 dentry->d_name.len, 0, opc, NULL);
859 GOTO(out, retval = ERR_CAST(op_data));
861 /* enforce umask if acl disabled or MDS doesn't support umask */
862 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
863 it->it_create_mode &= ~current_umask();
865 if (it->it_op & IT_CREAT &&
866 ll_i2sbi(parent)->ll_flags & LL_SBI_FILE_SECCTX) {
867 rc = ll_dentry_init_security(dentry, it->it_create_mode,
869 &op_data->op_file_secctx_name,
870 &op_data->op_file_secctx,
871 &op_data->op_file_secctx_size);
873 GOTO(out, retval = ERR_PTR(rc));
875 *secctx = op_data->op_file_secctx;
876 if (secctxlen != NULL)
877 *secctxlen = op_data->op_file_secctx_size;
881 if (secctxlen != NULL)
884 if (it->it_op & IT_CREAT && encrypt) {
885 rc = llcrypt_inherit_context(parent, NULL, op_data, false);
887 GOTO(out, retval = ERR_PTR(rc));
889 *encctx = op_data->op_file_encctx;
890 if (encctxlen != NULL)
891 *encctxlen = op_data->op_file_encctx_size;
895 if (encctxlen != NULL)
899 /* ask for security context upon intent */
900 if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_OPEN)) {
901 /* get name of security xattr to request to server */
902 rc = ll_listsecurity(parent, secctx_name,
903 sizeof(secctx_name));
905 CDEBUG(D_SEC, "cannot get security xattr name for "
907 PFID(ll_inode2fid(parent)), rc);
909 op_data->op_file_secctx_name = secctx_name;
910 op_data->op_file_secctx_name_size = rc;
911 CDEBUG(D_SEC, "'%.*s' is security xattr for "DFID"\n",
912 rc, secctx_name, PFID(ll_inode2fid(parent)));
916 if (pca && pca->pca_dataset) {
919 GOTO(out, retval = ERR_PTR(-ENOMEM));
921 lum->lmm_magic = LOV_USER_MAGIC_V1;
922 lum->lmm_pattern = LOV_PATTERN_F_RELEASED | LOV_PATTERN_RAID0;
923 op_data->op_data = lum;
924 op_data->op_data_size = sizeof(*lum);
925 op_data->op_archive_id = pca->pca_dataset->pccd_rwid;
926 it->it_flags |= MDS_OPEN_PCC;
929 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
930 &ll_md_blocking_ast, 0);
931 /* If the MDS allows the client to chgrp (CFS_SETGRP_PERM), but the
932 * client does not know which suppgid should be sent to the MDS, or
933 * some other(s) changed the target file's GID after this RPC sent
934 * to the MDS with the suppgid as the original GID, then we should
935 * try again with right suppgid. */
936 if (rc == -EACCES && it->it_op & IT_OPEN &&
937 it_disposition(it, DISP_OPEN_DENY)) {
938 struct mdt_body *body;
940 LASSERT(req != NULL);
942 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
943 if (op_data->op_suppgids[0] == body->mbo_gid ||
944 op_data->op_suppgids[1] == body->mbo_gid ||
945 !in_group_p(make_kgid(&init_user_ns, body->mbo_gid)))
946 GOTO(out, retval = ERR_PTR(-EACCES));
948 fid_zero(&op_data->op_fid2);
949 op_data->op_suppgids[1] = body->mbo_gid;
950 ptlrpc_req_finished(req);
952 ll_intent_release(it);
953 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
954 &ll_md_blocking_ast, 0);
958 GOTO(out, retval = ERR_PTR(rc));
960 if (pca && pca->pca_dataset) {
961 rc = pcc_inode_create(parent->i_sb, pca->pca_dataset,
965 GOTO(out, retval = ERR_PTR(rc));
968 /* dir layout may change */
969 ll_unlock_md_op_lsm(op_data);
970 rc = ll_lookup_it_finish(req, it, parent, &dentry,
971 secctx != NULL ? *secctx : NULL,
972 secctxlen != NULL ? *secctxlen : 0,
973 encctx != NULL ? *encctx : NULL,
974 encctxlen != NULL ? *encctxlen : 0,
977 ll_intent_release(it);
978 GOTO(out, retval = ERR_PTR(rc));
981 if ((it->it_op & IT_OPEN) && dentry->d_inode &&
982 !S_ISREG(dentry->d_inode->i_mode) &&
983 !S_ISDIR(dentry->d_inode->i_mode)) {
984 ll_release_openhandle(dentry, it);
986 ll_lookup_finish_locks(it, dentry);
988 GOTO(out, retval = (dentry == save) ? NULL : dentry);
991 if (op_data != NULL && !IS_ERR(op_data)) {
992 if (secctx != NULL && secctxlen != NULL) {
993 /* caller needs sec ctx info, so reset it in op_data to
994 * prevent it from being freed */
995 op_data->op_file_secctx = NULL;
996 op_data->op_file_secctx_size = 0;
998 if (encctx != NULL && encctxlen != NULL &&
999 it->it_op & IT_CREAT && encrypt) {
1000 /* caller needs enc ctx info, so reset it in op_data to
1001 * prevent it from being freed
1003 op_data->op_file_encctx = NULL;
1004 op_data->op_file_encctx_size = 0;
1006 ll_finish_md_op_data(op_data);
1012 ptlrpc_req_finished(req);
1016 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
1019 struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
1022 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p), flags=%u\n",
1023 dentry, PFID(ll_inode2fid(parent)), parent, flags);
1026 * Optimize away (CREATE && !OPEN). Let .create handle the race.
1027 * but only if we have write permissions there, otherwise we need
1028 * to proceed with lookup. LU-4185
1030 if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN) &&
1031 (inode_permission(parent, MAY_WRITE | MAY_EXEC) == 0))
1034 if (flags & (LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE))
1038 de = ll_lookup_it(parent, dentry, itp, NULL, NULL, NULL, false,
1042 ll_intent_release(itp);
1047 #ifdef FMODE_CREATED /* added in Linux v4.18-rc1-20-g73a09dd */
1048 # define ll_is_opened(o, f) ((f)->f_mode & FMODE_OPENED)
1049 # define ll_finish_open(f, d, o) finish_open((f), (d), NULL)
1050 # define ll_last_arg
1051 # define ll_set_created(o, f) \
1053 (f)->f_mode |= FMODE_CREATED; \
1057 # define ll_is_opened(o, f) (*(o))
1058 # define ll_finish_open(f, d, o) finish_open((f), (d), NULL, (o))
1059 # define ll_last_arg , int *opened
1060 # define ll_set_created(o, f) \
1062 *(o) |= FILE_CREATED; \
1068 * For cached negative dentry and new dentry, handle lookup/create/open
1071 static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
1072 struct file *file, unsigned open_flags,
1073 umode_t mode ll_last_arg)
1075 struct lookup_intent *it;
1077 long long lookup_flags = LOOKUP_OPEN;
1078 void *secctx = NULL;
1079 __u32 secctxlen = 0;
1080 void *encctx = NULL;
1081 __u32 encctxlen = 0;
1082 struct ll_sb_info *sbi = NULL;
1083 struct pcc_create_attach pca = { NULL, NULL };
1084 bool encrypt = false;
1089 "VFS Op:name=%pd, dir="DFID"(%p), file %p, open_flags %x, mode %x opened %d\n",
1090 dentry, PFID(ll_inode2fid(dir)), dir, file, open_flags, mode,
1091 ll_is_opened(opened, file));
1093 /* Only negative dentries enter here */
1094 LASSERT(dentry->d_inode == NULL);
1096 if (!d_unhashed(dentry)) {
1097 /* A valid negative dentry that just passed revalidation,
1098 * there's little point to try and open it server-side,
1099 * even though there's a minuscule chance it might succeed.
1100 * Either way it's a valid race to just return -ENOENT here.
1102 if (!(open_flags & O_CREAT))
1105 /* Otherwise we just unhash it to be rehashed afresh via
1106 * lookup if necessary
1111 OBD_ALLOC(it, sizeof(*it));
1115 it->it_op = IT_OPEN;
1116 if (open_flags & O_CREAT) {
1117 it->it_op |= IT_CREAT;
1118 lookup_flags |= LOOKUP_CREATE;
1119 sbi = ll_i2sbi(dir);
1120 /* Volatile file is used for HSM restore, so do not use PCC */
1121 if (!filename_is_volatile(dentry->d_name.name,
1122 dentry->d_name.len, NULL)) {
1123 struct pcc_matcher item;
1124 struct pcc_dataset *dataset;
1126 item.pm_uid = from_kuid(&init_user_ns, current_uid());
1127 item.pm_gid = from_kgid(&init_user_ns, current_gid());
1128 item.pm_projid = ll_i2info(dir)->lli_projid;
1129 item.pm_name = &dentry->d_name;
1130 dataset = pcc_dataset_match_get(&sbi->ll_pcc_super,
1132 pca.pca_dataset = dataset;
1135 it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
1136 it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
1137 it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
1139 if (ll_sbi_has_encrypt(ll_i2sbi(dir)) && IS_ENCRYPTED(dir)) {
1140 /* in case of create, this is going to be a regular file because
1141 * we set S_IFREG bit on it->it_create_mode above
1143 rc = llcrypt_get_encryption_info(dir);
1145 GOTO(out_release, rc);
1146 if (open_flags & O_CREAT) {
1147 if (!llcrypt_has_encryption_key(dir))
1148 GOTO(out_release, rc = -ENOKEY);
1153 OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE2, cfs_fail_val);
1155 /* We can only arrive at this path when we have no inode, so
1156 * we only need to request open lock if it was requested
1159 if (ll_i2sbi(dir)->ll_oc_thrsh_count == 1 &&
1160 exp_connect_flags2(ll_i2mdexp(dir)) &
1161 OBD_CONNECT2_ATOMIC_OPEN_LOCK)
1162 it->it_flags |= MDS_OPEN_LOCK;
1164 /* Dentry added to dcache tree in ll_lookup_it */
1165 de = ll_lookup_it(dir, dentry, it, &secctx, &secctxlen, &pca, encrypt,
1166 &encctx, &encctxlen);
1169 else if (de != NULL)
1172 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
1175 if (it_disposition(it, DISP_OPEN_CREATE)) {
1176 /* Dentry instantiated in ll_create_it. */
1177 rc = ll_create_it(dir, dentry, it, secctx, secctxlen,
1178 encrypt, encctx, encctxlen);
1179 ll_security_release_secctx(secctx, secctxlen);
1180 llcrypt_free_ctx(encctx, encctxlen);
1182 /* We dget in ll_splice_alias. */
1188 rc = pcc_inode_create_fini(dentry->d_inode, &pca);
1192 GOTO(out_release, rc);
1195 ll_set_created(opened, file);
1197 /* Open the file with O_CREAT, but the file already
1198 * existed on MDT. This may happend in the case that
1199 * the LOOKUP ibits lock is revoked and the
1200 * corresponding dentry cache is deleted.
1201 * i.e. In the current Lustre, the truncate operation
1202 * will revoke the LOOKUP ibits lock, and the file
1203 * dentry cache will be invalidated. The following open
1204 * with O_CREAT flag will call into ->atomic_open, the
1205 * file was wrongly though as newly created file and
1206 * try to auto cache the file. So after client knows it
1207 * is not a DISP_OPEN_CREATE, it should cleanup the
1208 * already created PCC copy.
1210 pcc_create_attach_cleanup(dir->i_sb, &pca);
1212 if (open_flags & O_CREAT && encrypt &&
1214 rc = ll_set_encflags(dentry->d_inode, encctx,
1216 llcrypt_free_ctx(encctx, encctxlen);
1218 GOTO(out_release, rc);
1222 /* check also if a foreign file is openable */
1223 if (dentry->d_inode && it_disposition(it, DISP_OPEN_OPEN) &&
1224 ll_foreign_is_openable(dentry, open_flags)) {
1226 if (S_ISFIFO(dentry->d_inode->i_mode)) {
1227 /* We cannot call open here as it might
1228 * deadlock. This case is unreachable in
1229 * practice because of OBD_CONNECT_NODEVOH. */
1230 rc = finish_no_open(file, de);
1232 file->private_data = it;
1233 rc = ll_finish_open(file, dentry, opened);
1234 /* We dget in ll_splice_alias. finish_open takes
1235 * care of dget for fd open.
1241 rc = finish_no_open(file, de);
1244 pcc_create_attach_cleanup(dir->i_sb, &pca);
1248 ll_intent_release(it);
1249 OBD_FREE(it, sizeof(*it));
1254 /* We depend on "mode" being set with the proper file type/umask by now */
1255 static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
1257 struct inode *inode = NULL;
1258 struct ptlrpc_request *request = NULL;
1259 struct ll_sb_info *sbi = ll_i2sbi(dir);
1263 LASSERT(it && it->it_disposition);
1265 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
1266 request = it->it_request;
1267 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
1268 rc = ll_prep_inode(&inode, &request->rq_pill, dir->i_sb, it);
1270 GOTO(out, inode = ERR_PTR(rc));
1272 /* Pause to allow for a race with concurrent access by fid */
1273 OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_NODE_PAUSE, cfs_fail_val);
1275 /* We asked for a lock on the directory, but were granted a
1276 * lock on the inode. Since we finally have an inode pointer,
1277 * stuff it in the lock. */
1278 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode "DFID"(%p)\n",
1279 PFID(ll_inode2fid(inode)), inode);
1280 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
1283 ptlrpc_req_finished(request);
1288 * By the time this is called, we already have created the directory cache
1289 * entry for the new file, but it is so far negative - it has no inode.
1291 * We defer creating the OBD object(s) until open, to keep the intent and
1292 * non-intent code paths similar, and also because we do not have the MDS
1293 * inode number before calling ll_create_node() (which is needed for LOV),
1294 * so we would need to do yet another RPC to the MDS to store the LOV EA
1295 * data on the MDS. If needed, we would pass the PACKED lmm as data and
1296 * lmm_size in datalen (the MDS still has code which will handle that).
1298 * If the create succeeds, we fill in the inode information
1299 * with d_instantiate().
1301 static int ll_create_it(struct inode *dir, struct dentry *dentry,
1302 struct lookup_intent *it,
1303 void *secctx, __u32 secctxlen, bool encrypt,
1304 void *encctx, __u32 encctxlen)
1306 struct inode *inode;
1311 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p), intent=%s\n",
1312 dentry, PFID(ll_inode2fid(dir)), dir, LL_IT2STR(it));
1314 rc = it_open_error(DISP_OPEN_CREATE, it);
1318 inode = ll_create_node(dir, it);
1320 RETURN(PTR_ERR(inode));
1322 if ((ll_i2sbi(inode)->ll_flags & LL_SBI_FILE_SECCTX) &&
1324 /* must be done before d_instantiate, because it calls
1325 * security_d_instantiate, which means a getxattr if security
1326 * context is not set yet */
1327 /* no need to protect selinux_inode_setsecurity() by
1328 * inode_lock. Taking it would lead to a client deadlock
1331 rc = security_inode_notifysecctx(inode, secctx, secctxlen);
1336 d_instantiate(dentry, inode);
1339 rc = ll_set_encflags(inode, encctx, encctxlen, true);
1344 if (!(ll_i2sbi(inode)->ll_flags & LL_SBI_FILE_SECCTX)) {
1345 rc = ll_inode_init_security(dentry, inode, dir);
1350 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, inode, it, &bits);
1351 if (bits & MDS_INODELOCK_LOOKUP)
1352 d_lustre_revalidate(dentry);
1357 void ll_update_times(struct ptlrpc_request *request, struct inode *inode)
1359 struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
1363 if (body->mbo_valid & OBD_MD_FLMTIME &&
1364 body->mbo_mtime > inode->i_mtime.tv_sec) {
1366 "setting fid " DFID " mtime from %lld to %llu\n",
1367 PFID(ll_inode2fid(inode)),
1368 (s64)inode->i_mtime.tv_sec, body->mbo_mtime);
1369 inode->i_mtime.tv_sec = body->mbo_mtime;
1372 if (body->mbo_valid & OBD_MD_FLCTIME &&
1373 body->mbo_ctime > inode->i_ctime.tv_sec)
1374 inode->i_ctime.tv_sec = body->mbo_ctime;
1377 static int ll_new_node(struct inode *dir, struct dentry *dchild,
1378 const char *tgt, umode_t mode, int rdev, __u32 opc)
1380 struct qstr *name = &dchild->d_name;
1381 struct ptlrpc_request *request = NULL;
1382 struct md_op_data *op_data = NULL;
1383 struct inode *inode = NULL;
1384 struct ll_sb_info *sbi = ll_i2sbi(dir);
1386 bool encrypt = false;
1390 if (unlikely(tgt != NULL))
1391 tgt_len = strlen(tgt) + 1;
1394 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
1395 name->len, 0, opc, NULL);
1396 if (IS_ERR(op_data))
1397 GOTO(err_exit, err = PTR_ERR(op_data));
1399 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
1400 err = ll_dentry_init_security(dchild, mode, &dchild->d_name,
1401 &op_data->op_file_secctx_name,
1402 &op_data->op_file_secctx,
1403 &op_data->op_file_secctx_size);
1405 GOTO(err_exit, err);
1408 if (ll_sbi_has_encrypt(sbi) &&
1409 ((IS_ENCRYPTED(dir) &&
1410 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) ||
1411 (unlikely(llcrypt_dummy_context_enabled(dir)) && S_ISDIR(mode)))) {
1412 err = llcrypt_get_encryption_info(dir);
1414 GOTO(err_exit, err);
1415 if (!llcrypt_has_encryption_key(dir))
1416 GOTO(err_exit, err = -ENOKEY);
1421 err = llcrypt_inherit_context(dir, NULL, op_data, false);
1423 GOTO(err_exit, err);
1426 err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
1427 from_kuid(&init_user_ns, current_fsuid()),
1428 from_kgid(&init_user_ns, current_fsgid()),
1429 cfs_curproc_cap_pack(), rdev, &request);
1430 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 58, 0)
1432 * server < 2.12.58 doesn't pack default LMV in intent_getattr reply,
1433 * fetch default LMV here.
1435 if (unlikely(err == -EREMOTE)) {
1436 struct ll_inode_info *lli = ll_i2info(dir);
1437 struct lmv_user_md *lum;
1441 ptlrpc_req_finished(request);
1444 err2 = ll_dir_getstripe(dir, (void **)&lum, &lumsize, &request,
1445 OBD_MD_DEFAULT_MEA);
1446 ll_finish_md_op_data(op_data);
1449 struct lustre_md md = { NULL };
1451 md.body = req_capsule_server_get(&request->rq_pill,
1454 GOTO(err_exit, err = -EPROTO);
1456 OBD_ALLOC_PTR(md.default_lmv);
1457 if (!md.default_lmv)
1458 GOTO(err_exit, err = -ENOMEM);
1460 md.default_lmv->lsm_md_magic = lum->lum_magic;
1461 md.default_lmv->lsm_md_stripe_count =
1462 lum->lum_stripe_count;
1463 md.default_lmv->lsm_md_master_mdt_index =
1464 lum->lum_stripe_offset;
1465 md.default_lmv->lsm_md_hash_type = lum->lum_hash_type;
1466 md.default_lmv->lsm_md_max_inherit =
1467 lum->lum_max_inherit;
1468 md.default_lmv->lsm_md_max_inherit_rr =
1469 lum->lum_max_inherit_rr;
1471 err = ll_update_inode(dir, &md);
1472 md_free_lustre_md(sbi->ll_md_exp, &md);
1474 GOTO(err_exit, err);
1475 } else if (err2 == -ENODATA && lli->lli_default_lsm_md) {
1477 * If there are no default stripe EA on the MDT, but the
1478 * client has default stripe, then it probably means
1479 * default stripe EA has just been deleted.
1481 down_write(&lli->lli_lsm_sem);
1482 if (lli->lli_default_lsm_md)
1483 OBD_FREE_PTR(lli->lli_default_lsm_md);
1484 lli->lli_default_lsm_md = NULL;
1485 up_write(&lli->lli_lsm_sem);
1487 GOTO(err_exit, err);
1490 ptlrpc_req_finished(request);
1497 GOTO(err_exit, err);
1499 ll_update_times(request, dir);
1501 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_NEWNODE_PAUSE, cfs_fail_val);
1503 err = ll_prep_inode(&inode, &request->rq_pill, dchild->d_sb, NULL);
1505 GOTO(err_exit, err);
1507 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
1508 /* must be done before d_instantiate, because it calls
1509 * security_d_instantiate, which means a getxattr if security
1510 * context is not set yet */
1511 /* no need to protect selinux_inode_setsecurity() by
1512 * inode_lock. Taking it would lead to a client deadlock
1515 err = security_inode_notifysecctx(inode,
1516 op_data->op_file_secctx,
1517 op_data->op_file_secctx_size);
1519 GOTO(err_exit, err);
1522 d_instantiate(dchild, inode);
1525 err = ll_set_encflags(inode, op_data->op_file_encctx,
1526 op_data->op_file_encctx_size, true);
1528 GOTO(err_exit, err);
1531 if (!(sbi->ll_flags & LL_SBI_FILE_SECCTX)) {
1532 err = ll_inode_init_security(dchild, inode, dir);
1534 GOTO(err_exit, err);
1539 if (request != NULL)
1540 ptlrpc_req_finished(request);
1542 if (!IS_ERR_OR_NULL(op_data))
1543 ll_finish_md_op_data(op_data);
1548 static int ll_mknod(struct inode *dir, struct dentry *dchild, umode_t mode,
1551 ktime_t kstart = ktime_get();
1555 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p) mode %o dev %x\n",
1556 dchild, PFID(ll_inode2fid(dir)), dir, mode, rdev);
1558 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1559 mode &= ~current_umask();
1561 switch (mode & S_IFMT) {
1570 err = ll_new_node(dir, dchild, NULL, mode, old_encode_dev(rdev),
1581 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD,
1582 ktime_us_delta(ktime_get(), kstart));
1588 * Plain create. Intent create is handled in atomic_open.
1590 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1591 umode_t mode, bool want_excl)
1593 ktime_t kstart = ktime_get();
1596 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
1599 "VFS Op:name=%pd, dir="DFID"(%p), flags=%u, excl=%d\n",
1600 dentry, PFID(ll_inode2fid(dir)), dir, mode, want_excl);
1602 /* Using mknod(2) to create a regular file is designed to not recognize
1603 * volatile file name, so we use ll_mknod() here. */
1604 rc = ll_mknod(dir, dentry, mode, 0);
1606 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, unhashed %d\n",
1607 dentry, d_unhashed(dentry));
1610 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE,
1611 ktime_us_delta(ktime_get(), kstart));
1616 static int ll_symlink(struct inode *dir, struct dentry *dchild,
1617 const char *oldpath)
1619 ktime_t kstart = ktime_get();
1623 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p), target=%.*s\n",
1624 dchild, PFID(ll_inode2fid(dir)), dir, 3000, oldpath);
1626 err = ll_new_node(dir, dchild, oldpath, S_IFLNK | S_IRWXUGO, 0,
1627 LUSTRE_OPC_SYMLINK);
1630 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK,
1631 ktime_us_delta(ktime_get(), kstart));
1636 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1637 struct dentry *new_dentry)
1639 struct inode *src = old_dentry->d_inode;
1640 struct qstr *name = &new_dentry->d_name;
1641 struct ll_sb_info *sbi = ll_i2sbi(dir);
1642 struct ptlrpc_request *request = NULL;
1643 struct md_op_data *op_data;
1644 ktime_t kstart = ktime_get();
1649 "VFS Op: inode="DFID"(%p), dir="DFID"(%p), target=%pd\n",
1650 PFID(ll_inode2fid(src)), src,
1651 PFID(ll_inode2fid(dir)), dir, new_dentry);
1653 err = llcrypt_prepare_link(old_dentry, dir, new_dentry);
1657 op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len,
1658 0, LUSTRE_OPC_ANY, NULL);
1659 if (IS_ERR(op_data))
1660 RETURN(PTR_ERR(op_data));
1662 err = md_link(sbi->ll_md_exp, op_data, &request);
1663 ll_finish_md_op_data(op_data);
1667 ll_update_times(request, dir);
1668 ll_stats_ops_tally(sbi, LPROC_LL_LINK,
1669 ktime_us_delta(ktime_get(), kstart));
1672 ptlrpc_req_finished(request);
1676 static int ll_mkdir(struct inode *dir, struct dentry *dchild, umode_t mode)
1678 ktime_t kstart = ktime_get();
1682 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p)\n",
1683 dchild, PFID(ll_inode2fid(dir)), dir);
1685 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1686 mode &= ~current_umask();
1688 mode = (mode & (S_IRWXUGO|S_ISVTX)) | S_IFDIR;
1690 err = ll_new_node(dir, dchild, NULL, mode, 0, LUSTRE_OPC_MKDIR);
1692 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR,
1693 ktime_us_delta(ktime_get(), kstart));
1698 static int ll_rmdir(struct inode *dir, struct dentry *dchild)
1700 struct qstr *name = &dchild->d_name;
1701 struct ptlrpc_request *request = NULL;
1702 struct md_op_data *op_data;
1703 ktime_t kstart = ktime_get();
1708 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p)\n",
1709 dchild, PFID(ll_inode2fid(dir)), dir);
1711 if (unlikely(d_mountpoint(dchild)))
1714 /* some foreign dir may not be allowed to be removed */
1715 if (!ll_foreign_is_removable(dchild, false))
1718 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
1719 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1720 if (IS_ERR(op_data))
1721 RETURN(PTR_ERR(op_data));
1723 if (dchild->d_inode != NULL)
1724 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1726 op_data->op_fid2 = op_data->op_fid3;
1727 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1728 ll_finish_md_op_data(op_data);
1730 struct mdt_body *body;
1732 ll_update_times(request, dir);
1733 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR,
1734 ktime_us_delta(ktime_get(), kstart));
1737 * The server puts attributes in on the last unlink, use them
1738 * to update the link count so the inode can be freed
1741 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
1742 if (body->mbo_valid & OBD_MD_FLNLINK)
1743 set_nlink(dchild->d_inode, body->mbo_nlink);
1746 ptlrpc_req_finished(request);
1754 int ll_rmdir_entry(struct inode *dir, char *name, int namelen)
1756 struct ptlrpc_request *request = NULL;
1757 struct md_op_data *op_data;
1758 ktime_t kstart = ktime_get();
1762 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1763 namelen, name, PFID(ll_inode2fid(dir)), dir);
1765 op_data = ll_prep_md_op_data(NULL, dir, NULL, name, strlen(name),
1766 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1767 if (IS_ERR(op_data))
1768 RETURN(PTR_ERR(op_data));
1769 op_data->op_cli_flags |= CLI_RM_ENTRY;
1770 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1771 ll_finish_md_op_data(op_data);
1773 ll_update_times(request, dir);
1775 ptlrpc_req_finished(request);
1777 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR,
1778 ktime_us_delta(ktime_get(), kstart));
1782 static int ll_unlink(struct inode *dir, struct dentry *dchild)
1784 struct qstr *name = &dchild->d_name;
1785 struct ptlrpc_request *request = NULL;
1786 struct md_op_data *op_data;
1787 struct mdt_body *body;
1788 ktime_t kstart = ktime_get();
1793 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p)\n",
1794 dchild, PFID(ll_inode2fid(dir)), dir);
1797 * XXX: unlink bind mountpoint maybe call to here,
1798 * just check it as vfs_unlink does.
1800 if (unlikely(d_mountpoint(dchild)))
1803 /* some foreign file/dir may not be allowed to be unlinked */
1804 if (!ll_foreign_is_removable(dchild, false))
1807 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0,
1808 LUSTRE_OPC_ANY, NULL);
1809 if (IS_ERR(op_data))
1810 RETURN(PTR_ERR(op_data));
1812 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1813 /* notify lower layer if inode has dirty pages */
1814 if (S_ISREG(dchild->d_inode->i_mode) &&
1815 ll_i2info(dchild->d_inode)->lli_clob &&
1816 dirty_cnt(dchild->d_inode))
1817 op_data->op_cli_flags |= CLI_DIRTY_DATA;
1818 op_data->op_fid2 = op_data->op_fid3;
1819 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1820 ll_finish_md_op_data(op_data);
1825 * The server puts attributes in on the last unlink, use them to update
1826 * the link count so the inode can be freed immediately.
1828 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
1829 if (body->mbo_valid & OBD_MD_FLNLINK)
1830 set_nlink(dchild->d_inode, body->mbo_nlink);
1832 ll_update_times(request, dir);
1835 ptlrpc_req_finished(request);
1837 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK,
1838 ktime_us_delta(ktime_get(), kstart));
1842 static int ll_rename(struct inode *src, struct dentry *src_dchild,
1843 struct inode *tgt, struct dentry *tgt_dchild
1844 #ifdef HAVE_IOPS_RENAME_WITH_FLAGS
1845 , unsigned int flags
1849 struct qstr *src_name = &src_dchild->d_name;
1850 struct qstr *tgt_name = &tgt_dchild->d_name;
1851 struct ptlrpc_request *request = NULL;
1852 struct ll_sb_info *sbi = ll_i2sbi(src);
1853 struct md_op_data *op_data;
1854 ktime_t kstart = ktime_get();
1859 #ifdef HAVE_IOPS_RENAME_WITH_FLAGS
1865 "VFS Op:oldname=%pd, src_dir="DFID"(%p), newname=%pd, tgt_dir="DFID"(%p)\n",
1866 src_dchild, PFID(ll_inode2fid(src)), src,
1867 tgt_dchild, PFID(ll_inode2fid(tgt)), tgt);
1869 if (unlikely(d_mountpoint(src_dchild) || d_mountpoint(tgt_dchild)))
1872 #ifdef HAVE_IOPS_RENAME_WITH_FLAGS
1873 err = llcrypt_prepare_rename(src, src_dchild, tgt, tgt_dchild, flags);
1875 err = llcrypt_prepare_rename(src, src_dchild, tgt, tgt_dchild, 0);
1879 /* we prevent an encrypted file from being renamed
1880 * into an unencrypted dir
1882 if (IS_ENCRYPTED(src) && !IS_ENCRYPTED(tgt))
1885 if (src_dchild->d_inode)
1886 mode = src_dchild->d_inode->i_mode;
1888 if (tgt_dchild->d_inode)
1889 mode = tgt_dchild->d_inode->i_mode;
1891 op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, mode,
1892 LUSTRE_OPC_ANY, NULL);
1893 if (IS_ERR(op_data))
1894 RETURN(PTR_ERR(op_data));
1896 if (src_dchild->d_inode)
1897 op_data->op_fid3 = *ll_inode2fid(src_dchild->d_inode);
1899 if (tgt_dchild->d_inode)
1900 op_data->op_fid4 = *ll_inode2fid(tgt_dchild->d_inode);
1902 err = md_rename(sbi->ll_md_exp, op_data,
1903 src_name->name, src_name->len,
1904 tgt_name->name, tgt_name->len, &request);
1905 ll_finish_md_op_data(op_data);
1907 ll_update_times(request, src);
1908 ll_update_times(request, tgt);
1911 ptlrpc_req_finished(request);
1914 d_move(src_dchild, tgt_dchild);
1915 ll_stats_ops_tally(sbi, LPROC_LL_RENAME,
1916 ktime_us_delta(ktime_get(), kstart));
1922 const struct inode_operations ll_dir_inode_operations = {
1924 .atomic_open = ll_atomic_open,
1925 .lookup = ll_lookup_nd,
1926 .create = ll_create_nd,
1927 /* We need all these non-raw things for NFSD, to not patch it. */
1928 .unlink = ll_unlink,
1931 .symlink = ll_symlink,
1933 .rename = ll_rename,
1934 .setattr = ll_setattr,
1935 .getattr = ll_getattr,
1936 .permission = ll_inode_permission,
1937 #ifdef HAVE_IOP_XATTR
1938 .setxattr = ll_setxattr,
1939 .getxattr = ll_getxattr,
1940 .removexattr = ll_removexattr,
1942 .listxattr = ll_listxattr,
1943 .get_acl = ll_get_acl,
1944 #ifdef HAVE_IOP_SET_ACL
1945 .set_acl = ll_set_acl,
1949 const struct inode_operations ll_special_inode_operations = {
1950 .setattr = ll_setattr,
1951 .getattr = ll_getattr,
1952 .permission = ll_inode_permission,
1953 #ifdef HAVE_IOP_XATTR
1954 .setxattr = ll_setxattr,
1955 .getxattr = ll_getxattr,
1956 .removexattr = ll_removexattr,
1958 .listxattr = ll_listxattr,
1959 .get_acl = ll_get_acl,
1960 #ifdef HAVE_IOP_SET_ACL
1961 .set_acl = ll_set_acl,