1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre Metadata Server (mds) handling of striped file data
7 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
9 * This file is part of Lustre, http://www.lustre.org.
11 * Lustre is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Lustre is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Lustre; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 # define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_MDS
30 #include <linux/module.h>
31 #include <linux/dcache.h>
32 #include <linux/namei.h>
33 #include <linux/obd_support.h>
34 #include <linux/obd_class.h>
35 #include <linux/obd.h>
36 #include <linux/lustre_lib.h>
37 #include <linux/lustre_mds.h>
38 #include <linux/lustre_idl.h>
39 #include <linux/obd_class.h>
40 #include <linux/obd_lov.h>
41 #include <linux/lustre_lib.h>
42 #include <linux/lustre_fsfilt.h>
43 #include <linux/lustre_lite.h>
45 #include "mds_internal.h"
49 * - magic in mea struct
51 int mds_md_connect(struct obd_device *obd, char *md_name)
53 struct mds_obd *mds = &obd->u.mds;
54 struct lustre_handle conn = {0};
55 int rc, valsize, value;
58 if (IS_ERR(mds->mds_md_obd))
59 RETURN(PTR_ERR(mds->mds_md_obd));
61 if (mds->mds_md_connected)
64 down(&mds->mds_md_sem);
65 if (mds->mds_md_connected) {
70 mds->mds_md_obd = class_name2obd(md_name);
71 if (!mds->mds_md_obd) {
72 CERROR("MDS cannot locate MD(LMV) %s\n",
74 mds->mds_md_obd = ERR_PTR(-ENOTCONN);
75 GOTO(err_last, rc = -ENOTCONN);
78 rc = obd_connect(&conn, mds->mds_md_obd,
79 &obd->obd_uuid, OBD_OPT_MDS_CONNECTION);
81 CERROR("MDS cannot connect to MD(LMV) %s (%d)\n",
83 mds->mds_md_obd = ERR_PTR(rc);
86 mds->mds_md_exp = class_conn2export(&conn);
87 if (mds->mds_md_exp == NULL)
88 CERROR("can't get export!\n");
90 rc = obd_register_observer(mds->mds_md_obd, obd);
92 CERROR("MDS cannot register as observer of MD(LMV) %s, "
93 "rc = %d\n", md_name, rc);
97 /* retrieve size of EA */
98 rc = obd_get_info(mds->mds_md_exp, strlen("mdsize"),
99 "mdsize", &valsize, &value);
103 if (value > mds->mds_max_mdsize)
104 mds->mds_max_mdsize = value;
106 /* find our number in LMV cluster */
107 rc = obd_get_info(mds->mds_md_exp, strlen("mdsnum"),
108 "mdsnum", &valsize, &value);
112 mds->mds_num = value;
114 rc = obd_set_info(mds->mds_md_exp, strlen("inter_mds"),
115 "inter_mds", 0, NULL);
119 if (mds->mds_mds_sec) {
120 rc = obd_set_info(mds->mds_md_exp, strlen("sec"), "sec",
121 strlen(mds->mds_mds_sec), mds->mds_mds_sec);
126 mds->mds_md_connected = 1;
127 up(&mds->mds_md_sem);
131 obd_register_observer(mds->mds_md_obd, NULL);
133 obd_disconnect(mds->mds_md_exp, 0);
134 mds->mds_md_exp = NULL;
135 mds->mds_md_obd = ERR_PTR(rc);
137 up(&mds->mds_md_sem);
141 int mds_md_postsetup(struct obd_device *obd)
143 struct mds_obd *mds = &obd->u.mds;
147 if (mds->mds_md_exp) {
148 rc = obd_init_ea_size(mds->mds_md_exp,
150 mds->mds_max_cookiesize);
156 int mds_md_disconnect(struct obd_device *obd, int flags)
158 struct mds_obd *mds = &obd->u.mds;
162 if (!mds->mds_md_connected)
165 down(&mds->mds_md_sem);
166 if (!IS_ERR(mds->mds_md_obd) && mds->mds_md_exp != NULL) {
167 LASSERT(mds->mds_md_connected);
169 obd_register_observer(mds->mds_md_obd, NULL);
171 if (flags & OBD_OPT_FORCE) {
172 struct obd_device *lmv_obd;
173 struct obd_ioctl_data ioc_data = { 0 };
175 lmv_obd = class_exp2obd(mds->mds_md_exp);
180 * making disconnecting lmv stuff do not send anything
181 * to all remote MDSs from LMV. This is needed to
182 * prevent possible hanging with endless recovery, when
183 * MDS sends disconnect to already disconnected
184 * target. Probably this is wrong, but client does the
185 * same in --force mode and I do not see why can't we do
188 lmv_obd->obd_no_recov = 1;
189 obd_iocontrol(IOC_OSC_SET_ACTIVE, mds->mds_md_exp,
190 sizeof(ioc_data), &ioc_data, NULL);
194 * if obd_disconnect() fails (probably because the export was
195 * disconnected by class_disconnect_exports()) then we just need
198 mds->mds_md_connected = 0;
199 rc = obd_disconnect(mds->mds_md_exp, flags);
201 class_export_put(mds->mds_md_exp);
204 mds->mds_md_exp = NULL;
205 mds->mds_md_obd = NULL;
207 up(&mds->mds_md_sem);
211 int mds_md_get_attr(struct obd_device *obd, struct inode *inode,
212 struct mea **mea, int *mea_size)
214 struct mds_obd *mds = &obd->u.mds;
218 if (!mds->mds_md_obd)
220 if (!S_ISDIR(inode->i_mode))
223 /* first calculate mea size */
224 *mea_size = obd_alloc_diskmd(mds->mds_md_exp,
225 (struct lov_mds_md **)mea);
226 if (*mea_size < 0 || *mea == NULL)
227 return *mea_size < 0 ? *mea_size : -EINVAL;
229 rc = mds_get_md(obd, inode, *mea, mea_size, 1, 1);
232 OBD_FREE(*mea, *mea_size);
250 #define DIR_ROUND (DIR_PAD - 1)
251 #define DIR_REC_LEN(name_len) (((name_len) + 16 + DIR_ROUND) & ~DIR_ROUND)
253 /* this struct holds dir entries for particular MDS to be flushed */
255 struct list_head list;
260 struct brw_page brwc;
263 struct dirsplit_control {
264 struct obd_device *obd;
266 struct dentry *dentry;
268 struct dir_cache *cache;
271 static int dc_new_page_to_cache(struct dir_cache * dirc)
275 if (!list_empty(&dirc->list) && dirc->free > sizeof(__u16)) {
276 /* current page became full, mark the end */
277 struct dir_entry *de = dirc->cur;
281 page = alloc_page(GFP_KERNEL);
284 list_add_tail(&page->lru, &dirc->list);
285 dirc->cur = page_address(page);
286 dirc->free = PAGE_SIZE;
290 static int retrieve_generation_numbers(struct dirsplit_control *dc, void *buf)
292 struct mds_obd *mds = &dc->obd->u.mds;
293 struct dir_entry *de;
294 struct dentry *dentry;
297 end = buf + PAGE_SIZE;
298 de = (struct dir_entry *) buf;
299 while ((char *) de < end && de->namelen) {
300 /* lookup an inode */
301 LASSERT(de->namelen <= 255);
302 dentry = ll_lookup_one_len(de->name, dc->dentry,
304 if (IS_ERR(dentry)) {
305 CERROR("can't lookup %*s: %d\n", de->namelen,
306 de->name, (int) PTR_ERR(dentry));
309 if (dentry->d_inode != NULL) {
311 struct lustre_id sid;
313 down(&dentry->d_inode->i_sem);
314 rc = mds_read_inode_sid(dc->obd,
315 dentry->d_inode, &sid);
316 up(&dentry->d_inode->i_sem);
318 CERROR("Can't read inode self id, "
319 "inode %lu, rc %d\n",
320 dentry->d_inode->i_ino, rc);
324 de->fid = id_fid(&sid);
325 de->mds = mds->mds_num;
326 de->ino = dentry->d_inode->i_ino;
327 de->generation = dentry->d_inode->i_generation;
328 } else if (dentry->d_flags & DCACHE_CROSS_REF) {
329 de->fid = dentry->d_fid;
330 de->ino = dentry->d_inum;
331 de->mds = dentry->d_mdsnum;
332 de->generation = dentry->d_generation;
334 CERROR("can't lookup %*s\n", de->namelen, de->name);
340 de = (struct dir_entry *)
341 ((char *) de + DIR_REC_LEN(de->namelen));
346 static int flush_buffer_onto_mds(struct dirsplit_control *dc, int mdsnum)
348 struct mds_obd *mds = &dc->obd->u.mds;
349 struct list_head *cur, *tmp;
350 struct dir_cache *ca;
353 ca = dc->cache + mdsnum;
355 if (ca->free > sizeof(__u16)) {
356 /* current page became full, mark the end */
357 struct dir_entry *de = ca->cur;
361 list_for_each_safe(cur, tmp, &ca->list) {
364 page = list_entry(cur, struct page, lru);
365 LASSERT(page != NULL);
367 retrieve_generation_numbers(dc, page_address(page));
370 ca->brwc.disk_offset = ca->brwc.page_offset = 0;
371 ca->brwc.count = PAGE_SIZE;
373 ca->oa.o_mds = mdsnum;
374 rc = obd_brw(OBD_BRW_WRITE, mds->mds_md_exp, &ca->oa,
375 (struct lov_stripe_md *) dc->mea,
384 static int remove_entries_from_orig_dir(struct dirsplit_control *dc, int mdsnum)
386 struct list_head *cur, *tmp;
387 struct dentry *dentry;
388 struct dir_cache *ca;
389 struct dir_entry *de;
395 ca = dc->cache + mdsnum;
396 list_for_each_safe(cur, tmp, &ca->list) {
397 page = list_entry(cur, struct page, lru);
398 buf = page_address(page);
399 end = buf + PAGE_SIZE;
401 de = (struct dir_entry *) buf;
402 while ((char *) de < end && de->namelen) {
403 /* lookup an inode */
404 LASSERT(de->namelen <= 255);
406 dentry = ll_lookup_one_len(de->name, dc->dentry,
408 if (IS_ERR(dentry)) {
409 CERROR("can't lookup %*s: %d\n", de->namelen,
410 de->name, (int) PTR_ERR(dentry));
413 rc = fsfilt_del_dir_entry(dc->obd, dentry);
416 de = (struct dir_entry *)
417 ((char *) de + DIR_REC_LEN(de->namelen));
423 static int filldir(void * __buf, const char * name, int namlen,
424 loff_t offset, ino_t ino, unsigned int d_type)
426 struct dirsplit_control *dc = __buf;
427 struct mds_obd *mds = &dc->obd->u.mds;
428 struct dir_cache *ca;
429 struct dir_entry *de;
434 if (name[0] == '.' &&
435 (namlen == 1 || (namlen == 2 && name[1] == '.'))) {
436 /* skip special entries */
441 newmds = mea_name2idx(dc->mea, (char *) name, namlen);
443 if (newmds == mds->mds_num) {
444 /* this entry remains on the current MDS, skip moving */
448 OBD_ALLOC(n, namlen + 1);
449 memcpy(n, name, namlen);
450 n[namlen] = (char) 0;
452 OBD_FREE(n, namlen + 1);
454 /* check for space in buffer for new entry */
455 ca = dc->cache + newmds;
456 if (DIR_REC_LEN(namlen) > ca->free) {
457 int err = dc_new_page_to_cache(ca);
461 /* insert found entry into buffer to be flushed later */
462 /* NOTE: we'll fill generations number later, because we
463 * it's stored in inode, thus we need to lookup an entry,
464 * but directory is locked for readdir(), so we delay this */
468 de->namelen = namlen;
469 memcpy(de->name, name, namlen);
470 ca->cur += DIR_REC_LEN(namlen);
471 ca->free -= DIR_REC_LEN(namlen);
477 int scan_and_distribute(struct obd_device *obd, struct dentry *dentry,
480 struct inode *dir = dentry->d_inode;
481 struct dirsplit_control dc;
486 nlen = strlen("__iopen__/") + 10 + 1;
487 OBD_ALLOC(file_name, nlen);
491 i = sprintf(file_name, "__iopen__/0x%lx",
492 dentry->d_inode->i_ino);
494 file = filp_open(file_name, O_RDONLY, 0);
496 CERROR("can't open directory %s: %d\n",
497 file_name, (int) PTR_ERR(file));
498 OBD_FREE(file_name, nlen);
499 RETURN(PTR_ERR(file));
502 memset(&dc, 0, sizeof(dc));
507 OBD_ALLOC(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
508 LASSERT(dc.cache != NULL);
509 for (i = 0; i < mea->mea_count; i++) {
510 INIT_LIST_HEAD(&dc.cache[i].list);
511 dc.cache[i].free = 0;
512 dc.cache[i].cached = 0;
515 err = vfs_readdir(file, filldir, &dc);
520 for (i = 0; i < mea->mea_count; i++) {
521 if (!dc.cache[i].cached)
523 err = flush_buffer_onto_mds(&dc, i);
528 for (i = 0; i < mea->mea_count; i++) {
529 if (!dc.cache[i].cached)
531 err = remove_entries_from_orig_dir(&dc, i);
538 for (i = 0; i < mea->mea_count; i++) {
539 struct list_head *cur, *tmp;
540 if (!dc.cache[i].cached)
542 list_for_each_safe(cur, tmp, &dc.cache[i].list) {
544 page = list_entry(cur, struct page, lru);
545 list_del(&page->lru);
549 OBD_FREE(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
550 OBD_FREE(file_name, nlen);
555 #define MAX_DIR_SIZE (64 * 1024)
556 #define I_NON_SPLITTABLE (256)
558 int mds_splitting_expected(struct obd_device *obd, struct dentry *dentry)
560 struct mds_obd *mds = &obd->u.mds;
561 struct mea *mea = NULL;
565 if (!mds->mds_md_obd)
566 return MDS_NO_SPLITTABLE;
569 if (dentry->d_inode == NULL)
570 return MDS_NO_SPLITTABLE;
572 /* a dir can be splitted only */
573 if (!S_ISDIR(dentry->d_inode->i_mode))
574 return MDS_NO_SPLITTABLE;
576 /* already splittied or slave directory (part of splitted dir) */
577 if (dentry->d_inode->i_flags & I_NON_SPLITTABLE)
578 return MDS_NO_SPLITTABLE;
580 /* don't split root directory */
581 if (dentry->d_inode->i_ino == id_ino(&mds->mds_rootid))
582 return MDS_NO_SPLITTABLE;
584 /* large enough to be splitted? */
585 if (dentry->d_inode->i_size < MAX_DIR_SIZE)
586 return MDS_NO_SPLIT_EXPECTED;
588 mds_md_get_attr(obd, dentry->d_inode, &mea, &size);
590 /* already splitted or slave object: shouldn't be splitted */
591 rc = MDS_NO_SPLITTABLE;
592 /* mark to skip subsequent checks */
593 dentry->d_inode->i_flags |= I_NON_SPLITTABLE;
596 /* may be splitted */
597 rc = MDS_EXPECT_SPLIT;
604 * must not be called on already splitted directories.
606 int mds_try_to_split_dir(struct obd_device *obd, struct dentry *dentry,
607 struct mea **mea, int nstripes, int update_mode)
609 struct inode *dir = dentry->d_inode;
610 struct mds_obd *mds = &obd->u.mds;
611 struct mea *tmea = NULL;
612 struct obdo *oa = NULL;
613 int rc, mea_size = 0;
618 if (update_mode != LCK_EX)
621 /* TODO: optimization possible - we already may have mea here */
622 rc = mds_splitting_expected(obd, dentry);
623 if (rc == MDS_NO_SPLITTABLE)
625 if (rc == MDS_NO_SPLIT_EXPECTED && nstripes == 0)
627 if (nstripes && nstripes == 1)
630 LASSERT(mea == NULL || *mea == NULL);
632 CDEBUG(D_OTHER, "%s: split directory %u/%lu/%lu\n",
633 obd->obd_name, mds->mds_num, dir->i_ino,
634 (unsigned long) dir->i_generation);
638 mea_size = obd_size_diskmd(mds->mds_md_exp, NULL);
640 /* FIXME: Actually we may only want to allocate enough space for
641 * necessary amount of stripes, but on the other hand with this
642 * approach of allocating maximal possible amount of MDS slots,
643 * it would be easier to split the dir over more MDSes */
644 rc = obd_alloc_diskmd(mds->mds_md_exp, (void *)mea);
646 CERROR("obd_alloc_diskmd() failed, error %d.\n", rc);
652 (*mea)->mea_count = nstripes;
654 /* 1) create directory objects on slave MDS'es */
655 /* FIXME: should this be OBD method? */
660 oa->o_generation = dir->i_generation;
662 obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
663 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
664 OBD_MD_FLUID | OBD_MD_FLGID);
666 oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
667 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
668 oa->o_mode = dir->i_mode;
671 * until lmv_obd_create() properly rewritten, it is important to have
672 * here oa->o_id = dir->i_ino, as otherwise master object will have
673 * invalid store cookie (zero inode num), what will lead to -ESTALE in
674 * mds_open() or somewhere else.
676 oa->o_id = dir->i_ino;
679 rc = mds_read_inode_sid(obd, dir, &id);
682 CERROR("Can't read inode self id, inode %lu, "
683 "rc %d.\n", dir->i_ino, rc);
686 oa->o_fid = id_fid(&id);
687 oa->o_mds = mds->mds_num;
688 LASSERT(oa->o_fid != 0);
690 CDEBUG(D_OTHER, "%s: create subdirs with mode %o, uid %u, gid %u\n",
691 obd->obd_name, dir->i_mode, dir->i_uid, dir->i_gid);
693 rc = obd_create(mds->mds_md_exp, oa,
694 (struct lov_stripe_md **)mea, NULL);
696 CERROR("Can't create remote inode, rc = %d\n", rc);
700 LASSERT(id_fid(&(*mea)->mea_ids[0]));
701 CDEBUG(D_OTHER, "%d dirobjects created\n", (int)(*mea)->mea_count);
703 /* 2) update dir attribute */
706 handle = fsfilt_start(obd, dir, FSFILT_OP_SETATTR, NULL);
707 if (IS_ERR(handle)) {
709 CERROR("fsfilt_start() failed: %d\n", (int) PTR_ERR(handle));
710 GOTO(err_oa, rc = PTR_ERR(handle));
713 rc = fsfilt_set_md(obd, dir, handle, *mea, mea_size, EA_MEA);
716 CERROR("fsfilt_set_md() failed, error %d.\n", rc);
720 rc = fsfilt_commit(obd, mds->mds_sb, dir, handle, 0);
723 CERROR("fsfilt_commit() failed, error %d.\n", rc);
730 /* 3) read through the dir and distribute it over objects */
731 rc = scan_and_distribute(obd, dentry, *mea);
733 obd_free_diskmd(mds->mds_md_exp, (struct lov_mds_md **)mea);
735 CERROR("scan_and_distribute() failed, error %d.\n", rc);
746 static int filter_start_page_write(struct inode *inode,
747 struct niobuf_local *lnb)
749 struct page *page = alloc_pages(GFP_HIGHUSER, 0);
751 CERROR("no memory for a temp page\n");
752 return lnb->rc = -ENOMEM;
754 POISON_PAGE(page, 0xf1);
755 page->index = lnb->offset >> PAGE_SHIFT;
760 struct dentry *filter_id2dentry(struct obd_device *obd,
761 struct dentry *dir_dentry,
762 obd_gr group, obd_id id);
764 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
765 int objcount, struct obd_ioobj *obj,
766 int niocount, struct niobuf_remote *nb,
767 struct niobuf_local *res,
768 struct obd_trans_info *oti)
770 struct niobuf_remote *rnb;
771 struct niobuf_local *lnb = NULL;
772 int rc = 0, i, tot_bytes = 0;
773 unsigned long now = jiffies;
774 struct dentry *dentry;
778 LASSERT(objcount == 1);
779 LASSERT(obj->ioo_bufcnt > 0);
781 memset(res, 0, niocount * sizeof(*res));
785 id_ino(&id) = obj->ioo_id;
786 id_gen(&id) = obj->ioo_gr;
788 dentry = mds_id2dentry(exp->exp_obd, &id, NULL);
789 if (IS_ERR(dentry)) {
790 CERROR("can't get dentry for "LPU64"/%u: %d\n",
791 id.li_stc.u.e3s.l3s_ino, id.li_stc.u.e3s.l3s_gen,
792 (int)PTR_ERR(dentry));
793 GOTO(cleanup, rc = (int) PTR_ERR(dentry));
796 if (dentry->d_inode == NULL) {
797 CERROR("trying to BRW to non-existent file "LPU64"\n",
800 GOTO(cleanup, rc = -ENOENT);
803 if (time_after(jiffies, now + 15 * HZ))
804 CERROR("slow preprw_write setup %lus\n", (jiffies - now) / HZ);
806 CDEBUG(D_INFO, "preprw_write setup: %lu jiffies\n",
809 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
811 lnb->dentry = dentry;
812 lnb->offset = rnb->offset;
814 lnb->flags = rnb->flags;
816 rc = filter_start_page_write(dentry->d_inode, lnb);
818 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR, "page err %u@"
819 LPU64" %u/%u %p: rc %d\n", lnb->len, lnb->offset,
820 i, obj->ioo_bufcnt, dentry, rc);
822 __free_pages(lnb->page, 0);
826 tot_bytes += lnb->len;
829 if (time_after(jiffies, now + 15 * HZ))
830 CERROR("slow start_page_write %lus\n", (jiffies - now) / HZ);
832 CDEBUG(D_INFO, "start_page_write: %lu jiffies\n",
840 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
841 int objcount, struct obd_ioobj *obj, int niocount,
842 struct niobuf_local *res, struct obd_trans_info *oti,
845 struct obd_device *obd = exp->exp_obd;
846 struct niobuf_local *lnb;
847 struct inode *inode = NULL;
848 int rc = 0, i, cleanup_phase = 0, err, entries = 0;
851 LASSERT(objcount == 1);
852 LASSERT(current->journal_info == NULL);
855 inode = res->dentry->d_inode;
857 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
859 struct dir_entry *de;
861 buf = kmap(lnb->page);
862 LASSERT(buf != NULL);
863 end = buf + lnb->len;
864 de = (struct dir_entry *) buf;
865 while ((char *) de < end && de->namelen) {
866 err = fsfilt_add_dir_entry(obd, res->dentry, de->name,
867 de->namelen, de->ino,
868 de->generation, de->mds,
871 CERROR("can't add dir entry %*s->%u/%u/%u"
872 " to %lu/%u: %d\n", de->namelen,
873 de->name, de->mds, (unsigned)de->ino,
874 (unsigned) de->generation,
875 res->dentry->d_inode->i_ino,
876 res->dentry->d_inode->i_generation,
882 de = (struct dir_entry *)
883 ((char *) de + DIR_REC_LEN(de->namelen));
889 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++)
890 __free_page(lnb->page);
896 int mds_choose_mdsnum(struct obd_device *obd, const char *name, int len, int flags)
899 struct mds_obd *mds = &obd->u.mds;
900 int i = mds->mds_num;
902 if (flags & REC_REINT_CREATE) {
904 } else if (mds->mds_md_exp) {
905 lmv = &mds->mds_md_exp->exp_obd->u.lmv;
906 i = raw_name2idx(MEA_MAGIC_LAST_CHAR, lmv->desc.ld_tgt_count, name, len);
911 int mds_lock_slave_objs(struct obd_device *obd, struct dentry *dentry,
912 struct lustre_handle **rlockh)
914 struct mds_obd *mds = &obd->u.mds;
915 struct mdc_op_data *op_data;
916 struct lookup_intent it;
917 struct mea *mea = NULL;
922 LASSERT(rlockh != NULL);
923 LASSERT(dentry != NULL);
924 LASSERT(dentry->d_inode != NULL);
927 if (!mds->mds_md_obd)
930 /* a dir can be splitted only */
931 if (!S_ISDIR(dentry->d_inode->i_mode))
934 rc = mds_md_get_attr(obd, dentry->d_inode,
942 if (mea->mea_count == 0)
943 /* this is slave object */
944 GOTO(cleanup, rc = 0);
946 CDEBUG(D_OTHER, "%s: lock slaves for %lu/%lu\n",
947 obd->obd_name, (unsigned long)dentry->d_inode->i_ino,
948 (unsigned long)dentry->d_inode->i_generation);
950 handle_size = sizeof(struct lustre_handle) *
953 OBD_ALLOC(*rlockh, handle_size);
955 GOTO(cleanup, rc = -ENOMEM);
957 memset(*rlockh, 0, handle_size);
958 OBD_ALLOC(op_data, sizeof(*op_data));
959 if (op_data == NULL) {
960 OBD_FREE(*rlockh, handle_size);
963 memset(op_data, 0, sizeof(*op_data));
966 it.it_op = IT_UNLINK;
968 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
970 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
971 op_data, *rlockh, NULL, 0, ldlm_completion_ast,
972 mds_blocking_ast, NULL);
973 OBD_FREE(op_data, sizeof(*op_data));
974 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
977 OBD_FREE(mea, mea_size);
981 void mds_unlock_slave_objs(struct obd_device *obd, struct dentry *dentry,
982 struct lustre_handle *lockh)
984 struct mds_obd *mds = &obd->u.mds;
985 struct mea *mea = NULL;
994 LASSERT(mds->mds_md_obd != NULL);
995 LASSERT(S_ISDIR(dentry->d_inode->i_mode));
997 rc = mds_md_get_attr(obd, dentry->d_inode, &mea, &mea_size);
999 CERROR("locks are leaked\n");
1003 LASSERT(mea_size != 0);
1004 LASSERT(mea != NULL);
1005 LASSERT(mea->mea_count != 0);
1007 CDEBUG(D_OTHER, "%s: unlock slaves for %lu/%lu\n", obd->obd_name,
1008 (unsigned long) dentry->d_inode->i_ino,
1009 (unsigned long) dentry->d_inode->i_generation);
1011 for (i = 0; i < mea->mea_count; i++) {
1012 if (lockh[i].cookie != 0)
1013 ldlm_lock_decref(lockh + i, LCK_EX);
1016 OBD_FREE(lockh, sizeof(struct lustre_handle) * mea->mea_count);
1017 OBD_FREE(mea, mea_size);
1021 int mds_unlink_slave_objs(struct obd_device *obd, struct dentry *dentry)
1023 struct mds_obd *mds = &obd->u.mds;
1024 struct ptlrpc_request *req = NULL;
1025 struct mdc_op_data *op_data;
1026 struct mea *mea = NULL;
1030 /* clustered MD ? */
1031 if (!mds->mds_md_obd)
1034 /* a dir can be splitted only */
1035 if (!S_ISDIR(dentry->d_inode->i_mode))
1038 rc = mds_md_get_attr(obd, dentry->d_inode, &mea, &mea_size);
1045 if (mea->mea_count == 0)
1046 GOTO(cleanup, rc = 0);
1048 CDEBUG(D_OTHER, "%s: unlink slaves for %lu/%lu\n", obd->obd_name,
1049 (unsigned long)dentry->d_inode->i_ino,
1050 (unsigned long)dentry->d_inode->i_generation);
1052 OBD_ALLOC(op_data, sizeof(*op_data));
1053 if (op_data == NULL)
1056 memset(op_data, 0, sizeof(*op_data));
1057 op_data->mea1 = mea;
1058 rc = md_unlink(mds->mds_md_exp, op_data, &req);
1059 OBD_FREE(op_data, sizeof(*op_data));
1060 LASSERT(req == NULL);
1063 OBD_FREE(mea, mea_size);
1067 struct ide_tracking {
1072 int mds_ide_filldir(void *__buf, const char *name, int namelen,
1073 loff_t offset, ino_t ino, unsigned int d_type)
1075 struct ide_tracking *it = __buf;
1081 if (it->entries > 2)
1085 if (name[0] == '.' && namelen == 1)
1087 if (name[0] == '.' && name[1] == '.' && namelen == 2)
1094 /* checks if passed dentry points to empty dir. */
1095 int mds_is_dir_empty(struct obd_device *obd, struct dentry *dentry)
1097 struct ide_tracking it;
1105 nlen = strlen("__iopen__/") + 10 + 1;
1106 OBD_ALLOC(file_name, nlen);
1110 LASSERT(dentry->d_inode != NULL);
1111 i = sprintf(file_name, "__iopen__/0x%lx",
1112 dentry->d_inode->i_ino);
1114 file = filp_open(file_name, O_RDONLY, 0);
1116 CERROR("can't open directory %s: %d\n",
1117 file_name, (int) PTR_ERR(file));
1118 GOTO(cleanup, rc = PTR_ERR(file));
1121 rc = vfs_readdir(file, mds_ide_filldir, &it);
1122 filp_close(file, 0);
1124 if (it.empty && rc == 0)
1130 OBD_FREE(file_name, nlen);
1134 int mds_lock_and_check_slave(int offset, struct ptlrpc_request *req,
1135 struct lustre_handle *lockh)
1137 struct obd_device *obd = req->rq_export->exp_obd;
1138 struct dentry *dentry = NULL;
1139 struct lvfs_run_ctxt saved;
1140 int cleanup_phase = 0;
1141 struct mds_req_sec_desc *rsd;
1142 struct mds_body *body;
1143 struct lvfs_ucred uc;
1144 int rc, update_mode;
1147 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1149 CERROR("Can't unpack security desc\n");
1150 GOTO(cleanup, rc = -EFAULT);
1153 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1154 lustre_swab_mds_body);
1156 CERROR("Can't swab mds_body\n");
1157 GOTO(cleanup, rc = -EFAULT);
1159 CDEBUG(D_OTHER, "%s: check slave "DLID4"\n", obd->obd_name,
1162 dentry = mds_id2locked_dentry(obd, &body->id1, NULL, LCK_EX,
1163 lockh, &update_mode, NULL, 0,
1164 MDS_INODELOCK_UPDATE);
1165 if (IS_ERR(dentry)) {
1166 CERROR("can't find inode: %d\n", (int) PTR_ERR(dentry));
1167 GOTO(cleanup, rc = PTR_ERR(dentry));
1172 * handling the case when remote MDS checks if dir is empty
1173 * before rename. But it also does it for all entries, because
1174 * inode is stored here and remote MDS does not know if rename
1175 * point to dir or to reg file. So we check it here.
1177 if (!S_ISDIR(dentry->d_inode->i_mode))
1178 GOTO(cleanup, rc = 0);
1180 rc = mds_init_ucred(&uc, req, rsd);
1182 CERROR("can't init ucred\n");
1186 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1187 rc = mds_is_dir_empty(obd, dentry) ? 0 : -ENOTEMPTY;
1188 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1190 mds_exit_ucred(&uc);
1193 switch(cleanup_phase) {
1196 ldlm_lock_decref(lockh, LCK_EX);
1204 int mds_convert_mea_ea(struct obd_device *obd, struct inode *inode,
1205 struct lov_mds_md *lmm, int lmm_size)
1207 struct lov_stripe_md *lsm = NULL;
1208 struct mea_old *old;
1214 mea = (struct mea *)lmm;
1215 old = (struct mea_old *)lmm;
1217 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
1218 mea->mea_magic == MEA_MAGIC_ALL_CHARS)
1222 * making MDS try LOV EA converting in the non-LMV configuration
1225 if (!obd->u.mds.mds_md_exp)
1228 CDEBUG(D_INODE, "converting MEA EA on %lu/%u from V0 to V1 (%u/%u)\n",
1229 inode->i_ino, inode->i_generation, old->mea_count,
1232 rc = obd_unpackmd(obd->u.mds.mds_md_exp, &lsm, lmm, lmm_size);
1236 rc = obd_packmd(obd->u.mds.mds_md_exp, &lmm, lsm);
1238 GOTO(conv_free, rc);
1241 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1242 if (IS_ERR(handle)) {
1243 rc = PTR_ERR(handle);
1244 GOTO(conv_free, rc);
1247 rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size, EA_MEA);
1248 err = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 0);
1250 rc = err ? err : lmm_size;
1251 GOTO(conv_free, rc);
1253 obd_free_memmd(obd->u.mds.mds_md_exp, &lsm);