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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 #include <linux/sched.h>
36 #include <linux/quotaops.h>
37 #include <linux/highmem.h>
38 #include <linux/pagemap.h>
39 #include <linux/security.h>
40 #include <linux/user_namespace.h>
41 #ifdef HAVE_UIDGID_HEADER
42 # include <linux/uidgid.h>
45 #define DEBUG_SUBSYSTEM S_LLITE
47 #include <obd_support.h>
48 #include <lustre_fid.h>
49 #include <lustre_dlm.h>
50 #include "llite_internal.h"
52 static int ll_create_it(struct inode *dir, struct dentry *dentry,
53 struct lookup_intent *it,
54 void *secctx, __u32 secctxlen);
56 /* called from iget5_locked->find_inode() under inode_lock spinlock */
57 static int ll_test_inode(struct inode *inode, void *opaque)
59 struct ll_inode_info *lli = ll_i2info(inode);
60 struct lustre_md *md = opaque;
62 if (unlikely(!(md->body->mbo_valid & OBD_MD_FLID))) {
63 CERROR("MDS body missing FID\n");
67 if (!lu_fid_eq(&lli->lli_fid, &md->body->mbo_fid1))
73 static int ll_set_inode(struct inode *inode, void *opaque)
75 struct ll_inode_info *lli = ll_i2info(inode);
76 struct mdt_body *body = ((struct lustre_md *)opaque)->body;
78 if (unlikely(!(body->mbo_valid & OBD_MD_FLID))) {
79 CERROR("MDS body missing FID\n");
83 lli->lli_fid = body->mbo_fid1;
84 if (unlikely(!(body->mbo_valid & OBD_MD_FLTYPE))) {
85 CERROR("Can not initialize inode "DFID" without object type: "
87 PFID(&lli->lli_fid), body->mbo_valid);
91 inode->i_mode = (inode->i_mode & ~S_IFMT) | (body->mbo_mode & S_IFMT);
92 if (unlikely(inode->i_mode == 0)) {
93 CERROR("Invalid inode "DFID" type\n", PFID(&lli->lli_fid));
104 * Get an inode by inode number(@hash), which is already instantiated by
105 * the intent lookup).
107 struct inode *ll_iget(struct super_block *sb, ino_t hash,
108 struct lustre_md *md)
116 inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
118 RETURN(ERR_PTR(-ENOMEM));
120 if (inode->i_state & I_NEW) {
121 rc = ll_read_inode2(inode, md);
122 if (rc == 0 && S_ISREG(inode->i_mode) &&
123 ll_i2info(inode)->lli_clob == NULL)
124 rc = cl_file_inode_init(inode, md);
127 /* Let's clear directory lsm here, otherwise
128 * make_bad_inode() will reset the inode mode
129 * to regular, then ll_clear_inode will not
130 * be able to clear lsm_md */
131 if (S_ISDIR(inode->i_mode))
132 ll_dir_clear_lsm_md(inode);
133 make_bad_inode(inode);
134 unlock_new_inode(inode);
138 inode_has_no_xattr(inode);
139 unlock_new_inode(inode);
141 } else if (is_bad_inode(inode)) {
143 inode = ERR_PTR(-ESTALE);
144 } else if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
145 rc = ll_update_inode(inode, md);
146 CDEBUG(D_VFSTRACE, "got inode: "DFID"(%p): rc = %d\n",
147 PFID(&md->body->mbo_fid1), inode, rc);
149 if (S_ISDIR(inode->i_mode))
150 ll_dir_clear_lsm_md(inode);
159 static void ll_invalidate_negative_children(struct inode *dir)
161 struct dentry *dentry, *tmp_subdir;
162 DECLARE_LL_D_HLIST_NODE_PTR(p);
164 spin_lock(&dir->i_lock);
165 ll_d_hlist_for_each_entry(dentry, p, &dir->i_dentry) {
166 spin_lock(&dentry->d_lock);
167 if (!list_empty(&dentry->d_subdirs)) {
168 struct dentry *child;
170 list_for_each_entry_safe(child, tmp_subdir,
173 if (child->d_inode == NULL)
174 d_lustre_invalidate(child, 1);
177 spin_unlock(&dentry->d_lock);
179 spin_unlock(&dir->i_lock);
182 int ll_test_inode_by_fid(struct inode *inode, void *opaque)
184 return lu_fid_eq(&ll_i2info(inode)->lli_fid, opaque);
187 static int ll_dom_lock_cancel(struct inode *inode, struct ldlm_lock *lock)
190 struct ll_inode_info *lli = ll_i2info(inode);
195 if (!lli->lli_clob) {
196 /* due to DoM read on open, there may exist pages for Lustre
197 * regular file even though cl_object is not set up yet. */
198 truncate_inode_pages(inode->i_mapping, 0);
202 env = cl_env_get(&refcheck);
204 RETURN(PTR_ERR(env));
206 /* reach MDC layer to flush data under the DoM ldlm lock */
207 rc = cl_object_flush(env, lli->lli_clob, lock);
209 cl_env_put(env, &refcheck);
213 static void ll_lock_cancel_bits(struct ldlm_lock *lock, __u64 to_cancel)
215 struct inode *inode = ll_inode_from_resource_lock(lock);
216 struct ll_inode_info *lli;
217 __u64 bits = to_cancel;
223 /* That means the inode is evicted most likely and may cause
224 * the skipping of lock cleanups below, so print the message
227 if (lock->l_resource->lr_lvb_inode)
229 "can't take inode for the lock (%sevicted)\n",
230 lock->l_resource->lr_lvb_inode->i_state &
231 I_FREEING ? "" : "not ");
235 if (!fid_res_name_eq(ll_inode2fid(inode),
236 &lock->l_resource->lr_name)) {
237 LDLM_ERROR(lock, "data mismatch with object "DFID"(%p)",
238 PFID(ll_inode2fid(inode)), inode);
242 if (bits & MDS_INODELOCK_XATTR) {
243 ll_xattr_cache_destroy(inode);
244 bits &= ~MDS_INODELOCK_XATTR;
247 /* For OPEN locks we differentiate between lock modes
248 * LCK_CR, LCK_CW, LCK_PR - bug 22891 */
249 if (bits & MDS_INODELOCK_OPEN)
250 ll_have_md_lock(inode, &bits, lock->l_req_mode);
252 if (bits & MDS_INODELOCK_OPEN) {
255 switch (lock->l_req_mode) {
266 LDLM_ERROR(lock, "bad lock mode for OPEN lock");
270 ll_md_real_close(inode, fmode);
272 bits &= ~MDS_INODELOCK_OPEN;
275 if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
276 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_PERM |
278 ll_have_md_lock(inode, &bits, LCK_MINMODE);
280 if (bits & MDS_INODELOCK_DOM) {
281 rc = ll_dom_lock_cancel(inode, lock);
283 CDEBUG(D_INODE, "cannot flush DoM data "
285 PFID(ll_inode2fid(inode)), rc);
288 if (bits & MDS_INODELOCK_LAYOUT) {
289 struct cl_object_conf conf = {
290 .coc_opc = OBJECT_CONF_INVALIDATE,
294 rc = ll_layout_conf(inode, &conf);
296 CDEBUG(D_INODE, "cannot invalidate layout of "
298 PFID(ll_inode2fid(inode)), rc);
301 lli = ll_i2info(inode);
303 if (bits & MDS_INODELOCK_UPDATE)
304 lli->lli_update_atime = 1;
306 if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
307 CDEBUG(D_INODE, "invalidating inode "DFID" lli = %p, "
308 "pfid = "DFID"\n", PFID(ll_inode2fid(inode)),
309 lli, PFID(&lli->lli_pfid));
310 truncate_inode_pages(inode->i_mapping, 0);
312 if (unlikely(!fid_is_zero(&lli->lli_pfid))) {
313 struct inode *master_inode = NULL;
316 /* This is slave inode, since all of the child dentry
317 * is connected on the master inode, so we have to
318 * invalidate the negative children on master inode */
319 CDEBUG(D_INODE, "Invalidate s"DFID" m"DFID"\n",
320 PFID(ll_inode2fid(inode)), PFID(&lli->lli_pfid));
322 hash = cl_fid_build_ino(&lli->lli_pfid,
323 ll_need_32bit_api(ll_i2sbi(inode)));
325 /* Do not lookup the inode with ilookup5, otherwise
326 * it will cause dead lock,
327 * 1. Client1 send chmod req to the MDT0, then on MDT0,
328 * it enqueues master and all of its slaves lock,
329 * (mdt_attr_set() -> mdt_lock_slaves()), after gets
330 * master and stripe0 lock, it will send the enqueue
331 * req (for stripe1) to MDT1, then MDT1 finds the lock
332 * has been granted to client2. Then MDT1 sends blocking
334 * 2. At the same time, client2 tries to unlink
335 * the striped dir (rm -rf striped_dir), and during
336 * lookup, it will hold the master inode of the striped
337 * directory, whose inode state is NEW, then tries to
338 * revalidate all of its slaves, (ll_prep_inode()->
339 * ll_iget()->ll_read_inode2()-> ll_update_inode().).
340 * And it will be blocked on the server side because
342 * 3. Then the client get the blocking_ast req, cancel
343 * the lock, but being blocked if using ->ilookup5()),
344 * because master inode state is NEW. */
345 master_inode = ilookup5_nowait(inode->i_sb, hash,
346 ll_test_inode_by_fid,
347 (void *)&lli->lli_pfid);
349 ll_invalidate_negative_children(master_inode);
353 ll_invalidate_negative_children(inode);
357 if ((bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM)) &&
358 inode->i_sb->s_root != NULL &&
359 inode != inode->i_sb->s_root->d_inode)
360 ll_invalidate_aliases(inode);
362 if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM))
363 forget_all_cached_acls(inode);
369 /* Check if the given lock may be downgraded instead of canceling and
370 * that convert is really needed. */
371 int ll_md_need_convert(struct ldlm_lock *lock)
373 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
375 __u64 wanted = lock->l_policy_data.l_inodebits.cancel_bits;
376 __u64 bits = lock->l_policy_data.l_inodebits.bits & ~wanted;
377 enum ldlm_mode mode = LCK_MINMODE;
379 if (!lock->l_conn_export ||
380 !exp_connect_lock_convert(lock->l_conn_export))
383 if (!wanted || !bits || ldlm_is_cancel(lock))
386 /* do not convert locks other than DOM for now */
387 if (!((bits | wanted) & MDS_INODELOCK_DOM))
390 /* We may have already remaining bits in some other lock so
391 * lock convert will leave us just extra lock for the same bit.
392 * Check if client has other lock with the same bits and the same
393 * or lower mode and don't convert if any.
395 switch (lock->l_req_mode) {
409 /* do not convert other modes */
413 /* is lock is too old to be converted? */
414 lock_res_and_lock(lock);
415 if (ktime_after(ktime_get(),
416 ktime_add(lock->l_last_used,
417 ktime_set(ns->ns_dirty_age_limit, 0)))) {
418 unlock_res_and_lock(lock);
421 unlock_res_and_lock(lock);
423 inode = ll_inode_from_resource_lock(lock);
424 ll_have_md_lock(inode, &bits, mode);
429 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
430 void *data, int flag)
432 struct lustre_handle lockh;
433 __u64 bits = lock->l_policy_data.l_inodebits.bits;
439 case LDLM_CB_BLOCKING:
441 __u64 cancel_flags = LCF_ASYNC;
443 if (ll_md_need_convert(lock)) {
444 cancel_flags |= LCF_CONVERT;
445 /* For lock convert some cancel actions may require
446 * this lock with non-dropped canceled bits, e.g. page
447 * flush for DOM lock. So call ll_lock_cancel_bits()
448 * here while canceled bits are still set.
450 bits = lock->l_policy_data.l_inodebits.cancel_bits;
451 if (bits & MDS_INODELOCK_DOM)
452 ll_lock_cancel_bits(lock, MDS_INODELOCK_DOM);
454 ldlm_lock2handle(lock, &lockh);
455 rc = ldlm_cli_cancel(&lockh, cancel_flags);
457 CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
462 case LDLM_CB_CANCELING:
463 /* Nothing to do for non-granted locks */
464 if (!ldlm_is_granted(lock))
467 if (ldlm_is_converting(lock)) {
468 /* this is called on already converted lock, so
469 * ibits has remained bits only and cancel_bits
470 * are bits that were dropped.
471 * Note that DOM lock is handled prior lock convert
472 * and is excluded here.
474 bits = lock->l_policy_data.l_inodebits.cancel_bits &
477 LASSERT(ldlm_is_canceling(lock));
479 ll_lock_cancel_bits(lock, bits);
488 __u32 ll_i2suppgid(struct inode *i)
490 if (in_group_p(i->i_gid))
491 return (__u32)from_kgid(&init_user_ns, i->i_gid);
493 return (__u32) __kgid_val(INVALID_GID);
496 /* Pack the required supplementary groups into the supplied groups array.
497 * If we don't need to use the groups from the target inode(s) then we
498 * instead pack one or more groups from the user's supplementary group
499 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
500 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
503 LASSERT(suppgids != NULL);
505 suppgids[0] = ll_i2suppgid(i1);
508 suppgids[1] = ll_i2suppgid(i2);
514 * try to reuse three types of dentry:
515 * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
516 * by concurrent .revalidate).
517 * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
518 * be cleared by others calling d_lustre_revalidate).
519 * 3. DISCONNECTED alias.
521 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
523 struct dentry *alias, *discon_alias, *invalid_alias;
524 DECLARE_LL_D_HLIST_NODE_PTR(p);
526 if (ll_d_hlist_empty(&inode->i_dentry))
529 discon_alias = invalid_alias = NULL;
531 spin_lock(&inode->i_lock);
532 ll_d_hlist_for_each_entry(alias, p, &inode->i_dentry) {
533 LASSERT(alias != dentry);
535 spin_lock(&alias->d_lock);
536 if ((alias->d_flags & DCACHE_DISCONNECTED) &&
537 S_ISDIR(inode->i_mode))
538 /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
539 discon_alias = alias;
540 else if (alias->d_parent == dentry->d_parent &&
541 alias->d_name.hash == dentry->d_name.hash &&
542 alias->d_name.len == dentry->d_name.len &&
543 memcmp(alias->d_name.name, dentry->d_name.name,
544 dentry->d_name.len) == 0)
545 invalid_alias = alias;
546 spin_unlock(&alias->d_lock);
551 alias = invalid_alias ?: discon_alias ?: NULL;
553 spin_lock(&alias->d_lock);
555 spin_unlock(&alias->d_lock);
557 spin_unlock(&inode->i_lock);
563 * Similar to d_splice_alias(), but lustre treats invalid alias
564 * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
566 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
572 new = ll_find_alias(inode, de);
582 "Reuse dentry %p inode %p refc %d flags %#x\n",
583 new, new->d_inode, ll_d_count(new), new->d_flags);
591 CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
592 de, de->d_inode, ll_d_count(de), de->d_flags);
596 static int ll_lookup_it_finish(struct ptlrpc_request *request,
597 struct lookup_intent *it,
598 struct inode *parent, struct dentry **de,
599 void *secctx, __u32 secctxlen)
601 struct inode *inode = NULL;
604 struct dentry *alias;
607 /* NB 1 request reference will be taken away by ll_intent_lock()
609 CDEBUG(D_DENTRY, "it %p it_disposition %x\n", it,
611 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
612 struct req_capsule *pill = &request->rq_pill;
613 struct mdt_body *body = req_capsule_server_get(pill,
616 rc = ll_prep_inode(&inode, request, (*de)->d_sb, it);
620 if (it->it_op & IT_OPEN)
621 ll_dom_finish_open(inode, request, it);
623 ll_set_lock_data(ll_i2sbi(parent)->ll_md_exp, inode, it, &bits);
625 /* We used to query real size from OSTs here, but actually
626 * this is not needed. For stat() calls size would be updated
627 * from subsequent do_revalidate()->ll_inode_revalidate_it() in
629 * vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
630 * Everybody else who needs correct file size would call
631 * ll_glimpse_size or some equivalent themselves anyway.
635 /* If security context was returned by MDT, put it in
636 * inode now to save an extra getxattr from security hooks,
637 * and avoid deadlock.
639 if (body->mbo_valid & OBD_MD_SECCTX) {
640 secctx = req_capsule_server_get(pill, &RMF_FILE_SECCTX);
641 secctxlen = req_capsule_get_size(pill,
646 CDEBUG(D_SEC, "server returned security context"
648 PFID(ll_inode2fid(inode)));
651 if (secctx != NULL && secctxlen != 0) {
653 rc = security_inode_notifysecctx(inode, secctx,
657 CWARN("cannot set security context for "
659 PFID(ll_inode2fid(inode)), rc);
663 /* Only hash *de if it is unhashed (new dentry).
664 * Atoimc_open may passin hashed dentries for open.
666 alias = ll_splice_alias(inode, *de);
668 GOTO(out, rc = PTR_ERR(alias));
672 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
673 /* we have lookup look - unhide dentry */
674 if (bits & MDS_INODELOCK_LOOKUP)
675 d_lustre_revalidate(*de);
676 } else if (!it_disposition(it, DISP_OPEN_CREATE)) {
678 * If file was created on the server, the dentry is revalidated
679 * in ll_create_it if the lock allows for it.
681 /* Check that parent has UPDATE lock. */
682 struct lookup_intent parent_it = {
684 .it_lock_handle = 0 };
685 struct lu_fid fid = ll_i2info(parent)->lli_fid;
687 /* If it is striped directory, get the real stripe parent */
688 if (unlikely(ll_dir_striped(parent))) {
689 rc = md_get_fid_from_lsm(ll_i2mdexp(parent),
690 ll_i2info(parent)->lli_lsm_md,
692 (*de)->d_name.len, &fid);
697 if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it, &fid,
699 d_lustre_revalidate(*de);
700 ll_intent_release(&parent_it);
707 if (rc != 0 && it->it_op & IT_OPEN)
708 ll_open_cleanup((*de)->d_sb, request);
713 struct pcc_create_attach {
714 struct pcc_dataset *pca_dataset;
715 struct dentry *pca_dentry;
718 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
719 struct lookup_intent *it,
720 void **secctx, __u32 *secctxlen,
721 struct pcc_create_attach *pca)
723 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
724 struct dentry *save = dentry, *retval;
725 struct ptlrpc_request *req = NULL;
726 struct md_op_data *op_data = NULL;
727 struct lov_user_md *lum = NULL;
730 char secctx_name[XATTR_NAME_MAX + 1];
734 if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
735 RETURN(ERR_PTR(-ENAMETOOLONG));
737 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), intent=%s\n",
738 dentry->d_name.len, dentry->d_name.name,
739 PFID(ll_inode2fid(parent)), parent, LL_IT2STR(it));
741 if (d_mountpoint(dentry))
742 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
744 if (it == NULL || it->it_op == IT_GETXATTR)
747 if (it->it_op == IT_GETATTR && dentry_may_statahead(parent, dentry)) {
748 rc = ll_statahead(parent, &dentry, 0);
750 RETURN(dentry == save ? NULL : dentry);
753 if (it->it_op & IT_OPEN && it->it_flags & FMODE_WRITE &&
754 dentry->d_sb->s_flags & SB_RDONLY)
755 RETURN(ERR_PTR(-EROFS));
757 if (it->it_op & IT_CREAT)
758 opc = LUSTRE_OPC_CREATE;
760 opc = LUSTRE_OPC_ANY;
762 op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
763 dentry->d_name.len, 0, opc, NULL);
765 GOTO(out, retval = ERR_CAST(op_data));
767 /* enforce umask if acl disabled or MDS doesn't support umask */
768 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
769 it->it_create_mode &= ~current_umask();
771 if (it->it_op & IT_CREAT &&
772 ll_i2sbi(parent)->ll_flags & LL_SBI_FILE_SECCTX) {
773 rc = ll_dentry_init_security(dentry, it->it_create_mode,
775 &op_data->op_file_secctx_name,
776 &op_data->op_file_secctx,
777 &op_data->op_file_secctx_size);
779 GOTO(out, retval = ERR_PTR(rc));
781 *secctx = op_data->op_file_secctx;
782 if (secctxlen != NULL)
783 *secctxlen = op_data->op_file_secctx_size;
787 if (secctxlen != NULL)
791 /* ask for security context upon intent */
792 if (it->it_op & (IT_LOOKUP | IT_GETATTR | IT_OPEN)) {
793 /* get name of security xattr to request to server */
794 rc = ll_listsecurity(parent, secctx_name,
795 sizeof(secctx_name));
797 CDEBUG(D_SEC, "cannot get security xattr name for "
799 PFID(ll_inode2fid(parent)), rc);
801 op_data->op_file_secctx_name = secctx_name;
802 op_data->op_file_secctx_name_size = rc;
803 CDEBUG(D_SEC, "'%.*s' is security xattr for "DFID"\n",
804 rc, secctx_name, PFID(ll_inode2fid(parent)));
808 if (pca && pca->pca_dataset) {
809 struct pcc_dataset *dataset = pca->pca_dataset;
813 GOTO(out, retval = ERR_PTR(-ENOMEM));
815 lum->lmm_magic = LOV_USER_MAGIC_V1;
816 lum->lmm_pattern = LOV_PATTERN_F_RELEASED | LOV_PATTERN_RAID0;
817 op_data->op_data = lum;
818 op_data->op_data_size = sizeof(*lum);
819 op_data->op_archive_id = dataset->pccd_rwid;
821 rc = obd_fid_alloc(NULL, ll_i2mdexp(parent), &op_data->op_fid2,
824 GOTO(out, retval = ERR_PTR(rc));
826 rc = pcc_inode_create(parent->i_sb, dataset, &op_data->op_fid2,
829 GOTO(out, retval = ERR_PTR(rc));
831 it->it_flags |= MDS_OPEN_PCC;
834 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
835 &ll_md_blocking_ast, 0);
836 /* If the MDS allows the client to chgrp (CFS_SETGRP_PERM), but the
837 * client does not know which suppgid should be sent to the MDS, or
838 * some other(s) changed the target file's GID after this RPC sent
839 * to the MDS with the suppgid as the original GID, then we should
840 * try again with right suppgid. */
841 if (rc == -EACCES && it->it_op & IT_OPEN &&
842 it_disposition(it, DISP_OPEN_DENY)) {
843 struct mdt_body *body;
845 LASSERT(req != NULL);
847 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
848 if (op_data->op_suppgids[0] == body->mbo_gid ||
849 op_data->op_suppgids[1] == body->mbo_gid ||
850 !in_group_p(make_kgid(&init_user_ns, body->mbo_gid)))
851 GOTO(out, retval = ERR_PTR(-EACCES));
853 fid_zero(&op_data->op_fid2);
854 op_data->op_suppgids[1] = body->mbo_gid;
855 ptlrpc_req_finished(req);
857 ll_intent_release(it);
858 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
859 &ll_md_blocking_ast, 0);
863 GOTO(out, retval = ERR_PTR(rc));
865 /* dir layout may change */
866 ll_unlock_md_op_lsm(op_data);
867 rc = ll_lookup_it_finish(req, it, parent, &dentry,
868 secctx != NULL ? *secctx : NULL,
869 secctxlen != NULL ? *secctxlen : 0);
871 ll_intent_release(it);
872 GOTO(out, retval = ERR_PTR(rc));
875 if ((it->it_op & IT_OPEN) && dentry->d_inode &&
876 !S_ISREG(dentry->d_inode->i_mode) &&
877 !S_ISDIR(dentry->d_inode->i_mode)) {
878 ll_release_openhandle(dentry, it);
880 ll_lookup_finish_locks(it, dentry);
882 GOTO(out, retval = (dentry == save) ? NULL : dentry);
885 if (op_data != NULL && !IS_ERR(op_data)) {
886 if (secctx != NULL && secctxlen != NULL) {
887 /* caller needs sec ctx info, so reset it in op_data to
888 * prevent it from being freed */
889 op_data->op_file_secctx = NULL;
890 op_data->op_file_secctx_size = 0;
892 ll_finish_md_op_data(op_data);
898 ptlrpc_req_finished(req);
902 #ifdef HAVE_IOP_ATOMIC_OPEN
903 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
906 struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
909 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), flags=%u\n",
910 dentry->d_name.len, dentry->d_name.name,
911 PFID(ll_inode2fid(parent)), parent, flags);
914 * Optimize away (CREATE && !OPEN). Let .create handle the race.
915 * but only if we have write permissions there, otherwise we need
916 * to proceed with lookup. LU-4185
918 if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN) &&
919 (inode_permission(parent, MAY_WRITE | MAY_EXEC) == 0))
922 if (flags & (LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE))
926 de = ll_lookup_it(parent, dentry, itp, NULL, NULL, NULL);
929 ll_intent_release(itp);
934 #ifdef FMODE_CREATED /* added in Linux v4.18-rc1-20-g73a09dd */
935 # define ll_is_opened(o, f) ((f)->f_mode & FMODE_OPENED)
936 # define ll_finish_open(f, d, o) finish_open((f), (d), NULL)
938 # define ll_set_created(o, f) \
940 (f)->f_mode |= FMODE_CREATED; \
944 # define ll_is_opened(o, f) (*(o))
945 # define ll_finish_open(f, d, o) finish_open((f), (d), NULL, (o))
946 # define ll_last_arg , int *opened
947 # define ll_set_created(o, f) \
949 *(o) |= FILE_CREATED; \
955 * For cached negative dentry and new dentry, handle lookup/create/open
958 static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
959 struct file *file, unsigned open_flags,
960 umode_t mode ll_last_arg)
962 struct lookup_intent *it;
964 long long lookup_flags = LOOKUP_OPEN;
967 struct ll_sb_info *sbi;
968 struct pcc_create_attach pca = {NULL, NULL};
969 struct pcc_dataset *dataset = NULL;
973 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), file %p,"
974 "open_flags %x, mode %x opened %d\n",
975 dentry->d_name.len, dentry->d_name.name,
976 PFID(ll_inode2fid(dir)), dir, file, open_flags, mode,
977 ll_is_opened(opened, file));
979 /* Only negative dentries enter here */
980 LASSERT(dentry->d_inode == NULL);
982 if (!d_unhashed(dentry)) {
983 /* A valid negative dentry that just passed revalidation,
984 * there's little point to try and open it server-side,
985 * even though there's a minuscule chance it might succeed.
986 * Either way it's a valid race to just return -ENOENT here.
988 if (!(open_flags & O_CREAT))
991 /* Otherwise we just unhash it to be rehashed afresh via
992 * lookup if necessary
997 OBD_ALLOC(it, sizeof(*it));
1001 it->it_op = IT_OPEN;
1002 if (open_flags & O_CREAT) {
1003 it->it_op |= IT_CREAT;
1004 lookup_flags |= LOOKUP_CREATE;
1005 sbi = ll_i2sbi(dir);
1006 /* Volatile file is used for HSM restore, so do not use PCC */
1007 if (!filename_is_volatile(dentry->d_name.name,
1008 dentry->d_name.len, NULL)) {
1009 struct pcc_matcher item;
1011 item.pm_uid = from_kuid(&init_user_ns, current_uid());
1012 item.pm_gid = from_kgid(&init_user_ns, current_gid());
1013 item.pm_projid = ll_i2info(dir)->lli_projid;
1014 item.pm_name = &dentry->d_name;
1015 dataset = pcc_dataset_match_get(&sbi->ll_pcc_super,
1017 pca.pca_dataset = dataset;
1020 it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
1021 it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
1022 it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
1024 /* Dentry added to dcache tree in ll_lookup_it */
1025 de = ll_lookup_it(dir, dentry, it, &secctx, &secctxlen, &pca);
1028 else if (de != NULL)
1031 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
1034 if (it_disposition(it, DISP_OPEN_CREATE)) {
1035 /* Dentry instantiated in ll_create_it. */
1036 rc = ll_create_it(dir, dentry, it, secctx, secctxlen);
1037 security_release_secctx(secctx, secctxlen);
1039 /* We dget in ll_splice_alias. */
1044 if (dataset != NULL && dentry->d_inode) {
1045 rc = pcc_inode_create_fini(dataset,
1051 GOTO(out_release, rc);
1054 ll_set_created(opened, file);
1057 if (dentry->d_inode && it_disposition(it, DISP_OPEN_OPEN)) {
1059 if (S_ISFIFO(dentry->d_inode->i_mode)) {
1060 /* We cannot call open here as it might
1061 * deadlock. This case is unreachable in
1062 * practice because of OBD_CONNECT_NODEVOH. */
1063 rc = finish_no_open(file, de);
1065 file->private_data = it;
1066 rc = ll_finish_open(file, dentry, opened);
1067 /* We dget in ll_splice_alias. finish_open takes
1068 * care of dget for fd open.
1074 rc = finish_no_open(file, de);
1079 if (dataset != NULL)
1080 pcc_dataset_put(dataset);
1081 ll_intent_release(it);
1082 OBD_FREE(it, sizeof(*it));
1087 #else /* !HAVE_IOP_ATOMIC_OPEN */
1088 static struct lookup_intent *
1089 ll_convert_intent(struct open_intent *oit, int lookup_flags, bool is_readonly)
1091 struct lookup_intent *it;
1095 return ERR_PTR(-ENOMEM);
1097 if (lookup_flags & LOOKUP_OPEN) {
1098 it->it_op = IT_OPEN;
1099 /* Avoid file creation for ro bind mount point(is_readonly) */
1100 if ((lookup_flags & LOOKUP_CREATE) && !is_readonly)
1101 it->it_op |= IT_CREAT;
1102 it->it_create_mode = (oit->create_mode & S_IALLUGO) | S_IFREG;
1103 it->it_flags = ll_namei_to_lookup_intent_flag(oit->flags &
1104 ~(is_readonly ? O_CREAT : 0));
1105 it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
1107 it->it_op = IT_GETATTR;
1113 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
1114 struct nameidata *nd)
1119 if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
1120 struct lookup_intent *it;
1122 if (ll_d2d(dentry) && ll_d2d(dentry)->lld_it) {
1123 it = ll_d2d(dentry)->lld_it;
1124 ll_d2d(dentry)->lld_it = NULL;
1127 * Optimize away (CREATE && !OPEN). Let .create handle
1128 * the race. But only if we have write permissions
1129 * there, otherwise we need to proceed with lookup.
1132 if ((nd->flags & LOOKUP_CREATE) &&
1133 !(nd->flags & LOOKUP_OPEN) &&
1134 (inode_permission(parent,
1135 MAY_WRITE | MAY_EXEC) == 0))
1138 it = ll_convert_intent(&nd->intent.open, nd->flags,
1139 (nd->path.mnt->mnt_flags & MNT_READONLY) ||
1140 (nd->path.mnt->mnt_sb->s_flags & SB_RDONLY));
1142 RETURN((struct dentry *)it);
1145 de = ll_lookup_it(parent, dentry, it, NULL, NULL, NULL);
1148 if ((nd->flags & LOOKUP_OPEN) && !IS_ERR(dentry)) { /* Open */
1149 if (dentry->d_inode &&
1150 it_disposition(it, DISP_OPEN_OPEN)) { /* nocreate */
1151 if (S_ISFIFO(dentry->d_inode->i_mode)) {
1152 /* We cannot call open here as it might
1153 * deadlock. This case is unreachable in
1154 * practice because of
1155 * OBD_CONNECT_NODEVOH. */
1159 nd->intent.open.file->private_data = it;
1160 filp = lookup_instantiate_filp(nd,
1166 de = (struct dentry *)filp;
1169 } else if (it_disposition(it, DISP_OPEN_CREATE)) {
1170 /* XXX This can only reliably work on assumption
1171 * that there are NO hashed negative dentries.*/
1172 ll_d2d(dentry)->lld_it = it;
1173 it = NULL; /* Will be freed in ll_create_nd */
1174 /* We absolutely depend on ll_create_nd to be
1175 * called to not leak this intent and possible
1176 * data attached to it */
1181 ll_intent_release(it);
1182 OBD_FREE(it, sizeof(*it));
1185 de = ll_lookup_it(parent, dentry, NULL, NULL, NULL, NULL);
1190 #endif /* HAVE_IOP_ATOMIC_OPEN */
1192 /* We depend on "mode" being set with the proper file type/umask by now */
1193 static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
1195 struct inode *inode = NULL;
1196 struct ptlrpc_request *request = NULL;
1197 struct ll_sb_info *sbi = ll_i2sbi(dir);
1201 LASSERT(it && it->it_disposition);
1203 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
1204 request = it->it_request;
1205 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
1206 rc = ll_prep_inode(&inode, request, dir->i_sb, it);
1208 GOTO(out, inode = ERR_PTR(rc));
1210 /* Pause to allow for a race with concurrent access by fid */
1211 OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_NODE_PAUSE, cfs_fail_val);
1213 /* We asked for a lock on the directory, but were granted a
1214 * lock on the inode. Since we finally have an inode pointer,
1215 * stuff it in the lock. */
1216 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode "DFID"(%p)\n",
1217 PFID(ll_inode2fid(inode)), inode);
1218 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
1221 ptlrpc_req_finished(request);
1226 * By the time this is called, we already have created the directory cache
1227 * entry for the new file, but it is so far negative - it has no inode.
1229 * We defer creating the OBD object(s) until open, to keep the intent and
1230 * non-intent code paths similar, and also because we do not have the MDS
1231 * inode number before calling ll_create_node() (which is needed for LOV),
1232 * so we would need to do yet another RPC to the MDS to store the LOV EA
1233 * data on the MDS. If needed, we would pass the PACKED lmm as data and
1234 * lmm_size in datalen (the MDS still has code which will handle that).
1236 * If the create succeeds, we fill in the inode information
1237 * with d_instantiate().
1239 static int ll_create_it(struct inode *dir, struct dentry *dentry,
1240 struct lookup_intent *it,
1241 void *secctx, __u32 secctxlen)
1243 struct inode *inode;
1248 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), intent=%s\n",
1249 dentry->d_name.len, dentry->d_name.name,
1250 PFID(ll_inode2fid(dir)), dir, LL_IT2STR(it));
1252 rc = it_open_error(DISP_OPEN_CREATE, it);
1256 inode = ll_create_node(dir, it);
1258 RETURN(PTR_ERR(inode));
1260 if ((ll_i2sbi(inode)->ll_flags & LL_SBI_FILE_SECCTX) &&
1263 /* must be done before d_instantiate, because it calls
1264 * security_d_instantiate, which means a getxattr if security
1265 * context is not set yet */
1266 rc = security_inode_notifysecctx(inode, secctx, secctxlen);
1267 inode_unlock(inode);
1272 d_instantiate(dentry, inode);
1274 if (!(ll_i2sbi(inode)->ll_flags & LL_SBI_FILE_SECCTX)) {
1275 rc = ll_inode_init_security(dentry, inode, dir);
1280 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, inode, it, &bits);
1281 if (bits & MDS_INODELOCK_LOOKUP)
1282 d_lustre_revalidate(dentry);
1287 void ll_update_times(struct ptlrpc_request *request, struct inode *inode)
1289 struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
1293 if (body->mbo_valid & OBD_MD_FLMTIME &&
1294 body->mbo_mtime > inode->i_mtime.tv_sec) {
1296 "setting fid " DFID " mtime from %lld to %llu\n",
1297 PFID(ll_inode2fid(inode)),
1298 (s64)inode->i_mtime.tv_sec, body->mbo_mtime);
1299 inode->i_mtime.tv_sec = body->mbo_mtime;
1302 if (body->mbo_valid & OBD_MD_FLCTIME &&
1303 body->mbo_ctime > inode->i_ctime.tv_sec)
1304 inode->i_ctime.tv_sec = body->mbo_ctime;
1307 static int ll_new_node(struct inode *dir, struct dentry *dchild,
1308 const char *tgt, umode_t mode, int rdev, __u32 opc)
1310 struct qstr *name = &dchild->d_name;
1311 struct ptlrpc_request *request = NULL;
1312 struct md_op_data *op_data;
1313 struct inode *inode = NULL;
1314 struct ll_sb_info *sbi = ll_i2sbi(dir);
1319 if (unlikely(tgt != NULL))
1320 tgt_len = strlen(tgt) + 1;
1323 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
1324 name->len, 0, opc, NULL);
1325 if (IS_ERR(op_data))
1326 GOTO(err_exit, err = PTR_ERR(op_data));
1328 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
1329 err = ll_dentry_init_security(dchild, mode, &dchild->d_name,
1330 &op_data->op_file_secctx_name,
1331 &op_data->op_file_secctx,
1332 &op_data->op_file_secctx_size);
1334 GOTO(err_exit, err);
1337 err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
1338 from_kuid(&init_user_ns, current_fsuid()),
1339 from_kgid(&init_user_ns, current_fsgid()),
1340 cfs_curproc_cap_pack(), rdev, &request);
1341 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 58, 0)
1343 * server < 2.12.58 doesn't pack default LMV in intent_getattr reply,
1344 * fetch default LMV here.
1346 if (unlikely(err == -EREMOTE)) {
1347 struct ll_inode_info *lli = ll_i2info(dir);
1348 struct lmv_user_md *lum;
1352 ptlrpc_req_finished(request);
1355 err2 = ll_dir_getstripe(dir, (void **)&lum, &lumsize, &request,
1356 OBD_MD_DEFAULT_MEA);
1357 ll_finish_md_op_data(op_data);
1360 struct lustre_md md = { NULL };
1362 md.body = req_capsule_server_get(&request->rq_pill,
1365 GOTO(err_exit, err = -EPROTO);
1367 OBD_ALLOC_PTR(md.default_lmv);
1368 if (!md.default_lmv)
1369 GOTO(err_exit, err = -ENOMEM);
1371 md.default_lmv->lsm_md_magic = lum->lum_magic;
1372 md.default_lmv->lsm_md_stripe_count =
1373 lum->lum_stripe_count;
1374 md.default_lmv->lsm_md_master_mdt_index =
1375 lum->lum_stripe_offset;
1376 md.default_lmv->lsm_md_hash_type = lum->lum_hash_type;
1378 err = ll_update_inode(dir, &md);
1379 md_free_lustre_md(sbi->ll_md_exp, &md);
1381 GOTO(err_exit, err);
1382 } else if (err2 == -ENODATA && lli->lli_default_lsm_md) {
1384 * If there are no default stripe EA on the MDT, but the
1385 * client has default stripe, then it probably means
1386 * default stripe EA has just been deleted.
1388 down_write(&lli->lli_lsm_sem);
1389 if (lli->lli_default_lsm_md)
1390 OBD_FREE_PTR(lli->lli_default_lsm_md);
1391 lli->lli_default_lsm_md = NULL;
1392 up_write(&lli->lli_lsm_sem);
1394 GOTO(err_exit, err);
1397 ptlrpc_req_finished(request);
1404 GOTO(err_exit, err);
1406 ll_update_times(request, dir);
1408 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_NEWNODE_PAUSE, cfs_fail_val);
1410 err = ll_prep_inode(&inode, request, dchild->d_sb, NULL);
1412 GOTO(err_exit, err);
1414 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
1416 /* must be done before d_instantiate, because it calls
1417 * security_d_instantiate, which means a getxattr if security
1418 * context is not set yet */
1419 err = security_inode_notifysecctx(inode,
1420 op_data->op_file_secctx,
1421 op_data->op_file_secctx_size);
1422 inode_unlock(inode);
1424 GOTO(err_exit, err);
1427 d_instantiate(dchild, inode);
1429 if (!(sbi->ll_flags & LL_SBI_FILE_SECCTX)) {
1430 err = ll_inode_init_security(dchild, inode, dir);
1432 GOTO(err_exit, err);
1437 if (request != NULL)
1438 ptlrpc_req_finished(request);
1440 if (!IS_ERR_OR_NULL(op_data))
1441 ll_finish_md_op_data(op_data);
1446 static int ll_mknod(struct inode *dir, struct dentry *dchild, ll_umode_t mode,
1449 struct qstr *name = &dchild->d_name;
1453 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p) mode %o dev %x\n",
1454 name->len, name->name, PFID(ll_inode2fid(dir)), dir,
1457 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1458 mode &= ~current_umask();
1460 switch (mode & S_IFMT) {
1469 err = ll_new_node(dir, dchild, NULL, mode, old_encode_dev(rdev),
1480 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
1485 #ifdef HAVE_IOP_ATOMIC_OPEN
1487 * Plain create. Intent create is handled in atomic_open.
1489 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1490 umode_t mode, bool want_excl)
1494 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
1496 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), "
1497 "flags=%u, excl=%d\n", dentry->d_name.len,
1498 dentry->d_name.name, PFID(ll_inode2fid(dir)),
1499 dir, mode, want_excl);
1501 /* Using mknod(2) to create a regular file is designed to not recognize
1502 * volatile file name, so we use ll_mknod() here. */
1503 rc = ll_mknod(dir, dentry, mode, 0);
1505 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1507 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, unhashed %d\n",
1508 dentry->d_name.len, dentry->d_name.name, d_unhashed(dentry));
1512 #else /* !HAVE_IOP_ATOMIC_OPEN */
1513 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1514 ll_umode_t mode, struct nameidata *nd)
1516 struct ll_dentry_data *lld = ll_d2d(dentry);
1517 struct lookup_intent *it = NULL;
1520 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
1526 /* LU-8559: use LUSTRE_OPC_CREATE for non atomic open case
1527 * so that volatile file name is recoginized.
1528 * Mknod(2), however, is designed to not recognize volatile
1529 * file name to avoid inode leak under orphan directory until
1531 return ll_new_node(dir, dentry, NULL, mode, 0,
1537 /* Was there an error? Propagate it! */
1538 if (it->it_status) {
1543 rc = ll_create_it(dir, dentry, it, NULL, 0);
1544 if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
1547 nd->intent.open.file->private_data = it;
1548 filp = lookup_instantiate_filp(nd, dentry, NULL);
1554 ll_intent_release(it);
1555 OBD_FREE(it, sizeof(*it));
1558 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1562 #endif /* HAVE_IOP_ATOMIC_OPEN */
1564 static int ll_symlink(struct inode *dir, struct dentry *dchild,
1565 const char *oldpath)
1567 struct qstr *name = &dchild->d_name;
1571 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), target=%.*s\n",
1572 name->len, name->name, PFID(ll_inode2fid(dir)),
1573 dir, 3000, oldpath);
1575 err = ll_new_node(dir, dchild, oldpath, S_IFLNK | S_IRWXUGO, 0,
1576 LUSTRE_OPC_SYMLINK);
1579 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
1584 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1585 struct dentry *new_dentry)
1587 struct inode *src = old_dentry->d_inode;
1588 struct qstr *name = &new_dentry->d_name;
1589 struct ll_sb_info *sbi = ll_i2sbi(dir);
1590 struct ptlrpc_request *request = NULL;
1591 struct md_op_data *op_data;
1595 CDEBUG(D_VFSTRACE, "VFS Op: inode="DFID"(%p), dir="DFID"(%p), "
1596 "target=%.*s\n", PFID(ll_inode2fid(src)), src,
1597 PFID(ll_inode2fid(dir)), dir, name->len, name->name);
1599 op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len,
1600 0, LUSTRE_OPC_ANY, NULL);
1601 if (IS_ERR(op_data))
1602 RETURN(PTR_ERR(op_data));
1604 err = md_link(sbi->ll_md_exp, op_data, &request);
1605 ll_finish_md_op_data(op_data);
1609 ll_update_times(request, dir);
1610 ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
1613 ptlrpc_req_finished(request);
1617 static int ll_mkdir(struct inode *dir, struct dentry *dchild, ll_umode_t mode)
1619 struct qstr *name = &dchild->d_name;
1623 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1624 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1626 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1627 mode &= ~current_umask();
1629 mode = (mode & (S_IRWXUGO|S_ISVTX)) | S_IFDIR;
1631 err = ll_new_node(dir, dchild, NULL, mode, 0, LUSTRE_OPC_MKDIR);
1633 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
1638 static int ll_rmdir(struct inode *dir, struct dentry *dchild)
1640 struct qstr *name = &dchild->d_name;
1641 struct ptlrpc_request *request = NULL;
1642 struct md_op_data *op_data;
1646 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1647 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1649 if (unlikely(d_mountpoint(dchild)))
1652 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
1653 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1654 if (IS_ERR(op_data))
1655 RETURN(PTR_ERR(op_data));
1657 if (dchild->d_inode != NULL)
1658 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1660 op_data->op_fid2 = op_data->op_fid3;
1661 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1662 ll_finish_md_op_data(op_data);
1664 ll_update_times(request, dir);
1665 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1668 ptlrpc_req_finished(request);
1675 int ll_rmdir_entry(struct inode *dir, char *name, int namelen)
1677 struct ptlrpc_request *request = NULL;
1678 struct md_op_data *op_data;
1682 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1683 namelen, name, PFID(ll_inode2fid(dir)), dir);
1685 op_data = ll_prep_md_op_data(NULL, dir, NULL, name, strlen(name),
1686 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1687 if (IS_ERR(op_data))
1688 RETURN(PTR_ERR(op_data));
1689 op_data->op_cli_flags |= CLI_RM_ENTRY;
1690 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1691 ll_finish_md_op_data(op_data);
1693 ll_update_times(request, dir);
1694 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1697 ptlrpc_req_finished(request);
1701 static int ll_unlink(struct inode *dir, struct dentry *dchild)
1703 struct qstr *name = &dchild->d_name;
1704 struct ptlrpc_request *request = NULL;
1705 struct md_op_data *op_data;
1706 struct mdt_body *body;
1709 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1710 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1713 * XXX: unlink bind mountpoint maybe call to here,
1714 * just check it as vfs_unlink does.
1716 if (unlikely(d_mountpoint(dchild)))
1719 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0,
1720 LUSTRE_OPC_ANY, NULL);
1721 if (IS_ERR(op_data))
1722 RETURN(PTR_ERR(op_data));
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);
1733 * The server puts attributes in on the last unlink, use them to update
1734 * the link count so the inode can be freed immediately.
1736 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
1737 if (body->mbo_valid & OBD_MD_FLNLINK)
1738 set_nlink(dchild->d_inode, body->mbo_nlink);
1740 ll_update_times(request, dir);
1741 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
1744 ptlrpc_req_finished(request);
1748 static int ll_rename(struct inode *src, struct dentry *src_dchild,
1749 struct inode *tgt, struct dentry *tgt_dchild
1750 #ifdef HAVE_IOPS_RENAME_WITH_FLAGS
1751 , unsigned int flags
1755 struct qstr *src_name = &src_dchild->d_name;
1756 struct qstr *tgt_name = &tgt_dchild->d_name;
1757 struct ptlrpc_request *request = NULL;
1758 struct ll_sb_info *sbi = ll_i2sbi(src);
1759 struct md_op_data *op_data;
1763 #ifdef HAVE_IOPS_RENAME_WITH_FLAGS
1768 CDEBUG(D_VFSTRACE, "VFS Op:oldname=%.*s, src_dir="DFID
1769 "(%p), newname=%.*s, tgt_dir="DFID"(%p)\n",
1770 src_name->len, src_name->name,
1771 PFID(ll_inode2fid(src)), src, tgt_name->len,
1772 tgt_name->name, PFID(ll_inode2fid(tgt)), tgt);
1774 if (unlikely(d_mountpoint(src_dchild) || d_mountpoint(tgt_dchild)))
1777 op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0,
1778 LUSTRE_OPC_ANY, NULL);
1779 if (IS_ERR(op_data))
1780 RETURN(PTR_ERR(op_data));
1782 if (src_dchild->d_inode != NULL)
1783 op_data->op_fid3 = *ll_inode2fid(src_dchild->d_inode);
1785 if (tgt_dchild->d_inode != NULL)
1786 op_data->op_fid4 = *ll_inode2fid(tgt_dchild->d_inode);
1788 err = md_rename(sbi->ll_md_exp, op_data,
1789 src_name->name, src_name->len,
1790 tgt_name->name, tgt_name->len, &request);
1791 ll_finish_md_op_data(op_data);
1793 ll_update_times(request, src);
1794 ll_update_times(request, tgt);
1795 ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1798 ptlrpc_req_finished(request);
1801 d_move(src_dchild, tgt_dchild);
1806 const struct inode_operations ll_dir_inode_operations = {
1808 #ifdef HAVE_IOP_ATOMIC_OPEN
1809 .atomic_open = ll_atomic_open,
1811 .lookup = ll_lookup_nd,
1812 .create = ll_create_nd,
1813 /* We need all these non-raw things for NFSD, to not patch it. */
1814 .unlink = ll_unlink,
1817 .symlink = ll_symlink,
1819 .rename = ll_rename,
1820 .setattr = ll_setattr,
1821 .getattr = ll_getattr,
1822 .permission = ll_inode_permission,
1823 #ifdef HAVE_IOP_XATTR
1824 .setxattr = ll_setxattr,
1825 .getxattr = ll_getxattr,
1826 .removexattr = ll_removexattr,
1828 .listxattr = ll_listxattr,
1829 #ifdef HAVE_IOP_GET_ACL
1830 .get_acl = ll_get_acl,
1832 #ifdef HAVE_IOP_SET_ACL
1833 .set_acl = ll_set_acl,
1837 const struct inode_operations ll_special_inode_operations = {
1838 .setattr = ll_setattr,
1839 .getattr = ll_getattr,
1840 .permission = ll_inode_permission,
1841 #ifdef HAVE_IOP_XATTR
1842 .setxattr = ll_setxattr,
1843 .getxattr = ll_getxattr,
1844 .removexattr = ll_removexattr,
1846 .listxattr = ll_listxattr,
1847 #ifdef HAVE_IOP_GET_ACL
1848 .get_acl = ll_get_acl,
1850 #ifdef HAVE_IOP_SET_ACL
1851 .set_acl = ll_set_acl,