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, &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 mds->mds_lmv_connected = 1;
115 up(&mds->mds_lmv_sem);
119 obd_register_observer(mds->mds_lmv_obd, NULL);
121 obd_disconnect(mds->mds_lmv_exp, 0);
122 mds->mds_lmv_exp = NULL;
123 mds->mds_lmv_obd = ERR_PTR(rc);
125 up(&mds->mds_lmv_sem);
129 int mds_lmv_postsetup(struct obd_device *obd)
131 struct mds_obd *mds = &obd->u.mds;
135 if (mds->mds_lmv_exp)
136 rc = obd_init_ea_size(mds->mds_lmv_exp, mds->mds_max_mdsize,
137 mds->mds_max_cookiesize);
142 int mds_lmv_disconnect(struct obd_device *obd, int flags)
144 struct mds_obd *mds = &obd->u.mds;
148 down(&mds->mds_lmv_sem);
149 if (!IS_ERR(mds->mds_lmv_obd) && mds->mds_lmv_exp != NULL) {
150 LASSERT(mds->mds_lmv_connected != 0);
151 mds->mds_lmv_connected = 0;
152 obd_register_observer(mds->mds_lmv_obd, NULL);
154 /* if obd_disconnect fails (probably because the export was
155 * disconnected by class_disconnect_exports) then we just need
156 * to drop our ref. */
157 rc = obd_disconnect(mds->mds_lmv_exp, flags);
159 class_export_put(mds->mds_lmv_exp);
161 mds->mds_lmv_exp = NULL;
162 mds->mds_lmv_obd = NULL;
164 up(&mds->mds_lmv_sem);
169 int mds_get_lmv_attr(struct obd_device *obd, struct inode *inode,
170 struct mea **mea, int *mea_size)
172 struct mds_obd *mds = &obd->u.mds;
176 if (!mds->mds_lmv_obd)
178 if (!S_ISDIR(inode->i_mode))
181 /* first calculate mea size */
182 *mea_size = obd_alloc_diskmd(mds->mds_lmv_exp,
183 (struct lov_mds_md **)mea);
184 if (*mea_size < 0 || *mea == NULL)
185 return *mea_size < 0 ? *mea_size : -EINVAL;
187 rc = mds_get_md(obd, inode, *mea, mea_size, 1);
190 OBD_FREE(*mea, *mea_size);
207 #define DIR_ROUND (DIR_PAD - 1)
208 #define DIR_REC_LEN(name_len) (((name_len) + 12 + DIR_ROUND) & ~DIR_ROUND)
210 /* this struct holds dir entries for particular MDS to be flushed */
212 struct list_head list;
217 struct brw_page brwc;
220 struct dirsplit_control {
221 struct obd_device *obd;
223 struct dentry *dentry;
225 struct dir_cache *cache;
228 static int dc_new_page_to_cache(struct dir_cache * dirc)
232 if (!list_empty(&dirc->list) && dirc->free > sizeof(__u16)) {
233 /* current page became full, mark the end */
234 struct dir_entry *de = dirc->cur;
238 page = alloc_page(GFP_KERNEL);
241 list_add_tail(&page->list, &dirc->list);
242 dirc->cur = page_address(page);
243 dirc->free = PAGE_SIZE;
247 static int retrieve_generation_numbers(struct dirsplit_control *dc, void *buf)
249 struct mds_obd *mds = &dc->obd->u.mds;
250 struct dir_entry *de;
251 struct dentry *dentry;
254 end = buf + PAGE_SIZE;
255 de = (struct dir_entry *) buf;
256 while ((char *) de < end && de->namelen) {
257 /* lookup an inode */
258 LASSERT(de->namelen <= 255);
259 dentry = ll_lookup_one_len(de->name, dc->dentry, de->namelen);
260 if (IS_ERR(dentry)) {
261 CERROR("can't lookup %*s: %d\n", de->namelen,
262 de->name, (int) PTR_ERR(dentry));
265 if (dentry->d_inode != NULL) {
266 de->mds = mds->mds_num;
267 de->ino = dentry->d_inode->i_ino;
268 de->generation = dentry->d_inode->i_generation;
269 } else if (dentry->d_flags & DCACHE_CROSS_REF) {
270 de->mds = dentry->d_mdsnum;
271 de->ino = dentry->d_inum;
272 de->generation = dentry->d_generation;
274 CERROR("can't lookup %*s\n", de->namelen, de->name);
280 de = (struct dir_entry *)
281 ((char *) de + DIR_REC_LEN(de->namelen));
286 static int flush_buffer_onto_mds(struct dirsplit_control *dc, int mdsnum)
288 struct mds_obd *mds = &dc->obd->u.mds;
289 struct list_head *cur, *tmp;
290 struct dir_cache *ca;
293 ca = dc->cache + mdsnum;
295 if (ca->free > sizeof(__u16)) {
296 /* current page became full, mark the end */
297 struct dir_entry *de = ca->cur;
301 list_for_each_safe(cur, tmp, &ca->list) {
304 page = list_entry(cur, struct page, list);
305 LASSERT(page != NULL);
307 retrieve_generation_numbers(dc, page_address(page));
311 ca->brwc.count = PAGE_SIZE;
313 ca->oa.o_mds = mdsnum;
314 rc = obd_brw(OBD_BRW_WRITE, mds->mds_lmv_exp, &ca->oa,
315 (struct lov_stripe_md *) dc->mea,
324 static int remove_entries_from_orig_dir(struct dirsplit_control *dc, int mdsnum)
326 struct list_head *cur, *tmp;
327 struct dentry *dentry;
328 struct dir_cache *ca;
329 struct dir_entry *de;
335 ca = dc->cache + mdsnum;
336 list_for_each_safe(cur, tmp, &ca->list) {
337 page = list_entry(cur, struct page, list);
338 buf = page_address(page);
339 end = buf + PAGE_SIZE;
341 de = (struct dir_entry *) buf;
342 while ((char *) de < end && de->namelen) {
343 /* lookup an inode */
344 LASSERT(de->namelen <= 255);
346 dentry = ll_lookup_one_len(de->name, dc->dentry,
348 if (IS_ERR(dentry)) {
349 CERROR("can't lookup %*s: %d\n", de->namelen,
350 de->name, (int) PTR_ERR(dentry));
353 rc = fsfilt_del_dir_entry(dc->obd, dentry);
356 de = (struct dir_entry *)
357 ((char *) de + DIR_REC_LEN(de->namelen));
363 static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
364 ino_t ino, unsigned int d_type)
366 struct dirsplit_control *dc = __buf;
367 struct mds_obd *mds = &dc->obd->u.mds;
368 struct dir_cache *ca;
369 struct dir_entry *de;
374 if (name[0] == '.' && (namlen == 1 ||
375 (namlen == 2 && name[1] == '.'))) {
376 /* skip special entries */
381 newmds = mea_name2idx(dc->mea, (char *) name, namlen);
383 if (newmds == mds->mds_num) {
384 /* this entry remains on the current MDS, skip moving */
388 OBD_ALLOC(n, namlen + 1);
389 memcpy(n, name, namlen);
390 n[namlen] = (char) 0;
392 OBD_FREE(n, namlen + 1);
394 /* check for space in buffer for new entry */
395 ca = dc->cache + newmds;
396 if (DIR_REC_LEN(namlen) > ca->free) {
397 int err = dc_new_page_to_cache(ca);
401 /* insert found entry into buffer to be flushed later */
402 /* NOTE: we'll fill generations number later, because we
403 * it's stored in inode, thus we need to lookup an entry,
404 * but directory is locked for readdir(), so we delay this */
408 de->namelen = namlen;
409 memcpy(de->name, name, namlen);
410 ca->cur += DIR_REC_LEN(namlen);
411 ca->free -= DIR_REC_LEN(namlen);
417 int scan_and_distribute(struct obd_device *obd, struct dentry *dentry,
420 struct inode *dir = dentry->d_inode;
421 struct dirsplit_control dc;
426 nlen = strlen("__iopen__/") + 10 + 1;
427 OBD_ALLOC(file_name, nlen);
430 i = sprintf(file_name, "__iopen__/0x%lx", dentry->d_inode->i_ino);
432 file = filp_open(file_name, O_RDONLY, 0);
434 CERROR("can't open directory %s: %d\n",
435 file_name, (int) PTR_ERR(file));
436 OBD_FREE(file_name, nlen);
437 RETURN(PTR_ERR(file));
440 memset(&dc, 0, sizeof(dc));
445 OBD_ALLOC(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
446 LASSERT(dc.cache != NULL);
447 for (i = 0; i < mea->mea_count; i++) {
448 INIT_LIST_HEAD(&dc.cache[i].list);
449 dc.cache[i].free = 0;
450 dc.cache[i].cached = 0;
453 err = vfs_readdir(file, filldir, &dc);
458 for (i = 0; i < mea->mea_count; i++) {
459 if (!dc.cache[i].cached)
461 err = flush_buffer_onto_mds(&dc, i);
466 for (i = 0; i < mea->mea_count; i++) {
467 if (!dc.cache[i].cached)
469 err = remove_entries_from_orig_dir(&dc, i);
475 for (i = 0; i < mea->mea_count; i++) {
476 struct list_head *cur, *tmp;
477 if (!dc.cache[i].cached)
479 list_for_each_safe(cur, tmp, &dc.cache[i].list) {
481 page = list_entry(cur, struct page, list);
482 list_del(&page->list);
486 OBD_FREE(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
487 OBD_FREE(file_name, nlen);
492 #define MAX_DIR_SIZE (64 * 1024)
494 #define I_NON_SPLITTABLE 256
496 int mds_splitting_expected(struct obd_device *obd, struct dentry *dentry)
498 struct mds_obd *mds = &obd->u.mds;
499 struct mea *mea = NULL;
503 if (!mds->mds_lmv_obd)
504 RETURN(MDS_NO_SPLITTABLE);
507 if (dentry->d_inode == NULL)
508 return MDS_NO_SPLITTABLE;
510 /* a dir can be splitted only */
511 if (!S_ISDIR(dentry->d_inode->i_mode))
512 return MDS_NO_SPLITTABLE;
514 /* already splittied or slave directory (part of splitted dir) */
515 if (dentry->d_inode->i_flags & I_NON_SPLITTABLE)
516 return MDS_NO_SPLITTABLE;
518 /* don't split root directory */
519 if (dentry->d_inode->i_ino == mds->mds_rootfid.id)
520 return MDS_NO_SPLITTABLE;
522 /* large enough to be splitted? */
523 if (dentry->d_inode->i_size < MAX_DIR_SIZE)
524 return MDS_NO_SPLIT_EXPECTED;
526 mds_get_lmv_attr(obd, dentry->d_inode, &mea, &size);
528 /* already splitted or slave object: shouldn't be splitted */
529 rc = MDS_NO_SPLITTABLE;
530 /* mark to skip subsequent checks */
531 dentry->d_inode->i_flags |= I_NON_SPLITTABLE;
533 /* may be splitted */
534 rc = MDS_EXPECT_SPLIT;
543 * must not be called on already splitted directories.
545 int mds_try_to_split_dir(struct obd_device *obd, struct dentry *dentry,
546 struct mea **mea, int nstripes, int update_mode)
548 struct inode *dir = dentry->d_inode;
549 struct mds_obd *mds = &obd->u.mds;
550 struct mea *tmea = NULL;
551 struct obdo *oa = NULL;
552 int rc, mea_size = 0;
556 if (update_mode != LCK_EX)
558 /* TODO: optimization possible - we already may have mea here */
559 rc = mds_splitting_expected(obd, dentry);
560 if (rc == MDS_NO_SPLITTABLE)
562 if (rc == MDS_NO_SPLIT_EXPECTED && nstripes == 0)
564 if (nstripes && nstripes == 1)
567 LASSERT(mea == NULL || *mea == NULL);
569 CDEBUG(D_OTHER, "%s: split directory %u/%lu/%lu\n",
570 obd->obd_name, mds->mds_num, dir->i_ino,
571 (unsigned long) dir->i_generation);
575 mea_size = obd_size_diskmd(mds->mds_lmv_exp, NULL);
577 /* FIXME: Actually we may only want to allocate enough space for
578 * necessary amount of stripes, but on the other hand with this
579 * approach of allocating maximal possible amount of MDS slots,
580 * it would be easier to split the dir over more MDSes */
581 rc = obd_alloc_diskmd(mds->mds_lmv_exp, (void *)mea);
583 CERROR("obd_alloc_diskmd() failed, error %d.\n", rc);
589 (*mea)->mea_count = nstripes;
591 /* 1) create directory objects on slave MDS'es */
592 /* FIXME: should this be OBD method? */
597 oa->o_id = dir->i_ino;
598 oa->o_generation = dir->i_generation;
600 obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
601 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
602 OBD_MD_FLUID | OBD_MD_FLGID);
604 oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
605 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
606 oa->o_mode = dir->i_mode;
608 CDEBUG(D_OTHER, "%s: create subdirs with mode %o, uid %u, gid %u\n",
609 obd->obd_name, dir->i_mode, dir->i_uid, dir->i_gid);
611 rc = obd_create(mds->mds_lmv_exp, oa,
612 (struct lov_stripe_md **)mea, NULL);
616 CDEBUG(D_OTHER, "%d dirobjects created\n", (int)(*mea)->mea_count);
618 /* 2) update dir attribute */
621 handle = fsfilt_start(obd, dir, FSFILT_OP_SETATTR, NULL);
622 if (IS_ERR(handle)) {
624 CERROR("fsfilt_start() failed: %d\n", (int) PTR_ERR(handle));
625 GOTO(err_oa, rc = PTR_ERR(handle));
628 rc = fsfilt_set_md(obd, dir, handle, *mea, mea_size);
631 CERROR("fsfilt_set_md() failed, error %d.\n", rc);
635 rc = fsfilt_commit(obd, mds->mds_sb, dir, handle, 0);
638 CERROR("fsfilt_commit() failed, error %d.\n", rc);
645 /* 3) read through the dir and distribute it over objects */
646 rc = scan_and_distribute(obd, dentry, *mea);
648 obd_free_diskmd(mds->mds_lmv_exp, (struct lov_mds_md **)mea);
650 CERROR("scan_and_distribute() failed, error %d.\n", rc);
661 static int filter_start_page_write(struct inode *inode,
662 struct niobuf_local *lnb)
664 struct page *page = alloc_pages(GFP_HIGHUSER, 0);
666 CERROR("no memory for a temp page\n");
667 RETURN(lnb->rc = -ENOMEM);
669 POISON_PAGE(page, 0xf1);
670 page->index = lnb->offset >> PAGE_SHIFT;
676 struct dentry *filter_fid2dentry(struct obd_device *obd,
677 struct dentry *dir_dentry,
678 obd_gr group, obd_id id);
680 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
681 int objcount, struct obd_ioobj *obj,
682 int niocount, struct niobuf_remote *nb,
683 struct niobuf_local *res,
684 struct obd_trans_info *oti)
686 struct mds_obd *mds = &exp->exp_obd->u.mds;
687 struct niobuf_remote *rnb;
688 struct niobuf_local *lnb = NULL;
689 int rc = 0, i, tot_bytes = 0;
690 unsigned long now = jiffies;
691 struct dentry *dentry;
694 LASSERT(objcount == 1);
695 LASSERT(obj->ioo_bufcnt > 0);
697 memset(res, 0, niocount * sizeof(*res));
699 fid.id = obj->ioo_id;
700 fid.generation = obj->ioo_gr;
701 dentry = mds_fid2dentry(mds, &fid, NULL);
702 if (IS_ERR(dentry)) {
703 CERROR("can't get dentry for "LPU64"/%u: %d\n",
704 fid.id, fid.generation, (int) PTR_ERR(dentry));
705 GOTO(cleanup, rc = (int) PTR_ERR(dentry));
708 if (dentry->d_inode == NULL) {
709 CERROR("trying to BRW to non-existent file "LPU64"\n",
712 GOTO(cleanup, rc = -ENOENT);
715 if (time_after(jiffies, now + 15 * HZ))
716 CERROR("slow preprw_write setup %lus\n", (jiffies - now) / HZ);
718 CDEBUG(D_INFO, "preprw_write setup: %lu jiffies\n",
721 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
723 lnb->dentry = dentry;
724 lnb->offset = rnb->offset;
726 lnb->flags = rnb->flags;
728 rc = filter_start_page_write(dentry->d_inode, lnb);
730 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR, "page err %u@"
731 LPU64" %u/%u %p: rc %d\n", lnb->len, lnb->offset,
732 i, obj->ioo_bufcnt, dentry, rc);
734 __free_pages(lnb->page, 0);
738 tot_bytes += lnb->len;
741 if (time_after(jiffies, now + 15 * HZ))
742 CERROR("slow start_page_write %lus\n", (jiffies - now) / HZ);
744 CDEBUG(D_INFO, "start_page_write: %lu jiffies\n",
752 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
753 int objcount, struct obd_ioobj *obj, int niocount,
754 struct niobuf_local *res, struct obd_trans_info *oti,
757 struct obd_device *obd = exp->exp_obd;
758 struct niobuf_local *lnb;
759 struct inode *inode = NULL;
760 int rc = 0, i, cleanup_phase = 0, err, entries = 0;
763 LASSERT(objcount == 1);
764 LASSERT(current->journal_info == NULL);
767 inode = res->dentry->d_inode;
769 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
771 struct dir_entry *de;
773 buf = kmap(lnb->page);
774 LASSERT(buf != NULL);
775 end = buf + lnb->len;
776 de = (struct dir_entry *) buf;
777 while ((char *) de < end && de->namelen) {
778 err = fsfilt_add_dir_entry(obd, res->dentry, de->name,
779 de->namelen, de->ino,
780 de->generation, de->mds);
782 CERROR("can't add dir entry %*s->%u/%u/%u"
784 de->namelen, de->name,
785 de->mds, (unsigned) de->ino,
786 (unsigned) de->generation,
787 res->dentry->d_inode->i_ino,
788 res->dentry->d_inode->i_generation,
794 de = (struct dir_entry *)
795 ((char *) de + DIR_REC_LEN(de->namelen));
801 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++)
802 __free_page(lnb->page);
808 int mds_choose_mdsnum(struct obd_device *obd, const char *name, int len, int flags)
811 struct mds_obd *mds = &obd->u.mds;
812 int i = mds->mds_num;
814 if (flags & REC_REINT_CREATE) {
816 } else if (mds->mds_lmv_exp) {
817 lmv = &mds->mds_lmv_exp->exp_obd->u.lmv;
818 i = raw_name2idx(MEA_MAGIC_LAST_CHAR, lmv->desc.ld_tgt_count, name, len);
823 int mds_lock_slave_objs(struct obd_device *obd, struct dentry *dentry,
824 struct lustre_handle **rlockh)
826 struct mds_obd *mds = &obd->u.mds;
827 struct mdc_op_data op_data;
828 struct lookup_intent it;
829 struct mea *mea = NULL;
832 LASSERT(rlockh != NULL);
833 LASSERT(dentry != NULL);
834 LASSERT(dentry->d_inode != NULL);
837 if (!mds->mds_lmv_obd)
840 /* a dir can be splitted only */
841 if (!S_ISDIR(dentry->d_inode->i_mode))
844 rc = mds_get_lmv_attr(obd, dentry->d_inode, &mea, &mea_size);
850 if (mea->mea_count == 0) {
851 /* this is slave object */
852 GOTO(cleanup, rc = 0);
855 CDEBUG(D_OTHER, "%s: lock slaves for %lu/%lu\n", obd->obd_name,
856 (unsigned long) dentry->d_inode->i_ino,
857 (unsigned long) dentry->d_inode->i_generation);
859 OBD_ALLOC(*rlockh, sizeof(struct lustre_handle) * mea->mea_count);
861 GOTO(cleanup, rc = -ENOMEM);
862 memset(*rlockh, 0, sizeof(struct lustre_handle) * mea->mea_count);
864 memset(&op_data, 0, sizeof(op_data));
866 it.it_op = IT_UNLINK;
867 rc = md_enqueue(mds->mds_lmv_exp, LDLM_IBITS, &it, LCK_EX, &op_data,
868 *rlockh, NULL, 0, ldlm_completion_ast, mds_blocking_ast,
871 OBD_FREE(mea, mea_size);
875 void mds_unlock_slave_objs(struct obd_device *obd, struct dentry *dentry,
876 struct lustre_handle *lockh)
878 struct mds_obd *mds = &obd->u.mds;
879 struct mea *mea = NULL;
885 LASSERT(mds->mds_lmv_obd != NULL);
886 LASSERT(S_ISDIR(dentry->d_inode->i_mode));
888 rc = mds_get_lmv_attr(obd, dentry->d_inode, &mea, &mea_size);
890 CERROR("locks are leaked\n");
893 LASSERT(mea_size != 0);
894 LASSERT(mea != NULL);
895 LASSERT(mea->mea_count != 0);
897 CDEBUG(D_OTHER, "%s: unlock slaves for %lu/%lu\n", obd->obd_name,
898 (unsigned long) dentry->d_inode->i_ino,
899 (unsigned long) dentry->d_inode->i_generation);
901 for (i = 0; i < mea->mea_count; i++) {
902 if (lockh[i].cookie != 0)
903 ldlm_lock_decref(lockh + i, LCK_EX);
906 OBD_FREE(lockh, sizeof(struct lustre_handle) * mea->mea_count);
907 OBD_FREE(mea, mea_size);
911 int mds_unlink_slave_objs(struct obd_device *obd, struct dentry *dentry)
913 struct mds_obd *mds = &obd->u.mds;
914 struct ptlrpc_request *req = NULL;
915 struct mdc_op_data op_data;
916 struct mea *mea = NULL;
920 if (!mds->mds_lmv_obd)
923 /* a dir can be splitted only */
924 if (!S_ISDIR(dentry->d_inode->i_mode))
927 rc = mds_get_lmv_attr(obd, dentry->d_inode, &mea, &mea_size);
933 if (mea->mea_count == 0)
934 GOTO(cleanup, rc = 0);
936 CDEBUG(D_OTHER, "%s: unlink slaves for %lu/%lu\n", obd->obd_name,
937 (unsigned long) dentry->d_inode->i_ino,
938 (unsigned long) dentry->d_inode->i_generation);
940 memset(&op_data, 0, sizeof(op_data));
942 rc = md_unlink(mds->mds_lmv_exp, &op_data, &req);
943 LASSERT(req == NULL);
945 OBD_FREE(mea, mea_size);
949 struct ide_tracking {
954 int mds_ide_filldir(void *__buf, const char *name, int namelen,
955 loff_t offset, ino_t ino, unsigned int d_type)
957 struct ide_tracking *it = __buf;
967 if (name[0] == '.' && namelen == 1)
969 if (name[0] == '.' && name[1] == '.' && namelen == 2)
976 int mds_is_dir_empty(struct obd_device *obd, struct dentry *dentry)
978 struct ide_tracking it;
986 nlen = strlen("__iopen__/") + 10 + 1;
987 OBD_ALLOC(file_name, nlen);
990 i = sprintf(file_name, "__iopen__/0x%lx", dentry->d_inode->i_ino);
992 file = filp_open(file_name, O_RDONLY, 0);
994 CERROR("can't open directory %s: %d\n",
995 file_name, (int) PTR_ERR(file));
996 GOTO(cleanup, rc = PTR_ERR(file));
999 rc = vfs_readdir(file, mds_ide_filldir, &it);
1000 filp_close(file, 0);
1002 if (it.empty && rc == 0)
1008 OBD_FREE(file_name, nlen);
1012 int mds_lock_and_check_slave(int offset, struct ptlrpc_request *req,
1013 struct lustre_handle *lockh)
1015 struct obd_device *obd = req->rq_export->exp_obd;
1016 struct dentry *dentry = NULL;
1017 struct lvfs_run_ctxt saved;
1018 int cleanup_phase = 0;
1019 struct mds_body *body;
1020 struct lvfs_ucred uc;
1021 int rc, update_mode;
1024 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1025 lustre_swab_mds_body);
1027 CERROR("Can't swab mds_body\n");
1028 GOTO(cleanup, rc = -EFAULT);
1030 CDEBUG(D_OTHER, "%s: check slave %lu/%lu\n", obd->obd_name,
1031 (unsigned long) body->fid1.id,
1032 (unsigned long) body->fid1.generation);
1033 dentry = mds_fid2locked_dentry(obd, &body->fid1, NULL, LCK_EX, lockh,
1034 &update_mode, NULL, 0,
1035 MDS_INODELOCK_UPDATE);
1036 if (IS_ERR(dentry)) {
1037 CERROR("can't find inode: %d\n", (int) PTR_ERR(dentry));
1038 GOTO(cleanup, rc = PTR_ERR(dentry));
1042 LASSERT(S_ISDIR(dentry->d_inode->i_mode));
1044 uc.luc_fsuid = body->fsuid;
1045 uc.luc_fsgid = body->fsgid;
1046 uc.luc_cap = body->capability;
1047 uc.luc_suppgid1 = body->suppgid;
1048 uc.luc_suppgid2 = -1;
1049 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1052 if (!mds_is_dir_empty(obd, dentry))
1056 switch(cleanup_phase) {
1059 ldlm_lock_decref(lockh, LCK_EX);
1061 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1068 int mds_convert_mea_ea(struct obd_device *obd, struct inode *inode,
1069 struct lov_mds_md *lmm, int lmmsize)
1071 int i, rc, err, size;
1072 struct mea_old *old;
1078 mea = (struct mea *) lmm;
1079 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
1080 mea->mea_magic == MEA_MAGIC_ALL_CHARS)
1083 old = (struct mea_old *) lmm;
1084 rc = sizeof(struct ll_fid) * old->mea_count + sizeof(struct mea_old);
1085 if (old->mea_count > 256 || old->mea_master > 256 || lmmsize < rc
1086 || old->mea_master > old->mea_count) {
1087 CWARN("unknown MEA format, dont convert it\n");
1088 CWARN(" count %u, master %u, size %u\n",
1089 old->mea_count, old->mea_master, rc);
1093 CWARN("converting MEA EA on %lu/%u from V0 to V1 (%u/%u)\n",
1094 inode->i_ino, inode->i_generation, old->mea_count, old->mea_master);
1096 size = sizeof(struct ll_fid) * old->mea_count + sizeof(struct mea);
1097 OBD_ALLOC(new, size);
1101 new->mea_magic = MEA_MAGIC_LAST_CHAR;
1102 new->mea_count = old->mea_count;
1103 new->mea_master = old->mea_master;
1104 for (i = 0; i < new->mea_count; i++)
1105 new->mea_fids[i] = old->mea_fids[i];
1107 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1108 if (IS_ERR(handle)) {
1109 rc = PTR_ERR(handle);
1110 GOTO(conv_free, rc);
1113 rc = fsfilt_set_md(obd, inode, handle, (struct lov_mds_md *) new, size);
1116 memcpy(lmm, new, size);
1118 err = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 0);
1120 rc = err ? err : size;
1121 GOTO(conv_free, rc);
1123 OBD_FREE(new, size);