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, 2012, 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>
45 #define DEBUG_SUBSYSTEM S_LLITE
47 #include "../include/obd_support.h"
48 #include "../include/lustre_fid.h"
49 #include "../include/lustre_lite.h"
50 #include "../include/lustre_dlm.h"
51 #include "../include/lustre_ver.h"
52 #include "llite_internal.h"
54 static int ll_create_it(struct inode *, struct dentry *,
55 int, struct lookup_intent *);
58 * Check if we have something mounted at the named dchild.
59 * In such a case there would always be dentry present.
61 static int ll_d_mountpoint(struct dentry *dparent, struct dentry *dchild,
66 if (unlikely(dchild)) {
67 mounted = d_mountpoint(dchild);
69 dchild = d_lookup(dparent, name);
71 mounted = d_mountpoint(dchild);
78 /* called from iget5_locked->find_inode() under inode_hash_lock spinlock */
79 static int ll_test_inode(struct inode *inode, void *opaque)
81 struct ll_inode_info *lli = ll_i2info(inode);
82 struct lustre_md *md = opaque;
84 if (unlikely(!(md->body->valid & OBD_MD_FLID))) {
85 CERROR("MDS body missing FID\n");
89 if (!lu_fid_eq(&lli->lli_fid, &md->body->fid1))
95 static int ll_set_inode(struct inode *inode, void *opaque)
97 struct ll_inode_info *lli = ll_i2info(inode);
98 struct mdt_body *body = ((struct lustre_md *)opaque)->body;
100 if (unlikely(!(body->valid & OBD_MD_FLID))) {
101 CERROR("MDS body missing FID\n");
105 lli->lli_fid = body->fid1;
106 if (unlikely(!(body->valid & OBD_MD_FLTYPE))) {
107 CERROR("Can not initialize inode "DFID" without object type: "
108 "valid = %#llx\n", PFID(&lli->lli_fid), body->valid);
112 inode->i_mode = (inode->i_mode & ~S_IFMT) | (body->mode & S_IFMT);
113 if (unlikely(inode->i_mode == 0)) {
114 CERROR("Invalid inode "DFID" type\n", PFID(&lli->lli_fid));
125 * Get an inode by inode number (already instantiated by the intent lookup).
126 * Returns inode or NULL
128 struct inode *ll_iget(struct super_block *sb, ino_t hash,
129 struct lustre_md *md)
134 inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
137 if (inode->i_state & I_NEW) {
140 ll_read_inode2(inode, md);
141 if (S_ISREG(inode->i_mode) &&
142 ll_i2info(inode)->lli_clob == NULL) {
144 "%s: apply lsm %p to inode "DFID".\n",
145 ll_get_fsname(sb, NULL, 0), md->lsm,
146 PFID(ll_inode2fid(inode)));
147 rc = cl_file_inode_init(inode, md);
150 make_bad_inode(inode);
151 unlock_new_inode(inode);
155 unlock_new_inode(inode);
156 } else if (!(inode->i_state & (I_FREEING | I_CLEAR)))
157 ll_update_inode(inode, md);
158 CDEBUG(D_VFSTRACE, "got inode: %p for "DFID"\n",
159 inode, PFID(&md->body->fid1));
164 static void ll_invalidate_negative_children(struct inode *dir)
166 struct dentry *dentry, *tmp_subdir;
167 struct ll_d_hlist_node *p;
170 ll_d_hlist_for_each_entry(dentry, p, &dir->i_dentry, d_alias) {
171 spin_lock(&dentry->d_lock);
172 if (!list_empty(&dentry->d_subdirs)) {
173 struct dentry *child;
175 list_for_each_entry_safe(child, tmp_subdir,
178 if (child->d_inode == NULL)
179 d_lustre_invalidate(child, 1);
182 spin_unlock(&dentry->d_lock);
184 ll_unlock_dcache(dir);
187 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
188 void *data, int flag)
190 struct lustre_handle lockh;
194 case LDLM_CB_BLOCKING:
195 ldlm_lock2handle(lock, &lockh);
196 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
198 CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
202 case LDLM_CB_CANCELING: {
203 struct inode *inode = ll_inode_from_resource_lock(lock);
204 __u64 bits = lock->l_policy_data.l_inodebits.bits;
206 /* Inode is set to lock->l_resource->lr_lvb_inode
207 * for mdc - bug 24555 */
208 LASSERT(lock->l_ast_data == NULL);
213 /* Invalidate all dentries associated with this inode */
214 LASSERT(lock->l_flags & LDLM_FL_CANCELING);
216 if (!fid_res_name_eq(ll_inode2fid(inode),
217 &lock->l_resource->lr_name)) {
218 LDLM_ERROR(lock, "data mismatch with object "DFID"(%p)",
219 PFID(ll_inode2fid(inode)), inode);
223 if (bits & MDS_INODELOCK_XATTR) {
224 ll_xattr_cache_destroy(inode);
225 bits &= ~MDS_INODELOCK_XATTR;
228 /* For OPEN locks we differentiate between lock modes
229 * LCK_CR, LCK_CW, LCK_PR - bug 22891 */
230 if (bits & MDS_INODELOCK_OPEN)
231 ll_have_md_lock(inode, &bits, lock->l_req_mode);
233 if (bits & MDS_INODELOCK_OPEN) {
236 switch (lock->l_req_mode) {
247 LDLM_ERROR(lock, "bad lock mode for OPEN lock");
251 ll_md_real_close(inode, fmode);
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) {
272 struct ll_inode_info *lli = ll_i2info(inode);
274 spin_lock(&lli->lli_lock);
275 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
276 spin_unlock(&lli->lli_lock);
279 if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
280 CDEBUG(D_INODE, "invalidating inode %lu\n",
282 truncate_inode_pages(inode->i_mapping, 0);
283 ll_invalidate_negative_children(inode);
286 if ((bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM)) &&
287 inode->i_sb->s_root != NULL &&
288 inode != inode->i_sb->s_root->d_inode)
289 ll_invalidate_aliases(inode);
301 __u32 ll_i2suppgid(struct inode *i)
303 if (in_group_p(i->i_gid))
304 return (__u32)from_kgid(&init_user_ns, i->i_gid);
309 /* Pack the required supplementary groups into the supplied groups array.
310 * If we don't need to use the groups from the target inode(s) then we
311 * instead pack one or more groups from the user's supplementary group
312 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
313 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
320 LASSERT(suppgids != NULL);
322 suppgids[0] = ll_i2suppgid(i1);
325 suppgids[1] = ll_i2suppgid(i2);
330 for (i = 0; i < current_ngroups; i++) {
331 if (suppgids[0] == -1) {
332 if (current_groups[i] != suppgids[1])
333 suppgids[0] = current_groups[i];
336 if (suppgids[1] == -1) {
337 if (current_groups[i] != suppgids[0])
338 suppgids[1] = current_groups[i];
347 * try to reuse three types of dentry:
348 * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
349 * by concurrent .revalidate).
350 * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
351 * be cleared by others calling d_lustre_revalidate).
352 * 3. DISCONNECTED alias.
354 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
356 struct dentry *alias, *discon_alias, *invalid_alias;
357 struct ll_d_hlist_node *p;
359 if (ll_d_hlist_empty(&inode->i_dentry))
362 discon_alias = invalid_alias = NULL;
364 ll_lock_dcache(inode);
365 ll_d_hlist_for_each_entry(alias, p, &inode->i_dentry, d_alias) {
366 LASSERT(alias != dentry);
368 spin_lock(&alias->d_lock);
369 if (alias->d_flags & DCACHE_DISCONNECTED)
370 /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
371 discon_alias = alias;
372 else if (alias->d_parent == dentry->d_parent &&
373 alias->d_name.hash == dentry->d_name.hash &&
374 alias->d_name.len == dentry->d_name.len &&
375 memcmp(alias->d_name.name, dentry->d_name.name,
376 dentry->d_name.len) == 0)
377 invalid_alias = alias;
378 spin_unlock(&alias->d_lock);
383 alias = invalid_alias ?: discon_alias ?: NULL;
385 spin_lock(&alias->d_lock);
387 spin_unlock(&alias->d_lock);
389 ll_unlock_dcache(inode);
395 * Similar to d_splice_alias(), but lustre treats invalid alias
396 * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
398 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
404 new = ll_find_alias(inode, de);
414 "Reuse dentry %p inode %p refc %d flags %#x\n",
415 new, new->d_inode, d_count(new), new->d_flags);
423 CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
424 de, de->d_inode, d_count(de), de->d_flags);
428 static int ll_lookup_it_finish(struct ptlrpc_request *request,
429 struct lookup_intent *it,
430 struct inode *parent, struct dentry **de)
432 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 passing hashed dentries for open.
460 if (d_unhashed(*de)) {
461 struct dentry *alias;
463 alias = ll_splice_alias(inode, *de);
465 return PTR_ERR(alias);
467 } else if (!it_disposition(it, DISP_LOOKUP_NEG) &&
468 !it_disposition(it, DISP_OPEN_CREATE)) {
469 /* With DISP_OPEN_CREATE dentry will
470 instantiated in ll_create_it. */
471 LASSERT((*de)->d_inode == NULL);
472 d_instantiate(*de, inode);
475 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
476 /* we have lookup look - unhide dentry */
477 if (bits & MDS_INODELOCK_LOOKUP)
478 d_lustre_revalidate(*de);
479 } else if (!it_disposition(it, DISP_OPEN_CREATE)) {
480 /* If file created on server, don't depend on parent UPDATE
481 * lock to unhide it. It is left hidden and next lookup can
482 * find it in ll_splice_alias.
484 /* Check that parent has UPDATE lock. */
485 struct lookup_intent parent_it = {
487 .d.lustre.it_lock_handle = 0 };
489 if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it,
490 &ll_i2info(parent)->lli_fid, NULL)) {
491 d_lustre_revalidate(*de);
492 ll_intent_release(&parent_it);
499 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
500 struct lookup_intent *it, int lookup_flags)
502 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
503 struct dentry *save = dentry, *retval;
504 struct ptlrpc_request *req = NULL;
505 struct md_op_data *op_data;
509 if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
510 return ERR_PTR(-ENAMETOOLONG);
512 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
513 dentry->d_name.len, dentry->d_name.name, parent->i_ino,
514 parent->i_generation, parent, LL_IT2STR(it));
516 if (d_mountpoint(dentry))
517 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
519 if (it == NULL || it->it_op == IT_GETXATTR)
522 if (it->it_op == IT_GETATTR) {
523 rc = ll_statahead_enter(parent, &dentry, 0);
533 if (it->it_op & IT_CREAT)
534 opc = LUSTRE_OPC_CREATE;
536 opc = LUSTRE_OPC_ANY;
538 op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
539 dentry->d_name.len, lookup_flags, opc,
542 return (void *)op_data;
544 /* enforce umask if acl disabled or MDS doesn't support umask */
545 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
546 it->it_create_mode &= ~current_umask();
548 rc = md_intent_lock(ll_i2mdexp(parent), op_data, NULL, 0, it,
549 lookup_flags, &req, ll_md_blocking_ast, 0);
550 ll_finish_md_op_data(op_data);
552 retval = ERR_PTR(rc);
556 rc = ll_lookup_it_finish(req, it, parent, &dentry);
558 ll_intent_release(it);
559 retval = ERR_PTR(rc);
563 if ((it->it_op & IT_OPEN) && dentry->d_inode &&
564 !S_ISREG(dentry->d_inode->i_mode) &&
565 !S_ISDIR(dentry->d_inode->i_mode)) {
566 ll_release_openhandle(dentry, it);
568 ll_lookup_finish_locks(it, dentry);
577 ptlrpc_req_finished(req);
578 if (it->it_op == IT_GETATTR && (retval == NULL || retval == dentry))
579 ll_statahead_mark(parent, dentry);
583 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
586 struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
589 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),flags=%u\n",
590 dentry->d_name.len, dentry->d_name.name, parent->i_ino,
591 parent->i_generation, parent, flags);
593 /* Optimize away (CREATE && !OPEN). Let .create handle the race. */
594 if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN))
597 if (flags & (LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE))
601 de = ll_lookup_it(parent, dentry, itp, 0);
604 ll_intent_release(itp);
610 * For cached negative dentry and new dentry, handle lookup/create/open
613 static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
614 struct file *file, unsigned open_flags,
615 umode_t mode, int *opened)
617 struct lookup_intent *it;
619 long long lookup_flags = LOOKUP_OPEN;
622 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),file %p,"
623 "open_flags %x,mode %x opened %d\n",
624 dentry->d_name.len, dentry->d_name.name, dir->i_ino,
625 dir->i_generation, dir, file, open_flags, mode, *opened);
627 it = kzalloc(sizeof(*it), GFP_NOFS);
632 if (open_flags & O_CREAT) {
633 it->it_op |= IT_CREAT;
634 lookup_flags |= LOOKUP_CREATE;
636 it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
637 it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
639 /* Dentry added to dcache tree in ll_lookup_it */
640 de = ll_lookup_it(dir, dentry, it, lookup_flags);
647 if (it_disposition(it, DISP_OPEN_CREATE)) {
648 /* Dentry instantiated in ll_create_it. */
649 rc = ll_create_it(dir, dentry, mode, it);
651 /* We dget in ll_splice_alias. */
657 *opened |= FILE_CREATED;
659 if (dentry->d_inode && it_disposition(it, DISP_OPEN_OPEN)) {
661 if (S_ISFIFO(dentry->d_inode->i_mode)) {
662 /* We cannot call open here as it would
665 if (it_disposition(it, DISP_ENQ_OPEN_REF))
667 (struct ptlrpc_request *)
668 it->d.lustre.it_data);
669 rc = finish_no_open(file, de);
671 file->private_data = it;
672 rc = finish_open(file, dentry, NULL, opened);
673 /* We dget in ll_splice_alias. finish_open takes
674 * care of dget for fd open.
680 rc = finish_no_open(file, de);
685 ll_intent_release(it);
686 OBD_FREE(it, sizeof(*it));
692 /* We depend on "mode" being set with the proper file type/umask by now */
693 static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
695 struct inode *inode = NULL;
696 struct ptlrpc_request *request = NULL;
697 struct ll_sb_info *sbi = ll_i2sbi(dir);
700 LASSERT(it && it->d.lustre.it_disposition);
702 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
703 request = it->d.lustre.it_data;
704 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
705 rc = ll_prep_inode(&inode, request, dir->i_sb, it);
711 LASSERT(ll_d_hlist_empty(&inode->i_dentry));
713 /* We asked for a lock on the directory, but were granted a
714 * lock on the inode. Since we finally have an inode pointer,
715 * stuff it in the lock. */
716 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode %p (%lu/%u)\n",
717 inode, inode->i_ino, inode->i_generation);
718 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
720 ptlrpc_req_finished(request);
725 * By the time this is called, we already have created the directory cache
726 * entry for the new file, but it is so far negative - it has no inode.
728 * We defer creating the OBD object(s) until open, to keep the intent and
729 * non-intent code paths similar, and also because we do not have the MDS
730 * inode number before calling ll_create_node() (which is needed for LOV),
731 * so we would need to do yet another RPC to the MDS to store the LOV EA
732 * data on the MDS. If needed, we would pass the PACKED lmm as data and
733 * lmm_size in datalen (the MDS still has code which will handle that).
735 * If the create succeeds, we fill in the inode information
736 * with d_instantiate().
738 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
739 struct lookup_intent *it)
744 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
745 dentry->d_name.len, dentry->d_name.name, dir->i_ino,
746 dir->i_generation, dir, LL_IT2STR(it));
748 rc = it_open_error(DISP_OPEN_CREATE, it);
752 inode = ll_create_node(dir, it);
754 return PTR_ERR(inode);
756 d_instantiate(dentry, inode);
760 static void ll_update_times(struct ptlrpc_request *request,
763 struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
767 if (body->valid & OBD_MD_FLMTIME &&
768 body->mtime > LTIME_S(inode->i_mtime)) {
769 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %llu\n",
770 inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
771 LTIME_S(inode->i_mtime) = body->mtime;
773 if (body->valid & OBD_MD_FLCTIME &&
774 body->ctime > LTIME_S(inode->i_ctime))
775 LTIME_S(inode->i_ctime) = body->ctime;
778 static int ll_new_node(struct inode *dir, struct qstr *name,
779 const char *tgt, int mode, int rdev,
780 struct dentry *dchild, __u32 opc)
782 struct ptlrpc_request *request = NULL;
783 struct md_op_data *op_data;
784 struct inode *inode = NULL;
785 struct ll_sb_info *sbi = ll_i2sbi(dir);
789 if (unlikely(tgt != NULL))
790 tgt_len = strlen(tgt) + 1;
792 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
793 name->len, 0, opc, NULL);
794 if (IS_ERR(op_data)) {
795 err = PTR_ERR(op_data);
799 err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
800 from_kuid(&init_user_ns, current_fsuid()),
801 from_kgid(&init_user_ns, current_fsgid()),
802 cfs_curproc_cap_pack(), rdev, &request);
803 ll_finish_md_op_data(op_data);
807 ll_update_times(request, dir);
810 err = ll_prep_inode(&inode, request, dchild->d_sb, NULL);
814 d_instantiate(dchild, inode);
817 ptlrpc_req_finished(request);
822 static int ll_mknod_generic(struct inode *dir, struct qstr *name, int mode,
823 unsigned rdev, struct dentry *dchild)
827 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p) mode %o dev %x\n",
828 name->len, name->name, dir->i_ino, dir->i_generation, dir,
831 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
832 mode &= ~current_umask();
834 switch (mode & S_IFMT) {
836 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
842 err = ll_new_node(dir, name, NULL, mode, rdev, dchild,
853 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
859 * Plain create. Intent create is handled in atomic_open.
861 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
862 umode_t mode, bool want_excl)
866 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),"
867 "flags=%u, excl=%d\n",
868 dentry->d_name.len, dentry->d_name.name, dir->i_ino,
869 dir->i_generation, dir, mode, want_excl);
871 rc = ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
873 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
875 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, unhashed %d\n",
876 dentry->d_name.len, dentry->d_name.name, d_unhashed(dentry));
881 static int ll_symlink_generic(struct inode *dir, struct qstr *name,
882 const char *tgt, struct dentry *dchild)
886 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),target=%.*s\n",
887 name->len, name->name, dir->i_ino, dir->i_generation,
890 err = ll_new_node(dir, name, (char *)tgt, S_IFLNK | S_IRWXUGO,
891 0, dchild, LUSTRE_OPC_SYMLINK);
894 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
899 static int ll_link_generic(struct inode *src, struct inode *dir,
900 struct qstr *name, struct dentry *dchild)
902 struct ll_sb_info *sbi = ll_i2sbi(dir);
903 struct ptlrpc_request *request = NULL;
904 struct md_op_data *op_data;
908 "VFS Op: inode=%lu/%u(%p), dir=%lu/%u(%p), target=%.*s\n",
909 src->i_ino, src->i_generation, src, dir->i_ino,
910 dir->i_generation, dir, name->len, name->name);
912 op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len,
913 0, LUSTRE_OPC_ANY, NULL);
915 return PTR_ERR(op_data);
917 err = md_link(sbi->ll_md_exp, op_data, &request);
918 ll_finish_md_op_data(op_data);
922 ll_update_times(request, dir);
923 ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
925 ptlrpc_req_finished(request);
929 static int ll_mkdir_generic(struct inode *dir, struct qstr *name,
930 int mode, struct dentry *dchild)
935 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
936 name->len, name->name, dir->i_ino, dir->i_generation, dir);
938 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
939 mode &= ~current_umask();
940 mode = (mode & (S_IRWXUGO|S_ISVTX)) | S_IFDIR;
941 err = ll_new_node(dir, name, NULL, mode, 0, dchild, LUSTRE_OPC_MKDIR);
944 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
949 /* Try to find the child dentry by its name.
950 If found, put the result fid into @fid. */
951 static void ll_get_child_fid(struct inode * dir, struct qstr *name,
954 struct dentry *parent, *child;
956 parent = ll_d_hlist_entry(dir->i_dentry, struct dentry, d_alias);
957 child = d_lookup(parent, name);
960 *fid = *ll_inode2fid(child->d_inode);
965 static int ll_rmdir_generic(struct inode *dir, struct dentry *dparent,
966 struct dentry *dchild, struct qstr *name)
968 struct ptlrpc_request *request = NULL;
969 struct md_op_data *op_data;
972 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
973 name->len, name->name, dir->i_ino, dir->i_generation, dir);
975 if (unlikely(ll_d_mountpoint(dparent, dchild, name)))
978 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
979 S_IFDIR, LUSTRE_OPC_ANY, NULL);
981 return PTR_ERR(op_data);
983 ll_get_child_fid(dir, name, &op_data->op_fid3);
984 op_data->op_fid2 = op_data->op_fid3;
985 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
986 ll_finish_md_op_data(op_data);
988 ll_update_times(request, dir);
989 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
992 ptlrpc_req_finished(request);
999 int ll_rmdir_entry(struct inode *dir, char *name, int namelen)
1001 struct ptlrpc_request *request = NULL;
1002 struct md_op_data *op_data;
1005 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1006 namelen, name, dir->i_ino, dir->i_generation, dir);
1008 op_data = ll_prep_md_op_data(NULL, dir, NULL, name, strlen(name),
1009 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1010 if (IS_ERR(op_data))
1011 return PTR_ERR(op_data);
1012 op_data->op_cli_flags |= CLI_RM_ENTRY;
1013 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1014 ll_finish_md_op_data(op_data);
1016 ll_update_times(request, dir);
1017 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1020 ptlrpc_req_finished(request);
1024 int ll_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
1026 struct mdt_body *body;
1027 struct lov_mds_md *eadata;
1028 struct lov_stripe_md *lsm = NULL;
1029 struct obd_trans_info oti = { 0 };
1031 struct obd_capa *oc = NULL;
1034 /* req is swabbed so this is safe */
1035 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
1036 if (!(body->valid & OBD_MD_FLEASIZE))
1039 if (body->eadatasize == 0) {
1040 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1045 /* The MDS sent back the EA because we unlinked the last reference
1046 * to this file. Use this EA to unlink the objects on the OST.
1047 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
1048 * check it is complete and sensible. */
1049 eadata = req_capsule_server_sized_get(&request->rq_pill, &RMF_MDT_MD,
1051 LASSERT(eadata != NULL);
1053 rc = obd_unpackmd(ll_i2dtexp(dir), &lsm, eadata, body->eadatasize);
1055 CERROR("obd_unpackmd: %d\n", rc);
1058 LASSERT(rc >= sizeof(*lsm));
1063 goto out_free_memmd;
1066 oa->o_oi = lsm->lsm_oi;
1067 oa->o_mode = body->mode & S_IFMT;
1068 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
1070 if (body->valid & OBD_MD_FLCOOKIE) {
1071 oa->o_valid |= OBD_MD_FLCOOKIE;
1072 oti.oti_logcookies =
1073 req_capsule_server_sized_get(&request->rq_pill,
1075 sizeof(struct llog_cookie) *
1076 lsm->lsm_stripe_count);
1077 if (oti.oti_logcookies == NULL) {
1078 oa->o_valid &= ~OBD_MD_FLCOOKIE;
1079 body->valid &= ~OBD_MD_FLCOOKIE;
1083 if (body->valid & OBD_MD_FLOSSCAPA) {
1084 rc = md_unpack_capa(ll_i2mdexp(dir), request, &RMF_CAPA2, &oc);
1086 goto out_free_memmd;
1089 rc = obd_destroy(NULL, ll_i2dtexp(dir), oa, lsm, &oti,
1090 ll_i2mdexp(dir), oc);
1093 CERROR("obd destroy objid "DOSTID" error %d\n",
1094 POSTID(&lsm->lsm_oi), rc);
1096 obd_free_memmd(ll_i2dtexp(dir), &lsm);
1102 /* ll_unlink_generic() doesn't update the inode with the new link count.
1103 * Instead, ll_ddelete() and ll_d_iput() will update it based upon if there
1104 * is any lock existing. They will recycle dentries and inodes based upon locks
1106 static int ll_unlink_generic(struct inode *dir, struct dentry *dparent,
1107 struct dentry *dchild, struct qstr *name)
1109 struct ptlrpc_request *request = NULL;
1110 struct md_op_data *op_data;
1112 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1113 name->len, name->name, dir->i_ino, dir->i_generation, dir);
1116 * XXX: unlink bind mountpoint maybe call to here,
1117 * just check it as vfs_unlink does.
1119 if (unlikely(ll_d_mountpoint(dparent, dchild, name)))
1122 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
1123 name->len, 0, LUSTRE_OPC_ANY, NULL);
1124 if (IS_ERR(op_data))
1125 return PTR_ERR(op_data);
1127 ll_get_child_fid(dir, name, &op_data->op_fid3);
1128 op_data->op_fid2 = op_data->op_fid3;
1129 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1130 ll_finish_md_op_data(op_data);
1134 ll_update_times(request, dir);
1135 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
1137 rc = ll_objects_destroy(request, dir);
1139 ptlrpc_req_finished(request);
1143 static int ll_rename_generic(struct inode *src, struct dentry *src_dparent,
1144 struct dentry *src_dchild, struct qstr *src_name,
1145 struct inode *tgt, struct dentry *tgt_dparent,
1146 struct dentry *tgt_dchild, struct qstr *tgt_name)
1148 struct ptlrpc_request *request = NULL;
1149 struct ll_sb_info *sbi = ll_i2sbi(src);
1150 struct md_op_data *op_data;
1154 "VFS Op:oldname=%.*s,src_dir=%lu/%u(%p),newname=%.*s,"
1155 "tgt_dir=%lu/%u(%p)\n", src_name->len, src_name->name,
1156 src->i_ino, src->i_generation, src, tgt_name->len,
1157 tgt_name->name, tgt->i_ino, tgt->i_generation, tgt);
1159 if (unlikely(ll_d_mountpoint(src_dparent, src_dchild, src_name) ||
1160 ll_d_mountpoint(tgt_dparent, tgt_dchild, tgt_name)))
1163 op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0,
1164 LUSTRE_OPC_ANY, NULL);
1165 if (IS_ERR(op_data))
1166 return PTR_ERR(op_data);
1168 ll_get_child_fid(src, src_name, &op_data->op_fid3);
1169 ll_get_child_fid(tgt, tgt_name, &op_data->op_fid4);
1170 err = md_rename(sbi->ll_md_exp, op_data,
1171 src_name->name, src_name->len,
1172 tgt_name->name, tgt_name->len, &request);
1173 ll_finish_md_op_data(op_data);
1175 ll_update_times(request, src);
1176 ll_update_times(request, tgt);
1177 ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1178 err = ll_objects_destroy(request, src);
1181 ptlrpc_req_finished(request);
1186 static int ll_mknod(struct inode *dir, struct dentry *dchild, ll_umode_t mode,
1189 return ll_mknod_generic(dir, &dchild->d_name, mode,
1190 old_encode_dev(rdev), dchild);
1193 static int ll_unlink(struct inode * dir, struct dentry *dentry)
1195 return ll_unlink_generic(dir, NULL, dentry, &dentry->d_name);
1198 static int ll_mkdir(struct inode *dir, struct dentry *dentry, ll_umode_t mode)
1200 return ll_mkdir_generic(dir, &dentry->d_name, mode, dentry);
1203 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
1205 return ll_rmdir_generic(dir, NULL, dentry, &dentry->d_name);
1208 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1209 const char *oldname)
1211 return ll_symlink_generic(dir, &dentry->d_name, oldname, dentry);
1214 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1215 struct dentry *new_dentry)
1217 return ll_link_generic(old_dentry->d_inode, dir, &new_dentry->d_name,
1221 static int ll_rename(struct inode *old_dir, struct dentry *old_dentry,
1222 struct inode *new_dir, struct dentry *new_dentry)
1225 err = ll_rename_generic(old_dir, NULL,
1226 old_dentry, &old_dentry->d_name,
1227 new_dir, NULL, new_dentry,
1228 &new_dentry->d_name);
1230 d_move(old_dentry, new_dentry);
1235 const struct inode_operations ll_dir_inode_operations = {
1237 .atomic_open = ll_atomic_open,
1238 .lookup = ll_lookup_nd,
1239 .create = ll_create_nd,
1240 /* We need all these non-raw things for NFSD, to not patch it. */
1241 .unlink = ll_unlink,
1244 .symlink = ll_symlink,
1246 .rename = ll_rename,
1247 .setattr = ll_setattr,
1248 .getattr = ll_getattr,
1249 .permission = ll_inode_permission,
1250 .setxattr = ll_setxattr,
1251 .getxattr = ll_getxattr,
1252 .listxattr = ll_listxattr,
1253 .removexattr = ll_removexattr,
1254 .get_acl = ll_get_acl,
1257 const struct inode_operations ll_special_inode_operations = {
1258 .setattr = ll_setattr,
1259 .getattr = ll_getattr,
1260 .permission = ll_inode_permission,
1261 .setxattr = ll_setxattr,
1262 .getxattr = ll_getxattr,
1263 .listxattr = ll_listxattr,
1264 .removexattr = ll_removexattr,
1265 .get_acl = ll_get_acl,