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 /* Pause to allow for a race with concurrent access by fid */
908 OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_NODE_PAUSE, cfs_fail_val);
910 /* We asked for a lock on the directory, but were granted a
911 * lock on the inode. Since we finally have an inode pointer,
912 * stuff it in the lock. */
913 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode "DFID"(%p)\n",
914 PFID(ll_inode2fid(inode)), inode);
915 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
918 ptlrpc_req_finished(request);
923 * By the time this is called, we already have created the directory cache
924 * entry for the new file, but it is so far negative - it has no inode.
926 * We defer creating the OBD object(s) until open, to keep the intent and
927 * non-intent code paths similar, and also because we do not have the MDS
928 * inode number before calling ll_create_node() (which is needed for LOV),
929 * so we would need to do yet another RPC to the MDS to store the LOV EA
930 * data on the MDS. If needed, we would pass the PACKED lmm as data and
931 * lmm_size in datalen (the MDS still has code which will handle that).
933 * If the create succeeds, we fill in the inode information
934 * with d_instantiate().
936 static int ll_create_it(struct inode *dir, struct dentry *dentry,
937 struct lookup_intent *it)
943 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), intent=%s\n",
944 dentry->d_name.len, dentry->d_name.name,
945 PFID(ll_inode2fid(dir)), dir, LL_IT2STR(it));
947 rc = it_open_error(DISP_OPEN_CREATE, it);
951 inode = ll_create_node(dir, it);
953 RETURN(PTR_ERR(inode));
955 d_instantiate(dentry, inode);
957 if (!(ll_i2sbi(inode)->ll_flags & LL_SBI_FILE_SECCTX)) {
958 rc = ll_inode_init_security(dentry, inode, dir);
966 void ll_update_times(struct ptlrpc_request *request, struct inode *inode)
968 struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
972 if (body->mbo_valid & OBD_MD_FLMTIME &&
973 body->mbo_mtime > LTIME_S(inode->i_mtime)) {
974 CDEBUG(D_INODE, "setting fid "DFID" mtime from %lu to %llu"
975 "\n", PFID(ll_inode2fid(inode)),
976 LTIME_S(inode->i_mtime), body->mbo_mtime);
977 LTIME_S(inode->i_mtime) = body->mbo_mtime;
980 if (body->mbo_valid & OBD_MD_FLCTIME &&
981 body->mbo_ctime > LTIME_S(inode->i_ctime))
982 LTIME_S(inode->i_ctime) = body->mbo_ctime;
985 static int ll_new_node(struct inode *dir, struct dentry *dchild,
986 const char *tgt, umode_t mode, int rdev, __u32 opc)
988 struct qstr *name = &dchild->d_name;
989 struct ptlrpc_request *request = NULL;
990 struct md_op_data *op_data;
991 struct inode *inode = NULL;
992 struct ll_sb_info *sbi = ll_i2sbi(dir);
997 if (unlikely(tgt != NULL))
998 tgt_len = strlen(tgt) + 1;
1001 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
1002 name->len, 0, opc, NULL);
1003 if (IS_ERR(op_data))
1004 GOTO(err_exit, err = PTR_ERR(op_data));
1006 if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
1007 err = ll_dentry_init_security(dchild, mode, &dchild->d_name,
1008 &op_data->op_file_secctx_name,
1009 &op_data->op_file_secctx,
1010 &op_data->op_file_secctx_size);
1012 GOTO(err_exit, err);
1015 err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
1016 from_kuid(&init_user_ns, current_fsuid()),
1017 from_kgid(&init_user_ns, current_fsgid()),
1018 cfs_curproc_cap_pack(), rdev, &request);
1019 ll_finish_md_op_data(op_data);
1021 if (err < 0 && err != -EREMOTE)
1022 GOTO(err_exit, err);
1024 /* If the client doesn't know where to create a subdirectory (or
1025 * in case of a race that sends the RPC to the wrong MDS), the
1026 * MDS will return -EREMOTE and the client will fetch the layout
1027 * of the directory, then create the directory on the right MDT. */
1028 if (unlikely(err == -EREMOTE)) {
1029 struct ll_inode_info *lli = ll_i2info(dir);
1030 struct lmv_user_md *lum;
1034 ptlrpc_req_finished(request);
1037 err2 = ll_dir_getstripe(dir, (void **)&lum, &lumsize, &request,
1038 OBD_MD_DEFAULT_MEA);
1040 /* Update stripe_offset and retry */
1041 lli->lli_def_stripe_offset = lum->lum_stripe_offset;
1042 } else if (err2 == -ENODATA &&
1043 lli->lli_def_stripe_offset != -1) {
1044 /* If there are no default stripe EA on the MDT, but the
1045 * client has default stripe, then it probably means
1046 * default stripe EA has just been deleted. */
1047 lli->lli_def_stripe_offset = -1;
1049 GOTO(err_exit, err);
1052 ptlrpc_req_finished(request);
1057 ll_update_times(request, dir);
1059 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_NEWNODE_PAUSE, cfs_fail_val);
1061 err = ll_prep_inode(&inode, request, dchild->d_sb, NULL);
1063 GOTO(err_exit, err);
1065 d_instantiate(dchild, inode);
1067 if (!(sbi->ll_flags & LL_SBI_FILE_SECCTX)) {
1068 err = ll_inode_init_security(dchild, inode, dir);
1070 GOTO(err_exit, err);
1075 if (request != NULL)
1076 ptlrpc_req_finished(request);
1078 if (!IS_ERR_OR_NULL(op_data))
1079 ll_finish_md_op_data(op_data);
1084 static int ll_mknod(struct inode *dir, struct dentry *dchild, ll_umode_t mode,
1087 struct qstr *name = &dchild->d_name;
1091 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p) mode %o dev %x\n",
1092 name->len, name->name, PFID(ll_inode2fid(dir)), dir,
1095 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1096 mode &= ~current_umask();
1098 switch (mode & S_IFMT) {
1100 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1106 err = ll_new_node(dir, dchild, NULL, mode, old_encode_dev(rdev),
1117 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
1122 #ifdef HAVE_IOP_ATOMIC_OPEN
1124 * Plain create. Intent create is handled in atomic_open.
1126 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1127 umode_t mode, bool want_excl)
1131 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
1133 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), "
1134 "flags=%u, excl=%d\n", dentry->d_name.len,
1135 dentry->d_name.name, PFID(ll_inode2fid(dir)),
1136 dir, mode, want_excl);
1138 /* Using mknod(2) to create a regular file is designed to not recognize
1139 * volatile file name, so we use ll_mknod() here. */
1140 rc = ll_mknod(dir, dentry, mode, 0);
1142 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1144 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, unhashed %d\n",
1145 dentry->d_name.len, dentry->d_name.name, d_unhashed(dentry));
1149 #else /* !HAVE_IOP_ATOMIC_OPEN */
1150 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1151 ll_umode_t mode, struct nameidata *nd)
1153 struct ll_dentry_data *lld = ll_d2d(dentry);
1154 struct lookup_intent *it = NULL;
1157 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
1163 /* LU-8559: use LUSTRE_OPC_CREATE for non atomic open case
1164 * so that volatile file name is recoginized.
1165 * Mknod(2), however, is designed to not recognize volatile
1166 * file name to avoid inode leak under orphan directory until
1168 return ll_new_node(dir, dentry, NULL, mode, 0,
1174 /* Was there an error? Propagate it! */
1175 if (it->it_status) {
1180 rc = ll_create_it(dir, dentry, it);
1181 if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
1184 nd->intent.open.file->private_data = it;
1185 filp = lookup_instantiate_filp(nd, dentry, NULL);
1191 ll_intent_release(it);
1192 OBD_FREE(it, sizeof(*it));
1195 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1199 #endif /* HAVE_IOP_ATOMIC_OPEN */
1201 static int ll_symlink(struct inode *dir, struct dentry *dchild,
1202 const char *oldpath)
1204 struct qstr *name = &dchild->d_name;
1208 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), target=%.*s\n",
1209 name->len, name->name, PFID(ll_inode2fid(dir)),
1210 dir, 3000, oldpath);
1212 err = ll_new_node(dir, dchild, oldpath, S_IFLNK | S_IRWXUGO, 0,
1213 LUSTRE_OPC_SYMLINK);
1216 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
1221 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1222 struct dentry *new_dentry)
1224 struct inode *src = old_dentry->d_inode;
1225 struct qstr *name = &new_dentry->d_name;
1226 struct ll_sb_info *sbi = ll_i2sbi(dir);
1227 struct ptlrpc_request *request = NULL;
1228 struct md_op_data *op_data;
1232 CDEBUG(D_VFSTRACE, "VFS Op: inode="DFID"(%p), dir="DFID"(%p), "
1233 "target=%.*s\n", PFID(ll_inode2fid(src)), src,
1234 PFID(ll_inode2fid(dir)), dir, name->len, name->name);
1236 op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len,
1237 0, LUSTRE_OPC_ANY, NULL);
1238 if (IS_ERR(op_data))
1239 RETURN(PTR_ERR(op_data));
1241 err = md_link(sbi->ll_md_exp, op_data, &request);
1242 ll_finish_md_op_data(op_data);
1246 ll_update_times(request, dir);
1247 ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
1250 ptlrpc_req_finished(request);
1254 static int ll_mkdir(struct inode *dir, struct dentry *dchild, ll_umode_t mode)
1256 struct qstr *name = &dchild->d_name;
1260 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1261 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1263 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1264 mode &= ~current_umask();
1266 mode = (mode & (S_IRWXUGO|S_ISVTX)) | S_IFDIR;
1268 err = ll_new_node(dir, dchild, NULL, mode, 0, LUSTRE_OPC_MKDIR);
1270 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
1275 static int ll_rmdir(struct inode *dir, struct dentry *dchild)
1277 struct qstr *name = &dchild->d_name;
1278 struct ptlrpc_request *request = NULL;
1279 struct md_op_data *op_data;
1283 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1284 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1286 if (unlikely(d_mountpoint(dchild)))
1289 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
1290 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1291 if (IS_ERR(op_data))
1292 RETURN(PTR_ERR(op_data));
1294 if (dchild->d_inode != NULL)
1295 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1297 op_data->op_fid2 = op_data->op_fid3;
1298 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1299 ll_finish_md_op_data(op_data);
1301 ll_update_times(request, dir);
1302 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1305 ptlrpc_req_finished(request);
1312 int ll_rmdir_entry(struct inode *dir, char *name, int namelen)
1314 struct ptlrpc_request *request = NULL;
1315 struct md_op_data *op_data;
1319 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1320 namelen, name, PFID(ll_inode2fid(dir)), dir);
1322 op_data = ll_prep_md_op_data(NULL, dir, NULL, name, strlen(name),
1323 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1324 if (IS_ERR(op_data))
1325 RETURN(PTR_ERR(op_data));
1326 op_data->op_cli_flags |= CLI_RM_ENTRY;
1327 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1328 ll_finish_md_op_data(op_data);
1330 ll_update_times(request, dir);
1331 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1334 ptlrpc_req_finished(request);
1338 static int ll_unlink(struct inode *dir, struct dentry *dchild)
1340 struct qstr *name = &dchild->d_name;
1341 struct ptlrpc_request *request = NULL;
1342 struct md_op_data *op_data;
1345 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1346 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1349 * XXX: unlink bind mountpoint maybe call to here,
1350 * just check it as vfs_unlink does.
1352 if (unlikely(d_mountpoint(dchild)))
1355 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0,
1356 LUSTRE_OPC_ANY, NULL);
1357 if (IS_ERR(op_data))
1358 RETURN(PTR_ERR(op_data));
1360 if (dchild->d_inode != NULL)
1361 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1363 op_data->op_fid2 = op_data->op_fid3;
1364 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1365 ll_finish_md_op_data(op_data);
1369 ll_update_times(request, dir);
1370 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
1373 ptlrpc_req_finished(request);
1377 static int ll_rename(struct inode *src, struct dentry *src_dchild,
1378 struct inode *tgt, struct dentry *tgt_dchild)
1380 struct qstr *src_name = &src_dchild->d_name;
1381 struct qstr *tgt_name = &tgt_dchild->d_name;
1382 struct ptlrpc_request *request = NULL;
1383 struct ll_sb_info *sbi = ll_i2sbi(src);
1384 struct md_op_data *op_data;
1387 CDEBUG(D_VFSTRACE, "VFS Op:oldname=%.*s, src_dir="DFID
1388 "(%p), newname=%.*s, tgt_dir="DFID"(%p)\n",
1389 src_name->len, src_name->name,
1390 PFID(ll_inode2fid(src)), src, tgt_name->len,
1391 tgt_name->name, PFID(ll_inode2fid(tgt)), tgt);
1393 if (unlikely(d_mountpoint(src_dchild) || d_mountpoint(tgt_dchild)))
1396 op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0,
1397 LUSTRE_OPC_ANY, NULL);
1398 if (IS_ERR(op_data))
1399 RETURN(PTR_ERR(op_data));
1401 if (src_dchild->d_inode != NULL)
1402 op_data->op_fid3 = *ll_inode2fid(src_dchild->d_inode);
1404 if (tgt_dchild->d_inode != NULL)
1405 op_data->op_fid4 = *ll_inode2fid(tgt_dchild->d_inode);
1407 err = md_rename(sbi->ll_md_exp, op_data,
1408 src_name->name, src_name->len,
1409 tgt_name->name, tgt_name->len, &request);
1410 ll_finish_md_op_data(op_data);
1412 ll_update_times(request, src);
1413 ll_update_times(request, tgt);
1414 ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1417 ptlrpc_req_finished(request);
1420 d_move(src_dchild, tgt_dchild);
1425 const struct inode_operations ll_dir_inode_operations = {
1427 #ifdef HAVE_IOP_ATOMIC_OPEN
1428 .atomic_open = ll_atomic_open,
1430 .lookup = ll_lookup_nd,
1431 .create = ll_create_nd,
1432 /* We need all these non-raw things for NFSD, to not patch it. */
1433 .unlink = ll_unlink,
1436 .symlink = ll_symlink,
1438 .rename = ll_rename,
1439 .setattr = ll_setattr,
1440 .getattr = ll_getattr,
1441 .permission = ll_inode_permission,
1442 .setxattr = ll_setxattr,
1443 .getxattr = ll_getxattr,
1444 .listxattr = ll_listxattr,
1445 .removexattr = ll_removexattr,
1446 #ifdef HAVE_IOP_GET_ACL
1447 .get_acl = ll_get_acl,
1451 const struct inode_operations ll_special_inode_operations = {
1452 .setattr = ll_setattr,
1453 .getattr = ll_getattr,
1454 .permission = ll_inode_permission,
1455 .setxattr = ll_setxattr,
1456 .getxattr = ll_getxattr,
1457 .listxattr = ll_listxattr,
1458 .removexattr = ll_removexattr,
1459 #ifdef HAVE_IOP_GET_ACL
1460 .get_acl = ll_get_acl,