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 CDEBUG(D_INODE, "%s: apply lsm %p to inode "DFID"\n",
129 ll_get_fsname(sb, NULL, 0), md->lsm,
130 PFID(ll_inode2fid(inode)));
131 rc = cl_file_inode_init(inode, md);
134 make_bad_inode(inode);
135 unlock_new_inode(inode);
139 unlock_new_inode(inode);
141 } else if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
142 rc = ll_update_inode(inode, md);
143 CDEBUG(D_VFSTRACE, "got inode: "DFID"(%p): rc = %d\n",
144 PFID(&md->body->mbo_fid1), inode, rc);
154 static void ll_invalidate_negative_children(struct inode *dir)
156 struct dentry *dentry, *tmp_subdir;
157 DECLARE_LL_D_HLIST_NODE_PTR(p);
160 ll_d_hlist_for_each_entry(dentry, p, &dir->i_dentry, d_alias) {
161 spin_lock(&dentry->d_lock);
162 if (!list_empty(&dentry->d_subdirs)) {
163 struct dentry *child;
165 list_for_each_entry_safe(child, tmp_subdir,
168 if (child->d_inode == NULL)
169 d_lustre_invalidate(child, 1);
172 spin_unlock(&dentry->d_lock);
174 ll_unlock_dcache(dir);
177 int ll_test_inode_by_fid(struct inode *inode, void *opaque)
179 return lu_fid_eq(&ll_i2info(inode)->lli_fid, opaque);
182 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
183 void *data, int flag)
185 struct lustre_handle lockh;
190 case LDLM_CB_BLOCKING:
191 ldlm_lock2handle(lock, &lockh);
192 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
194 CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
198 case LDLM_CB_CANCELING: {
199 struct inode *inode = ll_inode_from_resource_lock(lock);
200 __u64 bits = lock->l_policy_data.l_inodebits.bits;
202 /* Inode is set to lock->l_resource->lr_lvb_inode
203 * for mdc - bug 24555 */
204 LASSERT(lock->l_ast_data == NULL);
209 /* Invalidate all dentries associated with this inode */
210 LASSERT(ldlm_is_canceling(lock));
212 if (!fid_res_name_eq(ll_inode2fid(inode),
213 &lock->l_resource->lr_name)) {
214 LDLM_ERROR(lock, "data mismatch with object "DFID"(%p)",
215 PFID(ll_inode2fid(inode)), inode);
219 if (bits & MDS_INODELOCK_XATTR) {
220 if (S_ISDIR(inode->i_mode))
221 ll_i2info(inode)->lli_def_stripe_offset = -1;
222 ll_xattr_cache_destroy(inode);
223 bits &= ~MDS_INODELOCK_XATTR;
226 /* For OPEN locks we differentiate between lock modes
227 * LCK_CR, LCK_CW, LCK_PR - bug 22891 */
228 if (bits & MDS_INODELOCK_OPEN)
229 ll_have_md_lock(inode, &bits, lock->l_req_mode);
231 if (bits & MDS_INODELOCK_OPEN) {
234 switch (lock->l_req_mode) {
245 LDLM_ERROR(lock, "bad lock mode for OPEN lock");
249 ll_md_real_close(inode, fmode);
251 bits &= ~MDS_INODELOCK_OPEN;
254 if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
255 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_PERM))
256 ll_have_md_lock(inode, &bits, LCK_MINMODE);
258 if (bits & MDS_INODELOCK_LAYOUT) {
259 struct cl_object_conf conf = {
260 .coc_opc = OBJECT_CONF_INVALIDATE,
264 rc = ll_layout_conf(inode, &conf);
266 CDEBUG(D_INODE, "cannot invalidate layout of "
268 PFID(ll_inode2fid(inode)), rc);
271 if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
272 struct ll_inode_info *lli = ll_i2info(inode);
274 CDEBUG(D_INODE, "invalidating inode "DFID" lli = %p, "
275 "pfid = "DFID"\n", PFID(ll_inode2fid(inode)),
276 lli, PFID(&lli->lli_pfid));
277 truncate_inode_pages(inode->i_mapping, 0);
279 if (unlikely(!fid_is_zero(&lli->lli_pfid))) {
280 struct inode *master_inode = NULL;
283 /* This is slave inode, since all of the child
284 * dentry is connected on the master inode, so
285 * we have to invalidate the negative children
287 CDEBUG(D_INODE, "Invalidate s"DFID" m"DFID"\n",
288 PFID(ll_inode2fid(inode)),
289 PFID(&lli->lli_pfid));
291 hash = cl_fid_build_ino(&lli->lli_pfid,
292 ll_need_32bit_api(ll_i2sbi(inode)));
294 master_inode = ilookup5(inode->i_sb, hash,
295 ll_test_inode_by_fid,
296 (void *)&lli->lli_pfid);
297 if (master_inode != NULL &&
298 !IS_ERR(master_inode)) {
299 ll_invalidate_negative_children(
304 ll_invalidate_negative_children(inode);
308 if ((bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM)) &&
309 inode->i_sb->s_root != NULL &&
310 inode != inode->i_sb->s_root->d_inode)
311 ll_invalidate_aliases(inode);
323 __u32 ll_i2suppgid(struct inode *i)
325 if (in_group_p(i->i_gid))
326 return (__u32)from_kgid(&init_user_ns, i->i_gid);
328 return (__u32) __kgid_val(INVALID_GID);
331 /* Pack the required supplementary groups into the supplied groups array.
332 * If we don't need to use the groups from the target inode(s) then we
333 * instead pack one or more groups from the user's supplementary group
334 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
335 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
338 LASSERT(suppgids != NULL);
340 suppgids[0] = ll_i2suppgid(i1);
343 suppgids[1] = ll_i2suppgid(i2);
349 * try to reuse three types of dentry:
350 * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
351 * by concurrent .revalidate).
352 * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
353 * be cleared by others calling d_lustre_revalidate).
354 * 3. DISCONNECTED alias.
356 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
358 struct dentry *alias, *discon_alias, *invalid_alias;
359 DECLARE_LL_D_HLIST_NODE_PTR(p);
361 if (ll_d_hlist_empty(&inode->i_dentry))
364 discon_alias = invalid_alias = NULL;
366 ll_lock_dcache(inode);
367 ll_d_hlist_for_each_entry(alias, p, &inode->i_dentry, d_alias) {
368 LASSERT(alias != dentry);
370 spin_lock(&alias->d_lock);
371 if (alias->d_flags & DCACHE_DISCONNECTED)
372 /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
373 discon_alias = alias;
374 else if (alias->d_parent == dentry->d_parent &&
375 alias->d_name.hash == dentry->d_name.hash &&
376 alias->d_name.len == dentry->d_name.len &&
377 memcmp(alias->d_name.name, dentry->d_name.name,
378 dentry->d_name.len) == 0)
379 invalid_alias = alias;
380 spin_unlock(&alias->d_lock);
385 alias = invalid_alias ?: discon_alias ?: NULL;
387 spin_lock(&alias->d_lock);
389 spin_unlock(&alias->d_lock);
391 ll_unlock_dcache(inode);
397 * Similar to d_splice_alias(), but lustre treats invalid alias
398 * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
400 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
406 new = ll_find_alias(inode, de);
416 "Reuse dentry %p inode %p refc %d flags %#x\n",
417 new, new->d_inode, ll_d_count(new), new->d_flags);
425 CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
426 de, de->d_inode, ll_d_count(de), de->d_flags);
430 static int ll_lookup_it_finish(struct ptlrpc_request *request,
431 struct lookup_intent *it,
432 struct inode *parent, struct dentry **de)
434 struct inode *inode = NULL;
439 /* NB 1 request reference will be taken away by ll_intent_lock()
441 CDEBUG(D_DENTRY, "it %p it_disposition %x\n", it,
442 it->d.lustre.it_disposition);
443 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
444 rc = ll_prep_inode(&inode, request, (*de)->d_sb, it);
448 ll_set_lock_data(ll_i2sbi(parent)->ll_md_exp, inode, it, &bits);
450 /* We used to query real size from OSTs here, but actually
451 this is not needed. For stat() calls size would be updated
452 from subsequent do_revalidate()->ll_inode_revalidate_it() in
454 vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
455 Everybody else who needs correct file size would call
456 ll_glimpse_size or some equivalent themselves anyway.
457 Also see bug 7198. */
460 /* Only hash *de if it is unhashed (new dentry).
461 * Atoimc_open may passin hashed dentries for open.
463 if (d_unhashed(*de)) {
464 struct dentry *alias;
466 alias = ll_splice_alias(inode, *de);
468 GOTO(out, rc = PTR_ERR(alias));
471 } else if (!it_disposition(it, DISP_LOOKUP_NEG) &&
472 !it_disposition(it, DISP_OPEN_CREATE)) {
473 /* With DISP_OPEN_CREATE dentry will
474 instantiated in ll_create_it. */
475 LASSERT((*de)->d_inode == NULL);
476 d_instantiate(*de, inode);
479 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
480 /* we have lookup look - unhide dentry */
481 if (bits & MDS_INODELOCK_LOOKUP)
482 d_lustre_revalidate(*de);
483 } else if (!it_disposition(it, DISP_OPEN_CREATE)) {
484 /* If file created on server, don't depend on parent UPDATE
485 * lock to unhide it. It is left hidden and next lookup can
486 * find it in ll_splice_alias.
488 /* Check that parent has UPDATE lock. */
489 struct lookup_intent parent_it = {
491 .d.lustre.it_lock_handle = 0 };
492 struct lu_fid fid = ll_i2info(parent)->lli_fid;
494 /* If it is striped directory, get the real stripe parent */
495 if (unlikely(ll_i2info(parent)->lli_lsm_md != NULL)) {
496 rc = md_get_fid_from_lsm(ll_i2mdexp(parent),
497 ll_i2info(parent)->lli_lsm_md,
499 (*de)->d_name.len, &fid);
504 if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it, &fid,
506 d_lustre_revalidate(*de);
507 ll_intent_release(&parent_it);
514 if (rc != 0 && it->it_op & IT_OPEN)
515 ll_open_cleanup((*de)->d_sb, request);
520 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
521 struct lookup_intent *it)
523 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
524 struct dentry *save = dentry, *retval;
525 struct ptlrpc_request *req = NULL;
526 struct md_op_data *op_data = NULL;
531 if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
532 RETURN(ERR_PTR(-ENAMETOOLONG));
534 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), intent=%s\n",
535 dentry->d_name.len, dentry->d_name.name,
536 PFID(ll_inode2fid(parent)), parent, LL_IT2STR(it));
538 if (d_mountpoint(dentry))
539 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
541 if (it == NULL || it->it_op == IT_GETXATTR)
544 if (it->it_op == IT_GETATTR && dentry_may_statahead(parent, dentry)) {
545 rc = ll_statahead(parent, &dentry, 0);
547 RETURN(dentry == save ? NULL : dentry);
550 if (it->it_op & IT_CREAT)
551 opc = LUSTRE_OPC_CREATE;
553 opc = LUSTRE_OPC_ANY;
555 op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
556 dentry->d_name.len, 0, opc, NULL);
558 RETURN((void *)op_data);
560 /* enforce umask if acl disabled or MDS doesn't support umask */
561 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
562 it->it_create_mode &= ~current_umask();
564 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
565 &ll_md_blocking_ast, 0);
566 /* If the MDS allows the client to chgrp (CFS_SETGRP_PERM), but the
567 * client does not know which suppgid should be sent to the MDS, or
568 * some other(s) changed the target file's GID after this RPC sent
569 * to the MDS with the suppgid as the original GID, then we should
570 * try again with right suppgid. */
571 if (rc == -EACCES && it->it_op & IT_OPEN &&
572 it_disposition(it, DISP_OPEN_DENY)) {
573 struct mdt_body *body;
575 LASSERT(req != NULL);
577 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
578 if (op_data->op_suppgids[0] == body->mbo_gid ||
579 op_data->op_suppgids[1] == body->mbo_gid ||
580 !in_group_p(make_kgid(&init_user_ns, body->mbo_gid)))
581 GOTO(out, retval = ERR_PTR(-EACCES));
583 fid_zero(&op_data->op_fid2);
584 op_data->op_suppgids[1] = body->mbo_gid;
585 ptlrpc_req_finished(req);
587 ll_intent_release(it);
588 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
589 &ll_md_blocking_ast, 0);
593 GOTO(out, retval = ERR_PTR(rc));
595 rc = ll_lookup_it_finish(req, it, parent, &dentry);
597 ll_intent_release(it);
598 GOTO(out, retval = ERR_PTR(rc));
601 if ((it->it_op & IT_OPEN) && dentry->d_inode &&
602 !S_ISREG(dentry->d_inode->i_mode) &&
603 !S_ISDIR(dentry->d_inode->i_mode)) {
604 ll_release_openhandle(dentry, it);
606 ll_lookup_finish_locks(it, dentry);
608 GOTO(out, retval = (dentry == save) ? NULL : dentry);
611 if (op_data != NULL && !IS_ERR(op_data))
612 ll_finish_md_op_data(op_data);
614 ptlrpc_req_finished(req);
618 #ifdef HAVE_IOP_ATOMIC_OPEN
619 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
622 struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
625 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), flags=%u\n",
626 dentry->d_name.len, dentry->d_name.name,
627 PFID(ll_inode2fid(parent)), parent, flags);
629 /* Optimize away (CREATE && !OPEN). Let .create handle the race. */
630 if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN))
633 if (flags & (LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE))
637 de = ll_lookup_it(parent, dentry, itp);
640 ll_intent_release(itp);
646 * For cached negative dentry and new dentry, handle lookup/create/open
649 static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
650 struct file *file, unsigned open_flags,
651 umode_t mode, int *opened)
653 struct lookup_intent *it;
655 long long lookup_flags = LOOKUP_OPEN;
659 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), file %p,"
660 "open_flags %x, mode %x opened %d\n",
661 dentry->d_name.len, dentry->d_name.name,
662 PFID(ll_inode2fid(dir)), dir, file, open_flags, mode, *opened);
664 OBD_ALLOC(it, sizeof(*it));
669 if (open_flags & O_CREAT) {
670 it->it_op |= IT_CREAT;
671 lookup_flags |= LOOKUP_CREATE;
673 it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
674 it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
675 it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
677 /* Dentry added to dcache tree in ll_lookup_it */
678 de = ll_lookup_it(dir, dentry, it);
685 if (it_disposition(it, DISP_OPEN_CREATE)) {
686 /* Dentry instantiated in ll_create_it. */
687 rc = ll_create_it(dir, dentry, it);
689 /* We dget in ll_splice_alias. */
695 *opened |= FILE_CREATED;
697 if (dentry->d_inode && it_disposition(it, DISP_OPEN_OPEN)) {
699 if (S_ISFIFO(dentry->d_inode->i_mode)) {
700 /* We cannot call open here as it would
703 if (it_disposition(it, DISP_ENQ_OPEN_REF))
705 (struct ptlrpc_request *)
706 it->d.lustre.it_data);
707 rc = finish_no_open(file, de);
709 file->private_data = it;
710 rc = finish_open(file, dentry, NULL, opened);
711 /* We dget in ll_splice_alias. finish_open takes
712 * care of dget for fd open.
718 rc = finish_no_open(file, de);
723 ll_intent_release(it);
724 OBD_FREE(it, sizeof(*it));
729 #else /* !HAVE_IOP_ATOMIC_OPEN */
730 static struct lookup_intent *
731 ll_convert_intent(struct open_intent *oit, int lookup_flags)
733 struct lookup_intent *it;
737 return ERR_PTR(-ENOMEM);
739 if (lookup_flags & LOOKUP_OPEN) {
741 if (lookup_flags & LOOKUP_CREATE)
742 it->it_op |= IT_CREAT;
743 it->it_create_mode = (oit->create_mode & S_IALLUGO) | S_IFREG;
744 it->it_flags = ll_namei_to_lookup_intent_flag(oit->flags);
745 it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
747 it->it_op = IT_GETATTR;
753 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
754 struct nameidata *nd)
759 if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
760 struct lookup_intent *it;
762 if (ll_d2d(dentry) && ll_d2d(dentry)->lld_it) {
763 it = ll_d2d(dentry)->lld_it;
764 ll_d2d(dentry)->lld_it = NULL;
766 if ((nd->flags & LOOKUP_CREATE) &&
767 !(nd->flags & LOOKUP_OPEN))
770 it = ll_convert_intent(&nd->intent.open, nd->flags);
772 RETURN((struct dentry *)it);
775 de = ll_lookup_it(parent, dentry, it);
778 if ((nd->flags & LOOKUP_OPEN) && !IS_ERR(dentry)) { /* Open */
779 if (dentry->d_inode &&
780 it_disposition(it, DISP_OPEN_OPEN)) { /* nocreate */
781 if (S_ISFIFO(dentry->d_inode->i_mode)) {
782 // We cannot call open here as it would
785 (struct ptlrpc_request *)
786 it->d.lustre.it_data);
790 nd->intent.open.file->private_data = it;
791 filp = lookup_instantiate_filp(nd,
797 de = (struct dentry *)filp;
800 } else if (it_disposition(it, DISP_OPEN_CREATE)) {
801 // XXX This can only reliably work on assumption
802 // that there are NO hashed negative dentries.
803 ll_d2d(dentry)->lld_it = it;
804 it = NULL; /* Will be freed in ll_create_nd */
805 /* We absolutely depend on ll_create_nd to be
806 * called to not leak this intent and possible
807 * data attached to it */
812 ll_intent_release(it);
813 OBD_FREE(it, sizeof(*it));
816 de = ll_lookup_it(parent, dentry, NULL);
821 #endif /* HAVE_IOP_ATOMIC_OPEN */
823 /* We depend on "mode" being set with the proper file type/umask by now */
824 static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
826 struct inode *inode = NULL;
827 struct ptlrpc_request *request = NULL;
828 struct ll_sb_info *sbi = ll_i2sbi(dir);
832 LASSERT(it && it->d.lustre.it_disposition);
834 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
835 request = it->d.lustre.it_data;
836 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
837 rc = ll_prep_inode(&inode, request, dir->i_sb, it);
839 GOTO(out, inode = ERR_PTR(rc));
841 LASSERT(ll_d_hlist_empty(&inode->i_dentry));
843 /* We asked for a lock on the directory, but were granted a
844 * lock on the inode. Since we finally have an inode pointer,
845 * stuff it in the lock. */
846 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode "DFID"(%p)\n",
847 PFID(ll_inode2fid(inode)), inode);
848 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
851 ptlrpc_req_finished(request);
856 * By the time this is called, we already have created the directory cache
857 * entry for the new file, but it is so far negative - it has no inode.
859 * We defer creating the OBD object(s) until open, to keep the intent and
860 * non-intent code paths similar, and also because we do not have the MDS
861 * inode number before calling ll_create_node() (which is needed for LOV),
862 * so we would need to do yet another RPC to the MDS to store the LOV EA
863 * data on the MDS. If needed, we would pass the PACKED lmm as data and
864 * lmm_size in datalen (the MDS still has code which will handle that).
866 * If the create succeeds, we fill in the inode information
867 * with d_instantiate().
869 static int ll_create_it(struct inode *dir, struct dentry *dentry,
870 struct lookup_intent *it)
876 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), intent=%s\n",
877 dentry->d_name.len, dentry->d_name.name,
878 PFID(ll_inode2fid(dir)), dir, LL_IT2STR(it));
880 rc = it_open_error(DISP_OPEN_CREATE, it);
884 inode = ll_create_node(dir, it);
886 RETURN(PTR_ERR(inode));
888 d_instantiate(dentry, inode);
892 void ll_update_times(struct ptlrpc_request *request, struct inode *inode)
894 struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
898 if (body->mbo_valid & OBD_MD_FLMTIME &&
899 body->mbo_mtime > LTIME_S(inode->i_mtime)) {
900 CDEBUG(D_INODE, "setting fid "DFID" mtime from %lu to "LPU64
901 "\n", PFID(ll_inode2fid(inode)),
902 LTIME_S(inode->i_mtime), body->mbo_mtime);
903 LTIME_S(inode->i_mtime) = body->mbo_mtime;
906 if (body->mbo_valid & OBD_MD_FLCTIME &&
907 body->mbo_ctime > LTIME_S(inode->i_ctime))
908 LTIME_S(inode->i_ctime) = body->mbo_ctime;
911 static int ll_new_node(struct inode *dir, struct dentry *dchild,
912 const char *tgt, umode_t mode, int rdev, __u32 opc)
914 struct qstr *name = &dchild->d_name;
915 struct ptlrpc_request *request = NULL;
916 struct md_op_data *op_data;
917 struct inode *inode = NULL;
918 struct ll_sb_info *sbi = ll_i2sbi(dir);
923 if (unlikely(tgt != NULL))
924 tgt_len = strlen(tgt) + 1;
927 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
928 name->len, 0, opc, NULL);
930 GOTO(err_exit, err = PTR_ERR(op_data));
932 err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
933 from_kuid(&init_user_ns, current_fsuid()),
934 from_kgid(&init_user_ns, current_fsgid()),
935 cfs_curproc_cap_pack(), rdev, &request);
936 ll_finish_md_op_data(op_data);
938 /* If the client doesn't know where to create a subdirectory (or
939 * in case of a race that sends the RPC to the wrong MDS), the
940 * MDS will return -EREMOTE and the client will fetch the layout
941 * for the directory, either from the local xattr cache or the
942 * MDS, then create the directory on the right MDT. */
943 if (err == -EREMOTE) {
944 struct lmv_user_md *lum;
947 ptlrpc_req_finished(request);
952 GOTO(err_exit, err = -ENOMEM);
954 rc = ll_getxattr_common(dir, XATTR_NAME_DEFAULT_LMV,
965 ll_i2info(dir)->lli_def_stripe_offset =
966 le32_to_cpu(lum->lum_stripe_offset);
973 ll_update_times(request, dir);
975 err = ll_prep_inode(&inode, request, dchild->d_sb, NULL);
979 d_instantiate(dchild, inode);
984 ptlrpc_req_finished(request);
989 static int ll_mknod(struct inode *dir, struct dentry *dchild, ll_umode_t mode,
992 struct qstr *name = &dchild->d_name;
996 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p) mode %o dev %x\n",
997 name->len, name->name, PFID(ll_inode2fid(dir)), dir,
1000 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1001 mode &= ~current_umask();
1003 switch (mode & S_IFMT) {
1005 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1011 err = ll_new_node(dir, dchild, NULL, mode, old_encode_dev(rdev),
1022 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
1027 #ifdef HAVE_IOP_ATOMIC_OPEN
1029 * Plain create. Intent create is handled in atomic_open.
1031 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1032 umode_t mode, bool want_excl)
1036 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), "
1037 "flags=%u, excl=%d\n", dentry->d_name.len,
1038 dentry->d_name.name, PFID(ll_inode2fid(dir)),
1039 dir, mode, want_excl);
1041 rc = ll_mknod(dir, dentry, mode, 0);
1043 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1045 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, unhashed %d\n",
1046 dentry->d_name.len, dentry->d_name.name, d_unhashed(dentry));
1050 #else /* !HAVE_IOP_ATOMIC_OPEN */
1051 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1052 ll_umode_t mode, struct nameidata *nd)
1054 struct ll_dentry_data *lld = ll_d2d(dentry);
1055 struct lookup_intent *it = NULL;
1062 return ll_mknod(dir, dentry, mode, 0);
1066 /* Was there an error? Propagate it! */
1067 if (it->d.lustre.it_status) {
1068 rc = it->d.lustre.it_status;
1072 rc = ll_create_it(dir, dentry, it);
1073 if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
1076 nd->intent.open.file->private_data = it;
1077 filp = lookup_instantiate_filp(nd, dentry, NULL);
1083 ll_intent_release(it);
1084 OBD_FREE(it, sizeof(*it));
1087 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1091 #endif /* HAVE_IOP_ATOMIC_OPEN */
1093 static int ll_symlink(struct inode *dir, struct dentry *dchild,
1094 const char *oldpath)
1096 struct qstr *name = &dchild->d_name;
1100 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), target=%.*s\n",
1101 name->len, name->name, PFID(ll_inode2fid(dir)),
1102 dir, 3000, oldpath);
1104 err = ll_new_node(dir, dchild, oldpath, S_IFLNK | S_IRWXUGO, 0,
1105 LUSTRE_OPC_SYMLINK);
1108 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
1113 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1114 struct dentry *new_dentry)
1116 struct inode *src = old_dentry->d_inode;
1117 struct qstr *name = &new_dentry->d_name;
1118 struct ll_sb_info *sbi = ll_i2sbi(dir);
1119 struct ptlrpc_request *request = NULL;
1120 struct md_op_data *op_data;
1124 CDEBUG(D_VFSTRACE, "VFS Op: inode="DFID"(%p), dir="DFID"(%p), "
1125 "target=%.*s\n", PFID(ll_inode2fid(src)), src,
1126 PFID(ll_inode2fid(dir)), dir, name->len, name->name);
1128 op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len,
1129 0, LUSTRE_OPC_ANY, NULL);
1130 if (IS_ERR(op_data))
1131 RETURN(PTR_ERR(op_data));
1133 err = md_link(sbi->ll_md_exp, op_data, &request);
1134 ll_finish_md_op_data(op_data);
1138 ll_update_times(request, dir);
1139 ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
1142 ptlrpc_req_finished(request);
1146 static int ll_mkdir(struct inode *dir, struct dentry *dchild, ll_umode_t mode)
1148 struct qstr *name = &dchild->d_name;
1152 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1153 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1155 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1156 mode &= ~current_umask();
1158 mode = (mode & (S_IRWXUGO|S_ISVTX)) | S_IFDIR;
1160 err = ll_new_node(dir, dchild, NULL, mode, 0, LUSTRE_OPC_MKDIR);
1162 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
1167 static int ll_rmdir(struct inode *dir, struct dentry *dchild)
1169 struct qstr *name = &dchild->d_name;
1170 struct ptlrpc_request *request = NULL;
1171 struct md_op_data *op_data;
1175 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1176 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1178 if (unlikely(d_mountpoint(dchild)))
1181 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
1182 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1183 if (IS_ERR(op_data))
1184 RETURN(PTR_ERR(op_data));
1186 if (dchild->d_inode != NULL)
1187 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1189 op_data->op_fid2 = op_data->op_fid3;
1190 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1191 ll_finish_md_op_data(op_data);
1193 ll_update_times(request, dir);
1194 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1197 ptlrpc_req_finished(request);
1204 int ll_rmdir_entry(struct inode *dir, char *name, int namelen)
1206 struct ptlrpc_request *request = NULL;
1207 struct md_op_data *op_data;
1211 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1212 namelen, name, PFID(ll_inode2fid(dir)), dir);
1214 op_data = ll_prep_md_op_data(NULL, dir, NULL, name, strlen(name),
1215 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1216 if (IS_ERR(op_data))
1217 RETURN(PTR_ERR(op_data));
1218 op_data->op_cli_flags |= CLI_RM_ENTRY;
1219 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1220 ll_finish_md_op_data(op_data);
1222 ll_update_times(request, dir);
1223 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1226 ptlrpc_req_finished(request);
1230 /* ll_unlink() doesn't update the inode with the new link count.
1231 * Instead, ll_ddelete() and ll_d_iput() will update it based upon if
1232 * there is any lock existing. They will recycle dentries and inodes
1233 * based upon locks too. b=20433 */
1234 static int ll_unlink(struct inode *dir, struct dentry *dchild)
1236 struct qstr *name = &dchild->d_name;
1237 struct ptlrpc_request *request = NULL;
1238 struct md_op_data *op_data;
1241 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1242 name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1245 * XXX: unlink bind mountpoint maybe call to here,
1246 * just check it as vfs_unlink does.
1248 if (unlikely(d_mountpoint(dchild)))
1251 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0,
1252 LUSTRE_OPC_ANY, NULL);
1253 if (IS_ERR(op_data))
1254 RETURN(PTR_ERR(op_data));
1256 if (dchild->d_inode != NULL)
1257 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1259 op_data->op_fid2 = op_data->op_fid3;
1260 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1261 ll_finish_md_op_data(op_data);
1265 ll_update_times(request, dir);
1266 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
1269 ptlrpc_req_finished(request);
1273 static int ll_rename(struct inode *src, struct dentry *src_dchild,
1274 struct inode *tgt, struct dentry *tgt_dchild)
1276 struct qstr *src_name = &src_dchild->d_name;
1277 struct qstr *tgt_name = &tgt_dchild->d_name;
1278 struct ptlrpc_request *request = NULL;
1279 struct ll_sb_info *sbi = ll_i2sbi(src);
1280 struct md_op_data *op_data;
1283 CDEBUG(D_VFSTRACE, "VFS Op:oldname=%.*s, src_dir="DFID
1284 "(%p), newname=%.*s, tgt_dir="DFID"(%p)\n",
1285 src_name->len, src_name->name,
1286 PFID(ll_inode2fid(src)), src, tgt_name->len,
1287 tgt_name->name, PFID(ll_inode2fid(tgt)), tgt);
1289 if (unlikely(d_mountpoint(src_dchild) || d_mountpoint(tgt_dchild)))
1292 op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0,
1293 LUSTRE_OPC_ANY, NULL);
1294 if (IS_ERR(op_data))
1295 RETURN(PTR_ERR(op_data));
1297 if (src_dchild->d_inode != NULL)
1298 op_data->op_fid3 = *ll_inode2fid(src_dchild->d_inode);
1300 if (tgt_dchild->d_inode != NULL)
1301 op_data->op_fid4 = *ll_inode2fid(tgt_dchild->d_inode);
1303 err = md_rename(sbi->ll_md_exp, op_data,
1304 src_name->name, src_name->len,
1305 tgt_name->name, tgt_name->len, &request);
1306 ll_finish_md_op_data(op_data);
1308 ll_update_times(request, src);
1309 ll_update_times(request, tgt);
1310 ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1313 ptlrpc_req_finished(request);
1316 d_move(src_dchild, tgt_dchild);
1321 const struct inode_operations ll_dir_inode_operations = {
1323 #ifdef HAVE_IOP_ATOMIC_OPEN
1324 .atomic_open = ll_atomic_open,
1326 .lookup = ll_lookup_nd,
1327 .create = ll_create_nd,
1328 /* We need all these non-raw things for NFSD, to not patch it. */
1329 .unlink = ll_unlink,
1332 .symlink = ll_symlink,
1334 .rename = ll_rename,
1335 .setattr = ll_setattr,
1336 .getattr = ll_getattr,
1337 .permission = ll_inode_permission,
1338 .setxattr = ll_setxattr,
1339 .getxattr = ll_getxattr,
1340 .listxattr = ll_listxattr,
1341 .removexattr = ll_removexattr,
1342 #ifdef HAVE_IOP_GET_ACL
1343 .get_acl = ll_get_acl,
1347 const struct inode_operations ll_special_inode_operations = {
1348 .setattr = ll_setattr,
1349 .getattr = ll_getattr,
1350 .permission = ll_inode_permission,
1351 .setxattr = ll_setxattr,
1352 .getxattr = ll_getxattr,
1353 .listxattr = ll_listxattr,
1354 .removexattr = ll_removexattr,
1355 #ifdef HAVE_IOP_GET_ACL
1356 .get_acl = ll_get_acl,