4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 #include <linux/sched.h>
36 #include <linux/quotaops.h>
37 #include <linux/highmem.h>
38 #include <linux/pagemap.h>
39 #include <linux/security.h>
41 #define DEBUG_SUBSYSTEM S_LLITE
43 #include "../include/obd_support.h"
44 #include "../include/lustre_fid.h"
45 #include "../include/lustre_lite.h"
46 #include "../include/lustre_dlm.h"
47 #include "../include/lustre_ver.h"
48 #include "llite_internal.h"
50 static int ll_create_it(struct inode *dir, struct dentry *dentry,
51 struct lookup_intent *it);
53 /* called from iget5_locked->find_inode() under inode_hash_lock spinlock */
54 static int ll_test_inode(struct inode *inode, void *opaque)
56 struct ll_inode_info *lli = ll_i2info(inode);
57 struct lustre_md *md = opaque;
59 if (unlikely(!(md->body->mbo_valid & OBD_MD_FLID))) {
60 CERROR("MDS body missing FID\n");
64 if (!lu_fid_eq(&lli->lli_fid, &md->body->mbo_fid1))
70 static int ll_set_inode(struct inode *inode, void *opaque)
72 struct ll_inode_info *lli = ll_i2info(inode);
73 struct mdt_body *body = ((struct lustre_md *)opaque)->body;
75 if (unlikely(!(body->mbo_valid & OBD_MD_FLID))) {
76 CERROR("MDS body missing FID\n");
80 lli->lli_fid = body->mbo_fid1;
81 if (unlikely(!(body->mbo_valid & OBD_MD_FLTYPE))) {
82 CERROR("Can not initialize inode " DFID
83 " without object type: valid = %#llx\n",
84 PFID(&lli->lli_fid), body->mbo_valid);
88 inode->i_mode = (inode->i_mode & ~S_IFMT) | (body->mbo_mode & S_IFMT);
89 if (unlikely(inode->i_mode == 0)) {
90 CERROR("Invalid inode "DFID" type\n", PFID(&lli->lli_fid));
100 * Get an inode by inode number(@hash), which is already instantiated by
101 * the intent lookup).
103 struct inode *ll_iget(struct super_block *sb, ino_t hash,
104 struct lustre_md *md)
110 inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
112 return ERR_PTR(-ENOMEM);
114 if (inode->i_state & I_NEW) {
115 rc = ll_read_inode2(inode, md);
116 if (!rc && S_ISREG(inode->i_mode) &&
117 !ll_i2info(inode)->lli_clob) {
118 CDEBUG(D_INODE, "%s: apply lsm %p to inode "DFID"\n",
119 ll_get_fsname(sb, NULL, 0), md->lsm,
120 PFID(ll_inode2fid(inode)));
121 rc = cl_file_inode_init(inode, md);
124 make_bad_inode(inode);
125 unlock_new_inode(inode);
129 unlock_new_inode(inode);
131 } else if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
132 rc = ll_update_inode(inode, md);
133 CDEBUG(D_VFSTRACE, "got inode: "DFID"(%p): rc = %d\n",
134 PFID(&md->body->mbo_fid1), inode, rc);
136 make_bad_inode(inode);
144 static void ll_invalidate_negative_children(struct inode *dir)
146 struct dentry *dentry, *tmp_subdir;
148 spin_lock(&dir->i_lock);
149 hlist_for_each_entry(dentry, &dir->i_dentry, d_u.d_alias) {
150 spin_lock(&dentry->d_lock);
151 if (!list_empty(&dentry->d_subdirs)) {
152 struct dentry *child;
154 list_for_each_entry_safe(child, tmp_subdir,
157 if (d_really_is_negative(child))
158 d_lustre_invalidate(child, 1);
161 spin_unlock(&dentry->d_lock);
163 spin_unlock(&dir->i_lock);
166 int ll_test_inode_by_fid(struct inode *inode, void *opaque)
168 return lu_fid_eq(&ll_i2info(inode)->lli_fid, opaque);
171 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
172 void *data, int flag)
174 struct lustre_handle lockh;
178 case LDLM_CB_BLOCKING:
179 ldlm_lock2handle(lock, &lockh);
180 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
182 CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
186 case LDLM_CB_CANCELING: {
187 struct inode *inode = ll_inode_from_resource_lock(lock);
188 __u64 bits = lock->l_policy_data.l_inodebits.bits;
190 /* Inode is set to lock->l_resource->lr_lvb_inode
191 * for mdc - bug 24555
193 LASSERT(!lock->l_ast_data);
198 /* Invalidate all dentries associated with this inode */
199 LASSERT(ldlm_is_canceling(lock));
201 if (!fid_res_name_eq(ll_inode2fid(inode),
202 &lock->l_resource->lr_name)) {
203 LDLM_ERROR(lock, "data mismatch with object "DFID"(%p)",
204 PFID(ll_inode2fid(inode)), inode);
208 if (bits & MDS_INODELOCK_XATTR) {
209 ll_xattr_cache_destroy(inode);
210 bits &= ~MDS_INODELOCK_XATTR;
213 /* For OPEN locks we differentiate between lock modes
214 * LCK_CR, LCK_CW, LCK_PR - bug 22891
216 if (bits & MDS_INODELOCK_OPEN)
217 ll_have_md_lock(inode, &bits, lock->l_req_mode);
219 if (bits & MDS_INODELOCK_OPEN) {
222 switch (lock->l_req_mode) {
233 LDLM_ERROR(lock, "bad lock mode for OPEN lock");
237 ll_md_real_close(inode, fmode);
240 if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
241 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_PERM))
242 ll_have_md_lock(inode, &bits, LCK_MINMODE);
244 if (bits & MDS_INODELOCK_LAYOUT) {
245 struct cl_object_conf conf = {
246 .coc_opc = OBJECT_CONF_INVALIDATE,
250 rc = ll_layout_conf(inode, &conf);
252 CDEBUG(D_INODE, "cannot invalidate layout of "
254 PFID(ll_inode2fid(inode)), rc);
257 if (bits & MDS_INODELOCK_UPDATE) {
258 struct ll_inode_info *lli = ll_i2info(inode);
260 spin_lock(&lli->lli_lock);
261 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
262 spin_unlock(&lli->lli_lock);
265 if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
266 struct ll_inode_info *lli = ll_i2info(inode);
268 CDEBUG(D_INODE, "invalidating inode "DFID" lli = %p, pfid = "DFID"\n",
269 PFID(ll_inode2fid(inode)), lli,
270 PFID(&lli->lli_pfid));
272 truncate_inode_pages(inode->i_mapping, 0);
274 if (unlikely(!fid_is_zero(&lli->lli_pfid))) {
275 struct inode *master_inode = NULL;
279 * This is slave inode, since all of the child
280 * dentry is connected on the master inode, so
281 * 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 && !IS_ERR(master_inode)) {
295 ll_invalidate_negative_children(master_inode);
299 ll_invalidate_negative_children(inode);
303 if ((bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM)) &&
304 inode->i_sb->s_root &&
305 !is_root_inode(inode))
306 ll_invalidate_aliases(inode);
318 __u32 ll_i2suppgid(struct inode *i)
320 if (in_group_p(i->i_gid))
321 return (__u32)from_kgid(&init_user_ns, i->i_gid);
326 /* Pack the required supplementary groups into the supplied groups array.
327 * If we don't need to use the groups from the target inode(s) then we
328 * instead pack one or more groups from the user's supplementary group
329 * array in case it might be useful. Not needed if doing an MDS-side upcall.
331 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
335 suppgids[0] = ll_i2suppgid(i1);
338 suppgids[1] = ll_i2suppgid(i2);
344 * try to reuse three types of dentry:
345 * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
346 * by concurrent .revalidate).
347 * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
348 * be cleared by others calling d_lustre_revalidate).
349 * 3. DISCONNECTED alias.
351 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
353 struct dentry *alias, *discon_alias, *invalid_alias;
355 if (hlist_empty(&inode->i_dentry))
359 invalid_alias = NULL;
361 spin_lock(&inode->i_lock);
362 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
363 LASSERT(alias != dentry);
365 spin_lock(&alias->d_lock);
366 if ((alias->d_flags & DCACHE_DISCONNECTED) &&
367 S_ISDIR(inode->i_mode))
368 /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
369 discon_alias = alias;
370 else if (alias->d_parent == dentry->d_parent &&
371 alias->d_name.hash == dentry->d_name.hash &&
372 alias->d_name.len == dentry->d_name.len &&
373 memcmp(alias->d_name.name, dentry->d_name.name,
374 dentry->d_name.len) == 0)
375 invalid_alias = alias;
376 spin_unlock(&alias->d_lock);
381 alias = invalid_alias ?: discon_alias ?: NULL;
383 spin_lock(&alias->d_lock);
385 spin_unlock(&alias->d_lock);
387 spin_unlock(&inode->i_lock);
393 * Similar to d_splice_alias(), but lustre treats invalid alias
394 * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
396 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
402 new = ll_find_alias(inode, de);
412 "Reuse dentry %p inode %p refc %d flags %#x\n",
413 new, d_inode(new), d_count(new), new->d_flags);
421 CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
422 de, d_inode(de), d_count(de), de->d_flags);
426 static int ll_lookup_it_finish(struct ptlrpc_request *request,
427 struct lookup_intent *it,
428 struct inode *parent, struct dentry **de)
430 struct inode *inode = NULL;
433 struct dentry *alias;
435 /* NB 1 request reference will be taken away by ll_intent_lock()
438 CDEBUG(D_DENTRY, "it %p it_disposition %x\n", it,
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.
458 alias = ll_splice_alias(inode, *de);
465 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
466 /* we have lookup look - unhide dentry */
467 if (bits & MDS_INODELOCK_LOOKUP)
468 d_lustre_revalidate(*de);
469 } else if (!it_disposition(it, DISP_OPEN_CREATE)) {
470 /* If file created on server, don't depend on parent UPDATE
471 * lock to unhide it. It is left hidden and next lookup can
472 * find it in ll_splice_alias.
474 /* Check that parent has UPDATE lock. */
475 struct lookup_intent parent_it = {
477 .it_lock_handle = 0 };
478 struct lu_fid fid = ll_i2info(parent)->lli_fid;
480 /* If it is striped directory, get the real stripe parent */
481 if (unlikely(ll_i2info(parent)->lli_lsm_md)) {
482 rc = md_get_fid_from_lsm(ll_i2mdexp(parent),
483 ll_i2info(parent)->lli_lsm_md,
485 (*de)->d_name.len, &fid);
490 if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it, &fid,
492 d_lustre_revalidate(*de);
493 ll_intent_release(&parent_it);
498 if (rc != 0 && it->it_op & IT_OPEN)
499 ll_open_cleanup((*de)->d_sb, request);
504 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
505 struct lookup_intent *it)
507 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
508 struct dentry *save = dentry, *retval;
509 struct ptlrpc_request *req = NULL;
511 struct md_op_data *op_data;
515 if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
516 return ERR_PTR(-ENAMETOOLONG);
518 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p),intent=%s\n",
519 dentry, PFID(ll_inode2fid(parent)), parent, LL_IT2STR(it));
521 if (d_mountpoint(dentry))
522 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
524 if (!it || it->it_op == IT_GETXATTR)
527 if (it->it_op == IT_GETATTR) {
528 rc = ll_statahead_enter(parent, &dentry, 0);
538 if (it->it_op & IT_CREAT)
539 opc = LUSTRE_OPC_CREATE;
541 opc = LUSTRE_OPC_ANY;
543 op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
544 dentry->d_name.len, 0, opc, NULL);
546 return (void *)op_data;
548 /* enforce umask if acl disabled or MDS doesn't support umask */
549 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
550 it->it_create_mode &= ~current_umask();
552 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
553 &ll_md_blocking_ast, 0);
554 ll_finish_md_op_data(op_data);
556 retval = ERR_PTR(rc);
560 rc = ll_lookup_it_finish(req, it, parent, &dentry);
562 ll_intent_release(it);
563 retval = ERR_PTR(rc);
567 inode = d_inode(dentry);
568 if ((it->it_op & IT_OPEN) && inode &&
569 !S_ISREG(inode->i_mode) &&
570 !S_ISDIR(inode->i_mode)) {
571 ll_release_openhandle(inode, it);
573 ll_lookup_finish_locks(it, inode);
581 ptlrpc_req_finished(req);
582 if (it->it_op == IT_GETATTR && (!retval || retval == dentry))
583 ll_statahead_mark(parent, dentry);
587 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
590 struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
593 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p),flags=%u\n",
594 dentry, PFID(ll_inode2fid(parent)), parent, flags);
596 /* Optimize away (CREATE && !OPEN). Let .create handle the race.
597 * but only if we have write permissions there, otherwise we need
598 * to proceed with lookup. LU-4185
600 if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN) &&
601 (inode_permission(parent, MAY_WRITE | MAY_EXEC) == 0))
604 if (flags & (LOOKUP_PARENT | LOOKUP_OPEN | LOOKUP_CREATE))
608 de = ll_lookup_it(parent, dentry, itp);
611 ll_intent_release(itp);
617 * For cached negative dentry and new dentry, handle lookup/create/open
620 static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
621 struct file *file, unsigned open_flags,
622 umode_t mode, int *opened)
624 struct lookup_intent *it;
628 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p),file %p,open_flags %x,mode %x opened %d\n",
629 dentry, PFID(ll_inode2fid(dir)), dir, file, open_flags, mode,
632 /* Only negative dentries enter here */
633 LASSERT(!d_inode(dentry));
635 if (!d_in_lookup(dentry)) {
636 /* A valid negative dentry that just passed revalidation,
637 * there's little point to try and open it server-side,
638 * even though there's a minuscle chance it might succeed.
639 * Either way it's a valid race to just return -ENOENT here.
641 if (!(open_flags & O_CREAT))
644 /* Otherwise we just unhash it to be rehashed afresh via
645 * lookup if necessary
650 it = kzalloc(sizeof(*it), GFP_NOFS);
655 if (open_flags & O_CREAT)
656 it->it_op |= IT_CREAT;
657 it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
658 it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
659 it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
661 /* Dentry added to dcache tree in ll_lookup_it */
662 de = ll_lookup_it(dir, dentry, it);
669 if (it_disposition(it, DISP_OPEN_CREATE)) {
670 /* Dentry instantiated in ll_create_it. */
671 rc = ll_create_it(dir, dentry, it);
673 /* We dget in ll_splice_alias. */
679 *opened |= FILE_CREATED;
681 if (d_really_is_positive(dentry) && it_disposition(it, DISP_OPEN_OPEN)) {
683 if (S_ISFIFO(d_inode(dentry)->i_mode)) {
684 /* We cannot call open here as it might
685 * deadlock. This case is unreachable in
686 * practice because of OBD_CONNECT_NODEVOH.
688 rc = finish_no_open(file, de);
690 file->private_data = it;
691 rc = finish_open(file, dentry, NULL, opened);
692 /* We dget in ll_splice_alias. finish_open takes
693 * care of dget for fd open.
699 rc = finish_no_open(file, de);
704 ll_intent_release(it);
710 /* We depend on "mode" being set with the proper file type/umask by now */
711 static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
713 struct inode *inode = NULL;
714 struct ptlrpc_request *request = NULL;
715 struct ll_sb_info *sbi = ll_i2sbi(dir);
718 LASSERT(it && it->it_disposition);
720 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
721 request = it->it_request;
722 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
723 rc = ll_prep_inode(&inode, request, dir->i_sb, it);
729 LASSERT(hlist_empty(&inode->i_dentry));
731 /* We asked for a lock on the directory, but were granted a
732 * lock on the inode. Since we finally have an inode pointer,
733 * stuff it in the lock.
735 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode "DFID"(%p)\n",
736 PFID(ll_inode2fid(dir)), inode);
737 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
739 ptlrpc_req_finished(request);
744 * By the time this is called, we already have created the directory cache
745 * entry for the new file, but it is so far negative - it has no inode.
747 * We defer creating the OBD object(s) until open, to keep the intent and
748 * non-intent code paths similar, and also because we do not have the MDS
749 * inode number before calling ll_create_node() (which is needed for LOV),
750 * so we would need to do yet another RPC to the MDS to store the LOV EA
751 * data on the MDS. If needed, we would pass the PACKED lmm as data and
752 * lmm_size in datalen (the MDS still has code which will handle that).
754 * If the create succeeds, we fill in the inode information
755 * with d_instantiate().
757 static int ll_create_it(struct inode *dir, struct dentry *dentry,
758 struct lookup_intent *it)
763 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p), intent=%s\n",
764 dentry, PFID(ll_inode2fid(dir)), dir, LL_IT2STR(it));
766 rc = it_open_error(DISP_OPEN_CREATE, it);
770 inode = ll_create_node(dir, it);
772 return PTR_ERR(inode);
774 d_instantiate(dentry, inode);
778 void ll_update_times(struct ptlrpc_request *request, struct inode *inode)
780 struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
784 if (body->mbo_valid & OBD_MD_FLMTIME &&
785 body->mbo_mtime > LTIME_S(inode->i_mtime)) {
786 CDEBUG(D_INODE, "setting fid "DFID" mtime from %lu to %llu\n",
787 PFID(ll_inode2fid(inode)), LTIME_S(inode->i_mtime),
789 LTIME_S(inode->i_mtime) = body->mbo_mtime;
791 if (body->mbo_valid & OBD_MD_FLCTIME &&
792 body->mbo_ctime > LTIME_S(inode->i_ctime))
793 LTIME_S(inode->i_ctime) = body->mbo_ctime;
796 static int ll_new_node(struct inode *dir, struct dentry *dentry,
797 const char *tgt, umode_t mode, int rdev,
800 struct ptlrpc_request *request = NULL;
801 struct md_op_data *op_data;
802 struct inode *inode = NULL;
803 struct ll_sb_info *sbi = ll_i2sbi(dir);
808 tgt_len = strlen(tgt) + 1;
810 op_data = ll_prep_md_op_data(NULL, dir, NULL,
814 if (IS_ERR(op_data)) {
815 err = PTR_ERR(op_data);
819 err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
820 from_kuid(&init_user_ns, current_fsuid()),
821 from_kgid(&init_user_ns, current_fsgid()),
822 cfs_curproc_cap_pack(), rdev, &request);
823 ll_finish_md_op_data(op_data);
827 ll_update_times(request, dir);
829 err = ll_prep_inode(&inode, request, dir->i_sb, NULL);
833 d_instantiate(dentry, inode);
835 ptlrpc_req_finished(request);
840 static int ll_mknod(struct inode *dir, struct dentry *dchild,
841 umode_t mode, dev_t rdev)
845 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p) mode %o dev %x\n",
846 dchild, PFID(ll_inode2fid(dir)), dir, mode,
847 old_encode_dev(rdev));
849 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
850 mode &= ~current_umask();
852 switch (mode & S_IFMT) {
854 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
860 err = ll_new_node(dir, dchild, NULL, mode,
861 old_encode_dev(rdev),
872 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
878 * Plain create. Intent create is handled in atomic_open.
880 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
881 umode_t mode, bool want_excl)
885 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p), flags=%u, excl=%d\n",
886 dentry, PFID(ll_inode2fid(dir)), dir, mode, want_excl);
888 rc = ll_mknod(dir, dentry, mode, 0);
890 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
892 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, unhashed %d\n",
893 dentry, d_unhashed(dentry));
898 int ll_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
900 struct mdt_body *body;
901 struct lov_mds_md *eadata;
902 struct lov_stripe_md *lsm = NULL;
903 struct obd_trans_info oti = { 0 };
907 /* req is swabbed so this is safe */
908 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
909 if (!(body->mbo_valid & OBD_MD_FLEASIZE))
912 if (body->mbo_eadatasize == 0) {
913 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
918 /* The MDS sent back the EA because we unlinked the last reference
919 * to this file. Use this EA to unlink the objects on the OST.
920 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
921 * check it is complete and sensible.
923 eadata = req_capsule_server_sized_get(&request->rq_pill, &RMF_MDT_MD,
924 body->mbo_eadatasize);
927 rc = obd_unpackmd(ll_i2dtexp(dir), &lsm, eadata, body->mbo_eadatasize);
929 CERROR("obd_unpackmd: %d\n", rc);
932 LASSERT(rc >= sizeof(*lsm));
934 oa = kmem_cache_zalloc(obdo_cachep, GFP_NOFS);
940 oa->o_oi = lsm->lsm_oi;
941 oa->o_mode = body->mbo_mode & S_IFMT;
942 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
944 if (body->mbo_valid & OBD_MD_FLCOOKIE) {
945 oa->o_valid |= OBD_MD_FLCOOKIE;
947 req_capsule_server_sized_get(&request->rq_pill,
949 sizeof(struct llog_cookie) *
950 lsm->lsm_stripe_count);
951 if (!oti.oti_logcookies) {
952 oa->o_valid &= ~OBD_MD_FLCOOKIE;
953 body->mbo_valid &= ~OBD_MD_FLCOOKIE;
957 rc = obd_destroy(NULL, ll_i2dtexp(dir), oa, lsm, &oti,
960 CERROR("obd destroy objid "DOSTID" error %d\n",
961 POSTID(&lsm->lsm_oi), rc);
963 obd_free_memmd(ll_i2dtexp(dir), &lsm);
964 kmem_cache_free(obdo_cachep, oa);
969 /* ll_unlink() doesn't update the inode with the new link count.
970 * Instead, ll_ddelete() and ll_d_iput() will update it based upon if there
971 * is any lock existing. They will recycle dentries and inodes based upon locks
974 static int ll_unlink(struct inode *dir, struct dentry *dchild)
976 struct ptlrpc_request *request = NULL;
977 struct md_op_data *op_data;
980 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p)\n",
981 dchild, dir->i_ino, dir->i_generation, dir);
983 op_data = ll_prep_md_op_data(NULL, dir, NULL,
986 0, LUSTRE_OPC_ANY, NULL);
988 return PTR_ERR(op_data);
990 if (dchild && dchild->d_inode)
991 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
993 op_data->op_fid2 = op_data->op_fid3;
994 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
995 ll_finish_md_op_data(op_data);
999 ll_update_times(request, dir);
1000 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
1002 rc = ll_objects_destroy(request, dir);
1004 ptlrpc_req_finished(request);
1008 static int ll_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1012 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir"DFID"(%p)\n",
1013 dentry, PFID(ll_inode2fid(dir)), dir);
1015 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1016 mode &= ~current_umask();
1017 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
1019 err = ll_new_node(dir, dentry, NULL, mode, 0, LUSTRE_OPC_MKDIR);
1021 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
1026 static int ll_rmdir(struct inode *dir, struct dentry *dchild)
1028 struct ptlrpc_request *request = NULL;
1029 struct md_op_data *op_data;
1032 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p)\n",
1033 dchild, PFID(ll_inode2fid(dir)), dir);
1035 op_data = ll_prep_md_op_data(NULL, dir, NULL,
1036 dchild->d_name.name,
1038 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1039 if (IS_ERR(op_data))
1040 return PTR_ERR(op_data);
1042 if (dchild && dchild->d_inode)
1043 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1045 op_data->op_fid2 = op_data->op_fid3;
1046 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1047 ll_finish_md_op_data(op_data);
1049 ll_update_times(request, dir);
1050 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1053 ptlrpc_req_finished(request);
1057 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1058 const char *oldname)
1062 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p),target=%.*s\n",
1063 dentry, PFID(ll_inode2fid(dir)), dir, 3000, oldname);
1065 err = ll_new_node(dir, dentry, oldname, S_IFLNK | S_IRWXUGO,
1066 0, LUSTRE_OPC_SYMLINK);
1069 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
1074 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1075 struct dentry *new_dentry)
1077 struct inode *src = d_inode(old_dentry);
1078 struct ll_sb_info *sbi = ll_i2sbi(dir);
1079 struct ptlrpc_request *request = NULL;
1080 struct md_op_data *op_data;
1083 CDEBUG(D_VFSTRACE, "VFS Op: inode="DFID"(%p), dir="DFID"(%p), target=%pd\n",
1084 PFID(ll_inode2fid(src)), src, PFID(ll_inode2fid(dir)), dir,
1087 op_data = ll_prep_md_op_data(NULL, src, dir, new_dentry->d_name.name,
1088 new_dentry->d_name.len,
1089 0, LUSTRE_OPC_ANY, NULL);
1090 if (IS_ERR(op_data))
1091 return PTR_ERR(op_data);
1093 err = md_link(sbi->ll_md_exp, op_data, &request);
1094 ll_finish_md_op_data(op_data);
1098 ll_update_times(request, dir);
1099 ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
1101 ptlrpc_req_finished(request);
1105 static int ll_rename(struct inode *src, struct dentry *src_dchild,
1106 struct inode *tgt, struct dentry *tgt_dchild)
1108 struct ptlrpc_request *request = NULL;
1109 struct ll_sb_info *sbi = ll_i2sbi(src);
1110 struct md_op_data *op_data;
1114 "VFS Op:oldname=%pd, src_dir="DFID"(%p), newname=%pd, tgt_dir="DFID"(%p)\n",
1115 src_dchild, PFID(ll_inode2fid(src)), src,
1116 tgt_dchild, PFID(ll_inode2fid(tgt)), tgt);
1118 op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0,
1119 LUSTRE_OPC_ANY, NULL);
1120 if (IS_ERR(op_data))
1121 return PTR_ERR(op_data);
1123 if (src_dchild && src_dchild->d_inode)
1124 op_data->op_fid3 = *ll_inode2fid(src_dchild->d_inode);
1125 if (tgt_dchild && tgt_dchild->d_inode)
1126 op_data->op_fid4 = *ll_inode2fid(tgt_dchild->d_inode);
1128 err = md_rename(sbi->ll_md_exp, op_data,
1129 src_dchild->d_name.name,
1130 src_dchild->d_name.len,
1131 tgt_dchild->d_name.name,
1132 tgt_dchild->d_name.len, &request);
1133 ll_finish_md_op_data(op_data);
1135 ll_update_times(request, src);
1136 ll_update_times(request, tgt);
1137 ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1138 err = ll_objects_destroy(request, src);
1141 ptlrpc_req_finished(request);
1143 d_move(src_dchild, tgt_dchild);
1147 const struct inode_operations ll_dir_inode_operations = {
1149 .atomic_open = ll_atomic_open,
1150 .lookup = ll_lookup_nd,
1151 .create = ll_create_nd,
1152 /* We need all these non-raw things for NFSD, to not patch it. */
1153 .unlink = ll_unlink,
1156 .symlink = ll_symlink,
1158 .rename = ll_rename,
1159 .setattr = ll_setattr,
1160 .getattr = ll_getattr,
1161 .permission = ll_inode_permission,
1162 .setxattr = generic_setxattr,
1163 .getxattr = generic_getxattr,
1164 .listxattr = ll_listxattr,
1165 .removexattr = generic_removexattr,
1166 .get_acl = ll_get_acl,
1169 const struct inode_operations ll_special_inode_operations = {
1170 .setattr = ll_setattr,
1171 .getattr = ll_getattr,
1172 .permission = ll_inode_permission,
1173 .setxattr = generic_setxattr,
1174 .getxattr = generic_getxattr,
1175 .listxattr = ll_listxattr,
1176 .removexattr = generic_removexattr,
1177 .get_acl = ll_get_acl,