1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/sched.h>
25 #include <linux/smp_lock.h>
26 #include <linux/quotaops.h>
27 #include <linux/highmem.h>
28 #include <linux/pagemap.h>
30 #define DEBUG_SUBSYSTEM S_LLITE
32 #include <obd_support.h>
33 #include <lustre_fid.h>
34 #include <lustre_lite.h>
35 #include <lustre_dlm.h>
36 #include <lustre_ver.h>
37 #include <lustre_mdc.h>
38 #include "llite_internal.h"
42 extern struct dentry_operations ll_d_ops;
44 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
48 ldlm_lock_decref(lockh, mode);
53 /* Get an inode by inode number (already instantiated by the intent lookup).
54 * Returns inode or NULL
56 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
58 /* that variant provides correct mapping fid to inode,
59 * it is put here as alternative way to the current one ll_iget() below */
60 int ll_test_inode(struct inode *inode, void *opaque)
62 return lu_fid_eq(&ll_i2info(inode)->lli_fid, (struct lu_fid *)opaque);
65 int ll_set_inode(struct inode *inode, void *opaque)
67 struct lu_fid *fid = opaque;
68 ll_i2info(inode)->lli_fid = *fid;
69 inode->i_ino = ll_fid_build_ino(ll_s2sbi(inode->i_sb), fid);
73 struct inode *ll_iget(struct super_block *sb, ino_t hash,
76 struct ll_inode_info *lli;
80 inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode,
83 if (inode->i_state & I_NEW) {
84 lli = ll_i2info(inode);
85 ll_read_inode2(inode, md);
86 unlock_new_inode(inode);
88 ll_update_inode(inode, md);
90 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n",
91 inode->i_ino, inode->i_generation, inode);
97 struct inode *ll_iget(struct super_block *sb, ino_t hash,
100 struct ll_inode_info *lli;
104 inode = iget_locked(sb, hash);
106 if (inode->i_state & I_NEW) {
107 lli = ll_i2info(inode);
108 ll_read_inode2(inode, md);
109 unlock_new_inode(inode);
111 ll_update_inode(inode, md);
113 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n",
114 inode->i_ino, inode->i_generation, inode);
121 struct inode *ll_iget(struct super_block *sb, ino_t hash,
122 struct lustre_md *md)
127 inode = iget4(sb, hash, NULL, md);
129 if (!(inode->i_state & (I_FREEING | I_CLEAR)))
130 ll_update_inode(inode, md);
132 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n",
133 inode->i_ino, inode->i_generation, inode);
139 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
140 void *data, int flag)
143 struct lustre_handle lockh;
147 case LDLM_CB_BLOCKING:
148 ldlm_lock2handle(lock, &lockh);
149 rc = ldlm_cli_cancel(&lockh);
151 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
155 case LDLM_CB_CANCELING: {
156 struct inode *inode = ll_inode_from_lock(lock);
157 __u64 bits = lock->l_policy_data.l_inodebits.bits;
160 /* Invalidate all dentries associated with this inode */
164 fid = ll_inode2fid(inode);
165 if (lock->l_resource->lr_name.name[0] != fid_seq(fid) ||
166 lock->l_resource->lr_name.name[1] != fid_oid(fid) ||
167 lock->l_resource->lr_name.name[2] != fid_ver(fid)) {
168 LDLM_ERROR(lock, "data mismatch with object "
169 DFID" (%p)", PFID(fid), inode);
172 if (bits & MDS_INODELOCK_OPEN) {
174 switch (lock->l_req_mode) {
181 CERROR("open PR lock without FMODE_EXEC\n");
187 CERROR("Unexpected lock mode for OPEN lock "
188 "%d, inode %ld\n", lock->l_req_mode,
191 ll_md_real_close(inode, flags);
194 if (bits & MDS_INODELOCK_UPDATE)
195 ll_i2info(inode)->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
197 if (S_ISDIR(inode->i_mode) &&
198 (bits & MDS_INODELOCK_UPDATE)) {
199 struct dentry *dentry, *tmp, *dir;
200 struct list_head *list;
202 CDEBUG(D_INODE, "invalidating inode %lu\n",
204 truncate_inode_pages(inode->i_mapping, 0);
207 /* Drop possible cached negative dentries */
208 list = &inode->i_dentry;
210 spin_lock(&dcache_lock);
212 /* It is possible to have several dentries (with
214 while ((list = list->next) != &inode->i_dentry) {
215 dir = list_entry(list, struct dentry, d_alias);
216 #ifdef LUSTRE_KERNEL_VERSION
217 if (!(dir->d_flags & DCACHE_LUSTRE_INVALID))
219 if (!d_unhashed(dir))
228 list_for_each_entry_safe(dentry, tmp,
232 /* XXX Print some debug here? */
233 if (!dentry->d_inode)
234 /* Negative dentry. If we were
235 dropping dcache lock, go
236 throught the list again */
237 if (ll_drop_dentry(dentry))
241 spin_unlock(&dcache_lock);
244 if (inode->i_sb->s_root &&
245 inode != inode->i_sb->s_root->d_inode &&
246 (bits & MDS_INODELOCK_LOOKUP))
247 ll_unhash_aliases(inode);
258 /* Pack the required supplementary groups into the supplied groups array.
259 * If we don't need to use the groups from the target inode(s) then we
260 * instead pack one or more groups from the user's supplementary group
261 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
262 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
267 LASSERT(suppgids != NULL);
269 if (in_group_p(i1->i_gid))
270 suppgids[0] = i1->i_gid;
275 if (in_group_p(i2->i_gid))
276 suppgids[1] = i2->i_gid;
283 for (i = 0; i < current_ngroups; i++) {
284 if (suppgids[0] == -1) {
285 if (current_groups[i] != suppgids[1])
286 suppgids[0] = current_groups[i];
289 if (suppgids[1] == -1) {
290 if (current_groups[i] != suppgids[0])
291 suppgids[1] = current_groups[i];
298 static void ll_d_add(struct dentry *de, struct inode *inode)
300 CDEBUG(D_DENTRY, "adding inode %p to dentry %p\n", inode, de);
302 if (!list_empty(&de->d_alias)) {
303 spin_unlock(&dcache_lock);
304 CERROR("dentry %.*s %p alias next %p, prev %p\n",
305 de->d_name.len, de->d_name.name, de,
306 de->d_alias.next, de->d_alias.prev);
310 list_add(&de->d_alias, &inode->i_dentry);
314 if (!d_unhashed(de)) {
315 spin_unlock(&dcache_lock);
316 CERROR("dentry %.*s %p hash next %p\n",
317 de->d_name.len, de->d_name.name, de, de->d_hash.next);
323 /* 2.6.15 and prior versions have buggy d_instantiate_unique that leaks an inode
324 * if suitable alias is found. But we are not going to fix it by just freeing
325 * such inode, because if some vendor's kernel contains this bugfix already,
326 * we will break everything then. We will use our own reimplementation
328 #if !defined(HAVE_D_ADD_UNIQUE) || (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16))
329 /* Search "inode"'s alias list for a dentry that has the same name and parent as
330 * de. If found, return it. If not found, return de. */
331 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
333 struct list_head *tmp;
335 spin_lock(&dcache_lock);
336 list_for_each(tmp, &inode->i_dentry) {
337 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
339 /* We are called here with 'de' already on the aliases list. */
345 if (dentry->d_parent != de->d_parent)
348 if (dentry->d_name.len != de->d_name.len)
351 if (memcmp(dentry->d_name.name, de->d_name.name,
352 de->d_name.len) != 0)
358 #ifdef LUSTRE_KERNEL_VERSION
359 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
361 unlock_dentry(dentry);
362 __d_rehash(dentry, 0); /* avoid taking dcache_lock inside */
363 spin_unlock(&dcache_lock);
365 CDEBUG(D_DENTRY, "alias dentry %.*s (%p) parent %p inode %p "
366 "refc %d\n", de->d_name.len, de->d_name.name, de,
367 de->d_parent, de->d_inode, atomic_read(&de->d_count));
373 spin_unlock(&dcache_lock);
378 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
380 struct dentry *dentry;
382 dentry = d_add_unique(de, inode);
383 #ifdef LUSTRE_KERNEL_VERSION
386 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
387 unlock_dentry(dentry);
391 return dentry?dentry:de;
395 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
396 struct lookup_intent *it, void *data)
398 struct it_cb_data *icbd = data;
399 struct dentry **de = icbd->icbd_childp;
400 struct inode *parent = icbd->icbd_parent;
401 struct ll_sb_info *sbi = ll_i2sbi(parent);
402 struct inode *inode = NULL;
405 /* NB 1 request reference will be taken away by ll_intent_lock()
407 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
410 rc = ll_prep_inode(&inode, request, offset,
415 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
416 inode, inode->i_ino, inode->i_generation);
417 md_set_lock_data(sbi->ll_md_exp,
418 &it->d.lustre.it_lock_handle, inode);
420 /* We used to query real size from OSTs here, but actually
421 this is not needed. For stat() calls size would be updated
422 from subsequent do_revalidate()->ll_inode_revalidate_it() in
424 vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
425 Everybody else who needs correct file size would call
426 ll_glimpse_size or some equivalent themselves anyway.
427 Also see bug 7198. */
429 *de = ll_find_alias(inode, *de);
432 /* Check that parent has UPDATE lock. If there is none, we
433 cannot afford to hash this dentry (done by ll_d_add) as it
434 might get picked up later when UPDATE lock will appear */
435 if (ll_have_md_lock(parent, MDS_INODELOCK_UPDATE)) {
436 spin_lock(&dcache_lock);
437 ll_d_add(*de, inode);
438 spin_unlock(&dcache_lock);
440 (*de)->d_inode = NULL;
445 (*de)->d_op = &ll_d_ops;
450 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
451 struct lookup_intent *it, int lookup_flags)
453 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
454 struct dentry *save = dentry, *retval;
455 struct ptlrpc_request *req = NULL;
456 struct md_op_data *op_data;
457 struct it_cb_data icbd;
461 if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
462 RETURN(ERR_PTR(-ENAMETOOLONG));
464 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
465 dentry->d_name.len, dentry->d_name.name, parent->i_ino,
466 parent->i_generation, parent, LL_IT2STR(it));
468 if (d_mountpoint(dentry))
469 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
471 ll_frob_intent(&it, &lookup_it);
473 icbd.icbd_childp = &dentry;
474 icbd.icbd_parent = parent;
476 /* prepare operatoin hint first */
478 op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
479 dentry->d_name.len, lookup_flags);
481 RETURN(ERR_PTR(-ENOMEM));
483 /* allocate new fid for child */
484 if (it->it_op & IT_CREAT ||
485 (it->it_op & IT_OPEN && it->it_create_mode & O_CREAT)) {
486 struct lu_placement_hint hint = { .ph_pname = NULL,
487 .ph_pfid = ll_inode2fid(parent),
488 .ph_cname = &dentry->d_name,
489 .ph_opc = LUSTRE_OPC_CREATE };
491 rc = ll_fid_md_alloc(ll_i2sbi(parent), &op_data->fid2, &hint);
496 it->it_create_mode &= ~current->fs->umask;
498 rc = md_intent_lock(ll_i2mdexp(parent), op_data, NULL, 0, it,
499 lookup_flags, &req, ll_md_blocking_ast, 0);
500 ll_finish_md_op_data(op_data);
502 GOTO(out, retval = ERR_PTR(rc));
504 rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
506 ll_intent_release(it);
507 GOTO(out, retval = ERR_PTR(rc));
510 if ((it->it_op & IT_OPEN) && dentry->d_inode &&
511 !S_ISREG(dentry->d_inode->i_mode) &&
512 !S_ISDIR(dentry->d_inode->i_mode)) {
513 ll_release_openhandle(dentry, it);
515 ll_lookup_finish_locks(it, dentry);
518 GOTO(out, retval = NULL);
520 GOTO(out, retval = dentry);
523 ptlrpc_req_finished(req);
527 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
528 #ifdef LUSTRE_KERNEL_VERSION
529 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
530 struct nameidata *nd)
535 if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
536 de = ll_lookup_it(parent, dentry, &nd->intent, nd->flags);
538 de = ll_lookup_it(parent, dentry, NULL, 0);
543 struct lookup_intent *ll_convert_intent(struct open_intent *oit,
546 struct lookup_intent *it;
548 OBD_ALLOC(it, sizeof(*it));
550 return ERR_PTR(-ENOMEM);
552 if (lookup_flags & LOOKUP_OPEN) {
554 if (lookup_flags & LOOKUP_CREATE)
555 it->it_op |= IT_CREAT;
556 it->it_create_mode = oit->create_mode;
557 it->it_flags = oit->flags;
559 it->it_op = IT_GETATTR;
562 #ifndef HAVE_FILE_IN_STRUCT_INTENT
563 /* Since there is no way to pass our intent to ll_file_open,
564 * just check the file is there. Actual open will be done
566 if (it->it_op & IT_OPEN)
567 it->it_op = IT_LOOKUP;
573 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
574 struct nameidata *nd)
579 if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
580 struct lookup_intent *it;
582 #if defined(HAVE_FILE_IN_STRUCT_INTENT) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
583 /* Did we came here from failed revalidate just to propagate
585 if (nd->flags & LOOKUP_OPEN)
586 if (IS_ERR(nd->intent.open.file))
587 RETURN((struct dentry *)nd->intent.open.file);
590 if (ll_d2d(dentry) && ll_d2d(dentry)->lld_it) {
591 it = ll_d2d(dentry)->lld_it;
592 ll_d2d(dentry)->lld_it = NULL;
594 it = ll_convert_intent(&nd->intent.open, nd->flags);
596 RETURN((struct dentry *)it);
599 de = ll_lookup_it(parent, dentry, it, nd->flags);
602 if ((nd->flags & LOOKUP_OPEN) && !IS_ERR(dentry)) { /* Open */
603 if (dentry->d_inode &&
604 it_disposition(it, DISP_OPEN_OPEN)) { /* nocreate */
605 #ifdef HAVE_FILE_IN_STRUCT_INTENT
606 if (S_ISFIFO(dentry->d_inode->i_mode)) {
607 // We cannot call open here as it would
610 (struct ptlrpc_request *)
611 it->d.lustre.it_data);
614 nd->intent.open.file->private_data = it;
615 filp =lookup_instantiate_filp(nd,dentry,
617 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
618 /* 2.6.1[456] have a bug in open_namei() that forgets to check
619 * nd->intent.open.file for error, so we need to return it as lookup's result
624 de = (struct dentry *) filp;
629 #else /* HAVE_FILE_IN_STRUCT_INTENT */
630 /* Release open handle as we have no way to
631 * pass it to ll_file_open */
632 ll_release_openhandle(dentry, it);
633 #endif /* HAVE_FILE_IN_STRUCT_INTENT */
634 } else if (it_disposition(it, DISP_OPEN_CREATE)) {
635 // XXX This can only reliably work on assumption
636 // that there are NO hashed negative dentries.
637 ll_d2d(dentry)->lld_it = it;
638 it = NULL; /* Will be freed in ll_create_nd */
639 /* We absolutely depend on ll_create_nd to be
640 * called to not leak this intent and possible
641 * data attached to it */
646 ll_intent_release(it);
647 OBD_FREE(it, sizeof(*it));
650 de = ll_lookup_it(parent, dentry, NULL, 0);
658 /* We depend on "mode" being set with the proper file type/umask by now */
659 static struct inode *ll_create_node(struct inode *dir, const char *name,
660 int namelen, const void *data, int datalen,
661 int mode, __u64 extra,
662 struct lookup_intent *it)
664 struct inode *inode = NULL;
665 struct ptlrpc_request *request = NULL;
666 struct ll_sb_info *sbi = ll_i2sbi(dir);
670 LASSERT(it && it->d.lustre.it_disposition);
672 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
673 request = it->d.lustre.it_data;
674 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
675 rc = ll_prep_inode(&inode, request, DLM_REPLY_REC_OFF, dir->i_sb);
677 GOTO(out, inode = ERR_PTR(rc));
679 LASSERT(list_empty(&inode->i_dentry));
681 /* We asked for a lock on the directory, but were granted a
682 * lock on the inode. Since we finally have an inode pointer,
683 * stuff it in the lock. */
684 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode %p (%lu/%u)\n",
685 inode, inode->i_ino, inode->i_generation);
686 md_set_lock_data(sbi->ll_md_exp,
687 &it->d.lustre.it_lock_handle, inode);
690 ptlrpc_req_finished(request);
695 * By the time this is called, we already have created the directory cache
696 * entry for the new file, but it is so far negative - it has no inode.
698 * We defer creating the OBD object(s) until open, to keep the intent and
699 * non-intent code paths similar, and also because we do not have the MDS
700 * inode number before calling ll_create_node() (which is needed for LOV),
701 * so we would need to do yet another RPC to the MDS to store the LOV EA
702 * data on the MDS. If needed, we would pass the PACKED lmm as data and
703 * lmm_size in datalen (the MDS still has code which will handle that).
705 * If the create succeeds, we fill in the inode information
706 * with d_instantiate().
708 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
709 struct lookup_intent *it)
715 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
716 dentry->d_name.len, dentry->d_name.name, dir->i_ino,
717 dir->i_generation, dir, LL_IT2STR(it));
719 rc = it_open_error(DISP_OPEN_CREATE, it);
723 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
724 NULL, 0, mode, 0, it);
726 RETURN(PTR_ERR(inode));
729 d_instantiate(dentry, inode);
733 static void ll_update_times(struct ptlrpc_request *request, int offset,
736 struct mdt_body *body = lustre_msg_buf(request->rq_repmsg, offset,
740 /* mtime is always updated with ctime, but can be set in past.
741 As write and utime(2) may happen within 1 second, and utime's
742 mtime has a priority over write's one, so take mtime from mds
743 for the same ctimes. */
744 if (body->valid & OBD_MD_FLCTIME &&
745 body->ctime >= LTIME_S(inode->i_ctime)) {
746 LTIME_S(inode->i_ctime) = body->ctime;
748 if (body->valid & OBD_MD_FLMTIME) {
749 CDEBUG(D_INODE, "setting ino %lu mtime from %lu "
750 "to "LPU64"\n", inode->i_ino,
751 LTIME_S(inode->i_mtime), body->mtime);
752 LTIME_S(inode->i_mtime) = body->mtime;
757 static int ll_mknod_generic(struct inode *dir, struct qstr *name, int mode,
758 unsigned rdev, struct dentry *dchild)
760 struct ptlrpc_request *request = NULL;
761 struct inode *inode = NULL;
762 struct ll_sb_info *sbi = ll_i2sbi(dir);
763 struct md_op_data *op_data;
764 struct lu_placement_hint hint = {
766 .ph_pfid = ll_inode2fid(dir),
768 .ph_opc = LUSTRE_OPC_MKNOD
773 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p) mode %o dev %x\n",
774 name->len, name->name, dir->i_ino, dir->i_generation, dir,
777 mode &= ~current->fs->umask;
779 switch (mode & S_IFMT) {
782 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
787 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
791 err = ll_fid_md_alloc(sbi, &op_data->fid2, &hint);
793 ll_finish_md_op_data(op_data);
796 err = md_create(sbi->ll_md_exp, op_data, NULL, 0, mode,
797 current->fsuid, current->fsgid,
798 current->cap_effective, rdev, &request);
799 ll_finish_md_op_data(op_data);
802 ll_update_times(request, REPLY_REC_OFF, dir);
805 err = ll_prep_inode(&inode, request, REPLY_REC_OFF,
810 d_instantiate(dchild, inode);
819 ptlrpc_req_finished(request);
823 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
824 #ifndef LUSTRE_KERNEL_VERSION
825 static int ll_create_nd(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
827 struct lookup_intent *it = ll_d2d(dentry)->lld_it;
831 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
833 ll_d2d(dentry)->lld_it = NULL;
835 /* Was there an error? Propagate it! */
836 if (it->d.lustre.it_status) {
837 rc = it->d.lustre.it_status;
841 rc = ll_create_it(dir, dentry, mode, it);
842 #ifdef HAVE_FILE_IN_STRUCT_INTENT
843 if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
844 nd->intent.open.file->private_data = it;
845 lookup_instantiate_filp(nd, dentry, NULL);
848 ll_release_openhandle(dentry,it);
852 ll_intent_release(it);
853 OBD_FREE(it, sizeof(*it));
858 static int ll_create_nd(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
860 if (!nd || !nd->intent.d.lustre.it_disposition)
861 /* No saved request? Just mknod the file */
862 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
864 return ll_create_it(dir, dentry, mode, &nd->intent);
869 static int ll_symlink_generic(struct inode *dir, struct dentry *dchild,
872 struct qstr *name = &dchild->d_name;
873 struct lu_placement_hint hint = { .ph_pname = NULL,
874 .ph_pfid = ll_inode2fid(dir),
876 .ph_opc = LUSTRE_OPC_SYMLINK };
878 struct ptlrpc_request *request = NULL;
879 struct ll_sb_info *sbi = ll_i2sbi(dir);
880 struct inode *inode = NULL;
881 struct md_op_data *op_data;
885 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),target=%s\n",
886 name->len, name->name, dir->i_ino, dir->i_generation,
889 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0);
893 /* allocate new fid */
894 err = ll_fid_md_alloc(ll_i2sbi(dir), &op_data->fid2, &hint);
896 CERROR("can't allocate new fid, rc %d\n", err);
900 err = md_create(sbi->ll_md_exp, op_data,
901 tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
902 current->fsuid, current->fsgid, current->cap_effective,
904 ll_finish_md_op_data(op_data);
906 ll_update_times(request, REPLY_REC_OFF, dir);
909 err = ll_prep_inode(&inode, request, REPLY_REC_OFF,
912 d_instantiate(dchild, inode);
916 ptlrpc_req_finished(request);
920 static int ll_link_generic(struct inode *src, struct inode *dir,
923 struct ll_sb_info *sbi = ll_i2sbi(dir);
924 struct ptlrpc_request *request = NULL;
925 struct md_op_data *op_data;
930 "VFS Op: inode=%lu/%u(%p), dir=%lu/%u(%p), target=%.*s\n",
931 src->i_ino, src->i_generation, src, dir->i_ino,
932 dir->i_generation, dir, name->len, name->name);
934 op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len, 0);
937 err = md_link(sbi->ll_md_exp, op_data, &request);
938 ll_finish_md_op_data(op_data);
940 ll_update_times(request, REPLY_REC_OFF, dir);
942 ptlrpc_req_finished(request);
946 static int ll_mkdir_generic(struct inode *dir, struct qstr *name,
947 int mode, struct dentry *dchild)
950 struct lu_placement_hint hint = { .ph_pname = NULL,
951 .ph_pfid = ll_inode2fid(dir),
953 .ph_opc = LUSTRE_OPC_MKDIR };
954 struct ptlrpc_request *request = NULL;
955 struct ll_sb_info *sbi = ll_i2sbi(dir);
956 struct inode *inode = NULL;
957 struct md_op_data *op_data;
961 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
962 name->len, name->name, dir->i_ino, dir->i_generation, dir);
964 mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
966 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0);
970 /* Allocate new fid. */
971 err = ll_fid_md_alloc(ll_i2sbi(dir), &op_data->fid2, &hint);
973 CERROR("can't allocate new fid, rc %d\n", err);
977 err = md_create(sbi->ll_md_exp, op_data, NULL, 0, mode,
978 current->fsuid, current->fsgid,
979 current->cap_effective, 0, &request);
981 ll_finish_md_op_data(op_data);
983 ll_update_times(request, REPLY_REC_OFF, dir);
985 err = ll_prep_inode(&inode, request, REPLY_REC_OFF,
988 d_instantiate(dchild, inode);
992 ptlrpc_req_finished(request);
996 static int ll_rmdir_generic(struct inode *dir, struct dentry *dparent,
999 struct ptlrpc_request *request = NULL;
1000 struct md_op_data *op_data;
1001 struct dentry *dentry;
1004 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1005 name->len, name->name, dir->i_ino, dir->i_generation, dir);
1007 /* Check if we have something mounted at the dir we are going to delete
1008 * In such a case there would always be dentry present. */
1010 dentry = d_lookup(dparent, name);
1012 int mounted = d_mountpoint(dentry);
1019 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
1021 if (op_data == NULL)
1023 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1024 ll_finish_md_op_data(op_data);
1026 ll_update_times(request, REPLY_REC_OFF, dir);
1027 ptlrpc_req_finished(request);
1031 int ll_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
1033 struct mdt_body *body;
1034 struct lov_mds_md *eadata;
1035 struct lov_stripe_md *lsm = NULL;
1036 struct obd_trans_info oti = { 0 };
1041 /* req is swabbed so this is safe */
1042 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
1044 if (!(body->valid & OBD_MD_FLEASIZE))
1047 if (body->eadatasize == 0) {
1048 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1049 GOTO(out, rc = -EPROTO);
1052 /* The MDS sent back the EA because we unlinked the last reference
1053 * to this file. Use this EA to unlink the objects on the OST.
1054 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
1055 * check it is complete and sensible. */
1056 eadata = lustre_swab_repbuf(request, REPLY_REC_OFF + 1,
1057 body->eadatasize, NULL);
1058 LASSERT(eadata != NULL);
1059 if (eadata == NULL) {
1060 CERROR("Can't unpack MDS EA data\n");
1061 GOTO(out, rc = -EPROTO);
1064 rc = obd_unpackmd(ll_i2dtexp(dir), &lsm, eadata, body->eadatasize);
1066 CERROR("obd_unpackmd: %d\n", rc);
1069 LASSERT(rc >= sizeof(*lsm));
1071 rc = obd_checkmd(ll_i2dtexp(dir), ll_i2mdexp(dir), lsm);
1073 GOTO(out_free_memmd, rc);
1077 GOTO(out_free_memmd, rc = -ENOMEM);
1079 oa->o_id = lsm->lsm_object_id;
1080 oa->o_gr = lsm->lsm_object_gr;
1081 oa->o_mode = body->mode & S_IFMT;
1082 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
1084 if (body->valid & OBD_MD_FLCOOKIE) {
1085 oa->o_valid |= OBD_MD_FLCOOKIE;
1086 oti.oti_logcookies =
1087 lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF + 2,
1088 sizeof(struct llog_cookie) *
1089 lsm->lsm_stripe_count);
1090 if (oti.oti_logcookies == NULL) {
1091 oa->o_valid &= ~OBD_MD_FLCOOKIE;
1092 body->valid &= ~OBD_MD_FLCOOKIE;
1096 rc = obd_destroy(ll_i2dtexp(dir), oa, lsm, &oti, ll_i2mdexp(dir));
1099 CERROR("obd destroy objid "LPX64" error %d\n",
1100 lsm->lsm_object_id, rc);
1102 obd_free_memmd(ll_i2dtexp(dir), &lsm);
1107 static int ll_unlink_generic(struct inode *dir, struct qstr *name)
1109 struct ptlrpc_request *request = NULL;
1110 struct md_op_data *op_data;
1113 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1114 name->len, name->name, dir->i_ino, dir->i_generation, dir);
1116 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0);
1117 if (op_data == NULL)
1119 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1120 ll_finish_md_op_data(op_data);
1125 ll_update_times(request, REPLY_REC_OFF, dir);
1127 rc = ll_objects_destroy(request, dir);
1129 ptlrpc_req_finished(request);
1133 static int ll_rename_generic(struct inode *src, struct qstr *src_name,
1134 struct inode *tgt, struct qstr *tgt_name)
1136 struct ptlrpc_request *request = NULL;
1137 struct ll_sb_info *sbi = ll_i2sbi(src);
1138 struct md_op_data *op_data;
1141 CDEBUG(D_VFSTRACE,"VFS Op:oldname=%.*s,src_dir=%lu/%u(%p),newname=%.*s,"
1142 "tgt_dir=%lu/%u(%p)\n", src_name->len, src_name->name,
1143 src->i_ino, src->i_generation, src, tgt_name->len,
1144 tgt_name->name, tgt->i_ino, tgt->i_generation, tgt);
1146 op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0);
1147 if (op_data == NULL)
1149 err = md_rename(sbi->ll_md_exp, op_data,
1150 src_name->name, src_name->len,
1151 tgt_name->name, tgt_name->len, &request);
1152 ll_finish_md_op_data(op_data);
1154 ll_update_times(request, REPLY_REC_OFF, src);
1155 ll_update_times(request, REPLY_REC_OFF, tgt);
1156 err = ll_objects_destroy(request, src);
1159 ptlrpc_req_finished(request);
1164 #ifdef LUSTRE_KERNEL_VERSION
1165 static int ll_mknod_raw(struct nameidata *nd, int mode, dev_t rdev)
1167 return ll_mknod_generic(nd->dentry->d_inode, &nd->last, mode,rdev,NULL);
1169 static int ll_rename_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1171 return ll_rename_generic(srcnd->dentry->d_inode, &srcnd->last,
1172 tgtnd->dentry->d_inode, &tgtnd->last);
1174 static int ll_link_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1176 return ll_link_generic(srcnd->dentry->d_inode, tgtnd->dentry->d_inode,
1179 static int ll_symlink_raw(struct nameidata *nd, const char *tgt)
1183 static int ll_rmdir_raw(struct nameidata *nd)
1185 return ll_rmdir_generic(nd->dentry->d_inode, nd->dentry, &nd->last);
1187 static int ll_mkdir_raw(struct nameidata *nd, int mode)
1189 return ll_mkdir_generic(nd->dentry->d_inode, &nd->last, mode, NULL);
1191 static int ll_unlink_raw(struct nameidata *nd)
1193 return ll_unlink_generic(nd->dentry->d_inode, &nd->last);
1197 static int ll_mknod(struct inode *dir, struct dentry *dchild, int mode,
1200 return ll_mknod_generic(dir, &dchild->d_name, mode,
1201 old_encode_dev(rdev), dchild);
1204 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1205 static int ll_unlink(struct inode * dir, struct dentry *dentry)
1207 return ll_unlink_generic(dir, &dentry->d_name);
1209 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1211 return ll_mkdir_generic(dir, &dentry->d_name, mode, dentry);
1213 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
1215 return ll_rmdir_generic(dir, NULL, &dentry->d_name);
1217 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1218 const char *oldname)
1220 return ll_symlink_generic(dir, dentry, oldname);
1222 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1223 struct dentry *new_dentry)
1225 return ll_link_generic(old_dentry->d_inode, dir, &new_dentry->d_name);
1227 static int ll_rename(struct inode *old_dir, struct dentry *old_dentry,
1228 struct inode *new_dir, struct dentry *new_dentry)
1230 return ll_rename_generic(old_dir, &old_dentry->d_name, new_dir,
1231 &new_dentry->d_name);
1235 struct inode_operations ll_dir_inode_operations = {
1236 #ifdef LUSTRE_KERNEL_VERSION
1237 .link_raw = ll_link_raw,
1238 .unlink_raw = ll_unlink_raw,
1239 .symlink_raw = ll_symlink_raw,
1240 .mkdir_raw = ll_mkdir_raw,
1241 .rmdir_raw = ll_rmdir_raw,
1242 .mknod_raw = ll_mknod_raw,
1243 .rename_raw = ll_rename_raw,
1244 .setattr = ll_setattr,
1245 .setattr_raw = ll_setattr_raw,
1248 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1249 .create_it = ll_create_it,
1250 .lookup_it = ll_lookup_it,
1251 .revalidate_it = ll_inode_revalidate_it,
1253 .lookup = ll_lookup_nd,
1254 .create = ll_create_nd,
1255 /* We need all these non-raw things for NFSD, to not patch it. */
1256 .unlink = ll_unlink,
1259 .symlink = ll_symlink,
1261 .rename = ll_rename,
1262 .setattr = ll_setattr,
1263 .getattr = ll_getattr,
1265 .permission = ll_inode_permission,
1266 .setxattr = ll_setxattr,
1267 .getxattr = ll_getxattr,
1268 .listxattr = ll_listxattr,
1269 .removexattr = ll_removexattr,
1272 struct inode_operations ll_special_inode_operations = {
1273 #ifdef LUSTRE_KERNEL_VERSION
1274 .setattr_raw = ll_setattr_raw,
1276 .setattr = ll_setattr,
1277 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1278 .getattr = ll_getattr,
1280 .revalidate_it = ll_inode_revalidate_it,
1282 .permission = ll_inode_permission,
1283 .setxattr = ll_setxattr,
1284 .getxattr = ll_getxattr,
1285 .listxattr = ll_listxattr,
1286 .removexattr = ll_removexattr,