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_lite.h>
34 #include <lustre_dlm.h>
35 #include <linux/lustre_version.h>
36 #include "llite_internal.h"
40 /* called from iget{4,5_locked}->find_inode() under inode_lock spinlock */
41 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
42 static int ll_test_inode(struct inode *inode, unsigned long ino, void *opaque)
44 static int ll_test_inode(struct inode *inode, void *opaque)
47 static int last_ino, last_gen, last_count;
48 struct lustre_md *md = opaque;
50 if (!(md->body->valid & (OBD_MD_FLGENER | OBD_MD_FLID))) {
51 CERROR("MDS body missing inum or generation\n");
55 if (last_ino == md->body->ino && last_gen == md->body->generation &&
60 CDEBUG(D_VFSTRACE, "compared %u/%u %u times\n",
61 last_ino, last_gen, last_count);
63 last_ino = md->body->ino;
64 last_gen = md->body->generation;
66 "comparing inode %p ino %lu/%u to body "LPU64"/%u\n",
67 inode, inode->i_ino, inode->i_generation,
68 md->body->ino, md->body->generation);
71 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
72 if (inode->i_ino != md->body->ino)
75 if (inode->i_generation != md->body->generation) {
76 struct ll_sb_info *sbi = ll_i2sbi(inode);
77 struct ll_inode_info *lli = ll_i2info(inode);
79 if (inode->i_state & (I_FREEING | I_CLEAR))
81 if (inode->i_nlink == 0)
84 /* add "duplicate" inode into deathrow for destroy */
85 spin_lock(&sbi->ll_deathrow_lock);
86 if (list_empty(&lli->lli_dead_list)) {
87 atomic_inc(&inode->i_count);
88 list_add(&lli->lli_dead_list, &sbi->ll_deathrow);
90 spin_unlock(&sbi->ll_deathrow_lock);
95 /* Apply the attributes in 'opaque' to this inode */
96 if (!(inode->i_state & (I_FREEING | I_CLEAR)))
97 ll_update_inode(inode, md);
101 extern struct dentry_operations ll_d_ops;
103 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
107 ldlm_lock_decref(lockh, mode);
112 /* Get an inode by inode number (already instantiated by the intent lookup).
113 * Returns inode or NULL
115 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
116 int ll_set_inode(struct inode *inode, void *opaque)
118 ll_read_inode2(inode, opaque);
122 struct inode *ll_iget(struct super_block *sb, ino_t hash,
123 struct lustre_md *md)
128 inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
131 if (inode->i_state & I_NEW)
132 unlock_new_inode(inode);
133 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n", inode->i_ino,
134 inode->i_generation, inode);
140 struct inode *ll_iget(struct super_block *sb, ino_t hash,
141 struct lustre_md *md)
145 inode = iget4(sb, hash, ll_test_inode, md);
147 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n", inode->i_ino,
148 inode->i_generation, inode);
153 int ll_mdc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
154 void *data, int flag)
157 struct lustre_handle lockh;
161 case LDLM_CB_BLOCKING:
162 ldlm_lock2handle(lock, &lockh);
163 rc = ldlm_cli_cancel(&lockh);
165 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
169 case LDLM_CB_CANCELING: {
170 struct inode *inode = ll_inode_from_lock(lock);
171 __u64 bits = lock->l_policy_data.l_inodebits.bits;
173 /* Invalidate all dentries associated with this inode */
177 if (lock->l_resource->lr_name.name[0] != inode->i_ino ||
178 lock->l_resource->lr_name.name[1] != inode->i_generation) {
179 LDLM_ERROR(lock, "data mismatch with ino %lu/%u (%p)",
180 inode->i_ino, inode->i_generation, inode);
183 if (bits & MDS_INODELOCK_OPEN) {
185 switch (lock->l_req_mode) {
192 CERROR("open PR lock without FMODE_EXEC\n");
198 CERROR("Unexpected lock mode for OPEN lock "
199 "%d, inode %ld\n", lock->l_req_mode,
202 ll_mdc_real_close(inode, flags);
205 if (bits & MDS_INODELOCK_UPDATE)
206 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK,
207 &(ll_i2info(inode)->lli_flags));
209 if (S_ISDIR(inode->i_mode) &&
210 (bits & MDS_INODELOCK_UPDATE)) {
211 struct dentry *dentry, *tmp, *dir;
212 struct list_head *list;
214 CDEBUG(D_INODE, "invalidating inode %lu\n",
216 truncate_inode_pages(inode->i_mapping, 0);
218 /* Drop possible cached negative dentries */
219 list = &inode->i_dentry;
221 spin_lock(&dcache_lock);
223 /* It is possible to have several dentries (with
225 while ((list = list->next) != &inode->i_dentry) {
226 dir = list_entry(list, struct dentry, d_alias);
227 #ifdef LUSTRE_KERNEL_VERSION
228 if (!(dir->d_flags & DCACHE_LUSTRE_INVALID))
230 if (!d_unhashed(dir))
239 list_for_each_entry_safe(dentry, tmp,
243 /* XXX Print some debug here? */
244 if (!dentry->d_inode)
245 /* Negative dentry. If we were
246 dropping dcache lock, go
247 throught the list again */
248 if (ll_drop_dentry(dentry))
252 spin_unlock(&dcache_lock);
255 if (inode->i_sb->s_root &&
256 inode != inode->i_sb->s_root->d_inode &&
257 (bits & MDS_INODELOCK_LOOKUP))
258 ll_unhash_aliases(inode);
269 int ll_mdc_cancel_unused(struct lustre_handle *conn, struct inode *inode,
270 int flags, void *opaque)
272 struct ldlm_res_id res_id =
273 { .name = {inode->i_ino, inode->i_generation} };
274 struct obd_device *obddev = class_conn2obd(conn);
277 RETURN(ldlm_cli_cancel_unused(obddev->obd_namespace, &res_id, flags,
281 /* Pack the required supplementary groups into the supplied groups array.
282 * If we don't need to use the groups from the target inode(s) then we
283 * instead pack one or more groups from the user's supplementary group
284 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
285 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
290 LASSERT(suppgids != NULL);
292 if (in_group_p(i1->i_gid))
293 suppgids[0] = i1->i_gid;
298 if (in_group_p(i2->i_gid))
299 suppgids[1] = i2->i_gid;
306 for (i = 0; i < current_ngroups; i++) {
307 if (suppgids[0] == -1) {
308 if (current_groups[i] != suppgids[1])
309 suppgids[0] = current_groups[i];
312 if (suppgids[1] == -1) {
313 if (current_groups[i] != suppgids[0])
314 suppgids[1] = current_groups[i];
321 int ll_prepare_mdc_op_data(struct mdc_op_data *data, struct inode *i1,
322 struct inode *i2, const char *name, int namelen,
327 if (namelen > ll_i2sbi(i1)->ll_namelen)
328 return -ENAMETOOLONG;
329 ll_i2gids(data->suppgids, i1, i2);
330 ll_inode2fid(&data->fid1, i1);
333 ll_inode2fid(&data->fid2, i2);
335 memset(&data->fid2, 0, sizeof(data->fid2));
338 data->namelen = namelen;
339 data->create_mode = mode;
340 data->mod_time = CURRENT_SECONDS;
345 static void ll_d_add(struct dentry *de, struct inode *inode)
347 CDEBUG(D_DENTRY, "adding inode %p to dentry %p\n", inode, de);
349 if (!list_empty(&de->d_alias)) {
350 spin_unlock(&dcache_lock);
351 CERROR("dentry %.*s %p alias next %p, prev %p\n",
352 de->d_name.len, de->d_name.name, de,
353 de->d_alias.next, de->d_alias.prev);
357 list_add(&de->d_alias, &inode->i_dentry);
361 if (!d_unhashed(de)) {
362 spin_unlock(&dcache_lock);
363 CERROR("dentry %.*s %p hash next %p\n",
364 de->d_name.len, de->d_name.name, de, de->d_hash.next);
370 /* Search "inode"'s alias list for a dentry that has the same name and parent
371 * as de. If found, return it. If not found, return de.
372 * Lustre can't use d_add_unique because don't unhash aliases for directory
373 * in ll_revalidate_it. After revaliadate inode will be have hashed aliases
374 * and it triggers BUG_ON in d_instantiate_unique (bug #10954).
376 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
378 struct list_head *tmp;
379 struct dentry *dentry;
380 struct dentry *last_discon = NULL;
382 spin_lock(&dcache_lock);
383 list_for_each(tmp, &inode->i_dentry) {
384 dentry = list_entry(tmp, struct dentry, d_alias);
386 /* We are called here with 'de' already on the aliases list. */
387 if (unlikely(dentry == de)) {
392 if (dentry->d_flags & DCACHE_DISCONNECTED) {
393 LASSERT(last_discon == NULL);
394 last_discon = dentry;
398 if (dentry->d_parent != de->d_parent)
401 if (dentry->d_name.hash != de->d_name.hash)
404 if (dentry->d_name.len != de->d_name.len)
407 if (memcmp(dentry->d_name.name, de->d_name.name,
408 de->d_name.len) != 0)
414 #ifdef LUSTRE_KERNEL_VERSION
415 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
417 unlock_dentry(dentry);
418 __d_rehash(dentry, 0); /* avoid taking dcache_lock inside */
419 spin_unlock(&dcache_lock);
421 CDEBUG(D_DENTRY, "alias dentry %.*s (%p) parent %p inode %p "
422 "refc %d\n", de->d_name.len, de->d_name.name, de,
423 de->d_parent, de->d_inode, atomic_read(&de->d_count));
427 CDEBUG(D_DENTRY, "Reuse disconnected dentry %p inode %p "
428 "refc %d\n", last_discon, last_discon->d_inode,
429 atomic_read(&last_discon->d_count));
430 dget_locked(last_discon);
431 spin_unlock(&dcache_lock);
433 d_move(last_discon, de);
440 spin_unlock(&dcache_lock);
445 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
446 struct lookup_intent *it, void *data)
448 struct it_cb_data *icbd = data;
449 struct dentry **de = icbd->icbd_childp;
450 struct inode *parent = icbd->icbd_parent;
451 struct ll_sb_info *sbi = ll_i2sbi(parent);
452 struct inode *inode = NULL;
455 /* NB 1 request reference will be taken away by ll_intent_lock()
457 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
460 rc = ll_prep_inode(sbi->ll_osc_exp, &inode, request, offset,
465 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
466 inode, inode->i_ino, inode->i_generation);
467 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
469 /* We used to query real size from OSTs here, but actually
470 this is not needed. For stat() calls size would be updated
471 from subsequent do_revalidate()->ll_inode_revalidate_it() in
473 vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
474 Everybody else who needs correct file size would call
475 ll_glimpse_size or some equivalent themselves anyway.
476 Also see bug 7198. */
477 *de = ll_find_alias(inode, *de);
480 /* Check that parent has UPDATE lock. If there is none, we
481 cannot afford to hash this dentry (done by ll_d_add) as it
482 might get picked up later when UPDATE lock will appear */
483 if (ll_have_md_lock(parent, MDS_INODELOCK_UPDATE)) {
484 spin_lock(&dcache_lock);
485 ll_d_add(*de, inode);
486 spin_unlock(&dcache_lock);
488 /* We do not want to hash the dentry if don`t have a
489 * lock, but if this dentry is later used in d_move,
490 * we'd hit uninitialised list head d_hash, so we just
491 * do this to init d_hash field but leave dentry
492 * unhashed. (bug 10796). */
499 (*de)->d_op = &ll_d_ops;
504 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
505 struct lookup_intent *it, int lookup_flags)
507 struct dentry *save = dentry, *retval;
508 struct mdc_op_data op_data;
509 struct it_cb_data icbd;
510 struct ptlrpc_request *req = NULL;
511 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
515 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
516 dentry->d_name.len, dentry->d_name.name, parent->i_ino,
517 parent->i_generation, parent, LL_IT2STR(it));
519 if (d_mountpoint(dentry))
520 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
522 ll_frob_intent(&it, &lookup_it);
524 /* As do_lookup is called before follow_mount, root dentry may be left
525 * not valid, revalidate it here. */
526 if (parent->i_sb->s_root && (parent->i_sb->s_root->d_inode == parent) &&
527 (it->it_op & (IT_OPEN | IT_CREAT))) {
528 rc = ll_inode_revalidate_it(parent->i_sb->s_root, it);
533 icbd.icbd_childp = &dentry;
534 icbd.icbd_parent = parent;
536 rc = ll_prepare_mdc_op_data(&op_data, parent, NULL, dentry->d_name.name,
537 dentry->d_name.len, lookup_flags);
541 it->it_create_mode &= ~current->fs->umask;
543 rc = mdc_intent_lock(ll_i2mdcexp(parent), &op_data, NULL, 0, it,
544 lookup_flags, &req, ll_mdc_blocking_ast, 0);
547 GOTO(out, retval = ERR_PTR(rc));
549 rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
551 ll_intent_release(it);
552 GOTO(out, retval = ERR_PTR(rc));
555 if ((it->it_op & IT_OPEN) && dentry->d_inode &&
556 !S_ISREG(dentry->d_inode->i_mode) &&
557 !S_ISDIR(dentry->d_inode->i_mode)) {
558 ll_release_openhandle(dentry, it);
560 ll_lookup_finish_locks(it, dentry);
563 GOTO(out, retval = NULL);
565 GOTO(out, retval = dentry);
568 ptlrpc_req_finished(req);
572 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
573 #ifdef LUSTRE_KERNEL_VERSION
574 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
575 struct nameidata *nd)
580 if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
581 de = ll_lookup_it(parent, dentry, &nd->intent, nd->flags);
583 de = ll_lookup_it(parent, dentry, NULL, 0);
588 struct lookup_intent *ll_convert_intent(struct open_intent *oit,
591 struct lookup_intent *it;
593 OBD_ALLOC(it, sizeof(*it));
595 return ERR_PTR(-ENOMEM);
597 if (lookup_flags & LOOKUP_OPEN) {
599 if (lookup_flags & LOOKUP_CREATE)
600 it->it_op |= IT_CREAT;
601 it->it_create_mode = oit->create_mode;
602 it->it_flags = oit->flags;
604 it->it_op = IT_GETATTR;
607 #ifndef HAVE_FILE_IN_STRUCT_INTENT
608 /* Since there is no way to pass our intent to ll_file_open,
609 * just check the file is there. Actual open will be done
611 if (it->it_op & IT_OPEN)
612 it->it_op = IT_LOOKUP;
618 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
619 struct nameidata *nd)
624 if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
625 struct lookup_intent *it;
627 #if defined(HAVE_FILE_IN_STRUCT_INTENT) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
628 /* Did we came here from failed revalidate just to propagate
630 if (nd->flags & LOOKUP_OPEN)
631 if (IS_ERR(nd->intent.open.file))
632 RETURN((struct dentry *)nd->intent.open.file);
635 if (ll_d2d(dentry) && ll_d2d(dentry)->lld_it) {
636 it = ll_d2d(dentry)->lld_it;
637 ll_d2d(dentry)->lld_it = NULL;
639 it = ll_convert_intent(&nd->intent.open, nd->flags);
641 RETURN((struct dentry *)it);
644 de = ll_lookup_it(parent, dentry, it, nd->flags);
647 if ((nd->flags & LOOKUP_OPEN) && !IS_ERR(dentry)) { /* Open */
648 if (dentry->d_inode &&
649 it_disposition(it, DISP_OPEN_OPEN)) { /* nocreate */
650 #ifdef HAVE_FILE_IN_STRUCT_INTENT
651 if (S_ISFIFO(dentry->d_inode->i_mode)) {
652 // We cannot call open here as it would
655 (struct ptlrpc_request *)
656 it->d.lustre.it_data);
659 nd->intent.open.file->private_data = it;
660 filp =lookup_instantiate_filp(nd,dentry,
662 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
663 /* 2.6.1[456] have a bug in open_namei() that forgets to check
664 * nd->intent.open.file for error, so we need to return it as lookup's result
669 de = (struct dentry *) filp;
674 #else /* HAVE_FILE_IN_STRUCT_INTENT */
675 /* Release open handle as we have no way to
676 * pass it to ll_file_open */
677 ll_release_openhandle(dentry, it);
678 #endif /* HAVE_FILE_IN_STRUCT_INTENT */
679 } else if (it_disposition(it, DISP_OPEN_CREATE)) {
680 // XXX This can only reliably work on assumption
681 // that there are NO hashed negative dentries.
682 ll_d2d(dentry)->lld_it = it;
683 it = NULL; /* Will be freed in ll_create_nd */
684 /* We absolutely depend on ll_create_nd to be
685 * called to not leak this intent and possible
686 * data attached to it */
691 ll_intent_release(it);
692 OBD_FREE(it, sizeof(*it));
695 de = ll_lookup_it(parent, dentry, NULL, 0);
703 /* We depend on "mode" being set with the proper file type/umask by now */
704 static struct inode *ll_create_node(struct inode *dir, const char *name,
705 int namelen, const void *data, int datalen,
706 int mode, __u64 extra,
707 struct lookup_intent *it)
709 struct inode *inode = NULL;
710 struct ptlrpc_request *request = NULL;
711 struct ll_sb_info *sbi = ll_i2sbi(dir);
715 LASSERT(it && it->d.lustre.it_disposition);
717 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
718 request = it->d.lustre.it_data;
719 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
720 rc = ll_prep_inode(sbi->ll_osc_exp, &inode, request, DLM_REPLY_REC_OFF,
723 GOTO(out, inode = ERR_PTR(rc));
725 LASSERT(list_empty(&inode->i_dentry));
727 /* We asked for a lock on the directory, but were granted a
728 * lock on the inode. Since we finally have an inode pointer,
729 * stuff it in the lock. */
730 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode %p (%lu/%u)\n",
731 inode, inode->i_ino, inode->i_generation);
732 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
735 ptlrpc_req_finished(request);
740 * By the time this is called, we already have created the directory cache
741 * entry for the new file, but it is so far negative - it has no inode.
743 * We defer creating the OBD object(s) until open, to keep the intent and
744 * non-intent code paths similar, and also because we do not have the MDS
745 * inode number before calling ll_create_node() (which is needed for LOV),
746 * so we would need to do yet another RPC to the MDS to store the LOV EA
747 * data on the MDS. If needed, we would pass the PACKED lmm as data and
748 * lmm_size in datalen (the MDS still has code which will handle that).
750 * If the create succeeds, we fill in the inode information
751 * with d_instantiate().
753 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
754 struct lookup_intent *it)
757 struct ptlrpc_request *request = it->d.lustre.it_data;
761 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
762 dentry->d_name.len, dentry->d_name.name, dir->i_ino,
763 dir->i_generation, dir, LL_IT2STR(it));
765 rc = it_open_error(DISP_OPEN_CREATE, it);
769 mdc_store_inode_generation(request, DLM_INTENT_REC_OFF,
771 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
772 NULL, 0, mode, 0, it);
774 RETURN(PTR_ERR(inode));
777 d_instantiate(dentry, inode);
778 /* Negative dentry may be unhashed if parent does not have UPDATE lock,
779 * but some callers, e.g. do_coredump, expect dentry to be hashed after
780 * successful create. Hash it here. */
781 spin_lock(&dcache_lock);
782 if (d_unhashed(dentry))
783 __d_rehash(dentry, 0);
784 spin_unlock(&dcache_lock);
788 static void ll_update_times(struct ptlrpc_request *request, int offset,
791 struct mds_body *body = lustre_msg_buf(request->rq_repmsg, offset,
795 /* mtime is always updated with ctime, but can be set in past.
796 As write and utime(2) may happen within 1 second, and utime's
797 mtime has a priority over write's one, so take mtime from mds
798 for the same ctimes. */
799 if (body->valid & OBD_MD_FLCTIME &&
800 body->ctime >= LTIME_S(inode->i_ctime)) {
801 LTIME_S(inode->i_ctime) = body->ctime;
803 if (body->valid & OBD_MD_FLMTIME) {
804 CDEBUG(D_INODE, "setting ino %lu mtime from %lu "
805 "to "LPU64"\n", inode->i_ino,
806 LTIME_S(inode->i_mtime), body->mtime);
807 LTIME_S(inode->i_mtime) = body->mtime;
812 static int ll_new_node(struct inode *dir, struct qstr *name,
813 const char *tgt, int mode,
814 int rdev, struct dentry *dchild)
816 struct ptlrpc_request *request = NULL;
817 struct inode *inode = NULL;
818 struct ll_sb_info *sbi = ll_i2sbi(dir);
819 struct mdc_op_data op_data;
824 if (unlikely(tgt != NULL))
825 tgt_len = strlen(tgt)+1;
827 err = ll_prepare_mdc_op_data(&op_data, dir, NULL, name->name,
832 err = mdc_create(sbi->ll_mdc_exp, &op_data, tgt, tgt_len,
833 mode, current->fsuid, current->fsgid,
834 current->cap_effective, rdev, &request);
838 ll_update_times(request, REPLY_REC_OFF, dir);
841 err = ll_prep_inode(sbi->ll_osc_exp, &inode, request,
842 REPLY_REC_OFF, dchild->d_sb);
847 d_instantiate(dchild, inode);
851 ptlrpc_req_finished(request);
857 static int ll_mknod_generic(struct inode *dir, struct qstr *name, int mode,
858 unsigned rdev, struct dentry *dchild)
863 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p) mode %o dev %x\n",
864 name->len, name->name, dir->i_ino, dir->i_generation, dir,
867 mode &= ~current->fs->umask;
869 switch (mode & S_IFMT) {
871 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
877 err = ll_new_node(dir, name, NULL, mode, rdev, dchild);
888 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
889 #ifndef LUSTRE_KERNEL_VERSION
890 static int ll_create_nd(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
892 struct lookup_intent *it = ll_d2d(dentry)->lld_it;
896 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
898 ll_d2d(dentry)->lld_it = NULL;
900 /* Was there an error? Propagate it! */
901 if (it->d.lustre.it_status) {
902 rc = it->d.lustre.it_status;
906 rc = ll_create_it(dir, dentry, mode, it);
907 #ifdef HAVE_FILE_IN_STRUCT_INTENT
908 if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
909 nd->intent.open.file->private_data = it;
910 lookup_instantiate_filp(nd, dentry, NULL);
913 ll_release_openhandle(dentry,it);
917 ll_intent_release(it);
918 OBD_FREE(it, sizeof(*it));
923 static int ll_create_nd(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
926 if (!nd || !nd->intent.d.lustre.it_disposition)
927 /* No saved request? Just mknod the file */
928 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
930 return ll_create_it(dir, dentry, mode, &nd->intent);
935 static int ll_symlink_generic(struct inode *dir, struct qstr *name,
936 const char *tgt, struct dentry *dchild)
941 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),target=%.*s\n",
942 name->len, name->name, dir->i_ino, dir->i_generation,
945 err = ll_new_node(dir, name, (char *)tgt, S_IFLNK | S_IRWXUGO,
950 static int ll_link_generic(struct inode *src, struct inode *dir,
951 struct qstr *name, struct dentry *dchild)
953 struct ptlrpc_request *request = NULL;
954 struct mdc_op_data op_data;
956 struct ll_sb_info *sbi = ll_i2sbi(dir);
960 "VFS Op: inode=%lu/%u(%p), dir=%lu/%u(%p), target=%.*s\n",
961 src->i_ino, src->i_generation, src, dir->i_ino,
962 dir->i_generation, dir, name->len, name->name);
964 err = ll_prepare_mdc_op_data(&op_data, src, dir, name->name,
968 err = mdc_link(sbi->ll_mdc_exp, &op_data, &request);
975 ll_update_times(request, REPLY_REC_OFF, dir);
979 ptlrpc_req_finished(request);
983 static int ll_mkdir_generic(struct inode *dir, struct qstr *name, int mode,
984 struct dentry *dchild)
990 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
991 name->len, name->name, dir->i_ino, dir->i_generation, dir);
993 mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
994 err = ll_new_node(dir, name, NULL, mode, 0, dchild);
999 static int ll_rmdir_generic(struct inode *dir, struct dentry *dparent,
1002 struct ptlrpc_request *request = NULL;
1003 struct mdc_op_data op_data;
1004 struct dentry *dentry;
1007 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1008 name->len, name->name, dir->i_ino, dir->i_generation, dir);
1010 /* Check if we have something mounted at the dir we are going to delete
1011 * In such a case there would always be dentry present. */
1013 dentry = d_lookup(dparent, name);
1015 int mounted = d_mountpoint(dentry);
1018 GOTO(out, rc = -EBUSY);
1022 rc = ll_prepare_mdc_op_data(&op_data, dir, NULL, name->name,
1023 name->len, S_IFDIR);
1026 rc = mdc_unlink(ll_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
1029 ll_update_times(request, REPLY_REC_OFF, dir);
1033 ptlrpc_req_finished(request);
1037 int ll_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
1039 struct mds_body *body;
1040 struct lov_mds_md *eadata;
1041 struct lov_stripe_md *lsm = NULL;
1042 struct obd_trans_info oti = { 0 };
1047 /* req is swabbed so this is safe */
1048 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
1050 if (!(body->valid & OBD_MD_FLEASIZE))
1053 if (body->eadatasize == 0) {
1054 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1055 GOTO(out, rc = -EPROTO);
1058 /* The MDS sent back the EA because we unlinked the last reference
1059 * to this file. Use this EA to unlink the objects on the OST.
1060 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
1061 * check it is complete and sensible. */
1062 eadata = lustre_swab_repbuf(request, REPLY_REC_OFF + 1,
1063 body->eadatasize, NULL);
1064 LASSERT(eadata != NULL);
1065 if (eadata == NULL) {
1066 CERROR("Can't unpack MDS EA data\n");
1067 GOTO(out, rc = -EPROTO);
1070 rc = obd_unpackmd(ll_i2obdexp(dir), &lsm, eadata, body->eadatasize);
1072 CERROR("obd_unpackmd: %d\n", rc);
1075 LASSERT(rc >= sizeof(*lsm));
1077 rc = obd_checkmd(ll_i2obdexp(dir), ll_i2mdcexp(dir), lsm);
1079 GOTO(out_free_memmd, rc);
1083 GOTO(out_free_memmd, rc = -ENOMEM);
1085 oa->o_id = lsm->lsm_object_id;
1086 oa->o_mode = body->mode & S_IFMT;
1087 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
1089 if (body->valid & OBD_MD_FLCOOKIE) {
1090 oa->o_valid |= OBD_MD_FLCOOKIE;
1091 oti.oti_logcookies =
1092 lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF + 2,
1093 sizeof(struct llog_cookie) *
1094 lsm->lsm_stripe_count);
1095 if (oti.oti_logcookies == NULL) {
1096 oa->o_valid &= ~OBD_MD_FLCOOKIE;
1097 body->valid &= ~OBD_MD_FLCOOKIE;
1101 rc = obd_destroy(ll_i2obdexp(dir), oa, lsm, &oti, ll_i2mdcexp(dir));
1104 CERROR("obd destroy objid "LPX64" error %d\n",
1105 lsm->lsm_object_id, rc);
1107 obd_free_memmd(ll_i2obdexp(dir), &lsm);
1112 static int ll_unlink_generic(struct inode * dir, struct qstr *name)
1114 struct ptlrpc_request *request = NULL;
1115 struct mdc_op_data op_data;
1119 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1120 name->len, name->name, dir->i_ino, dir->i_generation, dir);
1122 rc = ll_prepare_mdc_op_data(&op_data, dir, NULL, name->name,
1126 rc = mdc_unlink(ll_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
1130 ll_update_times(request, REPLY_REC_OFF, dir);
1132 rc = ll_objects_destroy(request, dir);
1137 ptlrpc_req_finished(request);
1141 static int ll_rename_generic(struct inode *src, struct qstr *src_name,
1142 struct inode *tgt, struct qstr *tgt_name)
1144 struct ptlrpc_request *request = NULL;
1145 struct ll_sb_info *sbi = ll_i2sbi(src);
1146 struct mdc_op_data op_data;
1150 CDEBUG(D_VFSTRACE,"VFS Op:oldname=%.*s,src_dir=%lu/%u(%p),newname=%.*s,"
1151 "tgt_dir=%lu/%u(%p)\n", src_name->len, src_name->name,
1152 src->i_ino, src->i_generation, src, tgt_name->len,
1153 tgt_name->name, tgt->i_ino, tgt->i_generation, tgt);
1155 err = ll_prepare_mdc_op_data(&op_data, src, tgt, NULL, 0, 0);
1158 err = mdc_rename(sbi->ll_mdc_exp, &op_data,
1159 src_name->name, src_name->len,
1160 tgt_name->name, tgt_name->len, &request);
1163 ll_update_times(request, REPLY_REC_OFF, src);
1164 ll_update_times(request, REPLY_REC_OFF, tgt);
1165 err = ll_objects_destroy(request, src);
1171 ptlrpc_req_finished(request);
1176 #ifdef LUSTRE_KERNEL_VERSION
1177 static int ll_mknod_raw(struct nameidata *nd, int mode, dev_t rdev)
1179 return ll_mknod_generic(nd->dentry->d_inode, &nd->last, mode,rdev,NULL);
1181 static int ll_rename_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1183 return ll_rename_generic(srcnd->dentry->d_inode, &srcnd->last,
1184 tgtnd->dentry->d_inode, &tgtnd->last);
1186 static int ll_link_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1188 return ll_link_generic(srcnd->dentry->d_inode, tgtnd->dentry->d_inode,
1189 &tgtnd->last, NULL);
1191 static int ll_symlink_raw(struct nameidata *nd, const char *tgt)
1193 return ll_symlink_generic(nd->dentry->d_inode, &nd->last, tgt, NULL);
1195 static int ll_rmdir_raw(struct nameidata *nd)
1197 return ll_rmdir_generic(nd->dentry->d_inode, nd->dentry, &nd->last);
1199 static int ll_mkdir_raw(struct nameidata *nd, int mode)
1201 return ll_mkdir_generic(nd->dentry->d_inode, &nd->last, mode, NULL);
1203 static int ll_unlink_raw(struct nameidata *nd)
1205 return ll_unlink_generic(nd->dentry->d_inode, &nd->last);
1209 static int ll_mknod(struct inode *dir, struct dentry *dchild, int mode,
1212 return ll_mknod_generic(dir, &dchild->d_name, mode,
1213 old_encode_dev(rdev), dchild);
1216 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1217 static int ll_unlink(struct inode * dir, struct dentry *dentry)
1219 return ll_unlink_generic(dir, &dentry->d_name);
1221 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1223 return ll_mkdir_generic(dir, &dentry->d_name, mode, dentry);
1225 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
1227 return ll_rmdir_generic(dir, NULL, &dentry->d_name);
1229 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1230 const char *oldname)
1232 return ll_symlink_generic(dir, &dentry->d_name, oldname, dentry);
1234 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1235 struct dentry *new_dentry)
1237 return ll_link_generic(old_dentry->d_inode, dir,
1238 &new_dentry->d_name, new_dentry);
1240 static int ll_rename(struct inode *old_dir, struct dentry *old_dentry,
1241 struct inode *new_dir, struct dentry *new_dentry)
1243 return ll_rename_generic(old_dir, &old_dentry->d_name, new_dir,
1244 &new_dentry->d_name);
1248 struct inode_operations ll_dir_inode_operations = {
1249 #ifdef LUSTRE_KERNEL_VERSION
1250 .link_raw = ll_link_raw,
1251 .unlink_raw = ll_unlink_raw,
1252 .symlink_raw = ll_symlink_raw,
1253 .mkdir_raw = ll_mkdir_raw,
1254 .rmdir_raw = ll_rmdir_raw,
1255 .mknod_raw = ll_mknod_raw,
1256 .rename_raw = ll_rename_raw,
1257 .setattr = ll_setattr,
1258 .setattr_raw = ll_setattr_raw,
1261 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1262 .create_it = ll_create_it,
1263 .lookup_it = ll_lookup_it,
1264 .revalidate_it = ll_inode_revalidate_it,
1266 .lookup = ll_lookup_nd,
1267 .create = ll_create_nd,
1268 /* We need all these non-raw things for NFSD, to not patch it. */
1269 .unlink = ll_unlink,
1272 .symlink = ll_symlink,
1274 .rename = ll_rename,
1275 .setattr = ll_setattr,
1276 .getattr = ll_getattr,
1278 .permission = ll_inode_permission,
1279 .setxattr = ll_setxattr,
1280 .getxattr = ll_getxattr,
1281 .listxattr = ll_listxattr,
1282 .removexattr = ll_removexattr,
1285 struct inode_operations ll_special_inode_operations = {
1286 #ifdef LUSTRE_KERNEL_VERSION
1287 .setattr_raw = ll_setattr_raw,
1289 .setattr = ll_setattr,
1290 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1291 .getattr = ll_getattr,
1293 .revalidate_it = ll_inode_revalidate_it,
1295 .permission = ll_inode_permission,
1296 .setxattr = ll_setxattr,
1297 .getxattr = ll_getxattr,
1298 .listxattr = ll_listxattr,
1299 .removexattr = ll_removexattr,