1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/sched.h>
25 #include <linux/smp_lock.h>
26 #include <linux/quotaops.h>
27 #include <linux/highmem.h>
28 #include <linux/pagemap.h>
30 #define DEBUG_SUBSYSTEM S_LLITE
32 #include <obd_support.h>
33 #include <lustre_fid.h>
34 #include <lustre_lite.h>
35 #include <lustre_dlm.h>
36 #include <lustre_ver.h>
37 #include <lustre_mdc.h>
38 #include "llite_internal.h"
42 extern struct dentry_operations ll_d_ops;
44 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
48 ldlm_lock_decref(lockh, mode);
53 /* Get an inode by inode number (already instantiated by the intent lookup).
54 * Returns inode or NULL
56 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
58 /* that variant provides correct mapping fid to inode,
59 * it is put here as alternative way to the current one ll_iget() below */
60 int ll_test_inode(struct inode *inode, void *opaque)
62 return lu_fid_eq(&ll_i2info(inode)->lli_fid, (struct lu_fid *)opaque);
65 int ll_set_inode(struct inode *inode, void *opaque)
67 struct lu_fid *fid = opaque;
68 ll_i2info(inode)->lli_fid = *fid;
69 inode->i_ino = ll_fid_build_ino(ll_s2sbi(inode->i_sb), fid);
73 struct inode *ll_iget(struct super_block *sb, ino_t hash,
76 struct ll_inode_info *lli;
80 inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode,
83 if (inode->i_state & I_NEW) {
84 lli = ll_i2info(inode);
85 ll_read_inode2(inode, md);
86 unlock_new_inode(inode);
88 ll_update_inode(inode, md);
90 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n",
91 inode->i_ino, inode->i_generation, inode);
97 struct inode *ll_iget(struct super_block *sb, ino_t hash,
100 struct ll_inode_info *lli;
104 inode = iget_locked(sb, hash);
106 if (inode->i_state & I_NEW) {
107 lli = ll_i2info(inode);
108 ll_read_inode2(inode, md);
109 unlock_new_inode(inode);
111 ll_update_inode(inode, md);
113 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n",
114 inode->i_ino, inode->i_generation, inode);
121 struct inode *ll_iget(struct super_block *sb, ino_t hash,
122 struct lustre_md *md)
127 inode = iget4(sb, hash, NULL, md);
129 if (!(inode->i_state & (I_FREEING | I_CLEAR)))
130 ll_update_inode(inode, md);
132 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n",
133 inode->i_ino, inode->i_generation, inode);
139 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
140 void *data, int flag)
143 struct lustre_handle lockh;
147 case LDLM_CB_BLOCKING:
148 ldlm_lock2handle(lock, &lockh);
149 rc = ldlm_cli_cancel(&lockh);
151 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
155 case LDLM_CB_CANCELING: {
156 struct inode *inode = ll_inode_from_lock(lock);
157 __u64 bits = lock->l_policy_data.l_inodebits.bits;
160 /* Invalidate all dentries associated with this inode */
164 fid = ll_inode2fid(inode);
165 if (lock->l_resource->lr_name.name[0] != fid_seq(fid) ||
166 lock->l_resource->lr_name.name[1] != fid_oid(fid) ||
167 lock->l_resource->lr_name.name[2] != fid_ver(fid)) {
168 LDLM_ERROR(lock, "data mismatch with object "
169 DFID" (%p)", PFID(fid), inode);
172 if (bits & MDS_INODELOCK_OPEN) {
174 switch (lock->l_req_mode) {
181 CERROR("open PR lock without FMODE_EXEC\n");
187 CERROR("Unexpected lock mode for OPEN lock "
188 "%d, inode %ld\n", lock->l_req_mode,
191 ll_md_real_close(inode, flags);
194 if (bits & MDS_INODELOCK_UPDATE)
195 ll_i2info(inode)->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
197 if (S_ISDIR(inode->i_mode) &&
198 (bits & MDS_INODELOCK_UPDATE)) {
199 struct dentry *dentry, *tmp, *dir;
200 struct list_head *list;
202 CDEBUG(D_INODE, "invalidating inode %lu\n",
204 truncate_inode_pages(inode->i_mapping, 0);
207 /* Drop possible cached negative dentries */
208 list = &inode->i_dentry;
210 spin_lock(&dcache_lock);
212 /* It is possible to have several dentries (with
214 while ((list = list->next) != &inode->i_dentry) {
215 dir = list_entry(list, struct dentry, d_alias);
216 #ifdef LUSTRE_KERNEL_VERSION
217 if (!(dir->d_flags & DCACHE_LUSTRE_INVALID))
219 if (!d_unhashed(dir))
228 list_for_each_entry_safe(dentry, tmp,
232 /* XXX Print some debug here? */
233 if (!dentry->d_inode)
234 /* Negative dentry. If we were
235 dropping dcache lock, go
236 throught the list again */
237 if (ll_drop_dentry(dentry))
241 spin_unlock(&dcache_lock);
244 if (inode->i_sb->s_root &&
245 inode != inode->i_sb->s_root->d_inode &&
246 (bits & MDS_INODELOCK_LOOKUP))
247 ll_unhash_aliases(inode);
258 /* Pack the required supplementary groups into the supplied groups array.
259 * If we don't need to use the groups from the target inode(s) then we
260 * instead pack one or more groups from the user's supplementary group
261 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
262 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
267 LASSERT(suppgids != NULL);
269 if (in_group_p(i1->i_gid))
270 suppgids[0] = i1->i_gid;
275 if (in_group_p(i2->i_gid))
276 suppgids[1] = i2->i_gid;
283 for (i = 0; i < current_ngroups; i++) {
284 if (suppgids[0] == -1) {
285 if (current_groups[i] != suppgids[1])
286 suppgids[0] = current_groups[i];
289 if (suppgids[1] == -1) {
290 if (current_groups[i] != suppgids[0])
291 suppgids[1] = current_groups[i];
298 static void ll_d_add(struct dentry *de, struct inode *inode)
300 CDEBUG(D_DENTRY, "adding inode %p to dentry %p\n", inode, de);
302 if (!list_empty(&de->d_alias)) {
303 spin_unlock(&dcache_lock);
304 CERROR("dentry %.*s %p alias next %p, prev %p\n",
305 de->d_name.len, de->d_name.name, de,
306 de->d_alias.next, de->d_alias.prev);
310 list_add(&de->d_alias, &inode->i_dentry);
314 if (!d_unhashed(de)) {
315 spin_unlock(&dcache_lock);
316 CERROR("dentry %.*s %p hash next %p\n",
317 de->d_name.len, de->d_name.name, de, de->d_hash.next);
323 /* 2.6.15 and prior versions have buggy d_instantiate_unique that leaks an inode
324 * if suitable alias is found. But we are not going to fix it by just freeing
325 * such inode, because if some vendor's kernel contains this bugfix already,
326 * we will break everything then. We will use our own reimplementation
328 #if !defined(HAVE_D_ADD_UNIQUE) || (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16))
329 /* Search "inode"'s alias list for a dentry that has the same name and parent as
330 * de. If found, return it. If not found, return de. */
331 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
333 struct list_head *tmp;
335 spin_lock(&dcache_lock);
336 list_for_each(tmp, &inode->i_dentry) {
337 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
339 /* We are called here with 'de' already on the aliases list. */
345 if (dentry->d_parent != de->d_parent)
348 if (dentry->d_name.len != de->d_name.len)
351 if (memcmp(dentry->d_name.name, de->d_name.name,
352 de->d_name.len) != 0)
358 #ifdef LUSTRE_KERNEL_VERSION
359 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
361 unlock_dentry(dentry);
362 __d_rehash(dentry, 0); /* avoid taking dcache_lock inside */
363 spin_unlock(&dcache_lock);
365 CDEBUG(D_DENTRY, "alias dentry %.*s (%p) parent %p inode %p "
366 "refc %d\n", de->d_name.len, de->d_name.name, de,
367 de->d_parent, de->d_inode, atomic_read(&de->d_count));
373 spin_unlock(&dcache_lock);
378 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
380 struct dentry *dentry;
382 dentry = d_add_unique(de, inode);
383 #ifdef LUSTRE_KERNEL_VERSION
386 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
387 unlock_dentry(dentry);
391 return dentry?dentry:de;
395 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
396 struct lookup_intent *it, void *data)
398 struct it_cb_data *icbd = data;
399 struct dentry **de = icbd->icbd_childp;
400 struct inode *parent = icbd->icbd_parent;
401 struct ll_sb_info *sbi = ll_i2sbi(parent);
402 struct inode *inode = NULL;
405 /* NB 1 request reference will be taken away by ll_intent_lock()
407 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
410 rc = ll_prep_inode(&inode, request, offset,
415 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
416 inode, inode->i_ino, inode->i_generation);
417 md_set_lock_data(sbi->ll_md_exp,
418 &it->d.lustre.it_lock_handle, inode);
420 /* We used to query real size from OSTs here, but actually
421 this is not needed. For stat() calls size would be updated
422 from subsequent do_revalidate()->ll_inode_revalidate_it() in
424 vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
425 Everybody else who needs correct file size would call
426 ll_glimpse_size or some equivalent themselves anyway.
427 Also see bug 7198. */
429 *de = ll_find_alias(inode, *de);
432 /* Check that parent has UPDATE lock. If there is none, we
433 cannot afford to hash this dentry (done by ll_d_add) as it
434 might get picked up later when UPDATE lock will appear */
435 if (ll_have_md_lock(parent, MDS_INODELOCK_UPDATE)) {
436 spin_lock(&dcache_lock);
437 ll_d_add(*de, inode);
438 spin_unlock(&dcache_lock);
440 (*de)->d_inode = NULL;
445 (*de)->d_op = &ll_d_ops;
450 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
451 struct lookup_intent *it, int lookup_flags)
453 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
454 struct dentry *save = dentry, *retval;
455 struct ptlrpc_request *req = NULL;
456 struct md_op_data *op_data;
457 struct it_cb_data icbd;
461 if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
462 RETURN(ERR_PTR(-ENAMETOOLONG));
464 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
465 dentry->d_name.len, dentry->d_name.name, parent->i_ino,
466 parent->i_generation, parent, LL_IT2STR(it));
468 if (d_mountpoint(dentry))
469 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
471 ll_frob_intent(&it, &lookup_it);
473 icbd.icbd_childp = &dentry;
474 icbd.icbd_parent = parent;
476 /* prepare operatoin hint first */
478 op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
479 dentry->d_name.len, lookup_flags);
481 RETURN(ERR_PTR(-ENOMEM));
483 /* allocate new fid for child */
484 if (it->it_op & IT_CREAT ||
485 (it->it_op & IT_OPEN && it->it_create_mode & O_CREAT)) {
486 struct lu_placement_hint hint = { .ph_pname = NULL,
487 .ph_pfid = ll_inode2fid(parent),
488 .ph_cname = &dentry->d_name,
489 .ph_opc = LUSTRE_OPC_CREATE };
491 rc = ll_fid_md_alloc(ll_i2sbi(parent), &op_data->fid2, &hint);
493 ll_finish_md_op_data(op_data);
499 it->it_create_mode &= ~current->fs->umask;
501 rc = md_intent_lock(ll_i2mdexp(parent), op_data, NULL, 0, it,
502 lookup_flags, &req, ll_md_blocking_ast, 0);
503 ll_finish_md_op_data(op_data);
505 GOTO(out, retval = ERR_PTR(rc));
507 rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
509 ll_intent_release(it);
510 GOTO(out, retval = ERR_PTR(rc));
513 if ((it->it_op & IT_OPEN) && dentry->d_inode &&
514 !S_ISREG(dentry->d_inode->i_mode) &&
515 !S_ISDIR(dentry->d_inode->i_mode)) {
516 ll_release_openhandle(dentry, it);
518 ll_lookup_finish_locks(it, dentry);
521 GOTO(out, retval = NULL);
523 GOTO(out, retval = dentry);
526 ptlrpc_req_finished(req);
530 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
531 #ifdef LUSTRE_KERNEL_VERSION
532 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
533 struct nameidata *nd)
538 if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
539 de = ll_lookup_it(parent, dentry, &nd->intent, nd->flags);
541 de = ll_lookup_it(parent, dentry, NULL, 0);
546 struct lookup_intent *ll_convert_intent(struct open_intent *oit,
549 struct lookup_intent *it;
551 OBD_ALLOC(it, sizeof(*it));
553 return ERR_PTR(-ENOMEM);
555 if (lookup_flags & LOOKUP_OPEN) {
557 if (lookup_flags & LOOKUP_CREATE)
558 it->it_op |= IT_CREAT;
559 it->it_create_mode = oit->create_mode;
560 it->it_flags = oit->flags;
562 it->it_op = IT_GETATTR;
565 #ifndef HAVE_FILE_IN_STRUCT_INTENT
566 /* Since there is no way to pass our intent to ll_file_open,
567 * just check the file is there. Actual open will be done
569 if (it->it_op & IT_OPEN)
570 it->it_op = IT_LOOKUP;
576 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
577 struct nameidata *nd)
582 if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
583 struct lookup_intent *it;
585 #if defined(HAVE_FILE_IN_STRUCT_INTENT) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
586 /* Did we came here from failed revalidate just to propagate
588 if (nd->flags & LOOKUP_OPEN)
589 if (IS_ERR(nd->intent.open.file))
590 RETURN((struct dentry *)nd->intent.open.file);
593 if (ll_d2d(dentry) && ll_d2d(dentry)->lld_it) {
594 it = ll_d2d(dentry)->lld_it;
595 ll_d2d(dentry)->lld_it = NULL;
597 it = ll_convert_intent(&nd->intent.open, nd->flags);
599 RETURN((struct dentry *)it);
602 de = ll_lookup_it(parent, dentry, it, nd->flags);
605 if ((nd->flags & LOOKUP_OPEN) && !IS_ERR(dentry)) { /* Open */
606 if (dentry->d_inode &&
607 it_disposition(it, DISP_OPEN_OPEN)) { /* nocreate */
608 #ifdef HAVE_FILE_IN_STRUCT_INTENT
609 if (S_ISFIFO(dentry->d_inode->i_mode)) {
610 // We cannot call open here as it would
613 (struct ptlrpc_request *)
614 it->d.lustre.it_data);
617 nd->intent.open.file->private_data = it;
618 filp =lookup_instantiate_filp(nd,dentry,
620 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
621 /* 2.6.1[456] have a bug in open_namei() that forgets to check
622 * nd->intent.open.file for error, so we need to return it as lookup's result
627 de = (struct dentry *) filp;
632 #else /* HAVE_FILE_IN_STRUCT_INTENT */
633 /* Release open handle as we have no way to
634 * pass it to ll_file_open */
635 ll_release_openhandle(dentry, it);
636 #endif /* HAVE_FILE_IN_STRUCT_INTENT */
637 } else if (it_disposition(it, DISP_OPEN_CREATE)) {
638 // XXX This can only reliably work on assumption
639 // that there are NO hashed negative dentries.
640 ll_d2d(dentry)->lld_it = it;
641 it = NULL; /* Will be freed in ll_create_nd */
642 /* We absolutely depend on ll_create_nd to be
643 * called to not leak this intent and possible
644 * data attached to it */
649 ll_intent_release(it);
650 OBD_FREE(it, sizeof(*it));
653 de = ll_lookup_it(parent, dentry, NULL, 0);
661 /* We depend on "mode" being set with the proper file type/umask by now */
662 static struct inode *ll_create_node(struct inode *dir, const char *name,
663 int namelen, const void *data, int datalen,
664 int mode, __u64 extra,
665 struct lookup_intent *it)
667 struct inode *inode = NULL;
668 struct ptlrpc_request *request = NULL;
669 struct ll_sb_info *sbi = ll_i2sbi(dir);
673 LASSERT(it && it->d.lustre.it_disposition);
675 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
676 request = it->d.lustre.it_data;
677 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
678 rc = ll_prep_inode(&inode, request, DLM_REPLY_REC_OFF, dir->i_sb);
680 GOTO(out, inode = ERR_PTR(rc));
682 LASSERT(list_empty(&inode->i_dentry));
684 /* We asked for a lock on the directory, but were granted a
685 * lock on the inode. Since we finally have an inode pointer,
686 * stuff it in the lock. */
687 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode %p (%lu/%u)\n",
688 inode, inode->i_ino, inode->i_generation);
689 md_set_lock_data(sbi->ll_md_exp,
690 &it->d.lustre.it_lock_handle, inode);
693 ptlrpc_req_finished(request);
698 * By the time this is called, we already have created the directory cache
699 * entry for the new file, but it is so far negative - it has no inode.
701 * We defer creating the OBD object(s) until open, to keep the intent and
702 * non-intent code paths similar, and also because we do not have the MDS
703 * inode number before calling ll_create_node() (which is needed for LOV),
704 * so we would need to do yet another RPC to the MDS to store the LOV EA
705 * data on the MDS. If needed, we would pass the PACKED lmm as data and
706 * lmm_size in datalen (the MDS still has code which will handle that).
708 * If the create succeeds, we fill in the inode information
709 * with d_instantiate().
711 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
712 struct lookup_intent *it)
718 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
719 dentry->d_name.len, dentry->d_name.name, dir->i_ino,
720 dir->i_generation, dir, LL_IT2STR(it));
722 rc = it_open_error(DISP_OPEN_CREATE, it);
726 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
727 NULL, 0, mode, 0, it);
729 RETURN(PTR_ERR(inode));
732 d_instantiate(dentry, inode);
736 static void ll_update_times(struct ptlrpc_request *request, int offset,
739 struct mdt_body *body = lustre_msg_buf(request->rq_repmsg, offset,
743 /* mtime is always updated with ctime, but can be set in past.
744 As write and utime(2) may happen within 1 second, and utime's
745 mtime has a priority over write's one, so take mtime from mds
746 for the same ctimes. */
747 if (body->valid & OBD_MD_FLCTIME &&
748 body->ctime >= LTIME_S(inode->i_ctime)) {
749 LTIME_S(inode->i_ctime) = body->ctime;
751 if (body->valid & OBD_MD_FLMTIME) {
752 CDEBUG(D_INODE, "setting ino %lu mtime from %lu "
753 "to "LPU64"\n", inode->i_ino,
754 LTIME_S(inode->i_mtime), body->mtime);
755 LTIME_S(inode->i_mtime) = body->mtime;
760 static int ll_mknod_generic(struct inode *dir, struct qstr *name, int mode,
761 unsigned rdev, struct dentry *dchild)
763 struct ptlrpc_request *request = NULL;
764 struct inode *inode = NULL;
765 struct ll_sb_info *sbi = ll_i2sbi(dir);
766 struct md_op_data *op_data;
767 struct lu_placement_hint hint = {
769 .ph_pfid = ll_inode2fid(dir),
771 .ph_opc = LUSTRE_OPC_MKNOD
776 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p) mode %o dev %x\n",
777 name->len, name->name, dir->i_ino, dir->i_generation, dir,
780 mode &= ~current->fs->umask;
782 switch (mode & S_IFMT) {
785 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
790 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
794 err = ll_fid_md_alloc(sbi, &op_data->fid2, &hint);
796 ll_finish_md_op_data(op_data);
799 err = md_create(sbi->ll_md_exp, op_data, NULL, 0, mode,
800 current->fsuid, current->fsgid,
801 current->cap_effective, rdev, &request);
802 ll_finish_md_op_data(op_data);
805 ll_update_times(request, REPLY_REC_OFF, dir);
808 err = ll_prep_inode(&inode, request, REPLY_REC_OFF,
813 d_instantiate(dchild, inode);
822 ptlrpc_req_finished(request);
826 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
827 #ifndef LUSTRE_KERNEL_VERSION
828 static int ll_create_nd(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
830 struct lookup_intent *it = ll_d2d(dentry)->lld_it;
834 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
836 ll_d2d(dentry)->lld_it = NULL;
838 /* Was there an error? Propagate it! */
839 if (it->d.lustre.it_status) {
840 rc = it->d.lustre.it_status;
844 rc = ll_create_it(dir, dentry, mode, it);
845 #ifdef HAVE_FILE_IN_STRUCT_INTENT
846 if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
847 nd->intent.open.file->private_data = it;
848 lookup_instantiate_filp(nd, dentry, NULL);
851 ll_release_openhandle(dentry,it);
855 ll_intent_release(it);
856 OBD_FREE(it, sizeof(*it));
861 static int ll_create_nd(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
863 if (!nd || !nd->intent.d.lustre.it_disposition)
864 /* No saved request? Just mknod the file */
865 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
867 return ll_create_it(dir, dentry, mode, &nd->intent);
872 static int ll_symlink_generic(struct inode *dir, struct dentry *dchild,
875 struct qstr *name = &dchild->d_name;
876 struct lu_placement_hint hint = { .ph_pname = NULL,
877 .ph_pfid = ll_inode2fid(dir),
879 .ph_opc = LUSTRE_OPC_SYMLINK };
881 struct ptlrpc_request *request = NULL;
882 struct ll_sb_info *sbi = ll_i2sbi(dir);
883 struct inode *inode = NULL;
884 struct md_op_data *op_data;
888 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),target=%s\n",
889 name->len, name->name, dir->i_ino, dir->i_generation,
892 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0);
896 /* allocate new fid */
897 err = ll_fid_md_alloc(ll_i2sbi(dir), &op_data->fid2, &hint);
899 ll_finish_md_op_data(op_data);
903 err = md_create(sbi->ll_md_exp, op_data,
904 tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
905 current->fsuid, current->fsgid, current->cap_effective,
907 ll_finish_md_op_data(op_data);
909 ll_update_times(request, REPLY_REC_OFF, dir);
912 err = ll_prep_inode(&inode, request, REPLY_REC_OFF,
915 d_instantiate(dchild, inode);
919 ptlrpc_req_finished(request);
923 static int ll_link_generic(struct inode *src, struct inode *dir,
926 struct ll_sb_info *sbi = ll_i2sbi(dir);
927 struct ptlrpc_request *request = NULL;
928 struct md_op_data *op_data;
933 "VFS Op: inode=%lu/%u(%p), dir=%lu/%u(%p), target=%.*s\n",
934 src->i_ino, src->i_generation, src, dir->i_ino,
935 dir->i_generation, dir, name->len, name->name);
937 op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len, 0);
940 err = md_link(sbi->ll_md_exp, op_data, &request);
941 ll_finish_md_op_data(op_data);
943 ll_update_times(request, REPLY_REC_OFF, dir);
945 ptlrpc_req_finished(request);
949 static int ll_mkdir_generic(struct inode *dir, struct qstr *name,
950 int mode, struct dentry *dchild)
953 struct lu_placement_hint hint = { .ph_pname = NULL,
954 .ph_pfid = ll_inode2fid(dir),
956 .ph_opc = LUSTRE_OPC_MKDIR };
957 struct ptlrpc_request *request = NULL;
958 struct ll_sb_info *sbi = ll_i2sbi(dir);
959 struct inode *inode = NULL;
960 struct md_op_data *op_data;
964 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
965 name->len, name->name, dir->i_ino, dir->i_generation, dir);
967 mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
969 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0);
973 /* Allocate new fid. */
974 err = ll_fid_md_alloc(ll_i2sbi(dir), &op_data->fid2, &hint);
976 ll_finish_md_op_data(op_data);
980 err = md_create(sbi->ll_md_exp, op_data, NULL, 0, mode,
981 current->fsuid, current->fsgid,
982 current->cap_effective, 0, &request);
984 ll_finish_md_op_data(op_data);
986 ll_update_times(request, REPLY_REC_OFF, dir);
988 err = ll_prep_inode(&inode, request, REPLY_REC_OFF,
991 d_instantiate(dchild, inode);
995 ptlrpc_req_finished(request);
999 static int ll_rmdir_generic(struct inode *dir, struct dentry *dparent,
1002 struct ptlrpc_request *request = NULL;
1003 struct md_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);
1022 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
1024 if (op_data == NULL)
1026 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1027 ll_finish_md_op_data(op_data);
1029 ll_update_times(request, REPLY_REC_OFF, dir);
1030 ptlrpc_req_finished(request);
1034 int ll_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
1036 struct mdt_body *body;
1037 struct lov_mds_md *eadata;
1038 struct lov_stripe_md *lsm = NULL;
1039 struct obd_trans_info oti = { 0 };
1044 /* req is swabbed so this is safe */
1045 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
1047 if (!(body->valid & OBD_MD_FLEASIZE))
1050 if (body->eadatasize == 0) {
1051 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1052 GOTO(out, rc = -EPROTO);
1055 /* The MDS sent back the EA because we unlinked the last reference
1056 * to this file. Use this EA to unlink the objects on the OST.
1057 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
1058 * check it is complete and sensible. */
1059 eadata = lustre_swab_repbuf(request, REPLY_REC_OFF + 1,
1060 body->eadatasize, NULL);
1061 LASSERT(eadata != NULL);
1062 if (eadata == NULL) {
1063 CERROR("Can't unpack MDS EA data\n");
1064 GOTO(out, rc = -EPROTO);
1067 rc = obd_unpackmd(ll_i2dtexp(dir), &lsm, eadata, body->eadatasize);
1069 CERROR("obd_unpackmd: %d\n", rc);
1072 LASSERT(rc >= sizeof(*lsm));
1074 rc = obd_checkmd(ll_i2dtexp(dir), ll_i2mdexp(dir), lsm);
1076 GOTO(out_free_memmd, rc);
1080 GOTO(out_free_memmd, rc = -ENOMEM);
1082 oa->o_id = lsm->lsm_object_id;
1083 oa->o_gr = lsm->lsm_object_gr;
1084 oa->o_mode = body->mode & S_IFMT;
1085 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
1087 if (body->valid & OBD_MD_FLCOOKIE) {
1088 oa->o_valid |= OBD_MD_FLCOOKIE;
1089 oti.oti_logcookies =
1090 lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF + 2,
1091 sizeof(struct llog_cookie) *
1092 lsm->lsm_stripe_count);
1093 if (oti.oti_logcookies == NULL) {
1094 oa->o_valid &= ~OBD_MD_FLCOOKIE;
1095 body->valid &= ~OBD_MD_FLCOOKIE;
1099 rc = obd_destroy(ll_i2dtexp(dir), oa, lsm, &oti, ll_i2mdexp(dir));
1102 CERROR("obd destroy objid "LPX64" error %d\n",
1103 lsm->lsm_object_id, rc);
1105 obd_free_memmd(ll_i2dtexp(dir), &lsm);
1110 static int ll_unlink_generic(struct inode *dir, struct qstr *name)
1112 struct ptlrpc_request *request = NULL;
1113 struct md_op_data *op_data;
1116 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1117 name->len, name->name, dir->i_ino, dir->i_generation, dir);
1119 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0);
1120 if (op_data == NULL)
1122 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1123 ll_finish_md_op_data(op_data);
1128 ll_update_times(request, REPLY_REC_OFF, dir);
1130 rc = ll_objects_destroy(request, dir);
1132 ptlrpc_req_finished(request);
1136 static int ll_rename_generic(struct inode *src, struct qstr *src_name,
1137 struct inode *tgt, struct qstr *tgt_name)
1139 struct ptlrpc_request *request = NULL;
1140 struct ll_sb_info *sbi = ll_i2sbi(src);
1141 struct md_op_data *op_data;
1144 CDEBUG(D_VFSTRACE,"VFS Op:oldname=%.*s,src_dir=%lu/%u(%p),newname=%.*s,"
1145 "tgt_dir=%lu/%u(%p)\n", src_name->len, src_name->name,
1146 src->i_ino, src->i_generation, src, tgt_name->len,
1147 tgt_name->name, tgt->i_ino, tgt->i_generation, tgt);
1149 op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0);
1150 if (op_data == NULL)
1152 err = md_rename(sbi->ll_md_exp, op_data,
1153 src_name->name, src_name->len,
1154 tgt_name->name, tgt_name->len, &request);
1155 ll_finish_md_op_data(op_data);
1157 ll_update_times(request, REPLY_REC_OFF, src);
1158 ll_update_times(request, REPLY_REC_OFF, tgt);
1159 err = ll_objects_destroy(request, src);
1162 ptlrpc_req_finished(request);
1167 #ifdef LUSTRE_KERNEL_VERSION
1168 static int ll_mknod_raw(struct nameidata *nd, int mode, dev_t rdev)
1170 return ll_mknod_generic(nd->dentry->d_inode, &nd->last, mode,rdev,NULL);
1172 static int ll_rename_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1174 return ll_rename_generic(srcnd->dentry->d_inode, &srcnd->last,
1175 tgtnd->dentry->d_inode, &tgtnd->last);
1177 static int ll_link_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1179 return ll_link_generic(srcnd->dentry->d_inode, tgtnd->dentry->d_inode,
1182 static int ll_symlink_raw(struct nameidata *nd, const char *tgt)
1186 static int ll_rmdir_raw(struct nameidata *nd)
1188 return ll_rmdir_generic(nd->dentry->d_inode, nd->dentry, &nd->last);
1190 static int ll_mkdir_raw(struct nameidata *nd, int mode)
1192 return ll_mkdir_generic(nd->dentry->d_inode, &nd->last, mode, NULL);
1194 static int ll_unlink_raw(struct nameidata *nd)
1196 return ll_unlink_generic(nd->dentry->d_inode, &nd->last);
1200 static int ll_mknod(struct inode *dir, struct dentry *dchild, int mode,
1203 return ll_mknod_generic(dir, &dchild->d_name, mode,
1204 old_encode_dev(rdev), dchild);
1207 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1208 static int ll_unlink(struct inode * dir, struct dentry *dentry)
1210 return ll_unlink_generic(dir, &dentry->d_name);
1212 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1214 return ll_mkdir_generic(dir, &dentry->d_name, mode, dentry);
1216 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
1218 return ll_rmdir_generic(dir, NULL, &dentry->d_name);
1220 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1221 const char *oldname)
1223 return ll_symlink_generic(dir, dentry, oldname);
1225 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1226 struct dentry *new_dentry)
1228 return ll_link_generic(old_dentry->d_inode, dir, &new_dentry->d_name);
1230 static int ll_rename(struct inode *old_dir, struct dentry *old_dentry,
1231 struct inode *new_dir, struct dentry *new_dentry)
1233 return ll_rename_generic(old_dir, &old_dentry->d_name, new_dir,
1234 &new_dentry->d_name);
1238 struct inode_operations ll_dir_inode_operations = {
1239 #ifdef LUSTRE_KERNEL_VERSION
1240 .link_raw = ll_link_raw,
1241 .unlink_raw = ll_unlink_raw,
1242 .symlink_raw = ll_symlink_raw,
1243 .mkdir_raw = ll_mkdir_raw,
1244 .rmdir_raw = ll_rmdir_raw,
1245 .mknod_raw = ll_mknod_raw,
1246 .rename_raw = ll_rename_raw,
1247 .setattr = ll_setattr,
1248 .setattr_raw = ll_setattr_raw,
1251 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1252 .create_it = ll_create_it,
1253 .lookup_it = ll_lookup_it,
1254 .revalidate_it = ll_inode_revalidate_it,
1256 .lookup = ll_lookup_nd,
1257 .create = ll_create_nd,
1258 /* We need all these non-raw things for NFSD, to not patch it. */
1259 .unlink = ll_unlink,
1262 .symlink = ll_symlink,
1264 .rename = ll_rename,
1265 .setattr = ll_setattr,
1266 .getattr = ll_getattr,
1268 .permission = ll_inode_permission,
1269 .setxattr = ll_setxattr,
1270 .getxattr = ll_getxattr,
1271 .listxattr = ll_listxattr,
1272 .removexattr = ll_removexattr,
1275 struct inode_operations ll_special_inode_operations = {
1276 #ifdef LUSTRE_KERNEL_VERSION
1277 .setattr_raw = ll_setattr_raw,
1279 .setattr = ll_setattr,
1280 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1281 .getattr = ll_getattr,
1283 .revalidate_it = ll_inode_revalidate_it,
1285 .permission = ll_inode_permission,
1286 .setxattr = ll_setxattr,
1287 .getxattr = ll_getxattr,
1288 .listxattr = ll_listxattr,
1289 .removexattr = ll_removexattr,