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);
887 rc = ll_init_security(dentry, inode, dir);
894 void ll_update_times(struct ptlrpc_request *request, struct inode *inode)
896 struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
900 if (body->mbo_valid & OBD_MD_FLMTIME &&
901 body->mbo_mtime > LTIME_S(inode->i_mtime)) {
902 CDEBUG(D_INODE, "setting fid "DFID" mtime from %lu to "LPU64
903 "\n", PFID(ll_inode2fid(inode)),
904 LTIME_S(inode->i_mtime), body->mbo_mtime);
905 LTIME_S(inode->i_mtime) = body->mbo_mtime;
908 if (body->mbo_valid & OBD_MD_FLCTIME &&
909 body->mbo_ctime > LTIME_S(inode->i_ctime))
910 LTIME_S(inode->i_ctime) = body->mbo_ctime;
913 static int ll_new_node(struct inode *dir, struct dentry *dchild,
914 const char *tgt, umode_t mode, int rdev, __u32 opc)
916 struct qstr *name = &dchild->d_name;
917 struct ptlrpc_request *request = NULL;
918 struct md_op_data *op_data;
919 struct inode *inode = NULL;
920 struct ll_sb_info *sbi = ll_i2sbi(dir);
925 if (unlikely(tgt != NULL))
926 tgt_len = strlen(tgt) + 1;
929 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
930 name->len, 0, opc, NULL);
932 GOTO(err_exit, err = PTR_ERR(op_data));
934 err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
935 from_kuid(&init_user_ns, current_fsuid()),
936 from_kgid(&init_user_ns, current_fsgid()),
937 cfs_curproc_cap_pack(), rdev, &request);
938 ll_finish_md_op_data(op_data);
939 if (err < 0 && err != -EREMOTE)
942 /* If the client doesn't know where to create a subdirectory (or
943 * in case of a race that sends the RPC to the wrong MDS), the
944 * MDS will return -EREMOTE and the client will fetch the layout
945 * of the directory, then create the directory on the right MDT. */
946 if (unlikely(err == -EREMOTE)) {
947 struct ll_inode_info *lli = ll_i2info(dir);
948 struct lmv_user_md *lum;
952 ptlrpc_req_finished(request);
955 err2 = ll_dir_getstripe(dir, (void **)&lum, &lumsize, &request,
958 /* Update stripe_offset and retry */
959 lli->lli_def_stripe_offset = lum->lum_stripe_offset;
960 } else if (err2 == -ENODATA &&
961 lli->lli_def_stripe_offset != -1) {
962 /* If there are no default stripe EA on the MDT, but the
963 * client has default stripe, then it probably means
964 * default stripe EA has just been deleted. */
965 lli->lli_def_stripe_offset = -1;
970 ptlrpc_req_finished(request);
975 ll_update_times(request, dir);
977 err = ll_prep_inode(&inode, request, dchild->d_sb, NULL);
981 d_instantiate(dchild, inode);
983 err = ll_init_security(dchild, inode, dir);
990 ptlrpc_req_finished(request);
995 static int ll_mknod(struct inode *dir, struct dentry *dchild, ll_umode_t mode,
998 struct qstr *name = &dchild->d_name;
1002 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p) mode %o dev %x\n",
1003 name->len, name->name, PFID(ll_inode2fid(dir)), dir,
1006 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1007 mode &= ~current_umask();
1009 switch (mode & S_IFMT) {
1011 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1017 err = ll_new_node(dir, dchild, NULL, mode, old_encode_dev(rdev),
1028 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
1033 #ifdef HAVE_IOP_ATOMIC_OPEN
1035 * Plain create. Intent create is handled in atomic_open.
1037 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1038 umode_t mode, bool want_excl)
1042 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), "
1043 "flags=%u, excl=%d\n", dentry->d_name.len,
1044 dentry->d_name.name, PFID(ll_inode2fid(dir)),
1045 dir, mode, want_excl);
1047 rc = ll_mknod(dir, dentry, mode, 0);
1049 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1051 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, unhashed %d\n",
1052 dentry->d_name.len, dentry->d_name.name, d_unhashed(dentry));
1056 #else /* !HAVE_IOP_ATOMIC_OPEN */
1057 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1058 ll_umode_t mode, struct nameidata *nd)
1060 struct ll_dentry_data *lld = ll_d2d(dentry);
1061 struct lookup_intent *it = NULL;
1068 return ll_mknod(dir, dentry, mode, 0);
1072 /* Was there an error? Propagate it! */
1073 if (it->d.lustre.it_status) {
1074 rc = it->d.lustre.it_status;
1078 rc = ll_create_it(dir, dentry, it);
1079 if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
1082 nd->intent.open.file->private_data = it;
1083 filp = lookup_instantiate_filp(nd, dentry, NULL);
1089 ll_intent_release(it);
1090 OBD_FREE(it, sizeof(*it));
1093 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1097 #endif /* HAVE_IOP_ATOMIC_OPEN */
1099 static int ll_symlink(struct inode *dir, struct dentry *dchild,
1100 const char *oldpath)
1102 struct qstr *name = &dchild->d_name;
1106 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), target=%.*s\n",
1107 name->len, name->name, PFID(ll_inode2fid(dir)),
1108 dir, 3000, oldpath);
1110 err = ll_new_node(dir, dchild, oldpath, S_IFLNK | S_IRWXUGO, 0,
1111 LUSTRE_OPC_SYMLINK);
1114 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
1119 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1120 struct dentry *new_dentry)
1122 struct inode *src = old_dentry->d_inode;
1123 struct qstr *name = &new_dentry->d_name;
1124 struct ll_sb_info *sbi = ll_i2sbi(dir);
1125 struct ptlrpc_request *request = NULL;
1126 struct md_op_data *op_data;
1130 CDEBUG(D_VFSTRACE, "VFS Op: inode="DFID"(%p), dir="DFID"(%p), "
1131 "target=%.*s\n", PFID(ll_inode2fid(src)), src,
1132 PFID(ll_inode2fid(dir)), dir, name->len, name->name);
1134 op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len,
1135 0, LUSTRE_OPC_ANY, NULL);
1136 if (IS_ERR(op_data))
1137 RETURN(PTR_ERR(op_data));
1139 err = md_link(sbi->ll_md_exp, op_data, &request);
1140 ll_finish_md_op_data(op_data);
1144 ll_update_times(request, dir);
1145 ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
1148 ptlrpc_req_finished(request);
1152 static int ll_mkdir(struct inode *dir, struct dentry *dchild, ll_umode_t mode)
1154 struct qstr *name = &dchild->d_name;
1158 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1159 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1161 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1162 mode &= ~current_umask();
1164 mode = (mode & (S_IRWXUGO|S_ISVTX)) | S_IFDIR;
1166 err = ll_new_node(dir, dchild, NULL, mode, 0, LUSTRE_OPC_MKDIR);
1168 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
1173 static int ll_rmdir(struct inode *dir, struct dentry *dchild)
1175 struct qstr *name = &dchild->d_name;
1176 struct ptlrpc_request *request = NULL;
1177 struct md_op_data *op_data;
1181 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1182 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1184 if (unlikely(d_mountpoint(dchild)))
1187 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
1188 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1189 if (IS_ERR(op_data))
1190 RETURN(PTR_ERR(op_data));
1192 if (dchild->d_inode != NULL)
1193 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1195 op_data->op_fid2 = op_data->op_fid3;
1196 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1197 ll_finish_md_op_data(op_data);
1199 ll_update_times(request, dir);
1200 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1203 ptlrpc_req_finished(request);
1210 int ll_rmdir_entry(struct inode *dir, char *name, int namelen)
1212 struct ptlrpc_request *request = NULL;
1213 struct md_op_data *op_data;
1217 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1218 namelen, name, PFID(ll_inode2fid(dir)), dir);
1220 op_data = ll_prep_md_op_data(NULL, dir, NULL, name, strlen(name),
1221 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1222 if (IS_ERR(op_data))
1223 RETURN(PTR_ERR(op_data));
1224 op_data->op_cli_flags |= CLI_RM_ENTRY;
1225 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1226 ll_finish_md_op_data(op_data);
1228 ll_update_times(request, dir);
1229 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1232 ptlrpc_req_finished(request);
1236 /* ll_unlink() doesn't update the inode with the new link count.
1237 * Instead, ll_ddelete() and ll_d_iput() will update it based upon if
1238 * there is any lock existing. They will recycle dentries and inodes
1239 * based upon locks too. b=20433 */
1240 static int ll_unlink(struct inode *dir, struct dentry *dchild)
1242 struct qstr *name = &dchild->d_name;
1243 struct ptlrpc_request *request = NULL;
1244 struct md_op_data *op_data;
1247 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1248 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1251 * XXX: unlink bind mountpoint maybe call to here,
1252 * just check it as vfs_unlink does.
1254 if (unlikely(d_mountpoint(dchild)))
1257 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0,
1258 LUSTRE_OPC_ANY, NULL);
1259 if (IS_ERR(op_data))
1260 RETURN(PTR_ERR(op_data));
1262 if (dchild->d_inode != NULL)
1263 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1265 op_data->op_fid2 = op_data->op_fid3;
1266 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1267 ll_finish_md_op_data(op_data);
1271 ll_update_times(request, dir);
1272 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
1275 ptlrpc_req_finished(request);
1279 static int ll_rename(struct inode *src, struct dentry *src_dchild,
1280 struct inode *tgt, struct dentry *tgt_dchild)
1282 struct qstr *src_name = &src_dchild->d_name;
1283 struct qstr *tgt_name = &tgt_dchild->d_name;
1284 struct ptlrpc_request *request = NULL;
1285 struct ll_sb_info *sbi = ll_i2sbi(src);
1286 struct md_op_data *op_data;
1289 CDEBUG(D_VFSTRACE, "VFS Op:oldname=%.*s, src_dir="DFID
1290 "(%p), newname=%.*s, tgt_dir="DFID"(%p)\n",
1291 src_name->len, src_name->name,
1292 PFID(ll_inode2fid(src)), src, tgt_name->len,
1293 tgt_name->name, PFID(ll_inode2fid(tgt)), tgt);
1295 if (unlikely(d_mountpoint(src_dchild) || d_mountpoint(tgt_dchild)))
1298 op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0,
1299 LUSTRE_OPC_ANY, NULL);
1300 if (IS_ERR(op_data))
1301 RETURN(PTR_ERR(op_data));
1303 if (src_dchild->d_inode != NULL)
1304 op_data->op_fid3 = *ll_inode2fid(src_dchild->d_inode);
1306 if (tgt_dchild->d_inode != NULL)
1307 op_data->op_fid4 = *ll_inode2fid(tgt_dchild->d_inode);
1309 err = md_rename(sbi->ll_md_exp, op_data,
1310 src_name->name, src_name->len,
1311 tgt_name->name, tgt_name->len, &request);
1312 ll_finish_md_op_data(op_data);
1314 ll_update_times(request, src);
1315 ll_update_times(request, tgt);
1316 ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1319 ptlrpc_req_finished(request);
1322 d_move(src_dchild, tgt_dchild);
1327 const struct inode_operations ll_dir_inode_operations = {
1329 #ifdef HAVE_IOP_ATOMIC_OPEN
1330 .atomic_open = ll_atomic_open,
1332 .lookup = ll_lookup_nd,
1333 .create = ll_create_nd,
1334 /* We need all these non-raw things for NFSD, to not patch it. */
1335 .unlink = ll_unlink,
1338 .symlink = ll_symlink,
1340 .rename = ll_rename,
1341 .setattr = ll_setattr,
1342 .getattr = ll_getattr,
1343 .permission = ll_inode_permission,
1344 .setxattr = ll_setxattr,
1345 .getxattr = ll_getxattr,
1346 .listxattr = ll_listxattr,
1347 .removexattr = ll_removexattr,
1348 #ifdef HAVE_IOP_GET_ACL
1349 .get_acl = ll_get_acl,
1353 const struct inode_operations ll_special_inode_operations = {
1354 .setattr = ll_setattr,
1355 .getattr = ll_getattr,
1356 .permission = ll_inode_permission,
1357 .setxattr = ll_setxattr,
1358 .getxattr = ll_getxattr,
1359 .listxattr = ll_listxattr,
1360 .removexattr = ll_removexattr,
1361 #ifdef HAVE_IOP_GET_ACL
1362 .get_acl = ll_get_acl,