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));
60 down(&mds->mds_lmv_sem);
61 if (mds->mds_lmv_obd) {
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, &obd->obd_uuid, OBD_OPT_MDS_CONNECTION);
76 CERROR("MDS cannot connect to LMV %s (%d)\n",
78 mds->mds_lmv_obd = ERR_PTR(rc);
81 mds->mds_lmv_exp = class_conn2export(&conn);
82 if (mds->mds_lmv_exp == NULL)
83 CERROR("can't get export!\n");
85 rc = obd_register_observer(mds->mds_lmv_obd, obd);
87 CERROR("MDS cannot register as observer of LMV %s, "
88 "rc = %d\n", lmv_name, rc);
92 /* retrieve size of EA */
93 rc = obd_get_info(mds->mds_lmv_exp, strlen("mdsize"), "mdsize",
98 if (value > mds->mds_max_mdsize)
99 mds->mds_max_mdsize = value;
101 /* find our number in LMV cluster */
102 rc = obd_get_info(mds->mds_lmv_exp, strlen("mdsnum"), "mdsnum",
107 mds->mds_num = value;
109 rc = obd_set_info(mds->mds_lmv_exp, strlen("inter_mds"),
110 "inter_mds", 0, NULL);
114 up(&mds->mds_lmv_sem);
118 obd_register_observer(mds->mds_lmv_obd, NULL);
120 obd_disconnect(mds->mds_lmv_exp, 0);
121 mds->mds_lmv_exp = NULL;
122 mds->mds_lmv_obd = ERR_PTR(rc);
124 up(&mds->mds_lmv_sem);
128 int mds_lmv_postsetup(struct obd_device *obd)
130 struct mds_obd *mds = &obd->u.mds;
134 if (mds->mds_lmv_exp)
135 rc = obd_init_ea_size(mds->mds_lmv_exp, mds->mds_max_mdsize,
136 mds->mds_max_cookiesize);
141 int mds_lmv_disconnect(struct obd_device *obd, int flags)
143 struct mds_obd *mds = &obd->u.mds;
147 down(&mds->mds_lmv_sem);
148 if (!IS_ERR(mds->mds_lmv_obd) && mds->mds_lmv_exp != NULL) {
149 obd_register_observer(mds->mds_lmv_obd, NULL);
151 /* if obd_disconnect fails (probably because the export was
152 * disconnected by class_disconnect_exports) then we just need
153 * to drop our ref. */
154 rc = obd_disconnect(mds->mds_lmv_exp, flags);
156 class_export_put(mds->mds_lmv_exp);
158 mds->mds_lmv_exp = NULL;
159 mds->mds_lmv_obd = NULL;
161 up(&mds->mds_lmv_sem);
166 int mds_get_lmv_attr(struct obd_device *obd, struct inode *inode,
167 struct mea **mea, int *mea_size)
169 struct mds_obd *mds = &obd->u.mds;
173 if (!mds->mds_lmv_obd)
175 if (!S_ISDIR(inode->i_mode))
178 /* first calculate mea size */
179 *mea_size = obd_alloc_diskmd(mds->mds_lmv_exp,
180 (struct lov_mds_md **)mea);
181 if (*mea_size < 0 || *mea == NULL)
182 return *mea_size < 0 ? *mea_size : -EINVAL;
184 rc = mds_get_md(obd, inode, *mea, mea_size, 1);
187 OBD_FREE(*mea, *mea_size);
204 #define DIR_ROUND (DIR_PAD - 1)
205 #define DIR_REC_LEN(name_len) (((name_len) + 12 + DIR_ROUND) & ~DIR_ROUND)
207 /* this struct holds dir entries for particular MDS to be flushed */
209 struct list_head list;
214 struct brw_page brwc;
217 struct dirsplit_control {
218 struct obd_device *obd;
220 struct dentry *dentry;
222 struct dir_cache *cache;
225 static int dc_new_page_to_cache(struct dir_cache * dirc)
229 if (!list_empty(&dirc->list) && dirc->free > sizeof(__u16)) {
230 /* current page became full, mark the end */
231 struct dir_entry *de = dirc->cur;
235 page = alloc_page(GFP_KERNEL);
238 list_add_tail(&page->list, &dirc->list);
239 dirc->cur = page_address(page);
240 dirc->free = PAGE_SIZE;
244 static int retrieve_generation_numbers(struct dirsplit_control *dc, void *buf)
246 struct mds_obd *mds = &dc->obd->u.mds;
247 struct dir_entry *de;
248 struct dentry *dentry;
251 end = buf + PAGE_SIZE;
252 de = (struct dir_entry *) buf;
253 while ((char *) de < end && de->namelen) {
254 /* lookup an inode */
255 LASSERT(de->namelen <= 255);
256 dentry = ll_lookup_one_len(de->name, dc->dentry, de->namelen);
257 if (IS_ERR(dentry)) {
258 CERROR("can't lookup %*s: %d\n", de->namelen,
259 de->name, (int) PTR_ERR(dentry));
262 if (dentry->d_inode != NULL) {
263 de->mds = mds->mds_num;
264 de->ino = dentry->d_inode->i_ino;
265 de->generation = dentry->d_inode->i_generation;
266 } else if (dentry->d_flags & DCACHE_CROSS_REF) {
267 de->mds = dentry->d_mdsnum;
268 de->ino = dentry->d_inum;
269 de->generation = dentry->d_generation;
271 CERROR("can't lookup %*s\n", de->namelen, de->name);
277 de = (struct dir_entry *)
278 ((char *) de + DIR_REC_LEN(de->namelen));
283 static int flush_buffer_onto_mds(struct dirsplit_control *dc, int mdsnum)
285 struct mds_obd *mds = &dc->obd->u.mds;
286 struct list_head *cur, *tmp;
287 struct dir_cache *ca;
290 ca = dc->cache + mdsnum;
292 if (ca->free > sizeof(__u16)) {
293 /* current page became full, mark the end */
294 struct dir_entry *de = ca->cur;
298 list_for_each_safe(cur, tmp, &ca->list) {
301 page = list_entry(cur, struct page, list);
302 LASSERT(page != NULL);
304 retrieve_generation_numbers(dc, page_address(page));
308 ca->brwc.count = PAGE_SIZE;
310 ca->oa.o_mds = mdsnum;
311 rc = obd_brw(OBD_BRW_WRITE, mds->mds_lmv_exp, &ca->oa,
312 (struct lov_stripe_md *) dc->mea,
321 static int remove_entries_from_orig_dir(struct dirsplit_control *dc, int mdsnum)
323 struct list_head *cur, *tmp;
324 struct dentry *dentry;
325 struct dir_cache *ca;
326 struct dir_entry *de;
332 ca = dc->cache + mdsnum;
333 list_for_each_safe(cur, tmp, &ca->list) {
334 page = list_entry(cur, struct page, list);
335 buf = page_address(page);
336 end = buf + PAGE_SIZE;
338 de = (struct dir_entry *) buf;
339 while ((char *) de < end && de->namelen) {
340 /* lookup an inode */
341 LASSERT(de->namelen <= 255);
343 dentry = ll_lookup_one_len(de->name, dc->dentry,
345 if (IS_ERR(dentry)) {
346 CERROR("can't lookup %*s: %d\n", de->namelen,
347 de->name, (int) PTR_ERR(dentry));
350 rc = fsfilt_del_dir_entry(dc->obd, dentry);
353 de = (struct dir_entry *)
354 ((char *) de + DIR_REC_LEN(de->namelen));
360 static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
361 ino_t ino, unsigned int d_type)
363 struct dirsplit_control *dc = __buf;
364 struct mds_obd *mds = &dc->obd->u.mds;
365 struct dir_cache *ca;
366 struct dir_entry *de;
371 if (name[0] == '.' && (namlen == 1 ||
372 (namlen == 2 && name[1] == '.'))) {
373 /* skip special entries */
378 newmds = mea_name2idx(dc->mea, (char *) name, namlen);
380 if (newmds == mds->mds_num) {
381 /* this entry remains on the current MDS, skip moving */
385 OBD_ALLOC(n, namlen + 1);
386 memcpy(n, name, namlen);
387 n[namlen] = (char) 0;
389 OBD_FREE(n, namlen + 1);
391 /* check for space in buffer for new entry */
392 ca = dc->cache + newmds;
393 if (DIR_REC_LEN(namlen) > ca->free) {
394 int err = dc_new_page_to_cache(ca);
398 /* insert found entry into buffer to be flushed later */
399 /* NOTE: we'll fill generations number later, because we
400 * it's stored in inode, thus we need to lookup an entry,
401 * but directory is locked for readdir(), so we delay this */
405 de->namelen = namlen;
406 memcpy(de->name, name, namlen);
407 ca->cur += DIR_REC_LEN(namlen);
408 ca->free -= DIR_REC_LEN(namlen);
414 int scan_and_distribute(struct obd_device *obd, struct dentry *dentry,
417 struct inode *dir = dentry->d_inode;
418 struct dirsplit_control dc;
423 nlen = strlen("__iopen__/") + 10 + 1;
424 OBD_ALLOC(file_name, nlen);
427 i = sprintf(file_name, "__iopen__/0x%lx", dentry->d_inode->i_ino);
429 file = filp_open(file_name, O_RDONLY, 0);
431 CERROR("can't open directory %s: %d\n",
432 file_name, (int) PTR_ERR(file));
433 OBD_FREE(file_name, nlen);
434 RETURN(PTR_ERR(file));
437 memset(&dc, 0, sizeof(dc));
442 OBD_ALLOC(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
443 LASSERT(dc.cache != NULL);
444 for (i = 0; i < mea->mea_count; i++) {
445 INIT_LIST_HEAD(&dc.cache[i].list);
446 dc.cache[i].free = 0;
447 dc.cache[i].cached = 0;
450 err = vfs_readdir(file, filldir, &dc);
455 for (i = 0; i < mea->mea_count; i++) {
456 if (!dc.cache[i].cached)
458 err = flush_buffer_onto_mds(&dc, i);
463 for (i = 0; i < mea->mea_count; i++) {
464 if (!dc.cache[i].cached)
466 err = remove_entries_from_orig_dir(&dc, i);
472 for (i = 0; i < mea->mea_count; i++) {
473 struct list_head *cur, *tmp;
474 if (!dc.cache[i].cached)
476 list_for_each_safe(cur, tmp, &dc.cache[i].list) {
478 page = list_entry(cur, struct page, list);
479 list_del(&page->list);
483 OBD_FREE(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
484 OBD_FREE(file_name, nlen);
489 #define MAX_DIR_SIZE (64 * 1024)
491 #define I_NON_SPLITTABLE 256
493 int mds_splitting_expected(struct obd_device *obd, struct dentry *dentry)
495 struct mds_obd *mds = &obd->u.mds;
496 struct mea *mea = NULL;
500 if (!mds->mds_lmv_obd)
501 RETURN(MDS_NO_SPLITTABLE);
504 if (dentry->d_inode == NULL)
505 return MDS_NO_SPLITTABLE;
507 /* a dir can be splitted only */
508 if (!S_ISDIR(dentry->d_inode->i_mode))
509 return MDS_NO_SPLITTABLE;
511 /* already splittied or slave directory (part of splitted dir) */
512 if (dentry->d_inode->i_flags & I_NON_SPLITTABLE)
513 return MDS_NO_SPLITTABLE;
515 /* don't split root directory */
516 if (dentry->d_inode->i_ino == mds->mds_rootfid.id)
517 return MDS_NO_SPLITTABLE;
519 /* large enough to be splitted? */
520 if (dentry->d_inode->i_size < MAX_DIR_SIZE)
521 return MDS_NO_SPLIT_EXPECTED;
523 mds_get_lmv_attr(obd, dentry->d_inode, &mea, &size);
525 /* already splitted or slave object: shouldn't be splitted */
526 rc = MDS_NO_SPLITTABLE;
527 /* mark to skip subsequent checks */
528 dentry->d_inode->i_flags |= I_NON_SPLITTABLE;
530 /* may be splitted */
531 rc = MDS_EXPECT_SPLIT;
540 * must not be called on already splitted directories.
542 int mds_try_to_split_dir(struct obd_device *obd, struct dentry *dentry,
543 struct mea **mea, int nstripes, int update_mode)
545 struct inode *dir = dentry->d_inode;
546 struct mds_obd *mds = &obd->u.mds;
547 struct mea *tmea = NULL;
548 struct obdo *oa = NULL;
549 int rc, mea_size = 0;
553 if (update_mode != LCK_EX)
555 /* TODO: optimization possible - we already may have mea here */
556 rc = mds_splitting_expected(obd, dentry);
557 if (rc == MDS_NO_SPLITTABLE)
559 if (rc == MDS_NO_SPLIT_EXPECTED && nstripes == 0)
561 if (nstripes && nstripes == 1)
564 LASSERT(mea == NULL || *mea == NULL);
566 CDEBUG(D_OTHER, "%s: split directory %u/%lu/%lu\n",
567 obd->obd_name, mds->mds_num, dir->i_ino,
568 (unsigned long) dir->i_generation);
572 mea_size = obd_size_diskmd(mds->mds_lmv_exp, NULL);
574 /* FIXME: Actually we may only want to allocate enough space for
575 * necessary amount of stripes, but on the other hand with this
576 * approach of allocating maximal possible amount of MDS slots,
577 * it would be easier to split the dir over more MDSes */
578 rc = obd_alloc_diskmd(mds->mds_lmv_exp, (void *)mea);
580 CERROR("obd_alloc_diskmd() failed, error %d.\n", rc);
586 (*mea)->mea_count = nstripes;
588 /* 1) create directory objects on slave MDS'es */
589 /* FIXME: should this be OBD method? */
594 oa->o_id = dir->i_ino;
595 oa->o_generation = dir->i_generation;
597 obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
598 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
599 OBD_MD_FLUID | OBD_MD_FLGID);
601 oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
602 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
603 oa->o_mode = dir->i_mode;
605 CDEBUG(D_OTHER, "%s: create subdirs with mode %o, uid %u, gid %u\n",
606 obd->obd_name, dir->i_mode, dir->i_uid, dir->i_gid);
608 rc = obd_create(mds->mds_lmv_exp, oa,
609 (struct lov_stripe_md **)mea, NULL);
613 CDEBUG(D_OTHER, "%d dirobjects created\n", (int)(*mea)->mea_count);
615 /* 2) update dir attribute */
618 handle = fsfilt_start(obd, dir, FSFILT_OP_SETATTR, NULL);
619 if (IS_ERR(handle)) {
621 CERROR("fsfilt_start() failed: %d\n", (int) PTR_ERR(handle));
622 GOTO(err_oa, rc = PTR_ERR(handle));
625 rc = fsfilt_set_md(obd, dir, handle, *mea, mea_size);
628 CERROR("fsfilt_set_md() failed, error %d.\n", rc);
632 rc = fsfilt_commit(obd, mds->mds_sb, dir, handle, 0);
635 CERROR("fsfilt_commit() failed, error %d.\n", rc);
642 /* 3) read through the dir and distribute it over objects */
643 rc = scan_and_distribute(obd, dentry, *mea);
645 obd_free_diskmd(mds->mds_lmv_exp, (struct lov_mds_md **)mea);
647 CERROR("scan_and_distribute() failed, error %d.\n", rc);
658 static int filter_start_page_write(struct inode *inode,
659 struct niobuf_local *lnb)
661 struct page *page = alloc_pages(GFP_HIGHUSER, 0);
663 CERROR("no memory for a temp page\n");
664 RETURN(lnb->rc = -ENOMEM);
666 POISON_PAGE(page, 0xf1);
667 page->index = lnb->offset >> PAGE_SHIFT;
673 struct dentry *filter_fid2dentry(struct obd_device *obd,
674 struct dentry *dir_dentry,
675 obd_gr group, obd_id id);
677 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
678 int objcount, struct obd_ioobj *obj,
679 int niocount, struct niobuf_remote *nb,
680 struct niobuf_local *res,
681 struct obd_trans_info *oti)
683 struct mds_obd *mds = &exp->exp_obd->u.mds;
684 struct niobuf_remote *rnb;
685 struct niobuf_local *lnb = NULL;
686 int rc = 0, i, tot_bytes = 0;
687 unsigned long now = jiffies;
688 struct dentry *dentry;
691 LASSERT(objcount == 1);
692 LASSERT(obj->ioo_bufcnt > 0);
694 memset(res, 0, niocount * sizeof(*res));
696 fid.id = obj->ioo_id;
697 fid.generation = obj->ioo_gr;
698 dentry = mds_fid2dentry(mds, &fid, NULL);
699 if (IS_ERR(dentry)) {
700 CERROR("can't get dentry for "LPU64"/%u: %d\n",
701 fid.id, fid.generation, (int) PTR_ERR(dentry));
702 GOTO(cleanup, rc = (int) PTR_ERR(dentry));
705 if (dentry->d_inode == NULL) {
706 CERROR("trying to BRW to non-existent file "LPU64"\n",
709 GOTO(cleanup, rc = -ENOENT);
712 if (time_after(jiffies, now + 15 * HZ))
713 CERROR("slow preprw_write setup %lus\n", (jiffies - now) / HZ);
715 CDEBUG(D_INFO, "preprw_write setup: %lu jiffies\n",
718 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
720 lnb->dentry = dentry;
721 lnb->offset = rnb->offset;
723 lnb->flags = rnb->flags;
725 rc = filter_start_page_write(dentry->d_inode, lnb);
727 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR, "page err %u@"
728 LPU64" %u/%u %p: rc %d\n", lnb->len, lnb->offset,
729 i, obj->ioo_bufcnt, dentry, rc);
731 __free_pages(lnb->page, 0);
735 tot_bytes += lnb->len;
738 if (time_after(jiffies, now + 15 * HZ))
739 CERROR("slow start_page_write %lus\n", (jiffies - now) / HZ);
741 CDEBUG(D_INFO, "start_page_write: %lu jiffies\n",
749 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
750 int objcount, struct obd_ioobj *obj, int niocount,
751 struct niobuf_local *res, struct obd_trans_info *oti,
754 struct obd_device *obd = exp->exp_obd;
755 struct niobuf_local *lnb;
756 struct inode *inode = NULL;
757 int rc = 0, i, cleanup_phase = 0, err, entries = 0;
760 LASSERT(objcount == 1);
761 LASSERT(current->journal_info == NULL);
764 inode = res->dentry->d_inode;
766 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
768 struct dir_entry *de;
770 buf = kmap(lnb->page);
771 LASSERT(buf != NULL);
772 end = buf + lnb->len;
773 de = (struct dir_entry *) buf;
774 while ((char *) de < end && de->namelen) {
775 err = fsfilt_add_dir_entry(obd, res->dentry, de->name,
776 de->namelen, de->ino,
777 de->generation, de->mds);
779 CERROR("can't add dir entry %*s->%u/%u/%u"
781 de->namelen, de->name,
782 de->mds, (unsigned) de->ino,
783 (unsigned) de->generation,
784 res->dentry->d_inode->i_ino,
785 res->dentry->d_inode->i_generation,
791 de = (struct dir_entry *)
792 ((char *) de + DIR_REC_LEN(de->namelen));
798 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++)
799 __free_page(lnb->page);
805 int mds_choose_mdsnum(struct obd_device *obd, const char *name, int len, int flags)
808 struct mds_obd *mds = &obd->u.mds;
809 int i = mds->mds_num;
811 if (flags & REC_REINT_CREATE) {
813 } else if (mds->mds_lmv_exp) {
814 lmv = &mds->mds_lmv_exp->exp_obd->u.lmv;
815 i = raw_name2idx(MEA_MAGIC_LAST_CHAR, lmv->desc.ld_tgt_count, name, len);
820 int mds_lock_slave_objs(struct obd_device *obd, struct dentry *dentry,
821 struct lustre_handle **rlockh)
823 struct mds_obd *mds = &obd->u.mds;
824 struct mdc_op_data op_data;
825 struct lookup_intent it;
826 struct mea *mea = NULL;
829 LASSERT(rlockh != NULL);
830 LASSERT(dentry != NULL);
831 LASSERT(dentry->d_inode != NULL);
834 if (!mds->mds_lmv_obd)
837 /* a dir can be splitted only */
838 if (!S_ISDIR(dentry->d_inode->i_mode))
841 rc = mds_get_lmv_attr(obd, dentry->d_inode, &mea, &mea_size);
847 if (mea->mea_count == 0) {
848 /* this is slave object */
849 GOTO(cleanup, rc = 0);
852 CDEBUG(D_OTHER, "%s: lock slaves for %lu/%lu\n", obd->obd_name,
853 (unsigned long) dentry->d_inode->i_ino,
854 (unsigned long) dentry->d_inode->i_generation);
856 OBD_ALLOC(*rlockh, sizeof(struct lustre_handle) * mea->mea_count);
858 GOTO(cleanup, rc = -ENOMEM);
859 memset(*rlockh, 0, sizeof(struct lustre_handle) * mea->mea_count);
861 memset(&op_data, 0, sizeof(op_data));
863 it.it_op = IT_UNLINK;
864 rc = md_enqueue(mds->mds_lmv_exp, LDLM_IBITS, &it, LCK_EX, &op_data,
865 *rlockh, NULL, 0, ldlm_completion_ast, mds_blocking_ast,
868 OBD_FREE(mea, mea_size);
872 void mds_unlock_slave_objs(struct obd_device *obd, struct dentry *dentry,
873 struct lustre_handle *lockh)
875 struct mds_obd *mds = &obd->u.mds;
876 struct mea *mea = NULL;
882 LASSERT(mds->mds_lmv_obd != NULL);
883 LASSERT(S_ISDIR(dentry->d_inode->i_mode));
885 rc = mds_get_lmv_attr(obd, dentry->d_inode, &mea, &mea_size);
887 CERROR("locks are leaked\n");
890 LASSERT(mea_size != 0);
891 LASSERT(mea != NULL);
892 LASSERT(mea->mea_count != 0);
894 CDEBUG(D_OTHER, "%s: unlock slaves for %lu/%lu\n", obd->obd_name,
895 (unsigned long) dentry->d_inode->i_ino,
896 (unsigned long) dentry->d_inode->i_generation);
898 for (i = 0; i < mea->mea_count; i++) {
899 if (lockh[i].cookie != 0)
900 ldlm_lock_decref(lockh + i, LCK_EX);
903 OBD_FREE(lockh, sizeof(struct lustre_handle) * mea->mea_count);
904 OBD_FREE(mea, mea_size);
908 int mds_unlink_slave_objs(struct obd_device *obd, struct dentry *dentry)
910 struct mds_obd *mds = &obd->u.mds;
911 struct ptlrpc_request *req = NULL;
912 struct mdc_op_data op_data;
913 struct mea *mea = NULL;
917 if (!mds->mds_lmv_obd)
920 /* a dir can be splitted only */
921 if (!S_ISDIR(dentry->d_inode->i_mode))
924 rc = mds_get_lmv_attr(obd, dentry->d_inode, &mea, &mea_size);
930 if (mea->mea_count == 0)
931 GOTO(cleanup, rc = 0);
933 CDEBUG(D_OTHER, "%s: unlink slaves for %lu/%lu\n", obd->obd_name,
934 (unsigned long) dentry->d_inode->i_ino,
935 (unsigned long) dentry->d_inode->i_generation);
937 memset(&op_data, 0, sizeof(op_data));
939 rc = md_unlink(mds->mds_lmv_exp, &op_data, &req);
940 LASSERT(req == NULL);
942 OBD_FREE(mea, mea_size);
946 struct ide_tracking {
951 int mds_ide_filldir(void *__buf, const char *name, int namelen,
952 loff_t offset, ino_t ino, unsigned int d_type)
954 struct ide_tracking *it = __buf;
964 if (name[0] == '.' && namelen == 1)
966 if (name[0] == '.' && name[1] == '.' && namelen == 2)
973 int mds_is_dir_empty(struct obd_device *obd, struct dentry *dentry)
975 struct ide_tracking it;
983 nlen = strlen("__iopen__/") + 10 + 1;
984 OBD_ALLOC(file_name, nlen);
987 i = sprintf(file_name, "__iopen__/0x%lx", dentry->d_inode->i_ino);
989 file = filp_open(file_name, O_RDONLY, 0);
991 CERROR("can't open directory %s: %d\n",
992 file_name, (int) PTR_ERR(file));
993 GOTO(cleanup, rc = PTR_ERR(file));
996 rc = vfs_readdir(file, mds_ide_filldir, &it);
999 if (it.empty && rc == 0)
1005 OBD_FREE(file_name, nlen);
1009 int mds_lock_and_check_slave(int offset, struct ptlrpc_request *req,
1010 struct lustre_handle *lockh)
1012 struct obd_device *obd = req->rq_export->exp_obd;
1013 struct dentry *dentry = NULL;
1014 struct lvfs_run_ctxt saved;
1015 int cleanup_phase = 0;
1016 struct mds_body *body;
1017 struct lvfs_ucred uc;
1018 int rc, update_mode;
1021 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1022 lustre_swab_mds_body);
1024 CERROR("Can't swab mds_body\n");
1025 GOTO(cleanup, rc = -EFAULT);
1027 CDEBUG(D_OTHER, "%s: check slave %lu/%lu\n", obd->obd_name,
1028 (unsigned long) body->fid1.id,
1029 (unsigned long) body->fid1.generation);
1030 dentry = mds_fid2locked_dentry(obd, &body->fid1, NULL, LCK_EX, lockh,
1031 &update_mode, NULL, 0,
1032 MDS_INODELOCK_UPDATE);
1033 if (IS_ERR(dentry)) {
1034 CERROR("can't find inode: %d\n", (int) PTR_ERR(dentry));
1035 GOTO(cleanup, rc = PTR_ERR(dentry));
1039 LASSERT(S_ISDIR(dentry->d_inode->i_mode));
1041 uc.luc_fsuid = body->fsuid;
1042 uc.luc_fsgid = body->fsgid;
1043 uc.luc_cap = body->capability;
1044 uc.luc_suppgid1 = body->suppgid;
1045 uc.luc_suppgid2 = -1;
1046 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1049 if (!mds_is_dir_empty(obd, dentry))
1053 switch(cleanup_phase) {
1056 ldlm_lock_decref(lockh, LCK_EX);
1058 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1065 int mds_convert_mea_ea(struct obd_device *obd, struct inode *inode,
1066 struct lov_mds_md *lmm, int lmmsize)
1068 int i, rc, err, size;
1069 struct mea_old *old;
1075 mea = (struct mea *) lmm;
1076 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
1077 mea->mea_magic == MEA_MAGIC_ALL_CHARS)
1080 old = (struct mea_old *) lmm;
1081 rc = sizeof(struct ll_fid) * old->mea_count + sizeof(struct mea_old);
1082 if (old->mea_count > 256 || old->mea_master > 256 || lmmsize < rc
1083 || old->mea_master > old->mea_count) {
1084 CWARN("unknown MEA format, dont convert it\n");
1085 CWARN(" count %u, master %u, size %u\n",
1086 old->mea_count, old->mea_master, rc);
1090 CWARN("converting MEA EA on %lu/%u from V0 to V1 (%u/%u)\n",
1091 inode->i_ino, inode->i_generation, old->mea_count, old->mea_master);
1093 size = sizeof(struct ll_fid) * old->mea_count + sizeof(struct mea);
1094 OBD_ALLOC(new, size);
1098 new->mea_magic = MEA_MAGIC_LAST_CHAR;
1099 new->mea_count = old->mea_count;
1100 new->mea_master = old->mea_master;
1101 for (i = 0; i < new->mea_count; i++)
1102 new->mea_fids[i] = old->mea_fids[i];
1104 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1105 if (IS_ERR(handle)) {
1106 rc = PTR_ERR(handle);
1107 GOTO(conv_free, rc);
1110 rc = fsfilt_set_md(obd, inode, handle, (struct lov_mds_md *) new, size);
1113 memcpy(lmm, new, size);
1115 err = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 0);
1117 rc = err ? err : size;
1118 GOTO(conv_free, rc);
1120 OBD_FREE(new, size);