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, 2016, 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 <lustre_ver.h>
51 #include "llite_internal.h"
53 static int ll_create_it(struct inode *dir, struct dentry *dentry,
54 struct lookup_intent *it);
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 unlock_new_inode(inode);
140 } else if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
141 rc = ll_update_inode(inode, md);
142 CDEBUG(D_VFSTRACE, "got inode: "DFID"(%p): rc = %d\n",
143 PFID(&md->body->mbo_fid1), inode, rc);
145 if (S_ISDIR(inode->i_mode))
146 ll_dir_clear_lsm_md(inode);
155 static void ll_invalidate_negative_children(struct inode *dir)
157 struct dentry *dentry, *tmp_subdir;
158 DECLARE_LL_D_HLIST_NODE_PTR(p);
161 ll_d_hlist_for_each_entry(dentry, p, &dir->i_dentry) {
162 spin_lock(&dentry->d_lock);
163 if (!list_empty(&dentry->d_subdirs)) {
164 struct dentry *child;
166 list_for_each_entry_safe(child, tmp_subdir,
169 if (child->d_inode == NULL)
170 d_lustre_invalidate(child, 1);
173 spin_unlock(&dentry->d_lock);
175 ll_unlock_dcache(dir);
178 int ll_test_inode_by_fid(struct inode *inode, void *opaque)
180 return lu_fid_eq(&ll_i2info(inode)->lli_fid, opaque);
183 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
184 void *data, int flag)
186 struct lustre_handle lockh;
191 case LDLM_CB_BLOCKING:
192 ldlm_lock2handle(lock, &lockh);
193 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
195 CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
199 case LDLM_CB_CANCELING: {
200 struct inode *inode = ll_inode_from_resource_lock(lock);
201 __u64 bits = lock->l_policy_data.l_inodebits.bits;
203 /* Inode is set to lock->l_resource->lr_lvb_inode
204 * for mdc - bug 24555 */
205 LASSERT(lock->l_ast_data == NULL);
210 /* Invalidate all dentries associated with this inode */
211 LASSERT(ldlm_is_canceling(lock));
213 if (!fid_res_name_eq(ll_inode2fid(inode),
214 &lock->l_resource->lr_name)) {
215 LDLM_ERROR(lock, "data mismatch with object "DFID"(%p)",
216 PFID(ll_inode2fid(inode)), inode);
220 if (bits & MDS_INODELOCK_XATTR) {
221 if (S_ISDIR(inode->i_mode))
222 ll_i2info(inode)->lli_def_stripe_offset = -1;
223 ll_xattr_cache_destroy(inode);
224 bits &= ~MDS_INODELOCK_XATTR;
227 /* For OPEN locks we differentiate between lock modes
228 * LCK_CR, LCK_CW, LCK_PR - bug 22891 */
229 if (bits & MDS_INODELOCK_OPEN)
230 ll_have_md_lock(inode, &bits, lock->l_req_mode);
232 if (bits & MDS_INODELOCK_OPEN) {
235 switch (lock->l_req_mode) {
246 LDLM_ERROR(lock, "bad lock mode for OPEN lock");
250 ll_md_real_close(inode, fmode);
252 bits &= ~MDS_INODELOCK_OPEN;
255 if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
256 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_PERM))
257 ll_have_md_lock(inode, &bits, LCK_MINMODE);
259 if (bits & MDS_INODELOCK_LAYOUT) {
260 struct cl_object_conf conf = {
261 .coc_opc = OBJECT_CONF_INVALIDATE,
265 rc = ll_layout_conf(inode, &conf);
267 CDEBUG(D_INODE, "cannot invalidate layout of "
269 PFID(ll_inode2fid(inode)), rc);
272 if (bits & MDS_INODELOCK_UPDATE) {
273 struct ll_inode_info *lli = ll_i2info(inode);
274 lli->lli_update_atime = 1;
277 if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
278 struct ll_inode_info *lli = ll_i2info(inode);
280 CDEBUG(D_INODE, "invalidating inode "DFID" lli = %p, "
281 "pfid = "DFID"\n", PFID(ll_inode2fid(inode)),
282 lli, PFID(&lli->lli_pfid));
283 truncate_inode_pages(inode->i_mapping, 0);
285 if (unlikely(!fid_is_zero(&lli->lli_pfid))) {
286 struct inode *master_inode = NULL;
289 /* This is slave inode, since all of the child
290 * dentry is connected on the master inode, so
291 * we have to invalidate the negative children
293 CDEBUG(D_INODE, "Invalidate s"DFID" m"DFID"\n",
294 PFID(ll_inode2fid(inode)),
295 PFID(&lli->lli_pfid));
297 hash = cl_fid_build_ino(&lli->lli_pfid,
298 ll_need_32bit_api(ll_i2sbi(inode)));
300 /* Do not lookup the inode with ilookup5,
301 * otherwise it will cause dead lock,
303 * 1. Client1 send chmod req to the MDT0, then
304 * on MDT0, it enqueues master and all of its
305 * slaves lock, (mdt_attr_set() ->
306 * mdt_lock_slaves()), after gets master and
307 * stripe0 lock, it will send the enqueue req
308 * (for stripe1) to MDT1, then MDT1 finds the
309 * lock has been granted to client2. Then MDT1
310 * sends blocking ast to client2.
312 * 2. At the same time, client2 tries to unlink
313 * the striped dir (rm -rf striped_dir), and
314 * during lookup, it will hold the master inode
315 * of the striped directory, whose inode state
316 * is NEW, then tries to revalidate all of its
317 * slaves, (ll_prep_inode()->ll_iget()->
318 * ll_read_inode2()-> ll_update_inode().). And
319 * it will be blocked on the server side because
322 * 3. Then the client get the blocking_ast req,
323 * cancel the lock, but being blocked if using
324 * ->ilookup5()), because master inode state is
326 master_inode = ilookup5_nowait(inode->i_sb,
327 hash, ll_test_inode_by_fid,
328 (void *)&lli->lli_pfid);
330 ll_invalidate_negative_children(
335 ll_invalidate_negative_children(inode);
339 if ((bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM)) &&
340 inode->i_sb->s_root != NULL &&
341 inode != inode->i_sb->s_root->d_inode)
342 ll_invalidate_aliases(inode);
354 __u32 ll_i2suppgid(struct inode *i)
356 if (in_group_p(i->i_gid))
357 return (__u32)from_kgid(&init_user_ns, i->i_gid);
359 return (__u32) __kgid_val(INVALID_GID);
362 /* Pack the required supplementary groups into the supplied groups array.
363 * If we don't need to use the groups from the target inode(s) then we
364 * instead pack one or more groups from the user's supplementary group
365 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
366 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
369 LASSERT(suppgids != NULL);
371 suppgids[0] = ll_i2suppgid(i1);
374 suppgids[1] = ll_i2suppgid(i2);
380 * try to reuse three types of dentry:
381 * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
382 * by concurrent .revalidate).
383 * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
384 * be cleared by others calling d_lustre_revalidate).
385 * 3. DISCONNECTED alias.
387 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
389 struct dentry *alias, *discon_alias, *invalid_alias;
390 DECLARE_LL_D_HLIST_NODE_PTR(p);
392 if (ll_d_hlist_empty(&inode->i_dentry))
395 discon_alias = invalid_alias = NULL;
397 ll_lock_dcache(inode);
398 ll_d_hlist_for_each_entry(alias, p, &inode->i_dentry) {
399 LASSERT(alias != dentry);
401 spin_lock(&alias->d_lock);
402 if ((alias->d_flags & DCACHE_DISCONNECTED) &&
403 S_ISDIR(inode->i_mode))
404 /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
405 discon_alias = alias;
406 else if (alias->d_parent == dentry->d_parent &&
407 alias->d_name.hash == dentry->d_name.hash &&
408 alias->d_name.len == dentry->d_name.len &&
409 memcmp(alias->d_name.name, dentry->d_name.name,
410 dentry->d_name.len) == 0)
411 invalid_alias = alias;
412 spin_unlock(&alias->d_lock);
417 alias = invalid_alias ?: discon_alias ?: NULL;
419 spin_lock(&alias->d_lock);
421 spin_unlock(&alias->d_lock);
423 ll_unlock_dcache(inode);
429 * Similar to d_splice_alias(), but lustre treats invalid alias
430 * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
432 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
438 new = ll_find_alias(inode, de);
448 "Reuse dentry %p inode %p refc %d flags %#x\n",
449 new, new->d_inode, ll_d_count(new), new->d_flags);
457 CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
458 de, de->d_inode, ll_d_count(de), de->d_flags);
462 static int ll_lookup_it_finish(struct ptlrpc_request *request,
463 struct lookup_intent *it,
464 struct inode *parent, struct dentry **de)
466 struct inode *inode = NULL;
469 struct dentry *alias;
472 /* NB 1 request reference will be taken away by ll_intent_lock()
474 CDEBUG(D_DENTRY, "it %p it_disposition %x\n", it,
476 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
477 rc = ll_prep_inode(&inode, request, (*de)->d_sb, it);
481 ll_set_lock_data(ll_i2sbi(parent)->ll_md_exp, inode, it, &bits);
483 /* We used to query real size from OSTs here, but actually
484 this is not needed. For stat() calls size would be updated
485 from subsequent do_revalidate()->ll_inode_revalidate_it() in
487 vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
488 Everybody else who needs correct file size would call
489 ll_glimpse_size or some equivalent themselves anyway.
490 Also see bug 7198. */
493 /* Only hash *de if it is unhashed (new dentry).
494 * Atoimc_open may passin hashed dentries for open.
496 alias = ll_splice_alias(inode, *de);
498 GOTO(out, rc = PTR_ERR(alias));
502 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
503 /* we have lookup look - unhide dentry */
504 if (bits & MDS_INODELOCK_LOOKUP)
505 d_lustre_revalidate(*de);
506 } else if (!it_disposition(it, DISP_OPEN_CREATE)) {
507 /* If file created on server, don't depend on parent UPDATE
508 * lock to unhide it. It is left hidden and next lookup can
509 * find it in ll_splice_alias.
511 /* Check that parent has UPDATE lock. */
512 struct lookup_intent parent_it = {
514 .it_lock_handle = 0 };
515 struct lu_fid fid = ll_i2info(parent)->lli_fid;
517 /* If it is striped directory, get the real stripe parent */
518 if (unlikely(ll_i2info(parent)->lli_lsm_md != NULL)) {
519 rc = md_get_fid_from_lsm(ll_i2mdexp(parent),
520 ll_i2info(parent)->lli_lsm_md,
522 (*de)->d_name.len, &fid);
527 if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it, &fid,
529 d_lustre_revalidate(*de);
530 ll_intent_release(&parent_it);
537 if (rc != 0 && it->it_op & IT_OPEN)
538 ll_open_cleanup((*de)->d_sb, request);
543 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
544 struct lookup_intent *it)
546 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
547 struct dentry *save = dentry, *retval;
548 struct ptlrpc_request *req = NULL;
549 struct md_op_data *op_data = NULL;
554 if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
555 RETURN(ERR_PTR(-ENAMETOOLONG));
557 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), intent=%s\n",
558 dentry->d_name.len, dentry->d_name.name,
559 PFID(ll_inode2fid(parent)), parent, LL_IT2STR(it));
561 if (d_mountpoint(dentry))
562 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
564 if (it == NULL || it->it_op == IT_GETXATTR)
567 if (it->it_op == IT_GETATTR && dentry_may_statahead(parent, dentry)) {
568 rc = ll_statahead(parent, &dentry, 0);
570 RETURN(dentry == save ? NULL : dentry);
573 if (it->it_op & IT_OPEN && it->it_flags & FMODE_WRITE &&
574 dentry->d_sb->s_flags & MS_RDONLY)
575 RETURN(ERR_PTR(-EROFS));
577 if (it->it_op & IT_CREAT)
578 opc = LUSTRE_OPC_CREATE;
580 opc = LUSTRE_OPC_ANY;
582 op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
583 dentry->d_name.len, 0, opc, NULL);
585 GOTO(out, retval = ERR_CAST(op_data));
587 /* enforce umask if acl disabled or MDS doesn't support umask */
588 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
589 it->it_create_mode &= ~current_umask();
591 if (it->it_op & IT_CREAT &&
592 ll_i2sbi(parent)->ll_flags & LL_SBI_FILE_SECCTX) {
593 rc = ll_dentry_init_security(dentry, it->it_create_mode,
595 &op_data->op_file_secctx_name,
596 &op_data->op_file_secctx,
597 &op_data->op_file_secctx_size);
599 GOTO(out, retval = ERR_PTR(rc));
602 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
603 &ll_md_blocking_ast, 0);
604 /* If the MDS allows the client to chgrp (CFS_SETGRP_PERM), but the
605 * client does not know which suppgid should be sent to the MDS, or
606 * some other(s) changed the target file's GID after this RPC sent
607 * to the MDS with the suppgid as the original GID, then we should
608 * try again with right suppgid. */
609 if (rc == -EACCES && it->it_op & IT_OPEN &&
610 it_disposition(it, DISP_OPEN_DENY)) {
611 struct mdt_body *body;
613 LASSERT(req != NULL);
615 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
616 if (op_data->op_suppgids[0] == body->mbo_gid ||
617 op_data->op_suppgids[1] == body->mbo_gid ||
618 !in_group_p(make_kgid(&init_user_ns, body->mbo_gid)))
619 GOTO(out, retval = ERR_PTR(-EACCES));
621 fid_zero(&op_data->op_fid2);
622 op_data->op_suppgids[1] = body->mbo_gid;
623 ptlrpc_req_finished(req);
625 ll_intent_release(it);
626 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
627 &ll_md_blocking_ast, 0);
631 GOTO(out, retval = ERR_PTR(rc));
633 rc = ll_lookup_it_finish(req, it, parent, &dentry);
635 ll_intent_release(it);
636 GOTO(out, retval = ERR_PTR(rc));
639 if ((it->it_op & IT_OPEN) && dentry->d_inode &&
640 !S_ISREG(dentry->d_inode->i_mode) &&
641 !S_ISDIR(dentry->d_inode->i_mode)) {
642 ll_release_openhandle(dentry, it);
644 ll_lookup_finish_locks(it, dentry);
646 GOTO(out, retval = (dentry == save) ? NULL : dentry);
649 if (op_data != NULL && !IS_ERR(op_data))
650 ll_finish_md_op_data(op_data);
652 ptlrpc_req_finished(req);
656 #ifdef HAVE_IOP_ATOMIC_OPEN
657 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
660 struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
663 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), flags=%u\n",
664 dentry->d_name.len, dentry->d_name.name,
665 PFID(ll_inode2fid(parent)), parent, flags);
668 * Optimize away (CREATE && !OPEN). Let .create handle the race.
669 * but only if we have write permissions there, otherwise we need
670 * to proceed with lookup. LU-4185
672 if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN) &&
673 (inode_permission(parent, MAY_WRITE | MAY_EXEC) == 0))
676 if (flags & (LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE))
680 de = ll_lookup_it(parent, dentry, itp);
683 ll_intent_release(itp);
689 * For cached negative dentry and new dentry, handle lookup/create/open
692 static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
693 struct file *file, unsigned open_flags,
694 umode_t mode, int *opened)
696 struct lookup_intent *it;
698 long long lookup_flags = LOOKUP_OPEN;
702 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), file %p,"
703 "open_flags %x, mode %x opened %d\n",
704 dentry->d_name.len, dentry->d_name.name,
705 PFID(ll_inode2fid(dir)), dir, file, open_flags, mode, *opened);
707 /* Only negative dentries enter here */
708 LASSERT(dentry->d_inode == NULL);
710 if (!d_unhashed(dentry)) {
711 /* A valid negative dentry that just passed revalidation,
712 * there's little point to try and open it server-side,
713 * even though there's a minuscule chance it might succeed.
714 * Either way it's a valid race to just return -ENOENT here.
716 if (!(open_flags & O_CREAT))
719 /* Otherwise we just unhash it to be rehashed afresh via
720 * lookup if necessary
725 OBD_ALLOC(it, sizeof(*it));
730 if (open_flags & O_CREAT) {
731 it->it_op |= IT_CREAT;
732 lookup_flags |= LOOKUP_CREATE;
734 it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
735 it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
736 it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
738 /* Dentry added to dcache tree in ll_lookup_it */
739 de = ll_lookup_it(dir, dentry, it);
745 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
748 if (it_disposition(it, DISP_OPEN_CREATE)) {
749 /* Dentry instantiated in ll_create_it. */
750 rc = ll_create_it(dir, dentry, it);
752 /* We dget in ll_splice_alias. */
758 *opened |= FILE_CREATED;
760 if (dentry->d_inode && it_disposition(it, DISP_OPEN_OPEN)) {
762 if (S_ISFIFO(dentry->d_inode->i_mode)) {
763 /* We cannot call open here as it might
764 * deadlock. This case is unreachable in
765 * practice because of OBD_CONNECT_NODEVOH. */
766 rc = finish_no_open(file, de);
768 file->private_data = it;
769 rc = finish_open(file, dentry, NULL, opened);
770 /* We dget in ll_splice_alias. finish_open takes
771 * care of dget for fd open.
777 rc = finish_no_open(file, de);
782 ll_intent_release(it);
783 OBD_FREE(it, sizeof(*it));
788 #else /* !HAVE_IOP_ATOMIC_OPEN */
789 static struct lookup_intent *
790 ll_convert_intent(struct open_intent *oit, int lookup_flags)
792 struct lookup_intent *it;
796 return ERR_PTR(-ENOMEM);
798 if (lookup_flags & LOOKUP_OPEN) {
800 if (lookup_flags & LOOKUP_CREATE)
801 it->it_op |= IT_CREAT;
802 it->it_create_mode = (oit->create_mode & S_IALLUGO) | S_IFREG;
803 it->it_flags = ll_namei_to_lookup_intent_flag(oit->flags);
804 it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
806 it->it_op = IT_GETATTR;
812 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
813 struct nameidata *nd)
818 if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
819 struct lookup_intent *it;
821 if (ll_d2d(dentry) && ll_d2d(dentry)->lld_it) {
822 it = ll_d2d(dentry)->lld_it;
823 ll_d2d(dentry)->lld_it = NULL;
826 * Optimize away (CREATE && !OPEN). Let .create handle
827 * the race. But only if we have write permissions
828 * there, otherwise we need to proceed with lookup.
831 if ((nd->flags & LOOKUP_CREATE) &&
832 !(nd->flags & LOOKUP_OPEN) &&
833 (inode_permission(parent,
834 MAY_WRITE | MAY_EXEC) == 0))
837 it = ll_convert_intent(&nd->intent.open, nd->flags);
839 RETURN((struct dentry *)it);
842 de = ll_lookup_it(parent, dentry, it);
845 if ((nd->flags & LOOKUP_OPEN) && !IS_ERR(dentry)) { /* Open */
846 if (dentry->d_inode &&
847 it_disposition(it, DISP_OPEN_OPEN)) { /* nocreate */
848 if (S_ISFIFO(dentry->d_inode->i_mode)) {
849 /* We cannot call open here as it might
850 * deadlock. This case is unreachable in
851 * practice because of
852 * OBD_CONNECT_NODEVOH. */
856 nd->intent.open.file->private_data = it;
857 filp = lookup_instantiate_filp(nd,
863 de = (struct dentry *)filp;
866 } else if (it_disposition(it, DISP_OPEN_CREATE)) {
867 // XXX This can only reliably work on assumption
868 // that there are NO hashed negative dentries.
869 ll_d2d(dentry)->lld_it = it;
870 it = NULL; /* Will be freed in ll_create_nd */
871 /* We absolutely depend on ll_create_nd to be
872 * called to not leak this intent and possible
873 * data attached to it */
878 ll_intent_release(it);
879 OBD_FREE(it, sizeof(*it));
882 de = ll_lookup_it(parent, dentry, NULL);
887 #endif /* HAVE_IOP_ATOMIC_OPEN */
889 /* We depend on "mode" being set with the proper file type/umask by now */
890 static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
892 struct inode *inode = NULL;
893 struct ptlrpc_request *request = NULL;
894 struct ll_sb_info *sbi = ll_i2sbi(dir);
898 LASSERT(it && it->it_disposition);
900 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
901 request = it->it_request;
902 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
903 rc = ll_prep_inode(&inode, request, dir->i_sb, it);
905 GOTO(out, inode = ERR_PTR(rc));
907 LASSERT(ll_d_hlist_empty(&inode->i_dentry));
909 /* We asked for a lock on the directory, but were granted a
910 * lock on the inode. Since we finally have an inode pointer,
911 * stuff it in the lock. */
912 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode "DFID"(%p)\n",
913 PFID(ll_inode2fid(inode)), inode);
914 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
917 ptlrpc_req_finished(request);
922 * By the time this is called, we already have created the directory cache
923 * entry for the new file, but it is so far negative - it has no inode.
925 * We defer creating the OBD object(s) until open, to keep the intent and
926 * non-intent code paths similar, and also because we do not have the MDS
927 * inode number before calling ll_create_node() (which is needed for LOV),
928 * so we would need to do yet another RPC to the MDS to store the LOV EA
929 * data on the MDS. If needed, we would pass the PACKED lmm as data and
930 * lmm_size in datalen (the MDS still has code which will handle that).
932 * If the create succeeds, we fill in the inode information
933 * with d_instantiate().
935 static int ll_create_it(struct inode *dir, struct dentry *dentry,
936 struct lookup_intent *it)
942 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), intent=%s\n",
943 dentry->d_name.len, dentry->d_name.name,
944 PFID(ll_inode2fid(dir)), dir, LL_IT2STR(it));
946 rc = it_open_error(DISP_OPEN_CREATE, it);
950 inode = ll_create_node(dir, it);
952 RETURN(PTR_ERR(inode));
954 d_instantiate(dentry, inode);
956 if (!(ll_i2sbi(inode)->ll_flags & LL_SBI_FILE_SECCTX)) {
957 rc = ll_inode_init_security(dentry, inode, dir);
965 void ll_update_times(struct ptlrpc_request *request, struct inode *inode)
967 struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
971 if (body->mbo_valid & OBD_MD_FLMTIME &&
972 body->mbo_mtime > LTIME_S(inode->i_mtime)) {
973 CDEBUG(D_INODE, "setting fid "DFID" mtime from %lu to %llu"
974 "\n", PFID(ll_inode2fid(inode)),
975 LTIME_S(inode->i_mtime), body->mbo_mtime);
976 LTIME_S(inode->i_mtime) = body->mbo_mtime;
979 if (body->mbo_valid & OBD_MD_FLCTIME &&
980 body->mbo_ctime > LTIME_S(inode->i_ctime))
981 LTIME_S(inode->i_ctime) = body->mbo_ctime;
984 static int ll_new_node(struct inode *dir, struct dentry *dchild,
985 const char *tgt, umode_t mode, int rdev, __u32 opc)
987 struct qstr *name = &dchild->d_name;
988 struct ptlrpc_request *request = NULL;
989 struct md_op_data *op_data;
990 struct inode *inode = NULL;
991 struct ll_sb_info *sbi = ll_i2sbi(dir);
996 if (unlikely(tgt != NULL))
997 tgt_len = strlen(tgt) + 1;
1000 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
1001 name->len, 0, opc, NULL);
1002 if (IS_ERR(op_data))
1003 GOTO(err_exit, err = PTR_ERR(op_data));
1005 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
1006 err = ll_dentry_init_security(dchild, mode, &dchild->d_name,
1007 &op_data->op_file_secctx_name,
1008 &op_data->op_file_secctx,
1009 &op_data->op_file_secctx_size);
1011 GOTO(err_exit, err);
1014 err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
1015 from_kuid(&init_user_ns, current_fsuid()),
1016 from_kgid(&init_user_ns, current_fsgid()),
1017 cfs_curproc_cap_pack(), rdev, &request);
1018 ll_finish_md_op_data(op_data);
1020 if (err < 0 && err != -EREMOTE)
1021 GOTO(err_exit, err);
1023 /* If the client doesn't know where to create a subdirectory (or
1024 * in case of a race that sends the RPC to the wrong MDS), the
1025 * MDS will return -EREMOTE and the client will fetch the layout
1026 * of the directory, then create the directory on the right MDT. */
1027 if (unlikely(err == -EREMOTE)) {
1028 struct ll_inode_info *lli = ll_i2info(dir);
1029 struct lmv_user_md *lum;
1033 ptlrpc_req_finished(request);
1036 err2 = ll_dir_getstripe(dir, (void **)&lum, &lumsize, &request,
1037 OBD_MD_DEFAULT_MEA);
1039 /* Update stripe_offset and retry */
1040 lli->lli_def_stripe_offset = lum->lum_stripe_offset;
1041 } else if (err2 == -ENODATA &&
1042 lli->lli_def_stripe_offset != -1) {
1043 /* If there are no default stripe EA on the MDT, but the
1044 * client has default stripe, then it probably means
1045 * default stripe EA has just been deleted. */
1046 lli->lli_def_stripe_offset = -1;
1048 GOTO(err_exit, err);
1051 ptlrpc_req_finished(request);
1056 ll_update_times(request, dir);
1058 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_NEWNODE_PAUSE, cfs_fail_val);
1060 err = ll_prep_inode(&inode, request, dchild->d_sb, NULL);
1062 GOTO(err_exit, err);
1064 d_instantiate(dchild, inode);
1066 if (!(sbi->ll_flags & LL_SBI_FILE_SECCTX)) {
1067 err = ll_inode_init_security(dchild, inode, dir);
1069 GOTO(err_exit, err);
1074 if (request != NULL)
1075 ptlrpc_req_finished(request);
1077 if (!IS_ERR_OR_NULL(op_data))
1078 ll_finish_md_op_data(op_data);
1083 static int ll_mknod(struct inode *dir, struct dentry *dchild, ll_umode_t mode,
1086 struct qstr *name = &dchild->d_name;
1090 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p) mode %o dev %x\n",
1091 name->len, name->name, PFID(ll_inode2fid(dir)), dir,
1094 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1095 mode &= ~current_umask();
1097 switch (mode & S_IFMT) {
1099 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1105 err = ll_new_node(dir, dchild, NULL, mode, old_encode_dev(rdev),
1116 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
1121 #ifdef HAVE_IOP_ATOMIC_OPEN
1123 * Plain create. Intent create is handled in atomic_open.
1125 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1126 umode_t mode, bool want_excl)
1130 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
1132 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), "
1133 "flags=%u, excl=%d\n", dentry->d_name.len,
1134 dentry->d_name.name, PFID(ll_inode2fid(dir)),
1135 dir, mode, want_excl);
1137 /* Using mknod(2) to create a regular file is designed to not recognize
1138 * volatile file name, so we use ll_mknod() here. */
1139 rc = ll_mknod(dir, dentry, mode, 0);
1141 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1143 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, unhashed %d\n",
1144 dentry->d_name.len, dentry->d_name.name, d_unhashed(dentry));
1148 #else /* !HAVE_IOP_ATOMIC_OPEN */
1149 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1150 ll_umode_t mode, struct nameidata *nd)
1152 struct ll_dentry_data *lld = ll_d2d(dentry);
1153 struct lookup_intent *it = NULL;
1156 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
1162 /* LU-8559: use LUSTRE_OPC_CREATE for non atomic open case
1163 * so that volatile file name is recoginized.
1164 * Mknod(2), however, is designed to not recognize volatile
1165 * file name to avoid inode leak under orphan directory until
1167 return ll_new_node(dir, dentry, NULL, mode, 0,
1173 /* Was there an error? Propagate it! */
1174 if (it->it_status) {
1179 rc = ll_create_it(dir, dentry, it);
1180 if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
1183 nd->intent.open.file->private_data = it;
1184 filp = lookup_instantiate_filp(nd, dentry, NULL);
1190 ll_intent_release(it);
1191 OBD_FREE(it, sizeof(*it));
1194 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1198 #endif /* HAVE_IOP_ATOMIC_OPEN */
1200 static int ll_symlink(struct inode *dir, struct dentry *dchild,
1201 const char *oldpath)
1203 struct qstr *name = &dchild->d_name;
1207 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), target=%.*s\n",
1208 name->len, name->name, PFID(ll_inode2fid(dir)),
1209 dir, 3000, oldpath);
1211 err = ll_new_node(dir, dchild, oldpath, S_IFLNK | S_IRWXUGO, 0,
1212 LUSTRE_OPC_SYMLINK);
1215 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
1220 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1221 struct dentry *new_dentry)
1223 struct inode *src = old_dentry->d_inode;
1224 struct qstr *name = &new_dentry->d_name;
1225 struct ll_sb_info *sbi = ll_i2sbi(dir);
1226 struct ptlrpc_request *request = NULL;
1227 struct md_op_data *op_data;
1231 CDEBUG(D_VFSTRACE, "VFS Op: inode="DFID"(%p), dir="DFID"(%p), "
1232 "target=%.*s\n", PFID(ll_inode2fid(src)), src,
1233 PFID(ll_inode2fid(dir)), dir, name->len, name->name);
1235 op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len,
1236 0, LUSTRE_OPC_ANY, NULL);
1237 if (IS_ERR(op_data))
1238 RETURN(PTR_ERR(op_data));
1240 err = md_link(sbi->ll_md_exp, op_data, &request);
1241 ll_finish_md_op_data(op_data);
1245 ll_update_times(request, dir);
1246 ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
1249 ptlrpc_req_finished(request);
1253 static int ll_mkdir(struct inode *dir, struct dentry *dchild, ll_umode_t mode)
1255 struct qstr *name = &dchild->d_name;
1259 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1260 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1262 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1263 mode &= ~current_umask();
1265 mode = (mode & (S_IRWXUGO|S_ISVTX)) | S_IFDIR;
1267 err = ll_new_node(dir, dchild, NULL, mode, 0, LUSTRE_OPC_MKDIR);
1269 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
1274 static int ll_rmdir(struct inode *dir, struct dentry *dchild)
1276 struct qstr *name = &dchild->d_name;
1277 struct ptlrpc_request *request = NULL;
1278 struct md_op_data *op_data;
1282 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1283 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1285 if (unlikely(d_mountpoint(dchild)))
1288 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
1289 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1290 if (IS_ERR(op_data))
1291 RETURN(PTR_ERR(op_data));
1293 if (dchild->d_inode != NULL)
1294 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1296 op_data->op_fid2 = op_data->op_fid3;
1297 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1298 ll_finish_md_op_data(op_data);
1300 ll_update_times(request, dir);
1301 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1304 ptlrpc_req_finished(request);
1311 int ll_rmdir_entry(struct inode *dir, char *name, int namelen)
1313 struct ptlrpc_request *request = NULL;
1314 struct md_op_data *op_data;
1318 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1319 namelen, name, PFID(ll_inode2fid(dir)), dir);
1321 op_data = ll_prep_md_op_data(NULL, dir, NULL, name, strlen(name),
1322 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1323 if (IS_ERR(op_data))
1324 RETURN(PTR_ERR(op_data));
1325 op_data->op_cli_flags |= CLI_RM_ENTRY;
1326 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1327 ll_finish_md_op_data(op_data);
1329 ll_update_times(request, dir);
1330 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1333 ptlrpc_req_finished(request);
1337 static int ll_unlink(struct inode *dir, struct dentry *dchild)
1339 struct qstr *name = &dchild->d_name;
1340 struct ptlrpc_request *request = NULL;
1341 struct md_op_data *op_data;
1344 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1345 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1348 * XXX: unlink bind mountpoint maybe call to here,
1349 * just check it as vfs_unlink does.
1351 if (unlikely(d_mountpoint(dchild)))
1354 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0,
1355 LUSTRE_OPC_ANY, NULL);
1356 if (IS_ERR(op_data))
1357 RETURN(PTR_ERR(op_data));
1359 if (dchild->d_inode != NULL)
1360 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1362 op_data->op_fid2 = op_data->op_fid3;
1363 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1364 ll_finish_md_op_data(op_data);
1368 ll_update_times(request, dir);
1369 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
1372 ptlrpc_req_finished(request);
1376 static int ll_rename(struct inode *src, struct dentry *src_dchild,
1377 struct inode *tgt, struct dentry *tgt_dchild)
1379 struct qstr *src_name = &src_dchild->d_name;
1380 struct qstr *tgt_name = &tgt_dchild->d_name;
1381 struct ptlrpc_request *request = NULL;
1382 struct ll_sb_info *sbi = ll_i2sbi(src);
1383 struct md_op_data *op_data;
1386 CDEBUG(D_VFSTRACE, "VFS Op:oldname=%.*s, src_dir="DFID
1387 "(%p), newname=%.*s, tgt_dir="DFID"(%p)\n",
1388 src_name->len, src_name->name,
1389 PFID(ll_inode2fid(src)), src, tgt_name->len,
1390 tgt_name->name, PFID(ll_inode2fid(tgt)), tgt);
1392 if (unlikely(d_mountpoint(src_dchild) || d_mountpoint(tgt_dchild)))
1395 op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0,
1396 LUSTRE_OPC_ANY, NULL);
1397 if (IS_ERR(op_data))
1398 RETURN(PTR_ERR(op_data));
1400 if (src_dchild->d_inode != NULL)
1401 op_data->op_fid3 = *ll_inode2fid(src_dchild->d_inode);
1403 if (tgt_dchild->d_inode != NULL)
1404 op_data->op_fid4 = *ll_inode2fid(tgt_dchild->d_inode);
1406 err = md_rename(sbi->ll_md_exp, op_data,
1407 src_name->name, src_name->len,
1408 tgt_name->name, tgt_name->len, &request);
1409 ll_finish_md_op_data(op_data);
1411 ll_update_times(request, src);
1412 ll_update_times(request, tgt);
1413 ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1416 ptlrpc_req_finished(request);
1419 d_move(src_dchild, tgt_dchild);
1424 const struct inode_operations ll_dir_inode_operations = {
1426 #ifdef HAVE_IOP_ATOMIC_OPEN
1427 .atomic_open = ll_atomic_open,
1429 .lookup = ll_lookup_nd,
1430 .create = ll_create_nd,
1431 /* We need all these non-raw things for NFSD, to not patch it. */
1432 .unlink = ll_unlink,
1435 .symlink = ll_symlink,
1437 .rename = ll_rename,
1438 .setattr = ll_setattr,
1439 .getattr = ll_getattr,
1440 .permission = ll_inode_permission,
1441 .setxattr = ll_setxattr,
1442 .getxattr = ll_getxattr,
1443 .listxattr = ll_listxattr,
1444 .removexattr = ll_removexattr,
1445 #ifdef HAVE_IOP_GET_ACL
1446 .get_acl = ll_get_acl,
1450 const struct inode_operations ll_special_inode_operations = {
1451 .setattr = ll_setattr,
1452 .getattr = ll_getattr,
1453 .permission = ll_inode_permission,
1454 .setxattr = ll_setxattr,
1455 .getxattr = ll_getxattr,
1456 .listxattr = ll_listxattr,
1457 .removexattr = ll_removexattr,
1458 #ifdef HAVE_IOP_GET_ACL
1459 .get_acl = ll_get_acl,