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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 #include <linux/sched.h>
40 #include <linux/quotaops.h>
41 #include <linux/highmem.h>
42 #include <linux/pagemap.h>
43 #include <linux/security.h>
44 #include <linux/user_namespace.h>
45 #ifdef HAVE_UIDGID_HEADER
46 # include <linux/uidgid.h>
49 #define DEBUG_SUBSYSTEM S_LLITE
51 #include <obd_support.h>
52 #include <lustre_fid.h>
53 #include <lustre_dlm.h>
54 #include <lustre_ver.h>
55 #include "llite_internal.h"
57 static int ll_create_it(struct inode *dir, struct dentry *dentry,
58 struct lookup_intent *it);
60 /* called from iget5_locked->find_inode() under inode_lock spinlock */
61 static int ll_test_inode(struct inode *inode, void *opaque)
63 struct ll_inode_info *lli = ll_i2info(inode);
64 struct lustre_md *md = opaque;
66 if (unlikely(!(md->body->mbo_valid & OBD_MD_FLID))) {
67 CERROR("MDS body missing FID\n");
71 if (!lu_fid_eq(&lli->lli_fid, &md->body->mbo_fid1))
77 static int ll_set_inode(struct inode *inode, void *opaque)
79 struct ll_inode_info *lli = ll_i2info(inode);
80 struct mdt_body *body = ((struct lustre_md *)opaque)->body;
82 if (unlikely(!(body->mbo_valid & OBD_MD_FLID))) {
83 CERROR("MDS body missing FID\n");
87 lli->lli_fid = body->mbo_fid1;
88 if (unlikely(!(body->mbo_valid & OBD_MD_FLTYPE))) {
89 CERROR("Can not initialize inode "DFID" without object type: "
91 PFID(&lli->lli_fid), body->mbo_valid);
95 inode->i_mode = (inode->i_mode & ~S_IFMT) | (body->mbo_mode & S_IFMT);
96 if (unlikely(inode->i_mode == 0)) {
97 CERROR("Invalid inode "DFID" type\n", PFID(&lli->lli_fid));
108 * Get an inode by inode number(@hash), which is already instantiated by
109 * the intent lookup).
111 struct inode *ll_iget(struct super_block *sb, ino_t hash,
112 struct lustre_md *md)
120 inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
122 RETURN(ERR_PTR(-ENOMEM));
124 if (inode->i_state & I_NEW) {
125 rc = ll_read_inode2(inode, md);
126 if (rc == 0 && S_ISREG(inode->i_mode) &&
127 ll_i2info(inode)->lli_clob == NULL)
128 rc = cl_file_inode_init(inode, md);
131 make_bad_inode(inode);
132 unlock_new_inode(inode);
136 unlock_new_inode(inode);
138 } else if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
139 rc = ll_update_inode(inode, md);
140 CDEBUG(D_VFSTRACE, "got inode: "DFID"(%p): rc = %d\n",
141 PFID(&md->body->mbo_fid1), inode, rc);
151 static void ll_invalidate_negative_children(struct inode *dir)
153 struct dentry *dentry, *tmp_subdir;
154 DECLARE_LL_D_HLIST_NODE_PTR(p);
157 ll_d_hlist_for_each_entry(dentry, p, &dir->i_dentry) {
158 spin_lock(&dentry->d_lock);
159 if (!list_empty(&dentry->d_subdirs)) {
160 struct dentry *child;
162 list_for_each_entry_safe(child, tmp_subdir,
165 if (child->d_inode == NULL)
166 d_lustre_invalidate(child, 1);
169 spin_unlock(&dentry->d_lock);
171 ll_unlock_dcache(dir);
174 int ll_test_inode_by_fid(struct inode *inode, void *opaque)
176 return lu_fid_eq(&ll_i2info(inode)->lli_fid, opaque);
179 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
180 void *data, int flag)
182 struct lustre_handle lockh;
187 case LDLM_CB_BLOCKING:
188 ldlm_lock2handle(lock, &lockh);
189 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
191 CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
195 case LDLM_CB_CANCELING: {
196 struct inode *inode = ll_inode_from_resource_lock(lock);
197 __u64 bits = lock->l_policy_data.l_inodebits.bits;
199 /* Inode is set to lock->l_resource->lr_lvb_inode
200 * for mdc - bug 24555 */
201 LASSERT(lock->l_ast_data == NULL);
206 /* Invalidate all dentries associated with this inode */
207 LASSERT(ldlm_is_canceling(lock));
209 if (!fid_res_name_eq(ll_inode2fid(inode),
210 &lock->l_resource->lr_name)) {
211 LDLM_ERROR(lock, "data mismatch with object "DFID"(%p)",
212 PFID(ll_inode2fid(inode)), inode);
216 if (bits & MDS_INODELOCK_XATTR) {
217 if (S_ISDIR(inode->i_mode))
218 ll_i2info(inode)->lli_def_stripe_offset = -1;
219 ll_xattr_cache_destroy(inode);
220 bits &= ~MDS_INODELOCK_XATTR;
223 /* For OPEN locks we differentiate between lock modes
224 * LCK_CR, LCK_CW, LCK_PR - bug 22891 */
225 if (bits & MDS_INODELOCK_OPEN)
226 ll_have_md_lock(inode, &bits, lock->l_req_mode);
228 if (bits & MDS_INODELOCK_OPEN) {
231 switch (lock->l_req_mode) {
242 LDLM_ERROR(lock, "bad lock mode for OPEN lock");
246 ll_md_real_close(inode, fmode);
248 bits &= ~MDS_INODELOCK_OPEN;
251 if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
252 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_PERM))
253 ll_have_md_lock(inode, &bits, LCK_MINMODE);
255 if (bits & MDS_INODELOCK_LAYOUT) {
256 struct cl_object_conf conf = {
257 .coc_opc = OBJECT_CONF_INVALIDATE,
261 rc = ll_layout_conf(inode, &conf);
263 CDEBUG(D_INODE, "cannot invalidate layout of "
265 PFID(ll_inode2fid(inode)), rc);
268 if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
269 struct ll_inode_info *lli = ll_i2info(inode);
271 CDEBUG(D_INODE, "invalidating inode "DFID" lli = %p, "
272 "pfid = "DFID"\n", PFID(ll_inode2fid(inode)),
273 lli, PFID(&lli->lli_pfid));
274 truncate_inode_pages(inode->i_mapping, 0);
276 if (unlikely(!fid_is_zero(&lli->lli_pfid))) {
277 struct inode *master_inode = NULL;
280 /* This is slave inode, since all of the child
281 * dentry is connected on the master inode, so
282 * we have to invalidate the negative children
284 CDEBUG(D_INODE, "Invalidate s"DFID" m"DFID"\n",
285 PFID(ll_inode2fid(inode)),
286 PFID(&lli->lli_pfid));
288 hash = cl_fid_build_ino(&lli->lli_pfid,
289 ll_need_32bit_api(ll_i2sbi(inode)));
291 master_inode = ilookup5(inode->i_sb, hash,
292 ll_test_inode_by_fid,
293 (void *)&lli->lli_pfid);
294 if (master_inode != NULL &&
295 !IS_ERR(master_inode)) {
296 ll_invalidate_negative_children(
301 ll_invalidate_negative_children(inode);
305 if ((bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM)) &&
306 inode->i_sb->s_root != NULL &&
307 inode != inode->i_sb->s_root->d_inode)
308 ll_invalidate_aliases(inode);
320 __u32 ll_i2suppgid(struct inode *i)
322 if (in_group_p(i->i_gid))
323 return (__u32)from_kgid(&init_user_ns, i->i_gid);
325 return (__u32) __kgid_val(INVALID_GID);
328 /* Pack the required supplementary groups into the supplied groups array.
329 * If we don't need to use the groups from the target inode(s) then we
330 * instead pack one or more groups from the user's supplementary group
331 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
332 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
335 LASSERT(suppgids != NULL);
337 suppgids[0] = ll_i2suppgid(i1);
340 suppgids[1] = ll_i2suppgid(i2);
346 * try to reuse three types of dentry:
347 * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
348 * by concurrent .revalidate).
349 * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
350 * be cleared by others calling d_lustre_revalidate).
351 * 3. DISCONNECTED alias.
353 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
355 struct dentry *alias, *discon_alias, *invalid_alias;
356 DECLARE_LL_D_HLIST_NODE_PTR(p);
358 if (ll_d_hlist_empty(&inode->i_dentry))
361 discon_alias = invalid_alias = NULL;
363 ll_lock_dcache(inode);
364 ll_d_hlist_for_each_entry(alias, p, &inode->i_dentry) {
365 LASSERT(alias != dentry);
367 spin_lock(&alias->d_lock);
368 if (alias->d_flags & DCACHE_DISCONNECTED)
369 /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
370 discon_alias = alias;
371 else if (alias->d_parent == dentry->d_parent &&
372 alias->d_name.hash == dentry->d_name.hash &&
373 alias->d_name.len == dentry->d_name.len &&
374 memcmp(alias->d_name.name, dentry->d_name.name,
375 dentry->d_name.len) == 0)
376 invalid_alias = alias;
377 spin_unlock(&alias->d_lock);
382 alias = invalid_alias ?: discon_alias ?: NULL;
384 spin_lock(&alias->d_lock);
386 spin_unlock(&alias->d_lock);
388 ll_unlock_dcache(inode);
394 * Similar to d_splice_alias(), but lustre treats invalid alias
395 * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
397 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
403 new = ll_find_alias(inode, de);
413 "Reuse dentry %p inode %p refc %d flags %#x\n",
414 new, new->d_inode, ll_d_count(new), new->d_flags);
422 CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
423 de, de->d_inode, ll_d_count(de), de->d_flags);
427 static int ll_lookup_it_finish(struct ptlrpc_request *request,
428 struct lookup_intent *it,
429 struct inode *parent, struct dentry **de)
431 struct inode *inode = NULL;
436 /* NB 1 request reference will be taken away by ll_intent_lock()
438 CDEBUG(D_DENTRY, "it %p it_disposition %x\n", it,
439 it->d.lustre.it_disposition);
440 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
441 rc = ll_prep_inode(&inode, request, (*de)->d_sb, it);
445 ll_set_lock_data(ll_i2sbi(parent)->ll_md_exp, inode, it, &bits);
447 /* We used to query real size from OSTs here, but actually
448 this is not needed. For stat() calls size would be updated
449 from subsequent do_revalidate()->ll_inode_revalidate_it() in
451 vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
452 Everybody else who needs correct file size would call
453 ll_glimpse_size or some equivalent themselves anyway.
454 Also see bug 7198. */
457 /* Only hash *de if it is unhashed (new dentry).
458 * Atoimc_open may passin hashed dentries for open.
460 if (d_unhashed(*de)) {
461 struct dentry *alias;
463 alias = ll_splice_alias(inode, *de);
465 GOTO(out, rc = PTR_ERR(alias));
468 } else if (!it_disposition(it, DISP_LOOKUP_NEG) &&
469 !it_disposition(it, DISP_OPEN_CREATE)) {
470 /* With DISP_OPEN_CREATE dentry will
471 instantiated in ll_create_it. */
472 LASSERT((*de)->d_inode == NULL);
473 d_instantiate(*de, inode);
476 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
477 /* we have lookup look - unhide dentry */
478 if (bits & MDS_INODELOCK_LOOKUP)
479 d_lustre_revalidate(*de);
480 } else if (!it_disposition(it, DISP_OPEN_CREATE)) {
481 /* If file created on server, don't depend on parent UPDATE
482 * lock to unhide it. It is left hidden and next lookup can
483 * find it in ll_splice_alias.
485 /* Check that parent has UPDATE lock. */
486 struct lookup_intent parent_it = {
488 .d.lustre.it_lock_handle = 0 };
489 struct lu_fid fid = ll_i2info(parent)->lli_fid;
491 /* If it is striped directory, get the real stripe parent */
492 if (unlikely(ll_i2info(parent)->lli_lsm_md != NULL)) {
493 rc = md_get_fid_from_lsm(ll_i2mdexp(parent),
494 ll_i2info(parent)->lli_lsm_md,
496 (*de)->d_name.len, &fid);
501 if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it, &fid,
503 d_lustre_revalidate(*de);
504 ll_intent_release(&parent_it);
511 if (rc != 0 && it->it_op & IT_OPEN)
512 ll_open_cleanup((*de)->d_sb, request);
517 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
518 struct lookup_intent *it)
520 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
521 struct dentry *save = dentry, *retval;
522 struct ptlrpc_request *req = NULL;
523 struct md_op_data *op_data = NULL;
528 if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
529 RETURN(ERR_PTR(-ENAMETOOLONG));
531 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), intent=%s\n",
532 dentry->d_name.len, dentry->d_name.name,
533 PFID(ll_inode2fid(parent)), parent, LL_IT2STR(it));
535 if (d_mountpoint(dentry))
536 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
538 if (it == NULL || it->it_op == IT_GETXATTR)
541 if (it->it_op == IT_GETATTR && dentry_may_statahead(parent, dentry)) {
542 rc = ll_statahead(parent, &dentry, 0);
544 RETURN(dentry == save ? NULL : dentry);
547 if (it->it_op & IT_CREAT)
548 opc = LUSTRE_OPC_CREATE;
550 opc = LUSTRE_OPC_ANY;
552 op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
553 dentry->d_name.len, 0, opc, NULL);
555 RETURN((void *)op_data);
557 /* enforce umask if acl disabled or MDS doesn't support umask */
558 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
559 it->it_create_mode &= ~current_umask();
561 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
562 &ll_md_blocking_ast, 0);
563 /* If the MDS allows the client to chgrp (CFS_SETGRP_PERM), but the
564 * client does not know which suppgid should be sent to the MDS, or
565 * some other(s) changed the target file's GID after this RPC sent
566 * to the MDS with the suppgid as the original GID, then we should
567 * try again with right suppgid. */
568 if (rc == -EACCES && it->it_op & IT_OPEN &&
569 it_disposition(it, DISP_OPEN_DENY)) {
570 struct mdt_body *body;
572 LASSERT(req != NULL);
574 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
575 if (op_data->op_suppgids[0] == body->mbo_gid ||
576 op_data->op_suppgids[1] == body->mbo_gid ||
577 !in_group_p(make_kgid(&init_user_ns, body->mbo_gid)))
578 GOTO(out, retval = ERR_PTR(-EACCES));
580 fid_zero(&op_data->op_fid2);
581 op_data->op_suppgids[1] = body->mbo_gid;
582 ptlrpc_req_finished(req);
584 ll_intent_release(it);
585 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
586 &ll_md_blocking_ast, 0);
590 GOTO(out, retval = ERR_PTR(rc));
592 rc = ll_lookup_it_finish(req, it, parent, &dentry);
594 ll_intent_release(it);
595 GOTO(out, retval = ERR_PTR(rc));
598 if ((it->it_op & IT_OPEN) && dentry->d_inode &&
599 !S_ISREG(dentry->d_inode->i_mode) &&
600 !S_ISDIR(dentry->d_inode->i_mode)) {
601 ll_release_openhandle(dentry, it);
603 ll_lookup_finish_locks(it, dentry);
605 GOTO(out, retval = (dentry == save) ? NULL : dentry);
608 if (op_data != NULL && !IS_ERR(op_data))
609 ll_finish_md_op_data(op_data);
611 ptlrpc_req_finished(req);
615 #ifdef HAVE_IOP_ATOMIC_OPEN
616 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
619 struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
622 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), flags=%u\n",
623 dentry->d_name.len, dentry->d_name.name,
624 PFID(ll_inode2fid(parent)), parent, flags);
626 /* Optimize away (CREATE && !OPEN). Let .create handle the race. */
627 if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN))
630 if (flags & (LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE))
634 de = ll_lookup_it(parent, dentry, itp);
637 ll_intent_release(itp);
643 * For cached negative dentry and new dentry, handle lookup/create/open
646 static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
647 struct file *file, unsigned open_flags,
648 umode_t mode, int *opened)
650 struct lookup_intent *it;
652 long long lookup_flags = LOOKUP_OPEN;
656 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), file %p,"
657 "open_flags %x, mode %x opened %d\n",
658 dentry->d_name.len, dentry->d_name.name,
659 PFID(ll_inode2fid(dir)), dir, file, open_flags, mode, *opened);
661 OBD_ALLOC(it, sizeof(*it));
666 if (open_flags & O_CREAT) {
667 it->it_op |= IT_CREAT;
668 lookup_flags |= LOOKUP_CREATE;
670 it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
671 it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
672 it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
674 /* Dentry added to dcache tree in ll_lookup_it */
675 de = ll_lookup_it(dir, dentry, it);
682 if (it_disposition(it, DISP_OPEN_CREATE)) {
683 /* Dentry instantiated in ll_create_it. */
684 rc = ll_create_it(dir, dentry, it);
686 /* We dget in ll_splice_alias. */
692 *opened |= FILE_CREATED;
694 if (dentry->d_inode && it_disposition(it, DISP_OPEN_OPEN)) {
696 if (S_ISFIFO(dentry->d_inode->i_mode)) {
697 /* We cannot call open here as it would
700 if (it_disposition(it, DISP_ENQ_OPEN_REF))
702 (struct ptlrpc_request *)
703 it->d.lustre.it_data);
704 rc = finish_no_open(file, de);
706 file->private_data = it;
707 rc = finish_open(file, dentry, NULL, opened);
708 /* We dget in ll_splice_alias. finish_open takes
709 * care of dget for fd open.
715 rc = finish_no_open(file, de);
720 ll_intent_release(it);
721 OBD_FREE(it, sizeof(*it));
726 #else /* !HAVE_IOP_ATOMIC_OPEN */
727 static struct lookup_intent *
728 ll_convert_intent(struct open_intent *oit, int lookup_flags)
730 struct lookup_intent *it;
734 return ERR_PTR(-ENOMEM);
736 if (lookup_flags & LOOKUP_OPEN) {
738 if (lookup_flags & LOOKUP_CREATE)
739 it->it_op |= IT_CREAT;
740 it->it_create_mode = (oit->create_mode & S_IALLUGO) | S_IFREG;
741 it->it_flags = ll_namei_to_lookup_intent_flag(oit->flags);
742 it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
744 it->it_op = IT_GETATTR;
750 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
751 struct nameidata *nd)
756 if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
757 struct lookup_intent *it;
759 if (ll_d2d(dentry) && ll_d2d(dentry)->lld_it) {
760 it = ll_d2d(dentry)->lld_it;
761 ll_d2d(dentry)->lld_it = NULL;
763 if ((nd->flags & LOOKUP_CREATE) &&
764 !(nd->flags & LOOKUP_OPEN))
767 it = ll_convert_intent(&nd->intent.open, nd->flags);
769 RETURN((struct dentry *)it);
772 de = ll_lookup_it(parent, dentry, it);
775 if ((nd->flags & LOOKUP_OPEN) && !IS_ERR(dentry)) { /* Open */
776 if (dentry->d_inode &&
777 it_disposition(it, DISP_OPEN_OPEN)) { /* nocreate */
778 if (S_ISFIFO(dentry->d_inode->i_mode)) {
779 // We cannot call open here as it would
782 (struct ptlrpc_request *)
783 it->d.lustre.it_data);
787 nd->intent.open.file->private_data = it;
788 filp = lookup_instantiate_filp(nd,
794 de = (struct dentry *)filp;
797 } else if (it_disposition(it, DISP_OPEN_CREATE)) {
798 // XXX This can only reliably work on assumption
799 // that there are NO hashed negative dentries.
800 ll_d2d(dentry)->lld_it = it;
801 it = NULL; /* Will be freed in ll_create_nd */
802 /* We absolutely depend on ll_create_nd to be
803 * called to not leak this intent and possible
804 * data attached to it */
809 ll_intent_release(it);
810 OBD_FREE(it, sizeof(*it));
813 de = ll_lookup_it(parent, dentry, NULL);
818 #endif /* HAVE_IOP_ATOMIC_OPEN */
820 /* We depend on "mode" being set with the proper file type/umask by now */
821 static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
823 struct inode *inode = NULL;
824 struct ptlrpc_request *request = NULL;
825 struct ll_sb_info *sbi = ll_i2sbi(dir);
829 LASSERT(it && it->d.lustre.it_disposition);
831 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
832 request = it->d.lustre.it_data;
833 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
834 rc = ll_prep_inode(&inode, request, dir->i_sb, it);
836 GOTO(out, inode = ERR_PTR(rc));
838 LASSERT(ll_d_hlist_empty(&inode->i_dentry));
840 /* We asked for a lock on the directory, but were granted a
841 * lock on the inode. Since we finally have an inode pointer,
842 * stuff it in the lock. */
843 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode "DFID"(%p)\n",
844 PFID(ll_inode2fid(inode)), inode);
845 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
848 ptlrpc_req_finished(request);
853 * By the time this is called, we already have created the directory cache
854 * entry for the new file, but it is so far negative - it has no inode.
856 * We defer creating the OBD object(s) until open, to keep the intent and
857 * non-intent code paths similar, and also because we do not have the MDS
858 * inode number before calling ll_create_node() (which is needed for LOV),
859 * so we would need to do yet another RPC to the MDS to store the LOV EA
860 * data on the MDS. If needed, we would pass the PACKED lmm as data and
861 * lmm_size in datalen (the MDS still has code which will handle that).
863 * If the create succeeds, we fill in the inode information
864 * with d_instantiate().
866 static int ll_create_it(struct inode *dir, struct dentry *dentry,
867 struct lookup_intent *it)
873 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), intent=%s\n",
874 dentry->d_name.len, dentry->d_name.name,
875 PFID(ll_inode2fid(dir)), dir, LL_IT2STR(it));
877 rc = it_open_error(DISP_OPEN_CREATE, it);
881 inode = ll_create_node(dir, it);
883 RETURN(PTR_ERR(inode));
885 d_instantiate(dentry, inode);
889 void ll_update_times(struct ptlrpc_request *request, struct inode *inode)
891 struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
895 if (body->mbo_valid & OBD_MD_FLMTIME &&
896 body->mbo_mtime > LTIME_S(inode->i_mtime)) {
897 CDEBUG(D_INODE, "setting fid "DFID" mtime from %lu to "LPU64
898 "\n", PFID(ll_inode2fid(inode)),
899 LTIME_S(inode->i_mtime), body->mbo_mtime);
900 LTIME_S(inode->i_mtime) = body->mbo_mtime;
903 if (body->mbo_valid & OBD_MD_FLCTIME &&
904 body->mbo_ctime > LTIME_S(inode->i_ctime))
905 LTIME_S(inode->i_ctime) = body->mbo_ctime;
908 static int ll_new_node(struct inode *dir, struct dentry *dchild,
909 const char *tgt, umode_t mode, int rdev, __u32 opc)
911 struct qstr *name = &dchild->d_name;
912 struct ptlrpc_request *request = NULL;
913 struct md_op_data *op_data;
914 struct inode *inode = NULL;
915 struct ll_sb_info *sbi = ll_i2sbi(dir);
920 if (unlikely(tgt != NULL))
921 tgt_len = strlen(tgt) + 1;
924 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
925 name->len, 0, opc, NULL);
927 GOTO(err_exit, err = PTR_ERR(op_data));
929 err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
930 from_kuid(&init_user_ns, current_fsuid()),
931 from_kgid(&init_user_ns, current_fsgid()),
932 cfs_curproc_cap_pack(), rdev, &request);
933 ll_finish_md_op_data(op_data);
935 /* If the client doesn't know where to create a subdirectory (or
936 * in case of a race that sends the RPC to the wrong MDS), the
937 * MDS will return -EREMOTE and the client will fetch the layout
938 * for the directory, either from the local xattr cache or the
939 * MDS, then create the directory on the right MDT. */
940 if (err == -EREMOTE) {
941 struct lmv_user_md *lum;
944 ptlrpc_req_finished(request);
949 GOTO(err_exit, err = -ENOMEM);
951 rc = ll_getxattr_common(dir, XATTR_NAME_DEFAULT_LMV,
962 ll_i2info(dir)->lli_def_stripe_offset =
963 le32_to_cpu(lum->lum_stripe_offset);
970 ll_update_times(request, dir);
972 err = ll_prep_inode(&inode, request, dchild->d_sb, NULL);
976 d_instantiate(dchild, inode);
981 ptlrpc_req_finished(request);
986 static int ll_mknod(struct inode *dir, struct dentry *dchild, ll_umode_t mode,
989 struct qstr *name = &dchild->d_name;
993 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p) mode %o dev %x\n",
994 name->len, name->name, PFID(ll_inode2fid(dir)), dir,
997 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
998 mode &= ~current_umask();
1000 switch (mode & S_IFMT) {
1002 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1008 err = ll_new_node(dir, dchild, NULL, mode, old_encode_dev(rdev),
1019 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
1024 #ifdef HAVE_IOP_ATOMIC_OPEN
1026 * Plain create. Intent create is handled in atomic_open.
1028 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1029 umode_t mode, bool want_excl)
1033 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), "
1034 "flags=%u, excl=%d\n", dentry->d_name.len,
1035 dentry->d_name.name, PFID(ll_inode2fid(dir)),
1036 dir, mode, want_excl);
1038 rc = ll_mknod(dir, dentry, mode, 0);
1040 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1042 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, unhashed %d\n",
1043 dentry->d_name.len, dentry->d_name.name, d_unhashed(dentry));
1047 #else /* !HAVE_IOP_ATOMIC_OPEN */
1048 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1049 ll_umode_t mode, struct nameidata *nd)
1051 struct ll_dentry_data *lld = ll_d2d(dentry);
1052 struct lookup_intent *it = NULL;
1059 return ll_mknod(dir, dentry, mode, 0);
1063 /* Was there an error? Propagate it! */
1064 if (it->d.lustre.it_status) {
1065 rc = it->d.lustre.it_status;
1069 rc = ll_create_it(dir, dentry, it);
1070 if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
1073 nd->intent.open.file->private_data = it;
1074 filp = lookup_instantiate_filp(nd, dentry, NULL);
1080 ll_intent_release(it);
1081 OBD_FREE(it, sizeof(*it));
1084 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1088 #endif /* HAVE_IOP_ATOMIC_OPEN */
1090 static int ll_symlink(struct inode *dir, struct dentry *dchild,
1091 const char *oldpath)
1093 struct qstr *name = &dchild->d_name;
1097 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), target=%.*s\n",
1098 name->len, name->name, PFID(ll_inode2fid(dir)),
1099 dir, 3000, oldpath);
1101 err = ll_new_node(dir, dchild, oldpath, S_IFLNK | S_IRWXUGO, 0,
1102 LUSTRE_OPC_SYMLINK);
1105 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
1110 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1111 struct dentry *new_dentry)
1113 struct inode *src = old_dentry->d_inode;
1114 struct qstr *name = &new_dentry->d_name;
1115 struct ll_sb_info *sbi = ll_i2sbi(dir);
1116 struct ptlrpc_request *request = NULL;
1117 struct md_op_data *op_data;
1121 CDEBUG(D_VFSTRACE, "VFS Op: inode="DFID"(%p), dir="DFID"(%p), "
1122 "target=%.*s\n", PFID(ll_inode2fid(src)), src,
1123 PFID(ll_inode2fid(dir)), dir, name->len, name->name);
1125 op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len,
1126 0, LUSTRE_OPC_ANY, NULL);
1127 if (IS_ERR(op_data))
1128 RETURN(PTR_ERR(op_data));
1130 err = md_link(sbi->ll_md_exp, op_data, &request);
1131 ll_finish_md_op_data(op_data);
1135 ll_update_times(request, dir);
1136 ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
1139 ptlrpc_req_finished(request);
1143 static int ll_mkdir(struct inode *dir, struct dentry *dchild, ll_umode_t mode)
1145 struct qstr *name = &dchild->d_name;
1149 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1150 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1152 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1153 mode &= ~current_umask();
1155 mode = (mode & (S_IRWXUGO|S_ISVTX)) | S_IFDIR;
1157 err = ll_new_node(dir, dchild, NULL, mode, 0, LUSTRE_OPC_MKDIR);
1159 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
1164 static int ll_rmdir(struct inode *dir, struct dentry *dchild)
1166 struct qstr *name = &dchild->d_name;
1167 struct ptlrpc_request *request = NULL;
1168 struct md_op_data *op_data;
1172 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1173 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1175 if (unlikely(d_mountpoint(dchild)))
1178 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
1179 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1180 if (IS_ERR(op_data))
1181 RETURN(PTR_ERR(op_data));
1183 if (dchild->d_inode != NULL)
1184 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1186 op_data->op_fid2 = op_data->op_fid3;
1187 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1188 ll_finish_md_op_data(op_data);
1190 ll_update_times(request, dir);
1191 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1194 ptlrpc_req_finished(request);
1201 int ll_rmdir_entry(struct inode *dir, char *name, int namelen)
1203 struct ptlrpc_request *request = NULL;
1204 struct md_op_data *op_data;
1208 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1209 namelen, name, PFID(ll_inode2fid(dir)), dir);
1211 op_data = ll_prep_md_op_data(NULL, dir, NULL, name, strlen(name),
1212 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1213 if (IS_ERR(op_data))
1214 RETURN(PTR_ERR(op_data));
1215 op_data->op_cli_flags |= CLI_RM_ENTRY;
1216 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1217 ll_finish_md_op_data(op_data);
1219 ll_update_times(request, dir);
1220 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1223 ptlrpc_req_finished(request);
1227 /* ll_unlink() doesn't update the inode with the new link count.
1228 * Instead, ll_ddelete() and ll_d_iput() will update it based upon if
1229 * there is any lock existing. They will recycle dentries and inodes
1230 * based upon locks too. b=20433 */
1231 static int ll_unlink(struct inode *dir, struct dentry *dchild)
1233 struct qstr *name = &dchild->d_name;
1234 struct ptlrpc_request *request = NULL;
1235 struct md_op_data *op_data;
1238 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1239 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1242 * XXX: unlink bind mountpoint maybe call to here,
1243 * just check it as vfs_unlink does.
1245 if (unlikely(d_mountpoint(dchild)))
1248 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0,
1249 LUSTRE_OPC_ANY, NULL);
1250 if (IS_ERR(op_data))
1251 RETURN(PTR_ERR(op_data));
1253 if (dchild->d_inode != NULL)
1254 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1256 op_data->op_fid2 = op_data->op_fid3;
1257 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1258 ll_finish_md_op_data(op_data);
1262 ll_update_times(request, dir);
1263 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
1266 ptlrpc_req_finished(request);
1270 static int ll_rename(struct inode *src, struct dentry *src_dchild,
1271 struct inode *tgt, struct dentry *tgt_dchild)
1273 struct qstr *src_name = &src_dchild->d_name;
1274 struct qstr *tgt_name = &tgt_dchild->d_name;
1275 struct ptlrpc_request *request = NULL;
1276 struct ll_sb_info *sbi = ll_i2sbi(src);
1277 struct md_op_data *op_data;
1280 CDEBUG(D_VFSTRACE, "VFS Op:oldname=%.*s, src_dir="DFID
1281 "(%p), newname=%.*s, tgt_dir="DFID"(%p)\n",
1282 src_name->len, src_name->name,
1283 PFID(ll_inode2fid(src)), src, tgt_name->len,
1284 tgt_name->name, PFID(ll_inode2fid(tgt)), tgt);
1286 if (unlikely(d_mountpoint(src_dchild) || d_mountpoint(tgt_dchild)))
1289 op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0,
1290 LUSTRE_OPC_ANY, NULL);
1291 if (IS_ERR(op_data))
1292 RETURN(PTR_ERR(op_data));
1294 if (src_dchild->d_inode != NULL)
1295 op_data->op_fid3 = *ll_inode2fid(src_dchild->d_inode);
1297 if (tgt_dchild->d_inode != NULL)
1298 op_data->op_fid4 = *ll_inode2fid(tgt_dchild->d_inode);
1300 err = md_rename(sbi->ll_md_exp, op_data,
1301 src_name->name, src_name->len,
1302 tgt_name->name, tgt_name->len, &request);
1303 ll_finish_md_op_data(op_data);
1305 ll_update_times(request, src);
1306 ll_update_times(request, tgt);
1307 ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1310 ptlrpc_req_finished(request);
1313 d_move(src_dchild, tgt_dchild);
1318 const struct inode_operations ll_dir_inode_operations = {
1320 #ifdef HAVE_IOP_ATOMIC_OPEN
1321 .atomic_open = ll_atomic_open,
1323 .lookup = ll_lookup_nd,
1324 .create = ll_create_nd,
1325 /* We need all these non-raw things for NFSD, to not patch it. */
1326 .unlink = ll_unlink,
1329 .symlink = ll_symlink,
1331 .rename = ll_rename,
1332 .setattr = ll_setattr,
1333 .getattr = ll_getattr,
1334 .permission = ll_inode_permission,
1335 .setxattr = ll_setxattr,
1336 .getxattr = ll_getxattr,
1337 .listxattr = ll_listxattr,
1338 .removexattr = ll_removexattr,
1339 #ifdef HAVE_IOP_GET_ACL
1340 .get_acl = ll_get_acl,
1344 const struct inode_operations ll_special_inode_operations = {
1345 .setattr = ll_setattr,
1346 .getattr = ll_getattr,
1347 .permission = ll_inode_permission,
1348 .setxattr = ll_setxattr,
1349 .getxattr = ll_getxattr,
1350 .listxattr = ll_listxattr,
1351 .removexattr = ll_removexattr,
1352 #ifdef HAVE_IOP_GET_ACL
1353 .get_acl = ll_get_acl,