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/lustre_mds.h>
32 #include <linux/lustre_idl.h>
33 #include <linux/obd_class.h>
34 #include <linux/obd_lov.h>
35 #include <linux/lustre_lib.h>
36 #include <linux/lustre_fsfilt.h>
38 #include "mds_internal.h"
43 * - magic in mea struct
44 * - error handling is totally missed
47 int mds_lmv_connect(struct obd_device *obd, char *lmv_name)
49 struct mds_obd *mds = &obd->u.mds;
50 struct lustre_handle conn = {0};
51 int rc, valsize, value;
54 if (IS_ERR(mds->mds_lmv_obd))
55 RETURN(PTR_ERR(mds->mds_lmv_obd));
57 if (mds->mds_lmv_connected)
60 down(&mds->mds_lmv_sem);
61 if (mds->mds_lmv_connected) {
62 up(&mds->mds_lmv_sem);
66 mds->mds_lmv_obd = class_name2obd(lmv_name);
67 if (!mds->mds_lmv_obd) {
68 CERROR("MDS cannot locate LMV %s\n",
70 mds->mds_lmv_obd = ERR_PTR(-ENOTCONN);
71 GOTO(err_last, rc = -ENOTCONN);
74 rc = obd_connect(&conn, mds->mds_lmv_obd,
75 &obd->obd_uuid, OBD_OPT_MDS_CONNECTION);
77 CERROR("MDS cannot connect to LMV %s (%d)\n",
79 mds->mds_lmv_obd = ERR_PTR(rc);
82 mds->mds_lmv_exp = class_conn2export(&conn);
83 if (mds->mds_lmv_exp == NULL)
84 CERROR("can't get export!\n");
86 rc = obd_register_observer(mds->mds_lmv_obd, obd);
88 CERROR("MDS cannot register as observer of LMV %s, "
89 "rc = %d\n", lmv_name, rc);
93 /* retrieve size of EA */
94 rc = obd_get_info(mds->mds_lmv_exp, strlen("mdsize"),
95 "mdsize", &valsize, &value);
99 if (value > mds->mds_max_mdsize)
100 mds->mds_max_mdsize = value;
102 /* find our number in LMV cluster */
103 rc = obd_get_info(mds->mds_lmv_exp, strlen("mdsnum"),
104 "mdsnum", &valsize, &value);
108 mds->mds_num = value;
110 rc = obd_set_info(mds->mds_lmv_exp, strlen("inter_mds"),
111 "inter_mds", 0, NULL);
115 mds->mds_lmv_connected = 1;
116 up(&mds->mds_lmv_sem);
120 obd_register_observer(mds->mds_lmv_obd, NULL);
122 obd_disconnect(mds->mds_lmv_exp, 0);
123 mds->mds_lmv_exp = NULL;
124 mds->mds_lmv_obd = ERR_PTR(rc);
126 up(&mds->mds_lmv_sem);
130 int mds_lmv_postsetup(struct obd_device *obd)
132 struct mds_obd *mds = &obd->u.mds;
136 if (mds->mds_lmv_exp) {
137 rc = obd_init_ea_size(mds->mds_lmv_exp,
139 mds->mds_max_cookiesize);
145 int mds_lmv_disconnect(struct obd_device *obd, int flags)
147 struct mds_obd *mds = &obd->u.mds;
151 if (!mds->mds_lmv_connected)
154 down(&mds->mds_lmv_sem);
155 if (!IS_ERR(mds->mds_lmv_obd) && mds->mds_lmv_exp != NULL) {
156 LASSERT(mds->mds_lmv_connected);
158 obd_register_observer(mds->mds_lmv_obd, NULL);
160 if (flags & OBD_OPT_FORCE) {
161 struct obd_device *lmv_obd;
162 struct obd_ioctl_data ioc_data = { 0 };
164 lmv_obd = class_exp2obd(mds->mds_lmv_exp);
169 * making disconnecting lmv stuff do not send anything
170 * to all remote MDSs from LMV. This is needed to
171 * prevent possible hanging with endless recovery, when
172 * MDS sends disconnect to already disconnected
173 * target. Probably this is wrong, but client does the
174 * same in --force mode and I do not see why can't we do
177 lmv_obd->obd_no_recov = 1;
178 obd_iocontrol(IOC_OSC_SET_ACTIVE, mds->mds_lmv_exp,
179 sizeof(ioc_data), &ioc_data, NULL);
183 * if obd_disconnect() fails (probably because the export was
184 * disconnected by class_disconnect_exports()) then we just need
187 mds->mds_lmv_connected = 0;
188 rc = obd_disconnect(mds->mds_lmv_exp, flags);
190 class_export_put(mds->mds_lmv_exp);
193 mds->mds_lmv_exp = NULL;
194 mds->mds_lmv_obd = NULL;
196 up(&mds->mds_lmv_sem);
200 int mds_get_lmv_attr(struct obd_device *obd, struct inode *inode,
201 struct mea **mea, int *mea_size)
203 struct mds_obd *mds = &obd->u.mds;
207 if (!mds->mds_lmv_obd)
209 if (!S_ISDIR(inode->i_mode))
212 /* first calculate mea size */
213 *mea_size = obd_alloc_diskmd(mds->mds_lmv_exp,
214 (struct lov_mds_md **)mea);
215 if (*mea_size < 0 || *mea == NULL)
216 return *mea_size < 0 ? *mea_size : -EINVAL;
218 rc = mds_get_md(obd, inode, *mea, mea_size, 1);
221 OBD_FREE(*mea, *mea_size);
239 #define DIR_ROUND (DIR_PAD - 1)
240 #define DIR_REC_LEN(name_len) (((name_len) + 16 + DIR_ROUND) & ~DIR_ROUND)
242 /* this struct holds dir entries for particular MDS to be flushed */
244 struct list_head list;
249 struct brw_page brwc;
252 struct dirsplit_control {
253 struct obd_device *obd;
255 struct dentry *dentry;
257 struct dir_cache *cache;
260 static int dc_new_page_to_cache(struct dir_cache * dirc)
264 if (!list_empty(&dirc->list) && dirc->free > sizeof(__u16)) {
265 /* current page became full, mark the end */
266 struct dir_entry *de = dirc->cur;
270 page = alloc_page(GFP_KERNEL);
273 list_add_tail(&page->lru, &dirc->list);
274 dirc->cur = page_address(page);
275 dirc->free = PAGE_SIZE;
279 static int retrieve_generation_numbers(struct dirsplit_control *dc, void *buf)
281 struct mds_obd *mds = &dc->obd->u.mds;
282 struct dir_entry *de;
283 struct dentry *dentry;
286 end = buf + PAGE_SIZE;
287 de = (struct dir_entry *) buf;
288 while ((char *) de < end && de->namelen) {
289 /* lookup an inode */
290 LASSERT(de->namelen <= 255);
291 dentry = ll_lookup_one_len(de->name, dc->dentry,
293 if (IS_ERR(dentry)) {
294 CERROR("can't lookup %*s: %d\n", de->namelen,
295 de->name, (int) PTR_ERR(dentry));
298 if (dentry->d_inode != NULL) {
300 struct lustre_id sid;
302 down(&dentry->d_inode->i_sem);
303 rc = mds_read_inode_sid(dc->obd,
304 dentry->d_inode, &sid);
305 up(&dentry->d_inode->i_sem);
307 CERROR("Can't read inode self id, "
308 "inode %lu, rc %d\n",
309 dentry->d_inode->i_ino, rc);
313 de->fid = id_fid(&sid);
314 de->mds = mds->mds_num;
315 de->ino = dentry->d_inode->i_ino;
316 de->generation = dentry->d_inode->i_generation;
317 } else if (dentry->d_flags & DCACHE_CROSS_REF) {
318 de->fid = dentry->d_fid;
319 de->ino = dentry->d_inum;
320 de->mds = dentry->d_mdsnum;
321 de->generation = dentry->d_generation;
323 CERROR("can't lookup %*s\n", de->namelen, de->name);
329 de = (struct dir_entry *)
330 ((char *) de + DIR_REC_LEN(de->namelen));
335 static int flush_buffer_onto_mds(struct dirsplit_control *dc, int mdsnum)
337 struct mds_obd *mds = &dc->obd->u.mds;
338 struct list_head *cur, *tmp;
339 struct dir_cache *ca;
342 ca = dc->cache + mdsnum;
344 if (ca->free > sizeof(__u16)) {
345 /* current page became full, mark the end */
346 struct dir_entry *de = ca->cur;
350 list_for_each_safe(cur, tmp, &ca->list) {
353 page = list_entry(cur, struct page, lru);
354 LASSERT(page != NULL);
356 retrieve_generation_numbers(dc, page_address(page));
359 ca->brwc.disk_offset = ca->brwc.page_offset = 0;
360 ca->brwc.count = PAGE_SIZE;
362 ca->oa.o_mds = mdsnum;
363 rc = obd_brw(OBD_BRW_WRITE, mds->mds_lmv_exp, &ca->oa,
364 (struct lov_stripe_md *) dc->mea,
373 static int remove_entries_from_orig_dir(struct dirsplit_control *dc, int mdsnum)
375 struct list_head *cur, *tmp;
376 struct dentry *dentry;
377 struct dir_cache *ca;
378 struct dir_entry *de;
384 ca = dc->cache + mdsnum;
385 list_for_each_safe(cur, tmp, &ca->list) {
386 page = list_entry(cur, struct page, lru);
387 buf = page_address(page);
388 end = buf + PAGE_SIZE;
390 de = (struct dir_entry *) buf;
391 while ((char *) de < end && de->namelen) {
392 /* lookup an inode */
393 LASSERT(de->namelen <= 255);
395 dentry = ll_lookup_one_len(de->name, dc->dentry,
397 if (IS_ERR(dentry)) {
398 CERROR("can't lookup %*s: %d\n", de->namelen,
399 de->name, (int) PTR_ERR(dentry));
402 rc = fsfilt_del_dir_entry(dc->obd, dentry);
405 de = (struct dir_entry *)
406 ((char *) de + DIR_REC_LEN(de->namelen));
412 static int filldir(void * __buf, const char * name, int namlen,
413 loff_t offset, ino_t ino, unsigned int d_type)
415 struct dirsplit_control *dc = __buf;
416 struct mds_obd *mds = &dc->obd->u.mds;
417 struct dir_cache *ca;
418 struct dir_entry *de;
423 if (name[0] == '.' &&
424 (namlen == 1 || (namlen == 2 && name[1] == '.'))) {
425 /* skip special entries */
430 newmds = mea_name2idx(dc->mea, (char *) name, namlen);
432 if (newmds == mds->mds_num) {
433 /* this entry remains on the current MDS, skip moving */
437 OBD_ALLOC(n, namlen + 1);
438 memcpy(n, name, namlen);
439 n[namlen] = (char) 0;
441 OBD_FREE(n, namlen + 1);
443 /* check for space in buffer for new entry */
444 ca = dc->cache + newmds;
445 if (DIR_REC_LEN(namlen) > ca->free) {
446 int err = dc_new_page_to_cache(ca);
450 /* insert found entry into buffer to be flushed later */
451 /* NOTE: we'll fill generations number later, because we
452 * it's stored in inode, thus we need to lookup an entry,
453 * but directory is locked for readdir(), so we delay this */
457 de->namelen = namlen;
458 memcpy(de->name, name, namlen);
459 ca->cur += DIR_REC_LEN(namlen);
460 ca->free -= DIR_REC_LEN(namlen);
466 int scan_and_distribute(struct obd_device *obd, struct dentry *dentry,
469 struct inode *dir = dentry->d_inode;
470 struct dirsplit_control dc;
475 nlen = strlen("__iopen__/") + 10 + 1;
476 OBD_ALLOC(file_name, nlen);
480 i = sprintf(file_name, "__iopen__/0x%lx",
481 dentry->d_inode->i_ino);
483 file = filp_open(file_name, O_RDONLY, 0);
485 CERROR("can't open directory %s: %d\n",
486 file_name, (int) PTR_ERR(file));
487 OBD_FREE(file_name, nlen);
488 RETURN(PTR_ERR(file));
491 memset(&dc, 0, sizeof(dc));
496 OBD_ALLOC(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
497 LASSERT(dc.cache != NULL);
498 for (i = 0; i < mea->mea_count; i++) {
499 INIT_LIST_HEAD(&dc.cache[i].list);
500 dc.cache[i].free = 0;
501 dc.cache[i].cached = 0;
504 err = vfs_readdir(file, filldir, &dc);
509 for (i = 0; i < mea->mea_count; i++) {
510 if (!dc.cache[i].cached)
512 err = flush_buffer_onto_mds(&dc, i);
517 for (i = 0; i < mea->mea_count; i++) {
518 if (!dc.cache[i].cached)
520 err = remove_entries_from_orig_dir(&dc, i);
527 for (i = 0; i < mea->mea_count; i++) {
528 struct list_head *cur, *tmp;
529 if (!dc.cache[i].cached)
531 list_for_each_safe(cur, tmp, &dc.cache[i].list) {
533 page = list_entry(cur, struct page, lru);
534 list_del(&page->lru);
538 OBD_FREE(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
539 OBD_FREE(file_name, nlen);
544 #define MAX_DIR_SIZE (64 * 1024)
545 #define I_NON_SPLITTABLE (256)
547 int mds_splitting_expected(struct obd_device *obd, struct dentry *dentry)
549 struct mds_obd *mds = &obd->u.mds;
550 struct mea *mea = NULL;
554 if (!mds->mds_lmv_obd)
555 return MDS_NO_SPLITTABLE;
558 if (dentry->d_inode == NULL)
559 return MDS_NO_SPLITTABLE;
561 /* a dir can be splitted only */
562 if (!S_ISDIR(dentry->d_inode->i_mode))
563 return MDS_NO_SPLITTABLE;
565 /* already splittied or slave directory (part of splitted dir) */
566 if (dentry->d_inode->i_flags & I_NON_SPLITTABLE)
567 return MDS_NO_SPLITTABLE;
569 /* don't split root directory */
570 if (dentry->d_inode->i_ino == id_ino(&mds->mds_rootid))
571 return MDS_NO_SPLITTABLE;
573 /* large enough to be splitted? */
574 if (dentry->d_inode->i_size < MAX_DIR_SIZE)
575 return MDS_NO_SPLIT_EXPECTED;
577 mds_get_lmv_attr(obd, dentry->d_inode, &mea, &size);
579 /* already splitted or slave object: shouldn't be splitted */
580 rc = MDS_NO_SPLITTABLE;
581 /* mark to skip subsequent checks */
582 dentry->d_inode->i_flags |= I_NON_SPLITTABLE;
585 /* may be splitted */
586 rc = MDS_EXPECT_SPLIT;
593 * must not be called on already splitted directories.
595 int mds_try_to_split_dir(struct obd_device *obd, struct dentry *dentry,
596 struct mea **mea, int nstripes, int update_mode)
598 struct inode *dir = dentry->d_inode;
599 struct mds_obd *mds = &obd->u.mds;
600 struct mea *tmea = NULL;
601 struct obdo *oa = NULL;
602 int rc, mea_size = 0;
607 if (update_mode != LCK_EX)
610 /* TODO: optimization possible - we already may have mea here */
611 rc = mds_splitting_expected(obd, dentry);
612 if (rc == MDS_NO_SPLITTABLE)
614 if (rc == MDS_NO_SPLIT_EXPECTED && nstripes == 0)
616 if (nstripes && nstripes == 1)
619 LASSERT(mea == NULL || *mea == NULL);
621 CDEBUG(D_OTHER, "%s: split directory %u/%lu/%lu\n",
622 obd->obd_name, mds->mds_num, dir->i_ino,
623 (unsigned long) dir->i_generation);
627 mea_size = obd_size_diskmd(mds->mds_lmv_exp, NULL);
629 /* FIXME: Actually we may only want to allocate enough space for
630 * necessary amount of stripes, but on the other hand with this
631 * approach of allocating maximal possible amount of MDS slots,
632 * it would be easier to split the dir over more MDSes */
633 rc = obd_alloc_diskmd(mds->mds_lmv_exp, (void *)mea);
635 CERROR("obd_alloc_diskmd() failed, error %d.\n", rc);
641 (*mea)->mea_count = nstripes;
643 /* 1) create directory objects on slave MDS'es */
644 /* FIXME: should this be OBD method? */
649 oa->o_generation = dir->i_generation;
651 obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
652 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
653 OBD_MD_FLUID | OBD_MD_FLGID);
655 oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
656 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
657 oa->o_mode = dir->i_mode;
660 * until lmv_obd_create() properly rewritten, it is important to have
661 * here oa->o_id = dir->i_ino, as otherwise master object will have
662 * invalid store cookie (zero inode num), what will lead to -ESTALE in
663 * mds_open() or somewhere else.
665 oa->o_id = dir->i_ino;
668 rc = mds_read_inode_sid(obd, dir, &id);
671 CERROR("Can't read inode self id, inode %lu, "
672 "rc %d.\n", dir->i_ino, rc);
675 oa->o_fid = id_fid(&id);
676 oa->o_mds = mds->mds_num;
678 CDEBUG(D_OTHER, "%s: create subdirs with mode %o, uid %u, gid %u\n",
679 obd->obd_name, dir->i_mode, dir->i_uid, dir->i_gid);
681 rc = obd_create(mds->mds_lmv_exp, oa,
682 (struct lov_stripe_md **)mea, NULL);
684 CERROR("Can't create remote inode, rc = %d\n", rc);
688 LASSERT(id_fid(&(*mea)->mea_ids[0]));
689 CDEBUG(D_OTHER, "%d dirobjects created\n", (int)(*mea)->mea_count);
691 /* 2) update dir attribute */
694 handle = fsfilt_start(obd, dir, FSFILT_OP_SETATTR, NULL);
695 if (IS_ERR(handle)) {
697 CERROR("fsfilt_start() failed: %d\n", (int) PTR_ERR(handle));
698 GOTO(err_oa, rc = PTR_ERR(handle));
701 rc = fsfilt_set_md(obd, dir, handle, *mea, mea_size);
704 CERROR("fsfilt_set_md() failed, error %d.\n", rc);
708 rc = fsfilt_commit(obd, mds->mds_sb, dir, handle, 0);
711 CERROR("fsfilt_commit() failed, error %d.\n", rc);
718 /* 3) read through the dir and distribute it over objects */
719 rc = scan_and_distribute(obd, dentry, *mea);
721 obd_free_diskmd(mds->mds_lmv_exp, (struct lov_mds_md **)mea);
723 CERROR("scan_and_distribute() failed, error %d.\n", rc);
734 static int filter_start_page_write(struct inode *inode,
735 struct niobuf_local *lnb)
737 struct page *page = alloc_pages(GFP_HIGHUSER, 0);
739 CERROR("no memory for a temp page\n");
740 return lnb->rc = -ENOMEM;
742 POISON_PAGE(page, 0xf1);
743 page->index = lnb->offset >> PAGE_SHIFT;
748 struct dentry *filter_id2dentry(struct obd_device *obd,
749 struct dentry *dir_dentry,
750 obd_gr group, obd_id id);
752 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
753 int objcount, struct obd_ioobj *obj,
754 int niocount, struct niobuf_remote *nb,
755 struct niobuf_local *res,
756 struct obd_trans_info *oti)
758 struct niobuf_remote *rnb;
759 struct niobuf_local *lnb = NULL;
760 int rc = 0, i, tot_bytes = 0;
761 unsigned long now = jiffies;
762 struct dentry *dentry;
766 LASSERT(objcount == 1);
767 LASSERT(obj->ioo_bufcnt > 0);
769 memset(res, 0, niocount * sizeof(*res));
773 id_ino(&id) = obj->ioo_id;
774 id_gen(&id) = obj->ioo_gr;
776 dentry = mds_id2dentry(exp->exp_obd, &id, NULL);
777 if (IS_ERR(dentry)) {
778 CERROR("can't get dentry for "LPU64"/%u: %d\n",
779 id.li_stc.u.e3s.l3s_ino, id.li_stc.u.e3s.l3s_gen,
780 (int)PTR_ERR(dentry));
781 GOTO(cleanup, rc = (int) PTR_ERR(dentry));
784 if (dentry->d_inode == NULL) {
785 CERROR("trying to BRW to non-existent file "LPU64"\n",
788 GOTO(cleanup, rc = -ENOENT);
791 if (time_after(jiffies, now + 15 * HZ))
792 CERROR("slow preprw_write setup %lus\n", (jiffies - now) / HZ);
794 CDEBUG(D_INFO, "preprw_write setup: %lu jiffies\n",
797 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
799 lnb->dentry = dentry;
800 lnb->offset = rnb->offset;
802 lnb->flags = rnb->flags;
804 rc = filter_start_page_write(dentry->d_inode, lnb);
806 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR, "page err %u@"
807 LPU64" %u/%u %p: rc %d\n", lnb->len, lnb->offset,
808 i, obj->ioo_bufcnt, dentry, rc);
810 __free_pages(lnb->page, 0);
814 tot_bytes += lnb->len;
817 if (time_after(jiffies, now + 15 * HZ))
818 CERROR("slow start_page_write %lus\n", (jiffies - now) / HZ);
820 CDEBUG(D_INFO, "start_page_write: %lu jiffies\n",
828 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
829 int objcount, struct obd_ioobj *obj, int niocount,
830 struct niobuf_local *res, struct obd_trans_info *oti,
833 struct obd_device *obd = exp->exp_obd;
834 struct niobuf_local *lnb;
835 struct inode *inode = NULL;
836 int rc = 0, i, cleanup_phase = 0, err, entries = 0;
839 LASSERT(objcount == 1);
840 LASSERT(current->journal_info == NULL);
843 inode = res->dentry->d_inode;
845 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
847 struct dir_entry *de;
849 buf = kmap(lnb->page);
850 LASSERT(buf != NULL);
851 end = buf + lnb->len;
852 de = (struct dir_entry *) buf;
853 while ((char *) de < end && de->namelen) {
854 err = fsfilt_add_dir_entry(obd, res->dentry, de->name,
855 de->namelen, de->ino,
856 de->generation, de->mds,
859 CERROR("can't add dir entry %*s->%u/%u/%u"
860 " to %lu/%u: %d\n", de->namelen,
861 de->name, de->mds, (unsigned)de->ino,
862 (unsigned) de->generation,
863 res->dentry->d_inode->i_ino,
864 res->dentry->d_inode->i_generation,
870 de = (struct dir_entry *)
871 ((char *) de + DIR_REC_LEN(de->namelen));
877 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++)
878 __free_page(lnb->page);
884 int mds_choose_mdsnum(struct obd_device *obd, const char *name, int len, int flags)
887 struct mds_obd *mds = &obd->u.mds;
888 int i = mds->mds_num;
890 if (flags & REC_REINT_CREATE) {
892 } else if (mds->mds_lmv_exp) {
893 lmv = &mds->mds_lmv_exp->exp_obd->u.lmv;
894 i = raw_name2idx(MEA_MAGIC_LAST_CHAR, lmv->desc.ld_tgt_count, name, len);
899 int mds_lock_slave_objs(struct obd_device *obd, struct dentry *dentry,
900 struct lustre_handle **rlockh)
902 struct mds_obd *mds = &obd->u.mds;
903 struct mdc_op_data op_data;
904 struct lookup_intent it;
905 struct mea *mea = NULL;
910 LASSERT(rlockh != NULL);
911 LASSERT(dentry != NULL);
912 LASSERT(dentry->d_inode != NULL);
915 if (!mds->mds_lmv_obd)
918 /* a dir can be splitted only */
919 if (!S_ISDIR(dentry->d_inode->i_mode))
922 rc = mds_get_lmv_attr(obd, dentry->d_inode,
930 if (mea->mea_count == 0)
931 /* this is slave object */
932 GOTO(cleanup, rc = 0);
934 CDEBUG(D_OTHER, "%s: lock slaves for %lu/%lu\n",
935 obd->obd_name, (unsigned long)dentry->d_inode->i_ino,
936 (unsigned long)dentry->d_inode->i_generation);
938 handle_size = sizeof(struct lustre_handle) *
941 OBD_ALLOC(*rlockh, handle_size);
943 GOTO(cleanup, rc = -ENOMEM);
945 memset(*rlockh, 0, handle_size);
946 memset(&op_data, 0, sizeof(op_data));
949 it.it_op = IT_UNLINK;
951 rc = md_enqueue(mds->mds_lmv_exp, LDLM_IBITS, &it, LCK_EX,
952 &op_data, *rlockh, NULL, 0, ldlm_completion_ast,
953 mds_blocking_ast, NULL);
955 OBD_FREE(mea, mea_size);
959 void mds_unlock_slave_objs(struct obd_device *obd, struct dentry *dentry,
960 struct lustre_handle *lockh)
962 struct mds_obd *mds = &obd->u.mds;
963 struct mea *mea = NULL;
969 LASSERT(mds->mds_lmv_obd != NULL);
970 LASSERT(S_ISDIR(dentry->d_inode->i_mode));
972 rc = mds_get_lmv_attr(obd, dentry->d_inode, &mea, &mea_size);
974 CERROR("locks are leaked\n");
977 LASSERT(mea_size != 0);
978 LASSERT(mea != NULL);
979 LASSERT(mea->mea_count != 0);
981 CDEBUG(D_OTHER, "%s: unlock slaves for %lu/%lu\n", obd->obd_name,
982 (unsigned long) dentry->d_inode->i_ino,
983 (unsigned long) dentry->d_inode->i_generation);
985 for (i = 0; i < mea->mea_count; i++) {
986 if (lockh[i].cookie != 0)
987 ldlm_lock_decref(lockh + i, LCK_EX);
990 OBD_FREE(lockh, sizeof(struct lustre_handle) * mea->mea_count);
991 OBD_FREE(mea, mea_size);
994 int mds_unlink_slave_objs(struct obd_device *obd, struct dentry *dentry)
996 struct mds_obd *mds = &obd->u.mds;
997 struct ptlrpc_request *req = NULL;
998 struct mdc_op_data op_data;
999 struct mea *mea = NULL;
1003 /* clustered MD ? */
1004 if (!mds->mds_lmv_obd)
1007 /* a dir can be splitted only */
1008 if (!S_ISDIR(dentry->d_inode->i_mode))
1011 rc = mds_get_lmv_attr(obd, dentry->d_inode, &mea, &mea_size);
1018 if (mea->mea_count == 0)
1019 GOTO(cleanup, rc = 0);
1021 CDEBUG(D_OTHER, "%s: unlink slaves for %lu/%lu\n", obd->obd_name,
1022 (unsigned long) dentry->d_inode->i_ino,
1023 (unsigned long) dentry->d_inode->i_generation);
1025 memset(&op_data, 0, sizeof(op_data));
1027 rc = md_unlink(mds->mds_lmv_exp, &op_data, &req);
1028 LASSERT(req == NULL);
1031 OBD_FREE(mea, mea_size);
1035 struct ide_tracking {
1040 int mds_ide_filldir(void *__buf, const char *name, int namelen,
1041 loff_t offset, ino_t ino, unsigned int d_type)
1043 struct ide_tracking *it = __buf;
1049 if (it->entries > 2)
1053 if (name[0] == '.' && namelen == 1)
1055 if (name[0] == '.' && name[1] == '.' && namelen == 2)
1062 /* checks if passed dentry points to empty dir. */
1063 int mds_is_dir_empty(struct obd_device *obd, struct dentry *dentry)
1065 struct ide_tracking it;
1073 nlen = strlen("__iopen__/") + 10 + 1;
1074 OBD_ALLOC(file_name, nlen);
1078 LASSERT(dentry->d_inode != NULL);
1079 i = sprintf(file_name, "__iopen__/0x%lx",
1080 dentry->d_inode->i_ino);
1082 file = filp_open(file_name, O_RDONLY, 0);
1084 CERROR("can't open directory %s: %d\n",
1085 file_name, (int) PTR_ERR(file));
1086 GOTO(cleanup, rc = PTR_ERR(file));
1089 rc = vfs_readdir(file, mds_ide_filldir, &it);
1090 filp_close(file, 0);
1092 if (it.empty && rc == 0)
1098 OBD_FREE(file_name, nlen);
1102 int mds_lock_and_check_slave(int offset, struct ptlrpc_request *req,
1103 struct lustre_handle *lockh)
1105 struct obd_device *obd = req->rq_export->exp_obd;
1106 struct dentry *dentry = NULL;
1107 struct lvfs_run_ctxt saved;
1108 int cleanup_phase = 0;
1109 struct mds_req_sec_desc *rsd;
1110 struct mds_body *body;
1111 struct lvfs_ucred uc;
1112 int rc, update_mode;
1115 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1117 CERROR("Can't unpack security desc\n");
1118 GOTO(cleanup, rc = -EFAULT);
1120 mds_squash_root(&obd->u.mds, rsd, &req->rq_peer.peer_id.nid);
1122 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1123 lustre_swab_mds_body);
1125 CERROR("Can't swab mds_body\n");
1126 GOTO(cleanup, rc = -EFAULT);
1128 CDEBUG(D_OTHER, "%s: check slave "DLID4"\n", obd->obd_name,
1131 dentry = mds_id2locked_dentry(obd, &body->id1, NULL, LCK_EX,
1132 lockh, &update_mode, NULL, 0,
1133 MDS_INODELOCK_UPDATE);
1134 if (IS_ERR(dentry)) {
1135 CERROR("can't find inode: %d\n", (int) PTR_ERR(dentry));
1136 GOTO(cleanup, rc = PTR_ERR(dentry));
1140 LASSERT(S_ISDIR(dentry->d_inode->i_mode));
1142 rc = mds_init_ucred(&uc, rsd);
1144 CERROR("can't init ucred\n");
1148 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1151 if (!mds_is_dir_empty(obd, dentry))
1156 switch(cleanup_phase) {
1159 ldlm_lock_decref(lockh, LCK_EX);
1161 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1162 mds_exit_ucred(&uc);
1169 int mds_convert_mea_ea(struct obd_device *obd, struct inode *inode,
1170 struct lov_mds_md *lmm, int lmmsize)
1172 int i, rc, err, size;
1173 struct mea_old *old;
1179 mea = (struct mea *) lmm;
1180 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
1181 mea->mea_magic == MEA_MAGIC_ALL_CHARS)
1184 old = (struct mea_old *) lmm;
1186 rc = sizeof(struct lustre_id) * old->mea_count +
1187 sizeof(struct mea_old);
1189 if (old->mea_count > 256 || old->mea_master > 256 || lmmsize < rc
1190 || old->mea_master > old->mea_count) {
1191 CWARN("unknown MEA format, dont convert it\n");
1192 CWARN(" count %u, master %u, size %u\n",
1193 old->mea_count, old->mea_master, rc);
1197 CWARN("converting MEA EA on %lu/%u from V0 to V1 (%u/%u)\n",
1198 inode->i_ino, inode->i_generation, old->mea_count,
1201 size = sizeof(struct lustre_id) * old->mea_count +
1204 OBD_ALLOC(new, size);
1208 new->mea_magic = MEA_MAGIC_LAST_CHAR;
1209 new->mea_count = old->mea_count;
1210 new->mea_master = old->mea_master;
1211 for (i = 0; i < new->mea_count; i++)
1212 new->mea_ids[i] = old->mea_ids[i];
1214 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1215 if (IS_ERR(handle)) {
1216 rc = PTR_ERR(handle);
1217 GOTO(conv_free, rc);
1220 rc = fsfilt_set_md(obd, inode, handle, (struct lov_mds_md *) new, size);
1223 memcpy(lmm, new, size);
1225 err = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 0);
1227 rc = err ? err : size;
1230 OBD_FREE(new, size);