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);
54 * Get an inode by inode number (already instantiated by the intent lookup).
55 * Returns inode or NULL
57 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
58 struct inode *ll_iget(struct super_block *sb, ino_t hash,
61 struct ll_inode_info *lli;
65 inode = iget_locked(sb, hash);
67 if (inode->i_state & I_NEW) {
68 lli = ll_i2info(inode);
69 ll_read_inode2(inode, md);
70 unlock_new_inode(inode);
72 if (!(inode->i_state & (I_FREEING | I_CLEAR)))
73 ll_update_inode(inode, md);
75 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n",
76 inode->i_ino, inode->i_generation, inode);
82 struct inode *ll_iget(struct super_block *sb, ino_t hash,
88 inode = iget4(sb, hash, NULL, md);
90 if (!(inode->i_state & (I_FREEING | I_CLEAR)))
91 ll_update_inode(inode, md);
93 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n",
94 inode->i_ino, inode->i_generation, inode);
100 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
101 void *data, int flag)
104 struct lustre_handle lockh;
108 case LDLM_CB_BLOCKING:
109 ldlm_lock2handle(lock, &lockh);
110 rc = ldlm_cli_cancel(&lockh);
112 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
116 case LDLM_CB_CANCELING: {
117 struct inode *inode = ll_inode_from_lock(lock);
118 __u64 bits = lock->l_policy_data.l_inodebits.bits;
121 /* Invalidate all dentries associated with this inode */
125 fid = ll_inode2fid(inode);
126 if (lock->l_resource->lr_name.name[0] != fid_seq(fid) ||
127 lock->l_resource->lr_name.name[1] != fid_oid(fid) ||
128 lock->l_resource->lr_name.name[2] != fid_ver(fid)) {
129 LDLM_ERROR(lock, "data mismatch with object "
130 DFID" (%p)", PFID(fid), inode);
133 if (bits & MDS_INODELOCK_OPEN) {
135 switch (lock->l_req_mode) {
142 CERROR("open PR lock without FMODE_EXEC\n");
148 CERROR("Unexpected lock mode for OPEN lock "
149 "%d, inode %ld\n", lock->l_req_mode,
152 ll_md_real_close(inode, flags);
155 if (bits & MDS_INODELOCK_UPDATE)
156 ll_i2info(inode)->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
158 if (S_ISDIR(inode->i_mode) &&
159 (bits & MDS_INODELOCK_UPDATE)) {
160 struct dentry *dentry, *tmp, *dir;
161 struct list_head *list;
163 CDEBUG(D_INODE, "invalidating inode %lu\n",
165 truncate_inode_pages(inode->i_mapping, 0);
168 /* Drop possible cached negative dentries */
169 list = &inode->i_dentry;
171 spin_lock(&dcache_lock);
173 /* It is possible to have several dentries (with
175 while ((list = list->next) != &inode->i_dentry) {
176 dir = list_entry(list, struct dentry, d_alias);
177 #ifdef LUSTRE_KERNEL_VERSION
178 if (!(dir->d_flags & DCACHE_LUSTRE_INVALID))
180 if (!d_unhashed(dir))
189 list_for_each_entry_safe(dentry, tmp,
193 /* XXX Print some debug here? */
194 if (!dentry->d_inode)
195 /* Negative dentry. If we were
196 dropping dcache lock, go
197 throught the list again */
198 if (ll_drop_dentry(dentry))
202 spin_unlock(&dcache_lock);
205 if (inode->i_sb->s_root &&
206 inode != inode->i_sb->s_root->d_inode &&
207 (bits & MDS_INODELOCK_LOOKUP))
208 ll_unhash_aliases(inode);
219 /* Pack the required supplementary groups into the supplied groups array.
220 * If we don't need to use the groups from the target inode(s) then we
221 * instead pack one or more groups from the user's supplementary group
222 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
223 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
228 LASSERT(suppgids != NULL);
230 if (in_group_p(i1->i_gid))
231 suppgids[0] = i1->i_gid;
236 if (in_group_p(i2->i_gid))
237 suppgids[1] = i2->i_gid;
244 for (i = 0; i < current_ngroups; i++) {
245 if (suppgids[0] == -1) {
246 if (current_groups[i] != suppgids[1])
247 suppgids[0] = current_groups[i];
250 if (suppgids[1] == -1) {
251 if (current_groups[i] != suppgids[0])
252 suppgids[1] = current_groups[i];
259 static void ll_d_add(struct dentry *de, struct inode *inode)
261 CDEBUG(D_DENTRY, "adding inode %p to dentry %p\n", inode, de);
263 if (!list_empty(&de->d_alias)) {
264 spin_unlock(&dcache_lock);
265 CERROR("dentry %.*s %p alias next %p, prev %p\n",
266 de->d_name.len, de->d_name.name, de,
267 de->d_alias.next, de->d_alias.prev);
271 list_add(&de->d_alias, &inode->i_dentry);
275 if (!d_unhashed(de)) {
276 spin_unlock(&dcache_lock);
277 CERROR("dentry %.*s %p hash next %p\n",
278 de->d_name.len, de->d_name.name, de, de->d_hash.next);
284 /* 2.6.15 and prior versions have buggy d_instantiate_unique that leaks an inode
285 * if suitable alias is found. But we are not going to fix it by just freeing
286 * such inode, because if some vendor's kernel contains this bugfix already,
287 * we will break everything then. We will use our own reimplementation
289 #if !defined(HAVE_D_ADD_UNIQUE) || (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16))
290 /* Search "inode"'s alias list for a dentry that has the same name and parent as
291 * de. If found, return it. If not found, return de. */
292 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
294 struct list_head *tmp;
296 spin_lock(&dcache_lock);
297 list_for_each(tmp, &inode->i_dentry) {
298 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
300 /* We are called here with 'de' already on the aliases list. */
306 if (dentry->d_parent != de->d_parent)
309 if (dentry->d_name.len != de->d_name.len)
312 if (memcmp(dentry->d_name.name, de->d_name.name,
313 de->d_name.len) != 0)
319 #ifdef LUSTRE_KERNEL_VERSION
320 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
322 unlock_dentry(dentry);
323 __d_rehash(dentry, 0); /* avoid taking dcache_lock inside */
324 spin_unlock(&dcache_lock);
326 CDEBUG(D_DENTRY, "alias dentry %.*s (%p) parent %p inode %p "
327 "refc %d\n", de->d_name.len, de->d_name.name, de,
328 de->d_parent, de->d_inode, atomic_read(&de->d_count));
334 spin_unlock(&dcache_lock);
339 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
341 struct dentry *dentry;
343 dentry = d_add_unique(de, inode);
344 #ifdef LUSTRE_KERNEL_VERSION
347 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
348 unlock_dentry(dentry);
352 return dentry?dentry:de;
356 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
357 struct lookup_intent *it, void *data)
359 struct it_cb_data *icbd = data;
360 struct dentry **de = icbd->icbd_childp;
361 struct inode *parent = icbd->icbd_parent;
362 struct ll_sb_info *sbi = ll_i2sbi(parent);
363 struct inode *inode = NULL;
366 /* NB 1 request reference will be taken away by ll_intent_lock()
368 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
371 rc = ll_prep_inode(&inode, request, offset,
376 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
377 inode, inode->i_ino, inode->i_generation);
378 md_set_lock_data(sbi->ll_md_exp,
379 &it->d.lustre.it_lock_handle, inode);
381 /* We used to query real size from OSTs here, but actually
382 this is not needed. For stat() calls size would be updated
383 from subsequent do_revalidate()->ll_inode_revalidate_it() in
385 vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
386 Everybody else who needs correct file size would call
387 ll_glimpse_size or some equivalent themselves anyway.
388 Also see bug 7198. */
390 *de = ll_find_alias(inode, *de);
393 /* Check that parent has UPDATE lock. If there is none, we
394 cannot afford to hash this dentry (done by ll_d_add) as it
395 might get picked up later when UPDATE lock will appear */
396 if (ll_have_md_lock(parent, MDS_INODELOCK_UPDATE)) {
397 spin_lock(&dcache_lock);
398 ll_d_add(*de, inode);
399 spin_unlock(&dcache_lock);
401 (*de)->d_inode = NULL;
406 (*de)->d_op = &ll_d_ops;
411 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
412 struct lookup_intent *it, int lookup_flags)
414 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
415 struct dentry *save = dentry, *retval;
416 struct ptlrpc_request *req = NULL;
417 struct md_op_data *op_data;
418 struct it_cb_data icbd;
422 if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
423 RETURN(ERR_PTR(-ENAMETOOLONG));
425 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
426 dentry->d_name.len, dentry->d_name.name, parent->i_ino,
427 parent->i_generation, parent, LL_IT2STR(it));
429 if (d_mountpoint(dentry))
430 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
432 ll_frob_intent(&it, &lookup_it);
434 icbd.icbd_childp = &dentry;
435 icbd.icbd_parent = parent;
437 /* prepare operatoin hint first */
439 op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
440 dentry->d_name.len, lookup_flags);
442 RETURN(ERR_PTR(-ENOMEM));
444 /* allocate new fid for child */
445 if (it->it_op & IT_CREAT ||
446 (it->it_op & IT_OPEN && it->it_create_mode & O_CREAT)) {
447 struct lu_placement_hint hint = { .ph_pname = NULL,
448 .ph_pfid = ll_inode2fid(parent),
449 .ph_cname = &dentry->d_name,
450 .ph_opc = LUSTRE_OPC_CREATE };
452 rc = ll_fid_md_alloc(ll_i2sbi(parent), &op_data->fid2, &hint);
454 ll_finish_md_op_data(op_data);
460 it->it_create_mode &= ~current->fs->umask;
462 rc = md_intent_lock(ll_i2mdexp(parent), op_data, NULL, 0, it,
463 lookup_flags, &req, ll_md_blocking_ast, 0);
464 ll_finish_md_op_data(op_data);
466 GOTO(out, retval = ERR_PTR(rc));
468 rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
470 ll_intent_release(it);
471 GOTO(out, retval = ERR_PTR(rc));
474 if ((it->it_op & IT_OPEN) && dentry->d_inode &&
475 !S_ISREG(dentry->d_inode->i_mode) &&
476 !S_ISDIR(dentry->d_inode->i_mode)) {
477 ll_release_openhandle(dentry, it);
479 ll_lookup_finish_locks(it, dentry);
482 GOTO(out, retval = NULL);
484 GOTO(out, retval = dentry);
487 ptlrpc_req_finished(req);
491 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
492 #ifdef LUSTRE_KERNEL_VERSION
493 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
494 struct nameidata *nd)
499 if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
500 de = ll_lookup_it(parent, dentry, &nd->intent, nd->flags);
502 de = ll_lookup_it(parent, dentry, NULL, 0);
507 struct lookup_intent *ll_convert_intent(struct open_intent *oit,
510 struct lookup_intent *it;
512 OBD_ALLOC(it, sizeof(*it));
514 return ERR_PTR(-ENOMEM);
516 if (lookup_flags & LOOKUP_OPEN) {
518 if (lookup_flags & LOOKUP_CREATE)
519 it->it_op |= IT_CREAT;
520 it->it_create_mode = oit->create_mode;
521 it->it_flags = oit->flags;
523 it->it_op = IT_GETATTR;
526 #ifndef HAVE_FILE_IN_STRUCT_INTENT
527 /* Since there is no way to pass our intent to ll_file_open,
528 * just check the file is there. Actual open will be done
530 if (it->it_op & IT_OPEN)
531 it->it_op = IT_LOOKUP;
537 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
538 struct nameidata *nd)
543 if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
544 struct lookup_intent *it;
546 #if defined(HAVE_FILE_IN_STRUCT_INTENT) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
547 /* Did we came here from failed revalidate just to propagate
549 if (nd->flags & LOOKUP_OPEN)
550 if (IS_ERR(nd->intent.open.file))
551 RETURN((struct dentry *)nd->intent.open.file);
554 if (ll_d2d(dentry) && ll_d2d(dentry)->lld_it) {
555 it = ll_d2d(dentry)->lld_it;
556 ll_d2d(dentry)->lld_it = NULL;
558 it = ll_convert_intent(&nd->intent.open, nd->flags);
560 RETURN((struct dentry *)it);
563 de = ll_lookup_it(parent, dentry, it, nd->flags);
566 if ((nd->flags & LOOKUP_OPEN) && !IS_ERR(dentry)) { /* Open */
567 if (dentry->d_inode &&
568 it_disposition(it, DISP_OPEN_OPEN)) { /* nocreate */
569 #ifdef HAVE_FILE_IN_STRUCT_INTENT
570 if (S_ISFIFO(dentry->d_inode->i_mode)) {
571 // We cannot call open here as it would
574 (struct ptlrpc_request *)
575 it->d.lustre.it_data);
578 nd->intent.open.file->private_data = it;
579 filp =lookup_instantiate_filp(nd,dentry,
581 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
582 /* 2.6.1[456] have a bug in open_namei() that forgets to check
583 * nd->intent.open.file for error, so we need to return it as lookup's result
588 de = (struct dentry *) filp;
593 #else /* HAVE_FILE_IN_STRUCT_INTENT */
594 /* Release open handle as we have no way to
595 * pass it to ll_file_open */
596 ll_release_openhandle(dentry, it);
597 #endif /* HAVE_FILE_IN_STRUCT_INTENT */
598 } else if (it_disposition(it, DISP_OPEN_CREATE)) {
599 // XXX This can only reliably work on assumption
600 // that there are NO hashed negative dentries.
601 ll_d2d(dentry)->lld_it = it;
602 it = NULL; /* Will be freed in ll_create_nd */
603 /* We absolutely depend on ll_create_nd to be
604 * called to not leak this intent and possible
605 * data attached to it */
610 ll_intent_release(it);
611 OBD_FREE(it, sizeof(*it));
614 de = ll_lookup_it(parent, dentry, NULL, 0);
622 /* We depend on "mode" being set with the proper file type/umask by now */
623 static struct inode *ll_create_node(struct inode *dir, const char *name,
624 int namelen, const void *data, int datalen,
625 int mode, __u64 extra,
626 struct lookup_intent *it)
628 struct inode *inode = NULL;
629 struct ptlrpc_request *request = NULL;
630 struct ll_sb_info *sbi = ll_i2sbi(dir);
634 LASSERT(it && it->d.lustre.it_disposition);
636 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
637 request = it->d.lustre.it_data;
638 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
639 rc = ll_prep_inode(&inode, request, DLM_REPLY_REC_OFF, dir->i_sb);
641 GOTO(out, inode = ERR_PTR(rc));
643 LASSERT(list_empty(&inode->i_dentry));
645 /* We asked for a lock on the directory, but were granted a
646 * lock on the inode. Since we finally have an inode pointer,
647 * stuff it in the lock. */
648 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode %p (%lu/%u)\n",
649 inode, inode->i_ino, inode->i_generation);
650 md_set_lock_data(sbi->ll_md_exp,
651 &it->d.lustre.it_lock_handle, inode);
654 ptlrpc_req_finished(request);
659 * By the time this is called, we already have created the directory cache
660 * entry for the new file, but it is so far negative - it has no inode.
662 * We defer creating the OBD object(s) until open, to keep the intent and
663 * non-intent code paths similar, and also because we do not have the MDS
664 * inode number before calling ll_create_node() (which is needed for LOV),
665 * so we would need to do yet another RPC to the MDS to store the LOV EA
666 * data on the MDS. If needed, we would pass the PACKED lmm as data and
667 * lmm_size in datalen (the MDS still has code which will handle that).
669 * If the create succeeds, we fill in the inode information
670 * with d_instantiate().
672 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
673 struct lookup_intent *it)
679 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
680 dentry->d_name.len, dentry->d_name.name, dir->i_ino,
681 dir->i_generation, dir, LL_IT2STR(it));
683 rc = it_open_error(DISP_OPEN_CREATE, it);
687 inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
688 NULL, 0, mode, 0, it);
690 RETURN(PTR_ERR(inode));
693 d_instantiate(dentry, inode);
697 static void ll_update_times(struct ptlrpc_request *request, int offset,
700 struct mdt_body *body = lustre_msg_buf(request->rq_repmsg, offset,
704 /* mtime is always updated with ctime, but can be set in past.
705 As write and utime(2) may happen within 1 second, and utime's
706 mtime has a priority over write's one, so take mtime from mds
707 for the same ctimes. */
708 if (body->valid & OBD_MD_FLCTIME &&
709 body->ctime >= LTIME_S(inode->i_ctime)) {
710 LTIME_S(inode->i_ctime) = body->ctime;
712 if (body->valid & OBD_MD_FLMTIME) {
713 CDEBUG(D_INODE, "setting ino %lu mtime from %lu "
714 "to "LPU64"\n", inode->i_ino,
715 LTIME_S(inode->i_mtime), body->mtime);
716 LTIME_S(inode->i_mtime) = body->mtime;
721 static int ll_mknod_generic(struct inode *dir, struct qstr *name, int mode,
722 unsigned rdev, struct dentry *dchild)
724 struct ptlrpc_request *request = NULL;
725 struct inode *inode = NULL;
726 struct ll_sb_info *sbi = ll_i2sbi(dir);
727 struct md_op_data *op_data;
728 struct lu_placement_hint hint = {
730 .ph_pfid = ll_inode2fid(dir),
732 .ph_opc = LUSTRE_OPC_MKNOD
737 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p) mode %o dev %x\n",
738 name->len, name->name, dir->i_ino, dir->i_generation, dir,
741 mode &= ~current->fs->umask;
743 switch (mode & S_IFMT) {
746 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
751 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
755 err = ll_fid_md_alloc(sbi, &op_data->fid2, &hint);
757 ll_finish_md_op_data(op_data);
760 err = md_create(sbi->ll_md_exp, op_data, NULL, 0, mode,
761 current->fsuid, current->fsgid,
762 current->cap_effective, rdev, &request);
763 ll_finish_md_op_data(op_data);
766 ll_update_times(request, REPLY_REC_OFF, dir);
769 err = ll_prep_inode(&inode, request, REPLY_REC_OFF,
774 d_instantiate(dchild, inode);
783 ptlrpc_req_finished(request);
787 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
788 #ifndef LUSTRE_KERNEL_VERSION
789 static int ll_create_nd(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
791 struct lookup_intent *it = ll_d2d(dentry)->lld_it;
795 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
797 ll_d2d(dentry)->lld_it = NULL;
799 /* Was there an error? Propagate it! */
800 if (it->d.lustre.it_status) {
801 rc = it->d.lustre.it_status;
805 rc = ll_create_it(dir, dentry, mode, it);
806 #ifdef HAVE_FILE_IN_STRUCT_INTENT
807 if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
808 nd->intent.open.file->private_data = it;
809 lookup_instantiate_filp(nd, dentry, NULL);
812 ll_release_openhandle(dentry,it);
816 ll_intent_release(it);
817 OBD_FREE(it, sizeof(*it));
822 static int ll_create_nd(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
824 if (!nd || !nd->intent.d.lustre.it_disposition)
825 /* No saved request? Just mknod the file */
826 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
828 return ll_create_it(dir, dentry, mode, &nd->intent);
833 static int ll_symlink_generic(struct inode *dir, struct dentry *dchild,
836 struct qstr *name = &dchild->d_name;
837 struct lu_placement_hint hint = { .ph_pname = NULL,
838 .ph_pfid = ll_inode2fid(dir),
840 .ph_opc = LUSTRE_OPC_SYMLINK };
842 struct ptlrpc_request *request = NULL;
843 struct ll_sb_info *sbi = ll_i2sbi(dir);
844 struct inode *inode = NULL;
845 struct md_op_data *op_data;
849 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),target=%s\n",
850 name->len, name->name, dir->i_ino, dir->i_generation,
853 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0);
857 /* allocate new fid */
858 err = ll_fid_md_alloc(ll_i2sbi(dir), &op_data->fid2, &hint);
860 ll_finish_md_op_data(op_data);
864 err = md_create(sbi->ll_md_exp, op_data,
865 tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
866 current->fsuid, current->fsgid, current->cap_effective,
868 ll_finish_md_op_data(op_data);
870 ll_update_times(request, REPLY_REC_OFF, dir);
873 err = ll_prep_inode(&inode, request, REPLY_REC_OFF,
876 d_instantiate(dchild, inode);
880 ptlrpc_req_finished(request);
884 static int ll_link_generic(struct inode *src, struct inode *dir,
887 struct ll_sb_info *sbi = ll_i2sbi(dir);
888 struct ptlrpc_request *request = NULL;
889 struct md_op_data *op_data;
894 "VFS Op: inode=%lu/%u(%p), dir=%lu/%u(%p), target=%.*s\n",
895 src->i_ino, src->i_generation, src, dir->i_ino,
896 dir->i_generation, dir, name->len, name->name);
898 op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len, 0);
901 err = md_link(sbi->ll_md_exp, op_data, &request);
902 ll_finish_md_op_data(op_data);
904 ll_update_times(request, REPLY_REC_OFF, dir);
906 ptlrpc_req_finished(request);
910 static int ll_mkdir_generic(struct inode *dir, struct qstr *name,
911 int mode, struct dentry *dchild)
914 struct lu_placement_hint hint = { .ph_pname = NULL,
915 .ph_pfid = ll_inode2fid(dir),
917 .ph_opc = LUSTRE_OPC_MKDIR };
918 struct ptlrpc_request *request = NULL;
919 struct ll_sb_info *sbi = ll_i2sbi(dir);
920 struct inode *inode = NULL;
921 struct md_op_data *op_data;
925 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
926 name->len, name->name, dir->i_ino, dir->i_generation, dir);
928 mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
930 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0);
934 /* Allocate new fid. */
935 err = ll_fid_md_alloc(ll_i2sbi(dir), &op_data->fid2, &hint);
937 ll_finish_md_op_data(op_data);
941 err = md_create(sbi->ll_md_exp, op_data, NULL, 0, mode,
942 current->fsuid, current->fsgid,
943 current->cap_effective, 0, &request);
945 ll_finish_md_op_data(op_data);
947 ll_update_times(request, REPLY_REC_OFF, dir);
949 err = ll_prep_inode(&inode, request, REPLY_REC_OFF,
952 d_instantiate(dchild, inode);
956 ptlrpc_req_finished(request);
960 static int ll_rmdir_generic(struct inode *dir, struct dentry *dparent,
963 struct ptlrpc_request *request = NULL;
964 struct md_op_data *op_data;
965 struct dentry *dentry;
968 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
969 name->len, name->name, dir->i_ino, dir->i_generation, dir);
971 /* Check if we have something mounted at the dir we are going to delete
972 * In such a case there would always be dentry present. */
974 dentry = d_lookup(dparent, name);
976 int mounted = d_mountpoint(dentry);
983 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
987 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
988 ll_finish_md_op_data(op_data);
990 ll_update_times(request, REPLY_REC_OFF, dir);
991 ptlrpc_req_finished(request);
995 int ll_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
997 struct mdt_body *body;
998 struct lov_mds_md *eadata;
999 struct lov_stripe_md *lsm = NULL;
1000 struct obd_trans_info oti = { 0 };
1005 /* req is swabbed so this is safe */
1006 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
1008 if (!(body->valid & OBD_MD_FLEASIZE))
1011 if (body->eadatasize == 0) {
1012 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1013 GOTO(out, rc = -EPROTO);
1016 /* The MDS sent back the EA because we unlinked the last reference
1017 * to this file. Use this EA to unlink the objects on the OST.
1018 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
1019 * check it is complete and sensible. */
1020 eadata = lustre_swab_repbuf(request, REPLY_REC_OFF + 1,
1021 body->eadatasize, NULL);
1022 LASSERT(eadata != NULL);
1023 if (eadata == NULL) {
1024 CERROR("Can't unpack MDS EA data\n");
1025 GOTO(out, rc = -EPROTO);
1028 rc = obd_unpackmd(ll_i2dtexp(dir), &lsm, eadata, body->eadatasize);
1030 CERROR("obd_unpackmd: %d\n", rc);
1033 LASSERT(rc >= sizeof(*lsm));
1035 rc = obd_checkmd(ll_i2dtexp(dir), ll_i2mdexp(dir), lsm);
1037 GOTO(out_free_memmd, rc);
1041 GOTO(out_free_memmd, rc = -ENOMEM);
1043 oa->o_id = lsm->lsm_object_id;
1044 oa->o_gr = lsm->lsm_object_gr;
1045 oa->o_mode = body->mode & S_IFMT;
1046 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
1048 if (body->valid & OBD_MD_FLCOOKIE) {
1049 oa->o_valid |= OBD_MD_FLCOOKIE;
1050 oti.oti_logcookies =
1051 lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF + 2,
1052 sizeof(struct llog_cookie) *
1053 lsm->lsm_stripe_count);
1054 if (oti.oti_logcookies == NULL) {
1055 oa->o_valid &= ~OBD_MD_FLCOOKIE;
1056 body->valid &= ~OBD_MD_FLCOOKIE;
1060 rc = obd_destroy(ll_i2dtexp(dir), oa, lsm, &oti, ll_i2mdexp(dir));
1063 CERROR("obd destroy objid "LPX64" error %d\n",
1064 lsm->lsm_object_id, rc);
1066 obd_free_memmd(ll_i2dtexp(dir), &lsm);
1071 static int ll_unlink_generic(struct inode *dir, struct qstr *name)
1073 struct ptlrpc_request *request = NULL;
1074 struct md_op_data *op_data;
1077 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1078 name->len, name->name, dir->i_ino, dir->i_generation, dir);
1080 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0);
1081 if (op_data == NULL)
1083 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1084 ll_finish_md_op_data(op_data);
1089 ll_update_times(request, REPLY_REC_OFF, dir);
1091 rc = ll_objects_destroy(request, dir);
1093 ptlrpc_req_finished(request);
1097 static int ll_rename_generic(struct inode *src, struct qstr *src_name,
1098 struct inode *tgt, struct qstr *tgt_name)
1100 struct ptlrpc_request *request = NULL;
1101 struct ll_sb_info *sbi = ll_i2sbi(src);
1102 struct md_op_data *op_data;
1105 CDEBUG(D_VFSTRACE,"VFS Op:oldname=%.*s,src_dir=%lu/%u(%p),newname=%.*s,"
1106 "tgt_dir=%lu/%u(%p)\n", src_name->len, src_name->name,
1107 src->i_ino, src->i_generation, src, tgt_name->len,
1108 tgt_name->name, tgt->i_ino, tgt->i_generation, tgt);
1110 op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0);
1111 if (op_data == NULL)
1113 err = md_rename(sbi->ll_md_exp, op_data,
1114 src_name->name, src_name->len,
1115 tgt_name->name, tgt_name->len, &request);
1116 ll_finish_md_op_data(op_data);
1118 ll_update_times(request, REPLY_REC_OFF, src);
1119 ll_update_times(request, REPLY_REC_OFF, tgt);
1120 err = ll_objects_destroy(request, src);
1123 ptlrpc_req_finished(request);
1128 #ifdef LUSTRE_KERNEL_VERSION
1129 static int ll_mknod_raw(struct nameidata *nd, int mode, dev_t rdev)
1131 return ll_mknod_generic(nd->dentry->d_inode, &nd->last, mode,rdev,NULL);
1133 static int ll_rename_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1135 return ll_rename_generic(srcnd->dentry->d_inode, &srcnd->last,
1136 tgtnd->dentry->d_inode, &tgtnd->last);
1138 static int ll_link_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1140 return ll_link_generic(srcnd->dentry->d_inode, tgtnd->dentry->d_inode,
1143 static int ll_symlink_raw(struct nameidata *nd, const char *tgt)
1147 static int ll_rmdir_raw(struct nameidata *nd)
1149 return ll_rmdir_generic(nd->dentry->d_inode, nd->dentry, &nd->last);
1151 static int ll_mkdir_raw(struct nameidata *nd, int mode)
1153 return ll_mkdir_generic(nd->dentry->d_inode, &nd->last, mode, NULL);
1155 static int ll_unlink_raw(struct nameidata *nd)
1157 return ll_unlink_generic(nd->dentry->d_inode, &nd->last);
1161 static int ll_mknod(struct inode *dir, struct dentry *dchild, int mode,
1164 return ll_mknod_generic(dir, &dchild->d_name, mode,
1165 old_encode_dev(rdev), dchild);
1168 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1169 static int ll_unlink(struct inode * dir, struct dentry *dentry)
1171 return ll_unlink_generic(dir, &dentry->d_name);
1173 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1175 return ll_mkdir_generic(dir, &dentry->d_name, mode, dentry);
1177 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
1179 return ll_rmdir_generic(dir, NULL, &dentry->d_name);
1181 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1182 const char *oldname)
1184 return ll_symlink_generic(dir, dentry, oldname);
1186 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1187 struct dentry *new_dentry)
1189 return ll_link_generic(old_dentry->d_inode, dir, &new_dentry->d_name);
1191 static int ll_rename(struct inode *old_dir, struct dentry *old_dentry,
1192 struct inode *new_dir, struct dentry *new_dentry)
1194 return ll_rename_generic(old_dir, &old_dentry->d_name, new_dir,
1195 &new_dentry->d_name);
1199 struct inode_operations ll_dir_inode_operations = {
1200 #ifdef LUSTRE_KERNEL_VERSION
1201 .link_raw = ll_link_raw,
1202 .unlink_raw = ll_unlink_raw,
1203 .symlink_raw = ll_symlink_raw,
1204 .mkdir_raw = ll_mkdir_raw,
1205 .rmdir_raw = ll_rmdir_raw,
1206 .mknod_raw = ll_mknod_raw,
1207 .rename_raw = ll_rename_raw,
1208 .setattr = ll_setattr,
1209 .setattr_raw = ll_setattr_raw,
1212 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1213 .create_it = ll_create_it,
1214 .lookup_it = ll_lookup_it,
1215 .revalidate_it = ll_inode_revalidate_it,
1217 .lookup = ll_lookup_nd,
1218 .create = ll_create_nd,
1219 /* We need all these non-raw things for NFSD, to not patch it. */
1220 .unlink = ll_unlink,
1223 .symlink = ll_symlink,
1225 .rename = ll_rename,
1226 .setattr = ll_setattr,
1227 .getattr = ll_getattr,
1229 .permission = ll_inode_permission,
1230 .setxattr = ll_setxattr,
1231 .getxattr = ll_getxattr,
1232 .listxattr = ll_listxattr,
1233 .removexattr = ll_removexattr,
1236 struct inode_operations ll_special_inode_operations = {
1237 #ifdef LUSTRE_KERNEL_VERSION
1238 .setattr_raw = ll_setattr_raw,
1240 .setattr = ll_setattr,
1241 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1242 .getattr = ll_getattr,
1244 .revalidate_it = ll_inode_revalidate_it,
1246 .permission = ll_inode_permission,
1247 .setxattr = ll_setxattr,
1248 .getxattr = ll_getxattr,
1249 .listxattr = ll_listxattr,
1250 .removexattr = ll_removexattr,