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 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 static void ll_drop_negative_dentry(struct inode *dir)
155 struct dentry *dentry, *tmp_alias, *tmp_subdir;
157 spin_lock(&dcache_lock);
159 list_for_each_entry_safe(dentry, tmp_alias,
160 &dir->i_dentry,d_alias) {
161 if (!list_empty(&dentry->d_subdirs)) {
162 struct dentry *child;
163 list_for_each_entry_safe(child, tmp_subdir,
166 /* XXX Print some debug here? */
168 /* Negative dentry. If we were
169 dropping dcache lock, go
170 throught the list again */
171 if (ll_drop_dentry(child))
176 spin_unlock(&dcache_lock);
179 int ll_mdc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
180 void *data, int flag)
183 struct lustre_handle lockh;
187 case LDLM_CB_BLOCKING:
188 ldlm_lock2handle(lock, &lockh);
189 rc = ldlm_cli_cancel(&lockh);
191 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
195 case LDLM_CB_CANCELING: {
196 struct inode *inode = ll_inode_from_lock(lock);
197 __u64 bits = lock->l_policy_data.l_inodebits.bits;
199 /* Invalidate all dentries associated with this inode */
203 LASSERT(lock->l_flags & LDLM_FL_CANCELING);
204 if ((bits & MDS_INODELOCK_LOOKUP) &&
205 ll_have_md_lock(inode, MDS_INODELOCK_LOOKUP))
206 bits &= ~MDS_INODELOCK_LOOKUP;
207 if ((bits & MDS_INODELOCK_UPDATE) &&
208 ll_have_md_lock(inode, MDS_INODELOCK_UPDATE))
209 bits &= ~MDS_INODELOCK_UPDATE;
210 if ((bits & MDS_INODELOCK_OPEN) &&
211 ll_have_md_lock(inode, MDS_INODELOCK_OPEN))
212 bits &= ~MDS_INODELOCK_OPEN;
214 if (lock->l_resource->lr_name.name[0] != inode->i_ino ||
215 lock->l_resource->lr_name.name[1] != inode->i_generation) {
216 LDLM_ERROR(lock, "data mismatch with ino %lu/%u (%p)",
217 inode->i_ino, inode->i_generation, inode);
220 if (bits & MDS_INODELOCK_OPEN) {
222 switch (lock->l_req_mode) {
229 CERROR("open PR lock without FMODE_EXEC\n");
235 CERROR("Unexpected lock mode for OPEN lock "
236 "%d, inode %ld\n", lock->l_req_mode,
239 ll_mdc_real_close(inode, flags);
242 if (bits & MDS_INODELOCK_UPDATE)
243 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK,
244 &(ll_i2info(inode)->lli_flags));
246 if (S_ISDIR(inode->i_mode) &&
247 (bits & MDS_INODELOCK_UPDATE)) {
248 CDEBUG(D_INODE, "invalidating inode %lu\n",
250 truncate_inode_pages(inode->i_mapping, 0);
251 ll_drop_negative_dentry(inode);
254 if (inode->i_sb->s_root &&
255 inode != inode->i_sb->s_root->d_inode &&
256 (bits & MDS_INODELOCK_LOOKUP))
257 ll_unhash_aliases(inode);
268 int ll_mdc_cancel_unused(struct lustre_handle *conn, struct inode *inode,
269 int flags, void *opaque)
271 struct ldlm_res_id res_id =
272 { .name = {inode->i_ino, inode->i_generation} };
273 struct obd_device *obddev = class_conn2obd(conn);
276 RETURN(ldlm_cli_cancel_unused(obddev->obd_namespace, &res_id, flags,
280 /* Pack the required supplementary groups into the supplied groups array.
281 * If we don't need to use the groups from the target inode(s) then we
282 * instead pack one or more groups from the user's supplementary group
283 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
284 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
289 LASSERT(suppgids != NULL);
291 if (in_group_p(i1->i_gid))
292 suppgids[0] = i1->i_gid;
297 if (in_group_p(i2->i_gid))
298 suppgids[1] = i2->i_gid;
305 for (i = 0; i < current_ngroups; i++) {
306 if (suppgids[0] == -1) {
307 if (current_groups[i] != suppgids[1])
308 suppgids[0] = current_groups[i];
311 if (suppgids[1] == -1) {
312 if (current_groups[i] != suppgids[0])
313 suppgids[1] = current_groups[i];
320 int ll_prepare_mdc_op_data(struct mdc_op_data *op_data, struct inode *i1,
321 struct inode *i2, const char *name, int namelen,
322 int mode, void *data)
326 if (namelen > ll_i2sbi(i1)->ll_namelen)
327 return -ENAMETOOLONG;
328 ll_i2gids(op_data->suppgids, i1, i2);
329 ll_inode2fid(&op_data->fid1, i1);
332 ll_inode2fid(&op_data->fid2, i2);
334 memset(&op_data->fid2, 0, sizeof(op_data->fid2));
336 op_data->name = name;
337 op_data->namelen = namelen;
338 op_data->create_mode = mode;
339 op_data->mod_time = CURRENT_SECONDS;
340 op_data->data = data;
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 static 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 DCACHE_LUSTRE_INVALID
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 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 if (it->it_op == IT_GETATTR) {
534 rc = ll_statahead_enter(parent, &dentry, 1);
536 ll_statahead_exit(dentry, rc);
538 RETURN(retval = dentry);
542 icbd.icbd_parent = parent;
543 icbd.icbd_childp = &dentry;
545 rc = ll_prepare_mdc_op_data(&op_data, parent, NULL, dentry->d_name.name,
546 dentry->d_name.len, lookup_flags, NULL);
550 it->it_create_mode &= ~current->fs->umask;
553 rc = mdc_intent_lock(ll_i2mdcexp(parent), &op_data, NULL, 0, it,
554 lookup_flags, &req, ll_mdc_blocking_ast, 0);
555 down(&parent->i_sem);
557 GOTO(out, retval = ERR_PTR(rc));
559 rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
561 ll_intent_release(it);
562 GOTO(out, retval = ERR_PTR(rc));
565 if ((it->it_op & IT_OPEN) && dentry->d_inode &&
566 !S_ISREG(dentry->d_inode->i_mode) &&
567 !S_ISDIR(dentry->d_inode->i_mode)) {
568 ll_release_openhandle(dentry, it);
570 ll_lookup_finish_locks(it, dentry);
573 GOTO(out, retval = NULL);
575 GOTO(out, retval = dentry);
578 ptlrpc_req_finished(req);
582 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
583 #ifdef HAVE_VFS_INTENT_PATCHES
584 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
585 struct nameidata *nd)
590 if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
591 de = ll_lookup_it(parent, dentry, &nd->intent, nd->flags);
593 de = ll_lookup_it(parent, dentry, NULL, 0);
598 struct lookup_intent *ll_convert_intent(struct open_intent *oit,
601 struct lookup_intent *it;
603 OBD_ALLOC(it, sizeof(*it));
605 return ERR_PTR(-ENOMEM);
607 if (lookup_flags & LOOKUP_OPEN) {
609 if (lookup_flags & LOOKUP_CREATE)
610 it->it_op |= IT_CREAT;
611 it->it_create_mode = oit->create_mode;
612 it->it_flags = oit->flags;
614 it->it_op = IT_GETATTR;
617 #ifndef HAVE_FILE_IN_STRUCT_INTENT
618 /* Since there is no way to pass our intent to ll_file_open,
619 * just check the file is there. Actual open will be done
621 if (it->it_op & IT_OPEN)
622 it->it_op = IT_LOOKUP;
628 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
629 struct nameidata *nd)
634 if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
635 struct lookup_intent *it;
637 #if defined(HAVE_FILE_IN_STRUCT_INTENT) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
638 /* Did we came here from failed revalidate just to propagate
640 if (nd->flags & LOOKUP_OPEN)
641 if (IS_ERR(nd->intent.open.file))
642 RETURN((struct dentry *)nd->intent.open.file);
645 if (ll_d2d(dentry) && ll_d2d(dentry)->lld_it) {
646 it = ll_d2d(dentry)->lld_it;
647 ll_d2d(dentry)->lld_it = NULL;
649 it = ll_convert_intent(&nd->intent.open, nd->flags);
651 RETURN((struct dentry *)it);
654 de = ll_lookup_it(parent, dentry, it, nd->flags);
657 if ((nd->flags & LOOKUP_OPEN) && !IS_ERR(dentry)) { /* Open */
658 if (dentry->d_inode &&
659 it_disposition(it, DISP_OPEN_OPEN)) { /* nocreate */
660 #ifdef HAVE_FILE_IN_STRUCT_INTENT
661 if (S_ISFIFO(dentry->d_inode->i_mode)) {
662 // We cannot call open here as it would
665 (struct ptlrpc_request *)
666 it->d.lustre.it_data);
669 nd->intent.open.file->private_data = it;
670 filp =lookup_instantiate_filp(nd,dentry,
672 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
673 /* 2.6.1[456] have a bug in open_namei() that forgets to check
674 * nd->intent.open.file for error, so we need to return it as lookup's result
679 de = (struct dentry *) filp;
684 #else /* HAVE_FILE_IN_STRUCT_INTENT */
685 /* Release open handle as we have no way to
686 * pass it to ll_file_open */
687 ll_release_openhandle(dentry, it);
688 #endif /* HAVE_FILE_IN_STRUCT_INTENT */
689 } else if (it_disposition(it, DISP_OPEN_CREATE)) {
690 // XXX This can only reliably work on assumption
691 // that there are NO hashed negative dentries.
692 ll_d2d(dentry)->lld_it = it;
693 it = NULL; /* Will be freed in ll_create_nd */
694 /* We absolutely depend on ll_create_nd to be
695 * called to not leak this intent and possible
696 * data attached to it */
701 ll_intent_release(it);
702 OBD_FREE(it, sizeof(*it));
705 de = ll_lookup_it(parent, dentry, NULL, 0);
713 /* We depend on "mode" being set with the proper file type/umask by now */
714 static struct inode *ll_create_node(struct inode *dir, const char *name,
715 int namelen, const void *data, int datalen,
716 int mode, __u64 extra,
717 struct lookup_intent *it)
719 struct inode *inode = NULL;
720 struct ptlrpc_request *request = NULL;
721 struct ll_sb_info *sbi = ll_i2sbi(dir);
725 LASSERT(it && it->d.lustre.it_disposition);
727 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
728 request = it->d.lustre.it_data;
729 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
730 rc = ll_prep_inode(sbi->ll_osc_exp, &inode, request, DLM_REPLY_REC_OFF,
733 GOTO(out, inode = ERR_PTR(rc));
735 LASSERT(list_empty(&inode->i_dentry));
737 /* We asked for a lock on the directory, but were granted a
738 * lock on the inode. Since we finally have an inode pointer,
739 * stuff it in the lock. */
740 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode %p (%lu/%u)\n",
741 inode, inode->i_ino, inode->i_generation);
742 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
745 ptlrpc_req_finished(request);
750 * By the time this is called, we already have created the directory cache
751 * entry for the new file, but it is so far negative - it has no inode.
753 * We defer creating the OBD object(s) until open, to keep the intent and
754 * non-intent code paths similar, and also because we do not have the MDS
755 * inode number before calling ll_create_node() (which is needed for LOV),
756 * so we would need to do yet another RPC to the MDS to store the LOV EA
757 * data on the MDS. If needed, we would pass the PACKED lmm as data and
758 * lmm_size in datalen (the MDS still has code which will handle that).
760 * If the create succeeds, we fill in the inode information
761 * with d_instantiate().
763 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
764 struct lookup_intent *it)
767 struct ptlrpc_request *request = it->d.lustre.it_data;
771 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
772 dentry->d_name.len, dentry->d_name.name, dir->i_ino,
773 dir->i_generation, dir, LL_IT2STR(it));
775 rc = it_open_error(DISP_OPEN_CREATE, it);
779 mdc_store_inode_generation(request, DLM_INTENT_REC_OFF,
781 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
782 NULL, 0, mode, 0, it);
784 RETURN(PTR_ERR(inode));
787 d_instantiate(dentry, inode);
788 /* Negative dentry may be unhashed if parent does not have UPDATE lock,
789 * but some callers, e.g. do_coredump, expect dentry to be hashed after
790 * successful create. Hash it here. */
791 spin_lock(&dcache_lock);
792 if (d_unhashed(dentry))
793 __d_rehash(dentry, 0);
794 spin_unlock(&dcache_lock);
798 static void ll_update_times(struct ptlrpc_request *request, int offset,
801 struct mds_body *body = lustre_msg_buf(request->rq_repmsg, offset,
805 /* mtime is always updated with ctime, but can be set in past.
806 As write and utime(2) may happen within 1 second, and utime's
807 mtime has a priority over write's one, so take mtime from mds
808 for the same ctimes. */
809 if (body->valid & OBD_MD_FLCTIME &&
810 body->ctime >= LTIME_S(inode->i_ctime)) {
811 LTIME_S(inode->i_ctime) = body->ctime;
813 if (body->valid & OBD_MD_FLMTIME) {
814 CDEBUG(D_INODE, "setting ino %lu mtime from %lu "
815 "to "LPU64"\n", inode->i_ino,
816 LTIME_S(inode->i_mtime), body->mtime);
817 LTIME_S(inode->i_mtime) = body->mtime;
822 static int ll_new_node(struct inode *dir, struct qstr *name,
823 const char *tgt, int mode,
824 int rdev, struct dentry *dchild)
826 struct ptlrpc_request *request = NULL;
827 struct inode *inode = NULL;
828 struct ll_sb_info *sbi = ll_i2sbi(dir);
829 struct mdc_op_data op_data;
834 if (unlikely(tgt != NULL))
835 tgt_len = strlen(tgt)+1;
837 err = ll_prepare_mdc_op_data(&op_data, dir, NULL, name->name,
842 err = mdc_create(sbi->ll_mdc_exp, &op_data, tgt, tgt_len,
843 mode, current->fsuid, current->fsgid,
844 current->cap_effective, rdev, &request);
848 ll_update_times(request, REPLY_REC_OFF, dir);
851 err = ll_prep_inode(sbi->ll_osc_exp, &inode, request,
852 REPLY_REC_OFF, dchild->d_sb);
857 d_instantiate(dchild, inode);
861 ptlrpc_req_finished(request);
867 static int ll_mknod_generic(struct inode *dir, struct qstr *name, int mode,
868 unsigned rdev, struct dentry *dchild)
873 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p) mode %o dev %x\n",
874 name->len, name->name, dir->i_ino, dir->i_generation, dir,
877 mode &= ~current->fs->umask;
879 switch (mode & S_IFMT) {
881 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
887 err = ll_new_node(dir, name, NULL, mode, rdev, dchild);
898 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
899 #ifndef HAVE_VFS_INTENT_PATCHES
900 static int ll_create_nd(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
902 struct lookup_intent *it = ll_d2d(dentry)->lld_it;
906 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
908 ll_d2d(dentry)->lld_it = NULL;
910 /* Was there an error? Propagate it! */
911 if (it->d.lustre.it_status) {
912 rc = it->d.lustre.it_status;
916 rc = ll_create_it(dir, dentry, mode, it);
917 #ifdef HAVE_FILE_IN_STRUCT_INTENT
918 if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
919 nd->intent.open.file->private_data = it;
920 lookup_instantiate_filp(nd, dentry, NULL);
923 ll_release_openhandle(dentry,it);
927 ll_intent_release(it);
928 OBD_FREE(it, sizeof(*it));
933 static int ll_create_nd(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
936 if (!nd || !nd->intent.d.lustre.it_disposition)
937 /* No saved request? Just mknod the file */
938 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
940 return ll_create_it(dir, dentry, mode, &nd->intent);
945 static int ll_symlink_generic(struct inode *dir, struct qstr *name,
946 const char *tgt, struct dentry *dchild)
951 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),target=%.*s\n",
952 name->len, name->name, dir->i_ino, dir->i_generation,
955 err = ll_new_node(dir, name, (char *)tgt, S_IFLNK | S_IRWXUGO,
960 static int ll_link_generic(struct inode *src, struct inode *dir,
961 struct qstr *name, struct dentry *dchild)
963 struct ptlrpc_request *request = NULL;
964 struct mdc_op_data op_data;
966 struct ll_sb_info *sbi = ll_i2sbi(dir);
970 "VFS Op: inode=%lu/%u(%p), dir=%lu/%u(%p), target=%.*s\n",
971 src->i_ino, src->i_generation, src, dir->i_ino,
972 dir->i_generation, dir, name->len, name->name);
974 err = ll_prepare_mdc_op_data(&op_data, src, dir, name->name,
978 err = mdc_link(sbi->ll_mdc_exp, &op_data, &request);
985 ll_update_times(request, REPLY_REC_OFF, dir);
989 ptlrpc_req_finished(request);
993 static int ll_mkdir_generic(struct inode *dir, struct qstr *name, int mode,
994 struct dentry *dchild)
1000 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1001 name->len, name->name, dir->i_ino, dir->i_generation, dir);
1003 mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
1004 err = ll_new_node(dir, name, NULL, mode, 0, dchild);
1009 /* Try to find the child dentry by its name.
1010 If found, put the result fid into @fid. */
1011 static void ll_get_child_fid(struct inode * dir, struct qstr *name,
1014 struct dentry *parent, *child;
1016 parent = list_entry(dir->i_dentry.next, struct dentry, d_alias);
1017 child = d_lookup(parent, name);
1020 ll_inode2fid(fid, child->d_inode);
1025 static int ll_rmdir_generic(struct inode *dir, struct dentry *dparent,
1028 struct ptlrpc_request *request = NULL;
1029 struct mdc_op_data op_data = {{0}};
1030 struct dentry *dentry;
1033 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1034 name->len, name->name, dir->i_ino, dir->i_generation, dir);
1036 /* Check if we have something mounted at the dir we are going to delete
1037 * In such a case there would always be dentry present. */
1039 dentry = d_lookup(dparent, name);
1041 int mounted = d_mountpoint(dentry);
1044 GOTO(out, rc = -EBUSY);
1048 rc = ll_prepare_mdc_op_data(&op_data, dir, NULL, name->name,
1049 name->len, S_IFDIR, NULL);
1053 ll_get_child_fid(dir, name, &op_data.fid3);
1054 rc = mdc_unlink(ll_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
1057 ll_update_times(request, REPLY_REC_OFF, dir);
1061 ptlrpc_req_finished(request);
1065 int ll_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
1067 struct mds_body *body;
1068 struct lov_mds_md *eadata;
1069 struct lov_stripe_md *lsm = NULL;
1070 struct obd_trans_info oti = { 0 };
1075 /* req is swabbed so this is safe */
1076 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
1078 if (!(body->valid & OBD_MD_FLEASIZE))
1081 if (body->eadatasize == 0) {
1082 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1083 GOTO(out, rc = -EPROTO);
1086 /* The MDS sent back the EA because we unlinked the last reference
1087 * to this file. Use this EA to unlink the objects on the OST.
1088 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
1089 * check it is complete and sensible. */
1090 eadata = lustre_swab_repbuf(request, REPLY_REC_OFF + 1,
1091 body->eadatasize, NULL);
1092 LASSERT(eadata != NULL);
1093 if (eadata == NULL) {
1094 CERROR("Can't unpack MDS EA data\n");
1095 GOTO(out, rc = -EPROTO);
1098 rc = obd_unpackmd(ll_i2obdexp(dir), &lsm, eadata, body->eadatasize);
1100 CERROR("obd_unpackmd: %d\n", rc);
1103 LASSERT(rc >= sizeof(*lsm));
1105 rc = obd_checkmd(ll_i2obdexp(dir), ll_i2mdcexp(dir), lsm);
1107 GOTO(out_free_memmd, rc);
1111 GOTO(out_free_memmd, rc = -ENOMEM);
1113 oa->o_id = lsm->lsm_object_id;
1114 oa->o_mode = body->mode & S_IFMT;
1115 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
1117 if (body->valid & OBD_MD_FLCOOKIE) {
1118 oa->o_valid |= OBD_MD_FLCOOKIE;
1119 oti.oti_logcookies =
1120 lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF + 2,
1121 sizeof(struct llog_cookie) *
1122 lsm->lsm_stripe_count);
1123 if (oti.oti_logcookies == NULL) {
1124 oa->o_valid &= ~OBD_MD_FLCOOKIE;
1125 body->valid &= ~OBD_MD_FLCOOKIE;
1129 rc = obd_destroy(ll_i2obdexp(dir), oa, lsm, &oti, ll_i2mdcexp(dir));
1132 CERROR("obd destroy objid "LPX64" error %d\n",
1133 lsm->lsm_object_id, rc);
1135 obd_free_memmd(ll_i2obdexp(dir), &lsm);
1140 static int ll_unlink_generic(struct inode * dir, struct qstr *name)
1142 struct ptlrpc_request *request = NULL;
1143 struct mdc_op_data op_data = {{0}};
1147 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1148 name->len, name->name, dir->i_ino, dir->i_generation, dir);
1150 rc = ll_prepare_mdc_op_data(&op_data, dir, NULL, name->name,
1151 name->len, 0, NULL);
1155 ll_get_child_fid(dir, name, &op_data.fid3);
1156 rc = mdc_unlink(ll_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
1160 ll_update_times(request, REPLY_REC_OFF, dir);
1162 rc = ll_objects_destroy(request, dir);
1167 ptlrpc_req_finished(request);
1171 static int ll_rename_generic(struct inode *src, struct qstr *src_name,
1172 struct inode *tgt, struct qstr *tgt_name)
1174 struct ptlrpc_request *request = NULL;
1175 struct ll_sb_info *sbi = ll_i2sbi(src);
1176 struct mdc_op_data op_data = {{0}};
1180 CDEBUG(D_VFSTRACE,"VFS Op:oldname=%.*s,src_dir=%lu/%u(%p),newname=%.*s,"
1181 "tgt_dir=%lu/%u(%p)\n", src_name->len, src_name->name,
1182 src->i_ino, src->i_generation, src, tgt_name->len,
1183 tgt_name->name, tgt->i_ino, tgt->i_generation, tgt);
1185 err = ll_prepare_mdc_op_data(&op_data, src, tgt, NULL, 0, 0, NULL);
1189 ll_get_child_fid(src, src_name, &op_data.fid3);
1190 ll_get_child_fid(tgt, tgt_name, &op_data.fid4);
1191 err = mdc_rename(sbi->ll_mdc_exp, &op_data,
1192 src_name->name, src_name->len,
1193 tgt_name->name, tgt_name->len, &request);
1196 ll_update_times(request, REPLY_REC_OFF, src);
1197 ll_update_times(request, REPLY_REC_OFF, tgt);
1198 err = ll_objects_destroy(request, src);
1204 ptlrpc_req_finished(request);
1209 #ifdef HAVE_VFS_INTENT_PATCHES
1210 static int ll_mknod_raw(struct nameidata *nd, int mode, dev_t rdev)
1212 return ll_mknod_generic(nd->dentry->d_inode, &nd->last, mode,rdev,NULL);
1214 static int ll_rename_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1216 return ll_rename_generic(srcnd->dentry->d_inode, &srcnd->last,
1217 tgtnd->dentry->d_inode, &tgtnd->last);
1219 static int ll_link_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1221 return ll_link_generic(srcnd->dentry->d_inode, tgtnd->dentry->d_inode,
1222 &tgtnd->last, NULL);
1224 static int ll_symlink_raw(struct nameidata *nd, const char *tgt)
1226 return ll_symlink_generic(nd->dentry->d_inode, &nd->last, tgt, NULL);
1228 static int ll_rmdir_raw(struct nameidata *nd)
1230 return ll_rmdir_generic(nd->dentry->d_inode, nd->dentry, &nd->last);
1232 static int ll_mkdir_raw(struct nameidata *nd, int mode)
1234 return ll_mkdir_generic(nd->dentry->d_inode, &nd->last, mode, NULL);
1236 static int ll_unlink_raw(struct nameidata *nd)
1238 return ll_unlink_generic(nd->dentry->d_inode, &nd->last);
1242 static int ll_mknod(struct inode *dir, struct dentry *dchild, int mode,
1245 return ll_mknod_generic(dir, &dchild->d_name, mode,
1246 old_encode_dev(rdev), dchild);
1249 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1250 static int ll_unlink(struct inode * dir, struct dentry *dentry)
1252 return ll_unlink_generic(dir, &dentry->d_name);
1254 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1256 return ll_mkdir_generic(dir, &dentry->d_name, mode, dentry);
1258 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
1260 return ll_rmdir_generic(dir, NULL, &dentry->d_name);
1262 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1263 const char *oldname)
1265 return ll_symlink_generic(dir, &dentry->d_name, oldname, dentry);
1267 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1268 struct dentry *new_dentry)
1270 return ll_link_generic(old_dentry->d_inode, dir,
1271 &new_dentry->d_name, new_dentry);
1273 static int ll_rename(struct inode *old_dir, struct dentry *old_dentry,
1274 struct inode *new_dir, struct dentry *new_dentry)
1276 return ll_rename_generic(old_dir, &old_dentry->d_name, new_dir,
1277 &new_dentry->d_name);
1281 struct inode_operations ll_dir_inode_operations = {
1282 #ifdef HAVE_VFS_INTENT_PATCHES
1283 .link_raw = ll_link_raw,
1284 .unlink_raw = ll_unlink_raw,
1285 .symlink_raw = ll_symlink_raw,
1286 .mkdir_raw = ll_mkdir_raw,
1287 .rmdir_raw = ll_rmdir_raw,
1288 .mknod_raw = ll_mknod_raw,
1289 .rename_raw = ll_rename_raw,
1290 .setattr = ll_setattr,
1291 .setattr_raw = ll_setattr_raw,
1294 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1295 .create_it = ll_create_it,
1296 .lookup_it = ll_lookup_it,
1297 .revalidate_it = ll_inode_revalidate_it,
1299 .lookup = ll_lookup_nd,
1300 .create = ll_create_nd,
1301 /* We need all these non-raw things for NFSD, to not patch it. */
1302 .unlink = ll_unlink,
1305 .symlink = ll_symlink,
1307 .rename = ll_rename,
1308 .setattr = ll_setattr,
1309 .getattr = ll_getattr,
1311 .permission = ll_inode_permission,
1312 .setxattr = ll_setxattr,
1313 .getxattr = ll_getxattr,
1314 .listxattr = ll_listxattr,
1315 .removexattr = ll_removexattr,
1318 struct inode_operations ll_special_inode_operations = {
1319 #ifdef HAVE_VFS_INTENT_PATCHES
1320 .setattr_raw = ll_setattr_raw,
1322 .setattr = ll_setattr,
1323 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1324 .getattr = ll_getattr,
1326 .revalidate_it = ll_inode_revalidate_it,
1328 .permission = ll_inode_permission,
1329 .setxattr = ll_setxattr,
1330 .getxattr = ll_getxattr,
1331 .listxattr = ll_listxattr,
1332 .removexattr = ll_removexattr,