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 #ifdef HAVE_EXPORT___IGET
77 if (inode->i_state & (I_FREEING | I_CLEAR))
79 if (inode->i_nlink == 0)
82 /* add "duplicate" inode into deathrow for destroy */
83 spin_lock(&ll_i2sbi(inode)->ll_deathrow_lock);
84 if (list_empty(&ll_i2info(inode)->lli_dead_list)) {
86 list_add(&ll_i2info(inode)->lli_dead_list,
87 &ll_i2sbi(inode)->ll_deathrow);
89 spin_unlock(&ll_i2sbi(inode)->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 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
105 ldlm_lock_decref(lockh, mode);
110 /* Get an inode by inode number (already instantiated by the intent lookup).
111 * Returns inode or NULL
113 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
114 int ll_set_inode(struct inode *inode, void *opaque)
116 ll_read_inode2(inode, opaque);
120 struct inode *ll_iget(struct super_block *sb, ino_t hash,
121 struct lustre_md *md)
126 inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
129 if (inode->i_state & I_NEW)
130 unlock_new_inode(inode);
131 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n", inode->i_ino,
132 inode->i_generation, inode);
138 struct inode *ll_iget(struct super_block *sb, ino_t hash,
139 struct lustre_md *md)
143 inode = iget4(sb, hash, ll_test_inode, md);
145 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n", inode->i_ino,
146 inode->i_generation, inode);
151 static void ll_drop_negative_dentry(struct inode *dir)
153 struct dentry *dentry, *tmp_alias, *tmp_subdir;
155 spin_lock(&dcache_lock);
157 list_for_each_entry_safe(dentry, tmp_alias,
158 &dir->i_dentry,d_alias) {
159 if (!list_empty(&dentry->d_subdirs)) {
160 struct dentry *child;
161 list_for_each_entry_safe(child, tmp_subdir,
164 /* XXX Print some debug here? */
166 /* Negative dentry. If we were
167 dropping dcache lock, go
168 throught the list again */
169 if (ll_drop_dentry(child))
174 spin_unlock(&dcache_lock);
177 int ll_mdc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
178 void *data, int flag)
181 struct lustre_handle lockh;
185 case LDLM_CB_BLOCKING:
186 ldlm_lock2handle(lock, &lockh);
187 rc = ldlm_cli_cancel(&lockh);
189 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
193 case LDLM_CB_CANCELING: {
194 struct inode *inode = ll_inode_from_lock(lock);
195 __u64 bits = lock->l_policy_data.l_inodebits.bits;
197 /* Invalidate all dentries associated with this inode */
201 LASSERT(lock->l_flags & LDLM_FL_CANCELING);
202 if ((bits & MDS_INODELOCK_LOOKUP) &&
203 ll_have_md_lock(inode, MDS_INODELOCK_LOOKUP))
204 bits &= ~MDS_INODELOCK_LOOKUP;
205 if ((bits & MDS_INODELOCK_UPDATE) &&
206 ll_have_md_lock(inode, MDS_INODELOCK_UPDATE))
207 bits &= ~MDS_INODELOCK_UPDATE;
208 if ((bits & MDS_INODELOCK_OPEN) &&
209 ll_have_md_lock(inode, MDS_INODELOCK_OPEN))
210 bits &= ~MDS_INODELOCK_OPEN;
212 if (lock->l_resource->lr_name.name[0] != inode->i_ino ||
213 lock->l_resource->lr_name.name[1] != inode->i_generation) {
214 LDLM_ERROR(lock, "data mismatch with ino %lu/%u (%p)",
215 inode->i_ino, inode->i_generation, inode);
218 if (bits & MDS_INODELOCK_OPEN) {
220 switch (lock->l_req_mode) {
227 CERROR("open PR lock without FMODE_EXEC\n");
233 CERROR("Unexpected lock mode for OPEN lock "
234 "%d, inode %ld\n", lock->l_req_mode,
237 ll_mdc_real_close(inode, flags);
240 if (bits & MDS_INODELOCK_UPDATE)
241 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK,
242 &(ll_i2info(inode)->lli_flags));
244 if (S_ISDIR(inode->i_mode) &&
245 (bits & MDS_INODELOCK_UPDATE)) {
246 CDEBUG(D_INODE, "invalidating inode %lu\n",
248 truncate_inode_pages(inode->i_mapping, 0);
249 ll_drop_negative_dentry(inode);
250 inode->i_version++; /* XXX: remove with inode version*/
253 if (inode->i_sb->s_root &&
254 inode != inode->i_sb->s_root->d_inode &&
255 (bits & MDS_INODELOCK_LOOKUP))
256 ll_unhash_aliases(inode);
267 int ll_mdc_cancel_unused(struct lustre_handle *conn, struct inode *inode,
268 int flags, void *opaque)
270 struct ldlm_res_id res_id =
271 { .name = {inode->i_ino, inode->i_generation} };
272 struct obd_device *obddev = class_conn2obd(conn);
275 RETURN(ldlm_cli_cancel_unused(obddev->obd_namespace, &res_id, flags,
279 /* Pack the required supplementary groups into the supplied groups array.
280 * If we don't need to use the groups from the target inode(s) then we
281 * instead pack one or more groups from the user's supplementary group
282 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
283 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
288 LASSERT(suppgids != NULL);
290 if (in_group_p(i1->i_gid))
291 suppgids[0] = i1->i_gid;
296 if (in_group_p(i2->i_gid))
297 suppgids[1] = i2->i_gid;
304 for (i = 0; i < current_ngroups; i++) {
305 if (suppgids[0] == -1) {
306 if (current_groups[i] != suppgids[1])
307 suppgids[0] = current_groups[i];
310 if (suppgids[1] == -1) {
311 if (current_groups[i] != suppgids[0])
312 suppgids[1] = current_groups[i];
319 int ll_prepare_mdc_op_data(struct mdc_op_data *op_data, struct inode *i1,
320 struct inode *i2, const char *name, int namelen,
321 int mode, void *data)
325 if (namelen > ll_i2sbi(i1)->ll_namelen)
326 return -ENAMETOOLONG;
327 ll_i2gids(op_data->suppgids, i1, i2);
328 ll_inode2fid(&op_data->fid1, i1);
331 ll_inode2fid(&op_data->fid2, i2);
333 memset(&op_data->fid2, 0, sizeof(op_data->fid2));
335 op_data->name = name;
336 op_data->namelen = namelen;
337 op_data->create_mode = mode;
338 op_data->mod_time = CURRENT_SECONDS;
339 op_data->data = data;
344 static void ll_d_add(struct dentry *de, struct inode *inode)
346 CDEBUG(D_DENTRY, "adding inode %p to dentry %p\n", inode, de);
348 if (!list_empty(&de->d_alias)) {
349 spin_unlock(&dcache_lock);
350 CERROR("dentry %.*s %p alias next %p, prev %p\n",
351 de->d_name.len, de->d_name.name, de,
352 de->d_alias.next, de->d_alias.prev);
356 list_add(&de->d_alias, &inode->i_dentry);
360 if (!d_unhashed(de)) {
361 spin_unlock(&dcache_lock);
362 CERROR("dentry %.*s %p hash next %p\n",
363 de->d_name.len, de->d_name.name, de, de->d_hash.next);
366 d_rehash_cond(de, 0);
369 /* Search "inode"'s alias list for a dentry that has the same name and parent
370 * as de. If found, return it. If not found, return de.
371 * Lustre can't use d_add_unique because don't unhash aliases for directory
372 * in ll_revalidate_it. After revaliadate inode will be have hashed aliases
373 * and it triggers BUG_ON in d_instantiate_unique (bug #10954).
375 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
377 struct list_head *tmp;
378 struct dentry *dentry;
379 struct dentry *last_discon = NULL;
381 spin_lock(&dcache_lock);
382 list_for_each(tmp, &inode->i_dentry) {
383 dentry = list_entry(tmp, struct dentry, d_alias);
385 /* We are called here with 'de' already on the aliases list. */
386 if (unlikely(dentry == de)) {
391 if (dentry->d_flags & DCACHE_DISCONNECTED) {
392 LASSERT(last_discon == NULL);
393 last_discon = dentry;
397 if (dentry->d_parent != de->d_parent)
400 if (dentry->d_name.hash != de->d_name.hash)
403 if (dentry->d_name.len != de->d_name.len)
406 if (memcmp(dentry->d_name.name, de->d_name.name,
407 de->d_name.len) != 0)
413 #ifdef DCACHE_LUSTRE_INVALID
414 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
416 unlock_dentry(dentry);
417 d_rehash_cond(dentry, 0); /* avoid taking dcache_lock inside */
418 spin_unlock(&dcache_lock);
420 CDEBUG(D_DENTRY, "alias dentry %.*s (%p) parent %p inode %p "
421 "refc %d\n", de->d_name.len, de->d_name.name, de,
422 de->d_parent, de->d_inode, atomic_read(&de->d_count));
426 CDEBUG(D_DENTRY, "Reuse disconnected dentry %p inode %p "
427 "refc %d\n", last_discon, last_discon->d_inode,
428 atomic_read(&last_discon->d_count));
429 dget_locked(last_discon);
430 spin_unlock(&dcache_lock);
432 d_move(last_discon, de);
439 spin_unlock(&dcache_lock);
444 int lookup_it_finish(struct ptlrpc_request *request, int offset,
445 struct lookup_intent *it, void *data)
447 struct it_cb_data *icbd = data;
448 struct dentry **de = icbd->icbd_childp;
449 struct inode *parent = icbd->icbd_parent;
450 struct ll_sb_info *sbi = ll_i2sbi(parent);
451 struct inode *inode = NULL;
456 if (likely((*de)->d_op != &ll_d_ops)) {
457 (*de)->d_op = &ll_init_d_ops;
462 /* NB 1 request reference will be taken away by ll_intent_lock()
464 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
465 struct dentry *save = *de;
467 rc = ll_prep_inode(sbi->ll_osc_exp, &inode, request, offset,
472 if (likely((*de)->d_op == &ll_init_d_ops)) {
473 (*de)->d_op = &ll_fini_d_ops;
484 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
485 inode, inode->i_ino, inode->i_generation);
486 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
488 /* We used to query real size from OSTs here, but actually
489 this is not needed. For stat() calls size would be updated
490 from subsequent do_revalidate()->ll_inode_revalidate_it() in
492 vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
493 Everybody else who needs correct file size would call
494 ll_glimpse_size or some equivalent themselves anyway.
495 Also see bug 7198. */
496 *de = ll_find_alias(inode, *de);
497 if (set && *de != save) {
499 if (likely(save->d_op == &ll_init_d_ops)) {
500 save->d_op = &ll_fini_d_ops;
509 /* Check that parent has UPDATE lock. If there is none, we
510 cannot afford to hash this dentry (done by ll_d_add) as it
511 might get picked up later when UPDATE lock will appear */
512 if (ll_have_md_lock(parent, MDS_INODELOCK_UPDATE)) {
513 spin_lock(&dcache_lock);
514 ll_d_add(*de, inode);
515 spin_unlock(&dcache_lock);
517 /* We do not want to hash the dentry if don`t have a
518 * lock, but if this dentry is later used in d_move,
519 * we'd hit uninitialised list head d_hash, so we just
520 * do this to init d_hash field but leave dentry
521 * unhashed. (bug 10796). */
530 if (likely((*de)->d_op == &ll_init_d_ops)) {
531 (*de)->d_op = &ll_d_ops;
536 (*de)->d_op = &ll_d_ops;
543 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
544 struct lookup_intent *it, int lookup_flags)
546 struct dentry *save = dentry, *retval;
547 struct mdc_op_data op_data;
548 struct it_cb_data icbd;
549 struct ptlrpc_request *req = NULL;
550 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
554 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
555 dentry->d_name.len, dentry->d_name.name, parent->i_ino,
556 parent->i_generation, parent, LL_IT2STR(it));
558 if (d_mountpoint(dentry))
559 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
561 ll_frob_intent(&it, &lookup_it);
563 /* As do_lookup is called before follow_mount, root dentry may be left
564 * not valid, revalidate it here. */
565 if (parent->i_sb->s_root && (parent->i_sb->s_root->d_inode == parent) &&
566 (it->it_op & (IT_OPEN | IT_CREAT))) {
567 rc = ll_inode_revalidate_it(parent->i_sb->s_root, it);
572 if (it->it_op == IT_GETATTR) {
573 rc = ll_statahead_enter(parent, &dentry, 1);
575 ll_statahead_exit(dentry, rc);
577 RETURN(retval = dentry);
581 icbd.icbd_parent = parent;
582 icbd.icbd_childp = &dentry;
584 rc = ll_prepare_mdc_op_data(&op_data, parent, NULL, dentry->d_name.name,
585 dentry->d_name.len, lookup_flags, NULL);
589 it->it_create_mode &= ~current->fs->umask;
591 rc = mdc_intent_lock(ll_i2mdcexp(parent), &op_data, NULL, 0, it,
592 lookup_flags, &req, ll_mdc_blocking_ast, 0);
595 GOTO(out, retval = ERR_PTR(rc));
597 rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
599 ll_intent_release(it);
600 GOTO(out, retval = ERR_PTR(rc));
603 if ((it->it_op & IT_OPEN) && dentry->d_inode &&
604 !S_ISREG(dentry->d_inode->i_mode) &&
605 !S_ISDIR(dentry->d_inode->i_mode)) {
606 ll_release_openhandle(dentry, it);
608 ll_lookup_finish_locks(it, dentry);
611 GOTO(out, retval = NULL);
613 GOTO(out, retval = dentry);
616 ptlrpc_req_finished(req);
620 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
621 #ifdef HAVE_VFS_INTENT_PATCHES
622 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
623 struct nameidata *nd)
628 if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
629 de = ll_lookup_it(parent, dentry, &nd->intent, nd->flags);
631 de = ll_lookup_it(parent, dentry, NULL, 0);
636 struct lookup_intent *ll_convert_intent(struct open_intent *oit,
639 struct lookup_intent *it;
641 OBD_ALLOC(it, sizeof(*it));
643 return ERR_PTR(-ENOMEM);
645 if (lookup_flags & LOOKUP_OPEN) {
647 if (lookup_flags & LOOKUP_CREATE)
648 it->it_op |= IT_CREAT;
649 it->it_create_mode = oit->create_mode;
650 it->it_flags = oit->flags;
652 it->it_op = IT_GETATTR;
655 #ifndef HAVE_FILE_IN_STRUCT_INTENT
656 /* Since there is no way to pass our intent to ll_file_open,
657 * just check the file is there. Actual open will be done
659 if (it->it_op & IT_OPEN)
660 it->it_op = IT_LOOKUP;
666 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
667 struct nameidata *nd)
672 if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
673 struct lookup_intent *it;
675 #if defined(HAVE_FILE_IN_STRUCT_INTENT) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
676 /* Did we came here from failed revalidate just to propagate
678 if (nd->flags & LOOKUP_OPEN)
679 if (IS_ERR(nd->intent.open.file))
680 RETURN((struct dentry *)nd->intent.open.file);
683 if (ll_d2d(dentry) && ll_d2d(dentry)->lld_it) {
684 it = ll_d2d(dentry)->lld_it;
685 ll_d2d(dentry)->lld_it = NULL;
687 it = ll_convert_intent(&nd->intent.open, nd->flags);
689 RETURN((struct dentry *)it);
692 de = ll_lookup_it(parent, dentry, it, nd->flags);
695 if ((nd->flags & LOOKUP_OPEN) && !IS_ERR(dentry)) { /* Open */
696 if (dentry->d_inode &&
697 it_disposition(it, DISP_OPEN_OPEN)) { /* nocreate */
698 #ifdef HAVE_FILE_IN_STRUCT_INTENT
699 if (S_ISFIFO(dentry->d_inode->i_mode)) {
700 // We cannot call open here as it would
703 (struct ptlrpc_request *)
704 it->d.lustre.it_data);
707 nd->intent.open.file->private_data = it;
708 filp =lookup_instantiate_filp(nd,dentry,
710 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
711 /* 2.6.1[456] have a bug in open_namei() that forgets to check
712 * nd->intent.open.file for error, so we need to return it as lookup's result
717 de = (struct dentry *) filp;
722 #else /* HAVE_FILE_IN_STRUCT_INTENT */
723 /* Release open handle as we have no way to
724 * pass it to ll_file_open */
725 ll_release_openhandle(dentry, it);
726 #endif /* HAVE_FILE_IN_STRUCT_INTENT */
727 } else if (it_disposition(it, DISP_OPEN_CREATE)) {
728 // XXX This can only reliably work on assumption
729 // that there are NO hashed negative dentries.
730 ll_d2d(dentry)->lld_it = it;
731 it = NULL; /* Will be freed in ll_create_nd */
732 /* We absolutely depend on ll_create_nd to be
733 * called to not leak this intent and possible
734 * data attached to it */
739 ll_intent_release(it);
740 OBD_FREE(it, sizeof(*it));
743 de = ll_lookup_it(parent, dentry, NULL, 0);
751 /* We depend on "mode" being set with the proper file type/umask by now */
752 static struct inode *ll_create_node(struct inode *dir, const char *name,
753 int namelen, const void *data, int datalen,
754 int mode, __u64 extra,
755 struct lookup_intent *it)
757 struct inode *inode = NULL;
758 struct ptlrpc_request *request = NULL;
759 struct ll_sb_info *sbi = ll_i2sbi(dir);
763 LASSERT(it && it->d.lustre.it_disposition);
765 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
766 request = it->d.lustre.it_data;
767 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
768 rc = ll_prep_inode(sbi->ll_osc_exp, &inode, request, DLM_REPLY_REC_OFF,
771 GOTO(out, inode = ERR_PTR(rc));
773 LASSERT(list_empty(&inode->i_dentry));
775 /* We asked for a lock on the directory, but were granted a
776 * lock on the inode. Since we finally have an inode pointer,
777 * stuff it in the lock. */
778 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode %p (%lu/%u)\n",
779 inode, inode->i_ino, inode->i_generation);
780 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
783 ptlrpc_req_finished(request);
788 * By the time this is called, we already have created the directory cache
789 * entry for the new file, but it is so far negative - it has no inode.
791 * We defer creating the OBD object(s) until open, to keep the intent and
792 * non-intent code paths similar, and also because we do not have the MDS
793 * inode number before calling ll_create_node() (which is needed for LOV),
794 * so we would need to do yet another RPC to the MDS to store the LOV EA
795 * data on the MDS. If needed, we would pass the PACKED lmm as data and
796 * lmm_size in datalen (the MDS still has code which will handle that).
798 * If the create succeeds, we fill in the inode information
799 * with d_instantiate().
801 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
802 struct lookup_intent *it)
805 struct ptlrpc_request *request = it->d.lustre.it_data;
809 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
810 dentry->d_name.len, dentry->d_name.name, dir->i_ino,
811 dir->i_generation, dir, LL_IT2STR(it));
813 rc = it_open_error(DISP_OPEN_CREATE, it);
817 mdc_store_inode_generation(request, DLM_INTENT_REC_OFF,
819 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
820 NULL, 0, mode, 0, it);
822 RETURN(PTR_ERR(inode));
825 d_instantiate(dentry, inode);
826 /* Negative dentry may be unhashed if parent does not have UPDATE lock,
827 * but some callers, e.g. do_coredump, expect dentry to be hashed after
828 * successful create. Hash it here. */
829 spin_lock(&dcache_lock);
830 if (d_unhashed(dentry))
831 d_rehash_cond(dentry, 0);
832 spin_unlock(&dcache_lock);
836 static void ll_update_times(struct ptlrpc_request *request, int offset,
839 struct mds_body *body = lustre_msg_buf(request->rq_repmsg, offset,
843 /* mtime is always updated with ctime, but can be set in past.
844 As write and utime(2) may happen within 1 second, and utime's
845 mtime has a priority over write's one, so take mtime from mds
846 for the same ctimes. */
847 if (body->valid & OBD_MD_FLCTIME &&
848 body->ctime >= LTIME_S(inode->i_ctime)) {
849 LTIME_S(inode->i_ctime) = body->ctime;
851 if (body->valid & OBD_MD_FLMTIME) {
852 CDEBUG(D_INODE, "setting ino %lu mtime from %lu "
853 "to "LPU64"\n", inode->i_ino,
854 LTIME_S(inode->i_mtime), body->mtime);
855 LTIME_S(inode->i_mtime) = body->mtime;
860 static int ll_new_node(struct inode *dir, struct qstr *name,
861 const char *tgt, int mode,
862 int rdev, struct dentry *dchild)
864 struct ptlrpc_request *request = NULL;
865 struct inode *inode = NULL;
866 struct ll_sb_info *sbi = ll_i2sbi(dir);
867 struct mdc_op_data op_data;
872 if (unlikely(tgt != NULL))
873 tgt_len = strlen(tgt)+1;
875 err = ll_prepare_mdc_op_data(&op_data, dir, NULL, name->name,
880 err = mdc_create(sbi->ll_mdc_exp, &op_data, tgt, tgt_len,
881 mode, current->fsuid, current->fsgid,
882 current->cap_effective, rdev, &request);
886 ll_update_times(request, REPLY_REC_OFF, dir);
889 err = ll_prep_inode(sbi->ll_osc_exp, &inode, request,
890 REPLY_REC_OFF, dchild->d_sb);
895 d_instantiate(dchild, inode);
899 ptlrpc_req_finished(request);
905 static int ll_mknod_generic(struct inode *dir, struct qstr *name, int mode,
906 unsigned rdev, struct dentry *dchild)
911 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p) mode %o dev %x\n",
912 name->len, name->name, dir->i_ino, dir->i_generation, dir,
915 mode &= ~current->fs->umask;
917 switch (mode & S_IFMT) {
919 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
925 err = ll_new_node(dir, name, NULL, mode, rdev, dchild);
936 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
937 #ifndef HAVE_VFS_INTENT_PATCHES
938 static int ll_create_nd(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
940 struct lookup_intent *it = ll_d2d(dentry)->lld_it;
944 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
946 ll_d2d(dentry)->lld_it = NULL;
948 /* Was there an error? Propagate it! */
949 if (it->d.lustre.it_status) {
950 rc = it->d.lustre.it_status;
954 rc = ll_create_it(dir, dentry, mode, it);
955 #ifdef HAVE_FILE_IN_STRUCT_INTENT
956 if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
957 nd->intent.open.file->private_data = it;
958 lookup_instantiate_filp(nd, dentry, NULL);
961 ll_release_openhandle(dentry,it);
965 ll_intent_release(it);
966 OBD_FREE(it, sizeof(*it));
971 static int ll_create_nd(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
974 if (!nd || !nd->intent.d.lustre.it_disposition)
975 /* No saved request? Just mknod the file */
976 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
978 return ll_create_it(dir, dentry, mode, &nd->intent);
983 static int ll_symlink_generic(struct inode *dir, struct qstr *name,
984 const char *tgt, struct dentry *dchild)
989 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),target=%.*s\n",
990 name->len, name->name, dir->i_ino, dir->i_generation,
993 err = ll_new_node(dir, name, (char *)tgt, S_IFLNK | S_IRWXUGO,
998 static int ll_link_generic(struct inode *src, struct inode *dir,
999 struct qstr *name, struct dentry *dchild)
1001 struct ptlrpc_request *request = NULL;
1002 struct mdc_op_data op_data;
1004 struct ll_sb_info *sbi = ll_i2sbi(dir);
1008 "VFS Op: inode=%lu/%u(%p), dir=%lu/%u(%p), target=%.*s\n",
1009 src->i_ino, src->i_generation, src, dir->i_ino,
1010 dir->i_generation, dir, name->len, name->name);
1012 err = ll_prepare_mdc_op_data(&op_data, src, dir, name->name,
1013 name->len, 0, NULL);
1016 err = mdc_link(sbi->ll_mdc_exp, &op_data, &request);
1023 ll_update_times(request, REPLY_REC_OFF, dir);
1027 ptlrpc_req_finished(request);
1031 static int ll_mkdir_generic(struct inode *dir, struct qstr *name, int mode,
1032 struct dentry *dchild)
1038 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1039 name->len, name->name, dir->i_ino, dir->i_generation, dir);
1041 mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
1042 err = ll_new_node(dir, name, NULL, mode, 0, dchild);
1047 /* Try to find the child dentry by its name.
1048 If found, put the result fid into @fid. */
1049 static void ll_get_child_fid(struct inode * dir, struct qstr *name,
1052 struct dentry *parent, *child;
1054 parent = list_entry(dir->i_dentry.next, struct dentry, d_alias);
1055 child = d_lookup(parent, name);
1058 ll_inode2fid(fid, child->d_inode);
1063 static int ll_rmdir_generic(struct inode *dir, struct dentry *dparent,
1066 struct ptlrpc_request *request = NULL;
1067 struct mdc_op_data op_data = {{0}};
1068 struct dentry *dentry;
1071 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1072 name->len, name->name, dir->i_ino, dir->i_generation, dir);
1074 /* Check if we have something mounted at the dir we are going to delete
1075 * In such a case there would always be dentry present. */
1077 dentry = d_lookup(dparent, name);
1079 int mounted = d_mountpoint(dentry);
1082 GOTO(out, rc = -EBUSY);
1086 rc = ll_prepare_mdc_op_data(&op_data, dir, NULL, name->name,
1087 name->len, S_IFDIR, NULL);
1091 ll_get_child_fid(dir, name, &op_data.fid3);
1092 rc = mdc_unlink(ll_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
1095 ll_update_times(request, REPLY_REC_OFF, dir);
1099 ptlrpc_req_finished(request);
1103 int ll_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
1105 struct mds_body *body;
1106 struct lov_mds_md *eadata;
1107 struct lov_stripe_md *lsm = NULL;
1108 struct obd_trans_info oti = { 0 };
1113 /* req is swabbed so this is safe */
1114 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
1116 if (!(body->valid & OBD_MD_FLEASIZE))
1119 if (body->eadatasize == 0) {
1120 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1121 GOTO(out, rc = -EPROTO);
1124 /* The MDS sent back the EA because we unlinked the last reference
1125 * to this file. Use this EA to unlink the objects on the OST.
1126 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
1127 * check it is complete and sensible. */
1128 eadata = lustre_swab_repbuf(request, REPLY_REC_OFF + 1,
1129 body->eadatasize, NULL);
1130 LASSERT(eadata != NULL);
1131 if (eadata == NULL) {
1132 CERROR("Can't unpack MDS EA data\n");
1133 GOTO(out, rc = -EPROTO);
1136 rc = obd_unpackmd(ll_i2obdexp(dir), &lsm, eadata, body->eadatasize);
1138 CERROR("obd_unpackmd: %d\n", rc);
1141 LASSERT(rc >= sizeof(*lsm));
1143 rc = obd_checkmd(ll_i2obdexp(dir), ll_i2mdcexp(dir), lsm);
1145 GOTO(out_free_memmd, rc);
1149 GOTO(out_free_memmd, rc = -ENOMEM);
1151 oa->o_id = lsm->lsm_object_id;
1152 oa->o_mode = body->mode & S_IFMT;
1153 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
1155 if (body->valid & OBD_MD_FLCOOKIE) {
1156 oa->o_valid |= OBD_MD_FLCOOKIE;
1157 oti.oti_logcookies =
1158 lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF + 2,
1159 sizeof(struct llog_cookie) *
1160 lsm->lsm_stripe_count);
1161 if (oti.oti_logcookies == NULL) {
1162 oa->o_valid &= ~OBD_MD_FLCOOKIE;
1163 body->valid &= ~OBD_MD_FLCOOKIE;
1167 rc = obd_destroy(ll_i2obdexp(dir), oa, lsm, &oti, ll_i2mdcexp(dir));
1170 CERROR("obd destroy objid "LPX64" error %d\n",
1171 lsm->lsm_object_id, rc);
1173 obd_free_memmd(ll_i2obdexp(dir), &lsm);
1178 static int ll_unlink_generic(struct inode * dir, struct qstr *name)
1180 struct ptlrpc_request *request = NULL;
1181 struct mdc_op_data op_data = {{0}};
1185 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1186 name->len, name->name, dir->i_ino, dir->i_generation, dir);
1188 rc = ll_prepare_mdc_op_data(&op_data, dir, NULL, name->name,
1189 name->len, 0, NULL);
1193 ll_get_child_fid(dir, name, &op_data.fid3);
1194 rc = mdc_unlink(ll_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
1198 ll_update_times(request, REPLY_REC_OFF, dir);
1200 rc = ll_objects_destroy(request, dir);
1205 ptlrpc_req_finished(request);
1209 static int ll_rename_generic(struct inode *src, struct qstr *src_name,
1210 struct inode *tgt, struct qstr *tgt_name)
1212 struct ptlrpc_request *request = NULL;
1213 struct ll_sb_info *sbi = ll_i2sbi(src);
1214 struct mdc_op_data op_data = {{0}};
1218 CDEBUG(D_VFSTRACE,"VFS Op:oldname=%.*s,src_dir=%lu/%u(%p),newname=%.*s,"
1219 "tgt_dir=%lu/%u(%p)\n", src_name->len, src_name->name,
1220 src->i_ino, src->i_generation, src, tgt_name->len,
1221 tgt_name->name, tgt->i_ino, tgt->i_generation, tgt);
1223 err = ll_prepare_mdc_op_data(&op_data, src, tgt, NULL, 0, 0, NULL);
1227 ll_get_child_fid(src, src_name, &op_data.fid3);
1228 ll_get_child_fid(tgt, tgt_name, &op_data.fid4);
1229 err = mdc_rename(sbi->ll_mdc_exp, &op_data,
1230 src_name->name, src_name->len,
1231 tgt_name->name, tgt_name->len, &request);
1234 ll_update_times(request, REPLY_REC_OFF, src);
1235 ll_update_times(request, REPLY_REC_OFF, tgt);
1236 err = ll_objects_destroy(request, src);
1242 ptlrpc_req_finished(request);
1247 #ifdef HAVE_VFS_INTENT_PATCHES
1248 static int ll_mknod_raw(struct nameidata *nd, int mode, dev_t rdev)
1250 return ll_mknod_generic(nd->dentry->d_inode, &nd->last, mode,rdev,NULL);
1252 static int ll_rename_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1254 return ll_rename_generic(srcnd->dentry->d_inode, &srcnd->last,
1255 tgtnd->dentry->d_inode, &tgtnd->last);
1257 static int ll_link_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1259 return ll_link_generic(srcnd->dentry->d_inode, tgtnd->dentry->d_inode,
1260 &tgtnd->last, NULL);
1262 static int ll_symlink_raw(struct nameidata *nd, const char *tgt)
1264 return ll_symlink_generic(nd->dentry->d_inode, &nd->last, tgt, NULL);
1266 static int ll_rmdir_raw(struct nameidata *nd)
1268 return ll_rmdir_generic(nd->dentry->d_inode, nd->dentry, &nd->last);
1270 static int ll_mkdir_raw(struct nameidata *nd, int mode)
1272 return ll_mkdir_generic(nd->dentry->d_inode, &nd->last, mode, NULL);
1274 static int ll_unlink_raw(struct nameidata *nd)
1276 return ll_unlink_generic(nd->dentry->d_inode, &nd->last);
1280 static int ll_mknod(struct inode *dir, struct dentry *dchild, int mode,
1283 return ll_mknod_generic(dir, &dchild->d_name, mode,
1284 old_encode_dev(rdev), dchild);
1287 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1288 static int ll_unlink(struct inode * dir, struct dentry *dentry)
1290 return ll_unlink_generic(dir, &dentry->d_name);
1292 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1294 return ll_mkdir_generic(dir, &dentry->d_name, mode, dentry);
1296 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
1298 return ll_rmdir_generic(dir, NULL, &dentry->d_name);
1300 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1301 const char *oldname)
1303 return ll_symlink_generic(dir, &dentry->d_name, oldname, dentry);
1305 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1306 struct dentry *new_dentry)
1308 return ll_link_generic(old_dentry->d_inode, dir,
1309 &new_dentry->d_name, new_dentry);
1311 static int ll_rename(struct inode *old_dir, struct dentry *old_dentry,
1312 struct inode *new_dir, struct dentry *new_dentry)
1314 return ll_rename_generic(old_dir, &old_dentry->d_name, new_dir,
1315 &new_dentry->d_name);
1319 struct inode_operations ll_dir_inode_operations = {
1320 #ifdef HAVE_VFS_INTENT_PATCHES
1321 .link_raw = ll_link_raw,
1322 .unlink_raw = ll_unlink_raw,
1323 .symlink_raw = ll_symlink_raw,
1324 .mkdir_raw = ll_mkdir_raw,
1325 .rmdir_raw = ll_rmdir_raw,
1326 .mknod_raw = ll_mknod_raw,
1327 .rename_raw = ll_rename_raw,
1328 .setattr = ll_setattr,
1329 .setattr_raw = ll_setattr_raw,
1332 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1333 .create_it = ll_create_it,
1334 .lookup_it = ll_lookup_it,
1335 .revalidate_it = ll_inode_revalidate_it,
1337 .lookup = ll_lookup_nd,
1338 .create = ll_create_nd,
1339 /* We need all these non-raw things for NFSD, to not patch it. */
1340 .unlink = ll_unlink,
1343 .symlink = ll_symlink,
1345 .rename = ll_rename,
1346 .setattr = ll_setattr,
1347 .getattr = ll_getattr,
1349 .permission = ll_inode_permission,
1350 .setxattr = ll_setxattr,
1351 .getxattr = ll_getxattr,
1352 .listxattr = ll_listxattr,
1353 .removexattr = ll_removexattr,
1356 struct inode_operations ll_special_inode_operations = {
1357 #ifdef HAVE_VFS_INTENT_PATCHES
1358 .setattr_raw = ll_setattr_raw,
1360 .setattr = ll_setattr,
1361 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1362 .getattr = ll_getattr,
1364 .revalidate_it = ll_inode_revalidate_it,
1366 .permission = ll_inode_permission,
1367 .setxattr = ll_setxattr,
1368 .getxattr = ll_getxattr,
1369 .listxattr = ll_listxattr,
1370 .removexattr = ll_removexattr,