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"
41 extern struct dentry_operations ll_d_ops;
44 * Check if we have something mounted at the named dchild.
45 * In such a case there would always be dentry present.
47 static int ll_d_mountpoint(struct dentry *dparent, struct dentry *dchild,
52 if (unlikely(dchild)) {
53 mounted = d_mountpoint(dchild);
55 dchild = d_lookup(dparent, name);
57 mounted = d_mountpoint(dchild);
64 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
68 ldlm_lock_decref(lockh, mode);
74 * Get an inode by inode number (already instantiated by the intent lookup).
75 * Returns inode or NULL
77 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
78 struct inode *ll_iget(struct super_block *sb, ino_t hash,
81 struct ll_inode_info *lli;
85 inode = iget_locked(sb, hash);
87 if (inode->i_state & I_NEW) {
88 lli = ll_i2info(inode);
89 ll_read_inode2(inode, md);
90 unlock_new_inode(inode);
92 if (!(inode->i_state & (I_FREEING | I_CLEAR)))
93 ll_update_inode(inode, md);
95 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n",
96 inode->i_ino, inode->i_generation, inode);
102 struct inode *ll_iget(struct super_block *sb, ino_t hash,
103 struct lustre_md *md)
108 inode = iget4(sb, hash, NULL, md);
110 if (!(inode->i_state & (I_FREEING | I_CLEAR)))
111 ll_update_inode(inode, md);
113 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n",
114 inode->i_ino, inode->i_generation, inode);
120 static void ll_drop_negative_dentry(struct inode *dir)
122 struct dentry *dentry, *tmp_alias, *tmp_subdir;
124 spin_lock(&dcache_lock);
126 list_for_each_entry_safe(dentry, tmp_alias,
127 &dir->i_dentry,d_alias) {
128 if (!list_empty(&dentry->d_subdirs)) {
129 struct dentry *child;
130 list_for_each_entry_safe(child, tmp_subdir,
133 /* XXX Print some debug here? */
135 /* Negative dentry. If we were
136 dropping dcache lock, go
137 throught the list again */
138 if (ll_drop_dentry(child))
143 spin_unlock(&dcache_lock);
147 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
148 void *data, int flag)
151 struct lustre_handle lockh;
155 case LDLM_CB_BLOCKING:
156 ldlm_lock2handle(lock, &lockh);
157 rc = ldlm_cli_cancel(&lockh);
159 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
163 case LDLM_CB_CANCELING: {
164 struct inode *inode = ll_inode_from_lock(lock);
165 __u64 bits = lock->l_policy_data.l_inodebits.bits;
168 /* Invalidate all dentries associated with this inode */
172 fid = ll_inode2fid(inode);
173 if (lock->l_resource->lr_name.name[0] != fid_seq(fid) ||
174 lock->l_resource->lr_name.name[1] != fid_oid(fid) ||
175 lock->l_resource->lr_name.name[2] != fid_ver(fid)) {
176 LDLM_ERROR(lock, "data mismatch with object "
177 DFID" (%p)", PFID(fid), inode);
180 if (bits & MDS_INODELOCK_OPEN) {
182 switch (lock->l_req_mode) {
193 CERROR("Unexpected lock mode for OPEN lock "
194 "%d, inode %ld\n", lock->l_req_mode,
197 ll_md_real_close(inode, flags);
200 if (bits & MDS_INODELOCK_UPDATE)
201 ll_i2info(inode)->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
203 if (S_ISDIR(inode->i_mode) &&
204 (bits & MDS_INODELOCK_UPDATE)) {
205 CDEBUG(D_INODE, "invalidating inode %lu\n",
207 truncate_inode_pages(inode->i_mapping, 0);
208 ll_drop_negative_dentry(inode);
211 if (inode->i_sb->s_root &&
212 inode != inode->i_sb->s_root->d_inode &&
213 (bits & MDS_INODELOCK_LOOKUP))
214 ll_unhash_aliases(inode);
225 /* Pack the required supplementary groups into the supplied groups array.
226 * If we don't need to use the groups from the target inode(s) then we
227 * instead pack one or more groups from the user's supplementary group
228 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
229 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
234 LASSERT(suppgids != NULL);
236 if (in_group_p(i1->i_gid))
237 suppgids[0] = i1->i_gid;
242 if (in_group_p(i2->i_gid))
243 suppgids[1] = i2->i_gid;
250 for (i = 0; i < current_ngroups; i++) {
251 if (suppgids[0] == -1) {
252 if (current_groups[i] != suppgids[1])
253 suppgids[0] = current_groups[i];
256 if (suppgids[1] == -1) {
257 if (current_groups[i] != suppgids[0])
258 suppgids[1] = current_groups[i];
265 static void ll_d_add(struct dentry *de, struct inode *inode)
267 CDEBUG(D_DENTRY, "adding inode %p to dentry %p\n", inode, de);
269 if (!list_empty(&de->d_alias)) {
270 spin_unlock(&dcache_lock);
271 CERROR("dentry %.*s %p alias next %p, prev %p\n",
272 de->d_name.len, de->d_name.name, de,
273 de->d_alias.next, de->d_alias.prev);
277 list_add(&de->d_alias, &inode->i_dentry);
281 if (!d_unhashed(de)) {
282 spin_unlock(&dcache_lock);
283 CERROR("dentry %.*s %p hash next %p\n",
284 de->d_name.len, de->d_name.name, de, de->d_hash.next);
290 /* Search "inode"'s alias list for a dentry that has the same name and parent
291 * as de. If found, return it. If not found, return de.
292 * Lustre can't use d_add_unique because don't unhash aliases for directory
293 * in ll_revalidate_it. After revaliadate inode will be have hashed aliases
294 * and it triggers BUG_ON in d_instantiate_unique (bug #10954).
296 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
298 struct list_head *tmp;
299 struct dentry *dentry;
300 struct dentry *last_discon = NULL;
302 spin_lock(&dcache_lock);
303 list_for_each(tmp, &inode->i_dentry) {
304 dentry = list_entry(tmp, struct dentry, d_alias);
306 /* We are called here with 'de' already on the aliases list. */
307 if (unlikely(dentry == de)) {
312 if (dentry->d_flags & DCACHE_DISCONNECTED) {
313 LASSERT(last_discon == NULL);
314 last_discon = dentry;
318 if (dentry->d_parent != de->d_parent)
321 if (dentry->d_name.hash != de->d_name.hash)
324 if (dentry->d_name.len != de->d_name.len)
327 if (memcmp(dentry->d_name.name, de->d_name.name,
328 de->d_name.len) != 0)
334 #ifdef LUSTRE_KERNEL_VERSION
335 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
337 unlock_dentry(dentry);
338 __d_rehash(dentry, 0); /* avoid taking dcache_lock inside */
339 spin_unlock(&dcache_lock);
341 CDEBUG(D_DENTRY, "alias dentry %.*s (%p) parent %p inode %p "
342 "refc %d\n", de->d_name.len, de->d_name.name, de,
343 de->d_parent, de->d_inode, atomic_read(&de->d_count));
348 CDEBUG(D_DENTRY, "Reuse disconnected dentry %p inode %p "
349 "refc %d\n", last_discon, last_discon->d_inode,
350 atomic_read(&last_discon->d_count));
352 dget_locked(last_discon);
353 __d_move(last_discon, de);
354 spin_unlock(&dcache_lock);
362 spin_unlock(&dcache_lock);
367 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
368 struct lookup_intent *it, void *data)
370 struct it_cb_data *icbd = data;
371 struct dentry **de = icbd->icbd_childp;
372 struct inode *parent = icbd->icbd_parent;
373 struct ll_sb_info *sbi = ll_i2sbi(parent);
374 struct inode *inode = NULL;
377 /* NB 1 request reference will be taken away by ll_intent_lock()
379 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
382 rc = ll_prep_inode(&inode, request, offset,
387 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
388 inode, inode->i_ino, inode->i_generation);
389 md_set_lock_data(sbi->ll_md_exp,
390 &it->d.lustre.it_lock_handle, inode);
392 /* We used to query real size from OSTs here, but actually
393 this is not needed. For stat() calls size would be updated
394 from subsequent do_revalidate()->ll_inode_revalidate_it() in
396 vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
397 Everybody else who needs correct file size would call
398 ll_glimpse_size or some equivalent themselves anyway.
399 Also see bug 7198. */
401 *de = ll_find_alias(inode, *de);
404 /* Check that parent has UPDATE lock. If there is none, we
405 cannot afford to hash this dentry (done by ll_d_add) as it
406 might get picked up later when UPDATE lock will appear */
407 if (ll_have_md_lock(parent, MDS_INODELOCK_UPDATE)) {
408 spin_lock(&dcache_lock);
409 ll_d_add(*de, inode);
410 spin_unlock(&dcache_lock);
412 (*de)->d_inode = NULL;
413 /* We do not want to hash the dentry if don`t have a
414 * lock, but if this dentry is later used in d_move,
415 * we'd hit uninitialised list head d_hash, so we just
416 * do this to init d_hash field but leave dentry
417 * unhashed. (bug 10796). */
424 (*de)->d_op = &ll_d_ops;
429 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
430 struct lookup_intent *it, int lookup_flags)
432 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
433 struct dentry *save = dentry, *retval;
434 struct ptlrpc_request *req = NULL;
435 struct md_op_data *op_data;
436 struct it_cb_data icbd;
441 if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
442 RETURN(ERR_PTR(-ENAMETOOLONG));
444 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
445 dentry->d_name.len, dentry->d_name.name, parent->i_ino,
446 parent->i_generation, parent, LL_IT2STR(it));
448 if (d_mountpoint(dentry))
449 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
451 ll_frob_intent(&it, &lookup_it);
453 /* As do_lookup is called before follow_mount, root dentry may be left
454 * not valid, revalidate it here. */
455 if (parent->i_sb->s_root && (parent->i_sb->s_root->d_inode == parent) &&
456 (it->it_op & (IT_OPEN | IT_CREAT))) {
457 rc = ll_inode_revalidate_it(parent->i_sb->s_root, it);
462 icbd.icbd_childp = &dentry;
463 icbd.icbd_parent = parent;
465 if (it->it_op & IT_CREAT ||
466 (it->it_op & IT_OPEN && it->it_create_mode & O_CREAT))
467 opc = LUSTRE_OPC_CREATE;
469 opc = LUSTRE_OPC_ANY;
471 op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
472 dentry->d_name.len, lookup_flags, opc);
474 RETURN((void *)op_data);
476 it->it_create_mode &= ~current->fs->umask;
478 rc = md_intent_lock(ll_i2mdexp(parent), op_data, NULL, 0, it,
479 lookup_flags, &req, ll_md_blocking_ast, 0);
480 ll_finish_md_op_data(op_data);
482 GOTO(out, retval = ERR_PTR(rc));
484 rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
486 ll_intent_release(it);
487 GOTO(out, retval = ERR_PTR(rc));
490 if ((it->it_op & IT_OPEN) && dentry->d_inode &&
491 !S_ISREG(dentry->d_inode->i_mode) &&
492 !S_ISDIR(dentry->d_inode->i_mode)) {
493 ll_release_openhandle(dentry, it);
495 ll_lookup_finish_locks(it, dentry);
498 GOTO(out, retval = NULL);
500 GOTO(out, retval = dentry);
503 ptlrpc_req_finished(req);
507 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
508 #ifdef LUSTRE_KERNEL_VERSION
509 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
510 struct nameidata *nd)
515 if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
516 de = ll_lookup_it(parent, dentry, &nd->intent, nd->flags);
518 de = ll_lookup_it(parent, dentry, NULL, 0);
523 struct lookup_intent *ll_convert_intent(struct open_intent *oit,
526 struct lookup_intent *it;
528 OBD_ALLOC(it, sizeof(*it));
530 return ERR_PTR(-ENOMEM);
532 if (lookup_flags & LOOKUP_OPEN) {
534 if (lookup_flags & LOOKUP_CREATE)
535 it->it_op |= IT_CREAT;
536 it->it_create_mode = oit->create_mode;
537 it->it_flags = oit->flags;
539 it->it_op = IT_GETATTR;
542 #ifndef HAVE_FILE_IN_STRUCT_INTENT
543 /* Since there is no way to pass our intent to ll_file_open,
544 * just check the file is there. Actual open will be done
546 if (it->it_op & IT_OPEN)
547 it->it_op = IT_LOOKUP;
553 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
554 struct nameidata *nd)
559 if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
560 struct lookup_intent *it;
562 #if defined(HAVE_FILE_IN_STRUCT_INTENT) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
563 /* Did we came here from failed revalidate just to propagate
565 if (nd->flags & LOOKUP_OPEN)
566 if (IS_ERR(nd->intent.open.file))
567 RETURN((struct dentry *)nd->intent.open.file);
570 if (ll_d2d(dentry) && ll_d2d(dentry)->lld_it) {
571 it = ll_d2d(dentry)->lld_it;
572 ll_d2d(dentry)->lld_it = NULL;
574 it = ll_convert_intent(&nd->intent.open, nd->flags);
576 RETURN((struct dentry *)it);
579 de = ll_lookup_it(parent, dentry, it, nd->flags);
582 if ((nd->flags & LOOKUP_OPEN) && !IS_ERR(dentry)) { /* Open */
583 if (dentry->d_inode &&
584 it_disposition(it, DISP_OPEN_OPEN)) { /* nocreate */
585 #ifdef HAVE_FILE_IN_STRUCT_INTENT
586 if (S_ISFIFO(dentry->d_inode->i_mode)) {
587 // We cannot call open here as it would
590 (struct ptlrpc_request *)
591 it->d.lustre.it_data);
594 nd->intent.open.file->private_data = it;
595 filp =lookup_instantiate_filp(nd,dentry,
597 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
598 /* 2.6.1[456] have a bug in open_namei() that forgets to check
599 * nd->intent.open.file for error, so we need to return it as lookup's result
604 de = (struct dentry *) filp;
609 #else /* HAVE_FILE_IN_STRUCT_INTENT */
610 /* Release open handle as we have no way to
611 * pass it to ll_file_open */
612 ll_release_openhandle(dentry, it);
613 #endif /* HAVE_FILE_IN_STRUCT_INTENT */
614 } else if (it_disposition(it, DISP_OPEN_CREATE)) {
615 // XXX This can only reliably work on assumption
616 // that there are NO hashed negative dentries.
617 ll_d2d(dentry)->lld_it = it;
618 it = NULL; /* Will be freed in ll_create_nd */
619 /* We absolutely depend on ll_create_nd to be
620 * called to not leak this intent and possible
621 * data attached to it */
626 ll_intent_release(it);
627 OBD_FREE(it, sizeof(*it));
630 de = ll_lookup_it(parent, dentry, NULL, 0);
638 /* We depend on "mode" being set with the proper file type/umask by now */
639 static struct inode *ll_create_node(struct inode *dir, const char *name,
640 int namelen, const void *data, int datalen,
641 int mode, __u64 extra,
642 struct lookup_intent *it)
644 struct inode *inode = NULL;
645 struct ptlrpc_request *request = NULL;
646 struct ll_sb_info *sbi = ll_i2sbi(dir);
650 LASSERT(it && it->d.lustre.it_disposition);
652 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
653 request = it->d.lustre.it_data;
654 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
655 rc = ll_prep_inode(&inode, request, DLM_REPLY_REC_OFF, dir->i_sb);
657 GOTO(out, inode = ERR_PTR(rc));
659 LASSERT(list_empty(&inode->i_dentry));
661 /* We asked for a lock on the directory, but were granted a
662 * lock on the inode. Since we finally have an inode pointer,
663 * stuff it in the lock. */
664 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode %p (%lu/%u)\n",
665 inode, inode->i_ino, inode->i_generation);
666 md_set_lock_data(sbi->ll_md_exp,
667 &it->d.lustre.it_lock_handle, inode);
670 ptlrpc_req_finished(request);
675 * By the time this is called, we already have created the directory cache
676 * entry for the new file, but it is so far negative - it has no inode.
678 * We defer creating the OBD object(s) until open, to keep the intent and
679 * non-intent code paths similar, and also because we do not have the MDS
680 * inode number before calling ll_create_node() (which is needed for LOV),
681 * so we would need to do yet another RPC to the MDS to store the LOV EA
682 * data on the MDS. If needed, we would pass the PACKED lmm as data and
683 * lmm_size in datalen (the MDS still has code which will handle that).
685 * If the create succeeds, we fill in the inode information
686 * with d_instantiate().
688 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
689 struct lookup_intent *it)
695 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
696 dentry->d_name.len, dentry->d_name.name, dir->i_ino,
697 dir->i_generation, dir, LL_IT2STR(it));
699 rc = it_open_error(DISP_OPEN_CREATE, it);
703 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
704 NULL, 0, mode, 0, it);
706 RETURN(PTR_ERR(inode));
709 d_instantiate(dentry, inode);
710 /* Negative dentry may be unhashed if parent does not have UPDATE lock,
711 * but some callers, e.g. do_coredump, expect dentry to be hashed after
712 * successful create. Hash it here. */
713 spin_lock(&dcache_lock);
714 if (d_unhashed(dentry))
715 __d_rehash(dentry, 0);
716 spin_unlock(&dcache_lock);
720 static void ll_update_times(struct ptlrpc_request *request, int offset,
723 struct mdt_body *body = lustre_msg_buf(request->rq_repmsg, offset,
727 /* mtime is always updated with ctime, but can be set in past.
728 As write and utime(2) may happen within 1 second, and utime's
729 mtime has a priority over write's one, so take mtime from mds
730 for the same ctimes. */
731 if (body->valid & OBD_MD_FLCTIME &&
732 body->ctime >= LTIME_S(inode->i_ctime)) {
733 LTIME_S(inode->i_ctime) = body->ctime;
735 if (body->valid & OBD_MD_FLMTIME) {
736 CDEBUG(D_INODE, "setting ino %lu mtime from %lu "
737 "to "LPU64"\n", inode->i_ino,
738 LTIME_S(inode->i_mtime), body->mtime);
739 LTIME_S(inode->i_mtime) = body->mtime;
744 static int ll_new_node(struct inode *dir, struct qstr *name,
745 const char *tgt, int mode, int rdev,
746 struct dentry *dchild, __u32 opc)
748 struct ptlrpc_request *request = NULL;
749 struct md_op_data *op_data;
750 struct inode *inode = NULL;
751 struct ll_sb_info *sbi = ll_i2sbi(dir);
756 if (unlikely(tgt != NULL))
757 tgt_len = strlen(tgt) + 1;
759 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
762 GOTO(err_exit, err = PTR_ERR(op_data));
764 err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
765 current->fsuid, current->fsgid,
766 current->cap_effective, rdev, &request);
767 ll_finish_md_op_data(op_data);
771 ll_update_times(request, REPLY_REC_OFF, dir);
774 err = ll_prep_inode(&inode, request, REPLY_REC_OFF,
780 d_instantiate(dchild, inode);
784 ptlrpc_req_finished(request);
789 static int ll_mknod_generic(struct inode *dir, struct qstr *name, int mode,
790 unsigned rdev, struct dentry *dchild)
795 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p) mode %o dev %x\n",
796 name->len, name->name, dir->i_ino, dir->i_generation, dir,
799 mode &= ~current->fs->umask;
801 switch (mode & S_IFMT) {
803 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
809 err = ll_new_node(dir, name, NULL, mode, rdev, dchild,
821 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
822 #ifndef LUSTRE_KERNEL_VERSION
823 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
824 int mode, struct nameidata *nd)
826 struct lookup_intent *it = ll_d2d(dentry)->lld_it;
830 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
832 ll_d2d(dentry)->lld_it = NULL;
834 /* Was there an error? Propagate it! */
835 if (it->d.lustre.it_status) {
836 rc = it->d.lustre.it_status;
840 rc = ll_create_it(dir, dentry, mode, it);
841 #ifdef HAVE_FILE_IN_STRUCT_INTENT
842 if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
843 nd->intent.open.file->private_data = it;
844 lookup_instantiate_filp(nd, dentry, NULL);
847 ll_release_openhandle(dentry,it);
851 ll_intent_release(it);
852 OBD_FREE(it, sizeof(*it));
857 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
858 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 qstr *name,
870 const char *tgt, struct dentry *dchild)
875 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),target=%.*s\n",
876 name->len, name->name, dir->i_ino, dir->i_generation,
879 err = ll_new_node(dir, name, (char *)tgt, S_IFLNK | S_IRWXUGO,
880 0, dchild, LUSTRE_OPC_SYMLINK);
884 static int ll_link_generic(struct inode *src, struct inode *dir,
885 struct qstr *name, struct dentry *dchild)
887 struct ll_sb_info *sbi = ll_i2sbi(dir);
888 struct ptlrpc_request *request = NULL;
889 struct md_op_data *op_data;
894 "VFS Op: inode=%lu/%u(%p), dir=%lu/%u(%p), target=%.*s\n",
895 src->i_ino, src->i_generation, src, dir->i_ino,
896 dir->i_generation, dir, name->len, name->name);
898 op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len,
901 RETURN(PTR_ERR(op_data));
903 err = md_link(sbi->ll_md_exp, op_data, &request);
904 ll_finish_md_op_data(op_data);
910 ll_update_times(request, REPLY_REC_OFF, dir);
913 ptlrpc_req_finished(request);
917 static int ll_mkdir_generic(struct inode *dir, struct qstr *name,
918 int mode, struct dentry *dchild)
924 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
925 name->len, name->name, dir->i_ino, dir->i_generation, dir);
927 mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
928 err = ll_new_node(dir, name, NULL, mode, 0, dchild, LUSTRE_OPC_MKDIR);
933 static int ll_rmdir_generic(struct inode *dir, struct dentry *dparent,
934 struct dentry *dchild, struct qstr *name)
936 struct ptlrpc_request *request = NULL;
937 struct md_op_data *op_data;
941 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
942 name->len, name->name, dir->i_ino, dir->i_generation, dir);
944 if (unlikely(ll_d_mountpoint(dparent, dchild, name)))
947 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
948 S_IFDIR, LUSTRE_OPC_ANY);
950 RETURN(PTR_ERR(op_data));
952 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
953 ll_finish_md_op_data(op_data);
955 ll_update_times(request, REPLY_REC_OFF, dir);
956 ptlrpc_req_finished(request);
960 int ll_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
962 struct mdt_body *body;
963 struct lov_mds_md *eadata;
964 struct lov_stripe_md *lsm = NULL;
965 struct obd_trans_info oti = { 0 };
970 /* req is swabbed so this is safe */
971 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
973 if (!(body->valid & OBD_MD_FLEASIZE))
976 if (body->eadatasize == 0) {
977 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
978 GOTO(out, rc = -EPROTO);
981 /* The MDS sent back the EA because we unlinked the last reference
982 * to this file. Use this EA to unlink the objects on the OST.
983 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
984 * check it is complete and sensible. */
985 eadata = lustre_swab_repbuf(request, REPLY_REC_OFF + 1,
986 body->eadatasize, NULL);
987 LASSERT(eadata != NULL);
988 if (eadata == NULL) {
989 CERROR("Can't unpack MDS EA data\n");
990 GOTO(out, rc = -EPROTO);
993 rc = obd_unpackmd(ll_i2dtexp(dir), &lsm, eadata, body->eadatasize);
995 CERROR("obd_unpackmd: %d\n", rc);
998 LASSERT(rc >= sizeof(*lsm));
1000 rc = obd_checkmd(ll_i2dtexp(dir), ll_i2mdexp(dir), lsm);
1002 GOTO(out_free_memmd, rc);
1006 GOTO(out_free_memmd, rc = -ENOMEM);
1008 oa->o_id = lsm->lsm_object_id;
1009 oa->o_gr = lsm->lsm_object_gr;
1010 oa->o_mode = body->mode & S_IFMT;
1011 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
1013 if (body->valid & OBD_MD_FLCOOKIE) {
1014 oa->o_valid |= OBD_MD_FLCOOKIE;
1015 oti.oti_logcookies =
1016 lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF + 2,
1017 sizeof(struct llog_cookie) *
1018 lsm->lsm_stripe_count);
1019 if (oti.oti_logcookies == NULL) {
1020 oa->o_valid &= ~OBD_MD_FLCOOKIE;
1021 body->valid &= ~OBD_MD_FLCOOKIE;
1025 rc = obd_destroy(ll_i2dtexp(dir), oa, lsm, &oti, ll_i2mdexp(dir));
1028 CERROR("obd destroy objid "LPX64" error %d\n",
1029 lsm->lsm_object_id, rc);
1031 obd_free_memmd(ll_i2dtexp(dir), &lsm);
1036 static int ll_unlink_generic(struct inode *dir, struct dentry *dparent,
1037 struct dentry *dchild, struct qstr *name)
1039 struct ptlrpc_request *request = NULL;
1040 struct md_op_data *op_data;
1043 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1044 name->len, name->name, dir->i_ino, dir->i_generation, dir);
1047 * XXX: unlink bind mountpoint maybe call to here,
1048 * just check it as vfs_unlink does.
1050 if (unlikely(ll_d_mountpoint(dparent, dchild, name)))
1053 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
1054 name->len, 0, LUSTRE_OPC_ANY);
1055 if (IS_ERR(op_data))
1056 RETURN(PTR_ERR(op_data));
1058 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1059 ll_finish_md_op_data(op_data);
1064 ll_update_times(request, REPLY_REC_OFF, dir);
1066 rc = ll_objects_destroy(request, dir);
1068 ptlrpc_req_finished(request);
1072 static int ll_rename_generic(struct inode *src, struct dentry *src_dparent,
1073 struct dentry *src_dchild, struct qstr *src_name,
1074 struct inode *tgt, struct dentry *tgt_dparent,
1075 struct dentry *tgt_dchild, struct qstr *tgt_name)
1077 struct ptlrpc_request *request = NULL;
1078 struct ll_sb_info *sbi = ll_i2sbi(src);
1079 struct md_op_data *op_data;
1082 CDEBUG(D_VFSTRACE,"VFS Op:oldname=%.*s,src_dir=%lu/%u(%p),newname=%.*s,"
1083 "tgt_dir=%lu/%u(%p)\n", src_name->len, src_name->name,
1084 src->i_ino, src->i_generation, src, tgt_name->len,
1085 tgt_name->name, tgt->i_ino, tgt->i_generation, tgt);
1087 if (unlikely(ll_d_mountpoint(src_dparent, src_dchild, src_name) ||
1088 ll_d_mountpoint(tgt_dparent, tgt_dchild, tgt_name)))
1091 op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0,
1093 if (IS_ERR(op_data))
1094 RETURN(PTR_ERR(op_data));
1096 err = md_rename(sbi->ll_md_exp, op_data,
1097 src_name->name, src_name->len,
1098 tgt_name->name, tgt_name->len, &request);
1099 ll_finish_md_op_data(op_data);
1101 ll_update_times(request, REPLY_REC_OFF, src);
1102 ll_update_times(request, REPLY_REC_OFF, tgt);
1103 err = ll_objects_destroy(request, src);
1106 ptlrpc_req_finished(request);
1111 #ifdef LUSTRE_KERNEL_VERSION
1112 static int ll_mknod_raw(struct nameidata *nd, int mode, dev_t rdev)
1114 return ll_mknod_generic(nd->dentry->d_inode, &nd->last, mode,rdev,NULL);
1116 static int ll_rename_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1118 return ll_rename_generic(srcnd->dentry->d_inode, srcnd->dentry,
1120 tgtnd->dentry->d_inode, tgtnd->dentry,
1121 NULL, &tgtnd->last);
1123 static int ll_link_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1125 return ll_link_generic(srcnd->dentry->d_inode, tgtnd->dentry->d_inode,
1126 &tgtnd->last, NULL);
1128 static int ll_symlink_raw(struct nameidata *nd, const char *tgt)
1130 return ll_symlink_generic(nd->dentry->d_inode, &nd->last, tgt, NULL);
1132 static int ll_rmdir_raw(struct nameidata *nd)
1134 return ll_rmdir_generic(nd->dentry->d_inode, nd->dentry, NULL,
1137 static int ll_mkdir_raw(struct nameidata *nd, int mode)
1139 return ll_mkdir_generic(nd->dentry->d_inode, &nd->last, mode, NULL);
1141 static int ll_unlink_raw(struct nameidata *nd)
1143 return ll_unlink_generic(nd->dentry->d_inode, nd->dentry, NULL,
1148 static int ll_mknod(struct inode *dir, struct dentry *dchild, int mode,
1151 return ll_mknod_generic(dir, &dchild->d_name, mode,
1152 old_encode_dev(rdev), dchild);
1155 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1156 static int ll_unlink(struct inode * dir, struct dentry *dentry)
1158 return ll_unlink_generic(dir, NULL, dentry, &dentry->d_name);
1160 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1162 return ll_mkdir_generic(dir, &dentry->d_name, mode, dentry);
1164 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
1166 return ll_rmdir_generic(dir, NULL, dentry, &dentry->d_name);
1168 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1169 const char *oldname)
1171 return ll_symlink_generic(dir, &dentry->d_name, oldname, dentry);
1173 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1174 struct dentry *new_dentry)
1176 return ll_link_generic(old_dentry->d_inode, dir, &new_dentry->d_name,
1179 static int ll_rename(struct inode *old_dir, struct dentry *old_dentry,
1180 struct inode *new_dir, struct dentry *new_dentry)
1182 return ll_rename_generic(old_dir, NULL,
1183 old_dentry, &old_dentry->d_name,
1184 new_dir, NULL, new_dentry,
1185 &new_dentry->d_name);
1189 struct inode_operations ll_dir_inode_operations = {
1190 #ifdef LUSTRE_KERNEL_VERSION
1191 .link_raw = ll_link_raw,
1192 .unlink_raw = ll_unlink_raw,
1193 .symlink_raw = ll_symlink_raw,
1194 .mkdir_raw = ll_mkdir_raw,
1195 .rmdir_raw = ll_rmdir_raw,
1196 .mknod_raw = ll_mknod_raw,
1197 .rename_raw = ll_rename_raw,
1198 .setattr = ll_setattr,
1199 .setattr_raw = ll_setattr_raw,
1202 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1203 .create_it = ll_create_it,
1204 .lookup_it = ll_lookup_it,
1205 .revalidate_it = ll_inode_revalidate_it,
1207 .lookup = ll_lookup_nd,
1208 .create = ll_create_nd,
1209 /* We need all these non-raw things for NFSD, to not patch it. */
1210 .unlink = ll_unlink,
1213 .symlink = ll_symlink,
1215 .rename = ll_rename,
1216 .setattr = ll_setattr,
1217 .getattr = ll_getattr,
1219 .permission = ll_inode_permission,
1220 .setxattr = ll_setxattr,
1221 .getxattr = ll_getxattr,
1222 .listxattr = ll_listxattr,
1223 .removexattr = ll_removexattr,
1226 struct inode_operations ll_special_inode_operations = {
1227 #ifdef LUSTRE_KERNEL_VERSION
1228 .setattr_raw = ll_setattr_raw,
1230 .setattr = ll_setattr,
1231 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1232 .getattr = ll_getattr,
1234 .revalidate_it = ll_inode_revalidate_it,
1236 .permission = ll_inode_permission,
1237 .setxattr = ll_setxattr,
1238 .getxattr = ll_getxattr,
1239 .listxattr = ll_listxattr,
1240 .removexattr = ll_removexattr,