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, &obd->obd_uuid, NULL,
79 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_reconnect(struct obd_device *obd)
213 struct mds_obd *mds = &obd->u.mds;
214 struct obd_statfs osfs;
218 /* We don't know state of connections to another MDSes
219 * before the failure. If MDS we were connected to before
220 * the failure gets failed, then it will wait for us to
221 * reconnect and will timed recovery out. bug 4920 */
222 if (mds->mds_md_connected == 0)
224 if (mds->mds_md_obd == NULL)
227 err = obd_statfs(mds->mds_md_obd, &osfs, jiffies - HZ);
229 CERROR("can't reconnect to MDSes after recovery: %d\n", err);
233 int mds_md_get_attr(struct obd_device *obd, struct inode *inode,
234 struct mea **mea, int *mea_size)
236 struct mds_obd *mds = &obd->u.mds;
240 if (!mds->mds_md_obd)
242 if (!S_ISDIR(inode->i_mode))
245 /* first calculate mea size */
246 *mea_size = obd_alloc_diskmd(mds->mds_md_exp,
247 (struct lov_mds_md **)mea);
248 if (*mea_size < 0 || *mea == NULL)
249 return *mea_size < 0 ? *mea_size : -EINVAL;
251 rc = mds_get_md(obd, inode, *mea, mea_size, 1, 1);
254 OBD_FREE(*mea, *mea_size);
272 #define DIR_ROUND (DIR_PAD - 1)
273 #define DIR_REC_LEN(name_len) (((name_len) + 16 + DIR_ROUND) & ~DIR_ROUND)
275 /* this struct holds dir entries for particular MDS to be flushed */
277 struct list_head list;
282 struct brw_page brwc;
285 struct dirsplit_control {
286 struct obd_device *obd;
288 struct dentry *dentry;
290 struct dir_cache *cache;
293 static int dc_new_page_to_cache(struct dir_cache * dirc)
297 if (!list_empty(&dirc->list) && dirc->free > sizeof(__u16)) {
298 /* current page became full, mark the end */
299 struct dir_entry *de = dirc->cur;
303 page = alloc_page(GFP_KERNEL);
306 list_add_tail(&page->lru, &dirc->list);
307 dirc->cur = page_address(page);
308 dirc->free = PAGE_SIZE;
312 static int retrieve_generation_numbers(struct dirsplit_control *dc, void *buf)
314 struct mds_obd *mds = &dc->obd->u.mds;
315 struct dir_entry *de;
316 struct dentry *dentry;
319 end = buf + PAGE_SIZE;
320 de = (struct dir_entry *) buf;
321 while ((char *) de < end && de->namelen) {
322 /* lookup an inode */
323 LASSERT(de->namelen <= 255);
324 dentry = ll_lookup_one_len(de->name, dc->dentry,
326 if (IS_ERR(dentry)) {
327 CERROR("can't lookup %*s: %d\n", de->namelen,
328 de->name, (int) PTR_ERR(dentry));
331 if (dentry->d_inode != NULL) {
333 struct lustre_id sid;
335 down(&dentry->d_inode->i_sem);
336 rc = mds_read_inode_sid(dc->obd,
337 dentry->d_inode, &sid);
338 up(&dentry->d_inode->i_sem);
340 CERROR("Can't read inode self id, "
341 "inode %lu, rc %d\n",
342 dentry->d_inode->i_ino, rc);
346 de->fid = id_fid(&sid);
347 de->mds = mds->mds_num;
348 de->ino = dentry->d_inode->i_ino;
349 de->generation = dentry->d_inode->i_generation;
350 } else if (dentry->d_flags & DCACHE_CROSS_REF) {
351 de->fid = dentry->d_fid;
352 de->ino = dentry->d_inum;
353 de->mds = dentry->d_mdsnum;
354 de->generation = dentry->d_generation;
356 CERROR("can't lookup %*s\n", de->namelen, de->name);
362 de = (struct dir_entry *)
363 ((char *) de + DIR_REC_LEN(de->namelen));
368 static int flush_buffer_onto_mds(struct dirsplit_control *dc, int mdsnum)
370 struct mds_obd *mds = &dc->obd->u.mds;
371 struct list_head *cur, *tmp;
372 struct dir_cache *ca;
375 ca = dc->cache + mdsnum;
377 if (ca->free > sizeof(__u16)) {
378 /* current page became full, mark the end */
379 struct dir_entry *de = ca->cur;
383 list_for_each_safe(cur, tmp, &ca->list) {
386 page = list_entry(cur, struct page, lru);
387 LASSERT(page != NULL);
389 retrieve_generation_numbers(dc, page_address(page));
392 ca->brwc.disk_offset = ca->brwc.page_offset = 0;
393 ca->brwc.count = PAGE_SIZE;
395 ca->oa.o_mds = mdsnum;
396 rc = obd_brw(OBD_BRW_WRITE, mds->mds_md_exp, &ca->oa,
397 (struct lov_stripe_md *) dc->mea,
406 static int remove_entries_from_orig_dir(struct dirsplit_control *dc, int mdsnum)
408 struct list_head *cur, *tmp;
409 struct dentry *dentry;
410 struct dir_cache *ca;
411 struct dir_entry *de;
417 ca = dc->cache + mdsnum;
418 list_for_each_safe(cur, tmp, &ca->list) {
419 page = list_entry(cur, struct page, lru);
420 buf = page_address(page);
421 end = buf + PAGE_SIZE;
423 de = (struct dir_entry *) buf;
424 while ((char *) de < end && de->namelen) {
425 /* lookup an inode */
426 LASSERT(de->namelen <= 255);
428 dentry = ll_lookup_one_len(de->name, dc->dentry,
430 if (IS_ERR(dentry)) {
431 CERROR("can't lookup %*s: %d\n", de->namelen,
432 de->name, (int) PTR_ERR(dentry));
435 rc = fsfilt_del_dir_entry(dc->obd, dentry);
438 de = (struct dir_entry *)
439 ((char *) de + DIR_REC_LEN(de->namelen));
445 static int filldir(void * __buf, const char * name, int namlen,
446 loff_t offset, ino_t ino, unsigned int d_type)
448 struct dirsplit_control *dc = __buf;
449 struct mds_obd *mds = &dc->obd->u.mds;
450 struct dir_cache *ca;
451 struct dir_entry *de;
456 if (name[0] == '.' &&
457 (namlen == 1 || (namlen == 2 && name[1] == '.'))) {
458 /* skip special entries */
463 newmds = mea_name2idx(dc->mea, (char *) name, namlen);
465 if (newmds == mds->mds_num) {
466 /* this entry remains on the current MDS, skip moving */
470 OBD_ALLOC(n, namlen + 1);
471 memcpy(n, name, namlen);
472 n[namlen] = (char) 0;
474 OBD_FREE(n, namlen + 1);
476 /* check for space in buffer for new entry */
477 ca = dc->cache + newmds;
478 if (DIR_REC_LEN(namlen) > ca->free) {
479 int err = dc_new_page_to_cache(ca);
483 /* insert found entry into buffer to be flushed later */
484 /* NOTE: we'll fill generations number later, because we
485 * it's stored in inode, thus we need to lookup an entry,
486 * but directory is locked for readdir(), so we delay this */
490 de->namelen = namlen;
491 memcpy(de->name, name, namlen);
492 ca->cur += DIR_REC_LEN(namlen);
493 ca->free -= DIR_REC_LEN(namlen);
499 int scan_and_distribute(struct obd_device *obd, struct dentry *dentry,
502 struct inode *dir = dentry->d_inode;
503 struct dirsplit_control dc;
508 nlen = strlen("__iopen__/") + 10 + 1;
509 OBD_ALLOC(file_name, nlen);
513 i = sprintf(file_name, "__iopen__/0x%lx",
514 dentry->d_inode->i_ino);
516 file = filp_open(file_name, O_RDONLY, 0);
518 CERROR("can't open directory %s: %d\n",
519 file_name, (int) PTR_ERR(file));
520 OBD_FREE(file_name, nlen);
521 RETURN(PTR_ERR(file));
524 memset(&dc, 0, sizeof(dc));
529 OBD_ALLOC(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
530 LASSERT(dc.cache != NULL);
531 for (i = 0; i < mea->mea_count; i++) {
532 INIT_LIST_HEAD(&dc.cache[i].list);
533 dc.cache[i].free = 0;
534 dc.cache[i].cached = 0;
537 err = vfs_readdir(file, filldir, &dc);
542 for (i = 0; i < mea->mea_count; i++) {
543 if (!dc.cache[i].cached)
545 err = flush_buffer_onto_mds(&dc, i);
550 for (i = 0; i < mea->mea_count; i++) {
551 if (!dc.cache[i].cached)
553 err = remove_entries_from_orig_dir(&dc, i);
560 for (i = 0; i < mea->mea_count; i++) {
561 struct list_head *cur, *tmp;
562 if (!dc.cache[i].cached)
564 list_for_each_safe(cur, tmp, &dc.cache[i].list) {
566 page = list_entry(cur, struct page, lru);
567 list_del(&page->lru);
571 OBD_FREE(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
572 OBD_FREE(file_name, nlen);
577 #define MAX_DIR_SIZE (64 * 1024)
578 #define I_NON_SPLITTABLE (256)
580 int mds_splitting_expected(struct obd_device *obd, struct dentry *dentry)
582 struct mds_obd *mds = &obd->u.mds;
583 struct mea *mea = NULL;
587 if (!mds->mds_md_obd)
588 return MDS_NO_SPLITTABLE;
591 if (dentry->d_inode == NULL)
592 return MDS_NO_SPLITTABLE;
594 /* a dir can be splitted only */
595 if (!S_ISDIR(dentry->d_inode->i_mode))
596 return MDS_NO_SPLITTABLE;
598 /* already splittied or slave directory (part of splitted dir) */
599 if (dentry->d_inode->i_flags & I_NON_SPLITTABLE)
600 return MDS_NO_SPLITTABLE;
602 /* don't split root directory */
603 if (dentry->d_inode->i_ino == id_ino(&mds->mds_rootid))
604 return MDS_NO_SPLITTABLE;
606 /* large enough to be splitted? */
607 if (dentry->d_inode->i_size < MAX_DIR_SIZE)
608 return MDS_NO_SPLIT_EXPECTED;
610 mds_md_get_attr(obd, dentry->d_inode, &mea, &size);
612 /* already splitted or slave object: shouldn't be splitted */
613 rc = MDS_NO_SPLITTABLE;
614 /* mark to skip subsequent checks */
615 dentry->d_inode->i_flags |= I_NON_SPLITTABLE;
618 /* may be splitted */
619 rc = MDS_EXPECT_SPLIT;
626 * must not be called on already splitted directories.
628 int mds_try_to_split_dir(struct obd_device *obd, struct dentry *dentry,
629 struct mea **mea, int nstripes, int update_mode)
631 struct inode *dir = dentry->d_inode;
632 struct mds_obd *mds = &obd->u.mds;
633 struct mea *tmea = NULL;
634 struct obdo *oa = NULL;
635 int rc, mea_size = 0;
640 if (update_mode != LCK_EX)
643 /* TODO: optimization possible - we already may have mea here */
644 rc = mds_splitting_expected(obd, dentry);
645 if (rc == MDS_NO_SPLITTABLE)
647 if (rc == MDS_NO_SPLIT_EXPECTED && nstripes == 0)
649 if (nstripes && nstripes == 1)
652 LASSERT(mea == NULL || *mea == NULL);
654 CDEBUG(D_OTHER, "%s: split directory %u/%lu/%lu\n",
655 obd->obd_name, mds->mds_num, dir->i_ino,
656 (unsigned long) dir->i_generation);
660 mea_size = obd_size_diskmd(mds->mds_md_exp, NULL);
662 /* FIXME: Actually we may only want to allocate enough space for
663 * necessary amount of stripes, but on the other hand with this
664 * approach of allocating maximal possible amount of MDS slots,
665 * it would be easier to split the dir over more MDSes */
666 rc = obd_alloc_diskmd(mds->mds_md_exp, (void *)mea);
668 CERROR("obd_alloc_diskmd() failed, error %d.\n", rc);
674 (*mea)->mea_count = nstripes;
676 /* 1) create directory objects on slave MDS'es */
677 /* FIXME: should this be OBD method? */
682 oa->o_generation = dir->i_generation;
684 obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
685 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
686 OBD_MD_FLUID | OBD_MD_FLGID);
688 oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
689 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
690 oa->o_mode = dir->i_mode;
693 * until lmv_obd_create() properly rewritten, it is important to have
694 * here oa->o_id = dir->i_ino, as otherwise master object will have
695 * invalid store cookie (zero inode num), what will lead to -ESTALE in
696 * mds_open() or somewhere else.
698 oa->o_id = dir->i_ino;
701 rc = mds_read_inode_sid(obd, dir, &id);
704 CERROR("Can't read inode self id, inode %lu, "
705 "rc %d.\n", dir->i_ino, rc);
708 oa->o_fid = id_fid(&id);
709 oa->o_mds = mds->mds_num;
710 LASSERT(oa->o_fid != 0);
712 CDEBUG(D_OTHER, "%s: create subdirs with mode %o, uid %u, gid %u\n",
713 obd->obd_name, dir->i_mode, dir->i_uid, dir->i_gid);
715 rc = obd_create(mds->mds_md_exp, oa,
716 (struct lov_stripe_md **)mea, NULL);
718 CERROR("Can't create remote inode, rc = %d\n", rc);
722 LASSERT(id_fid(&(*mea)->mea_ids[0]));
723 CDEBUG(D_OTHER, "%d dirobjects created\n", (int)(*mea)->mea_count);
725 /* 2) update dir attribute */
728 handle = fsfilt_start(obd, dir, FSFILT_OP_SETATTR, NULL);
729 if (IS_ERR(handle)) {
731 CERROR("fsfilt_start() failed: %d\n", (int) PTR_ERR(handle));
732 GOTO(err_oa, rc = PTR_ERR(handle));
735 rc = fsfilt_set_md(obd, dir, handle, *mea, mea_size, EA_MEA);
738 CERROR("fsfilt_set_md() failed, error %d.\n", rc);
742 rc = fsfilt_commit(obd, mds->mds_sb, dir, handle, 0);
745 CERROR("fsfilt_commit() failed, error %d.\n", rc);
752 /* 3) read through the dir and distribute it over objects */
753 rc = scan_and_distribute(obd, dentry, *mea);
755 obd_free_diskmd(mds->mds_md_exp, (struct lov_mds_md **)mea);
757 CERROR("scan_and_distribute() failed, error %d.\n", rc);
768 static int filter_start_page_write(struct inode *inode,
769 struct niobuf_local *lnb)
771 struct page *page = alloc_pages(GFP_HIGHUSER, 0);
773 CERROR("no memory for a temp page\n");
774 return lnb->rc = -ENOMEM;
776 POISON_PAGE(page, 0xf1);
777 page->index = lnb->offset >> PAGE_SHIFT;
782 struct dentry *filter_id2dentry(struct obd_device *obd,
783 struct dentry *dir_dentry,
784 obd_gr group, obd_id id);
786 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
787 int objcount, struct obd_ioobj *obj,
788 int niocount, struct niobuf_remote *nb,
789 struct niobuf_local *res,
790 struct obd_trans_info *oti)
792 struct niobuf_remote *rnb;
793 struct niobuf_local *lnb = NULL;
794 int rc = 0, i, tot_bytes = 0;
795 unsigned long now = jiffies;
796 struct dentry *dentry;
800 LASSERT(objcount == 1);
801 LASSERT(obj->ioo_bufcnt > 0);
803 memset(res, 0, niocount * sizeof(*res));
807 id_ino(&id) = obj->ioo_id;
808 id_gen(&id) = obj->ioo_gr;
810 dentry = mds_id2dentry(exp->exp_obd, &id, NULL);
811 if (IS_ERR(dentry)) {
812 CERROR("can't get dentry for "LPU64"/%u: %d\n",
813 id.li_stc.u.e3s.l3s_ino, id.li_stc.u.e3s.l3s_gen,
814 (int)PTR_ERR(dentry));
815 GOTO(cleanup, rc = (int) PTR_ERR(dentry));
818 if (dentry->d_inode == NULL) {
819 CERROR("trying to BRW to non-existent file "LPU64"\n",
822 GOTO(cleanup, rc = -ENOENT);
825 if (time_after(jiffies, now + 15 * HZ))
826 CERROR("slow preprw_write setup %lus\n", (jiffies - now) / HZ);
828 CDEBUG(D_INFO, "preprw_write setup: %lu jiffies\n",
831 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
833 lnb->dentry = dentry;
834 lnb->offset = rnb->offset;
836 lnb->flags = rnb->flags;
838 rc = filter_start_page_write(dentry->d_inode, lnb);
840 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR, "page err %u@"
841 LPU64" %u/%u %p: rc %d\n", lnb->len, lnb->offset,
842 i, obj->ioo_bufcnt, dentry, rc);
844 __free_pages(lnb->page, 0);
848 tot_bytes += lnb->len;
851 if (time_after(jiffies, now + 15 * HZ))
852 CERROR("slow start_page_write %lus\n", (jiffies - now) / HZ);
854 CDEBUG(D_INFO, "start_page_write: %lu jiffies\n",
862 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
863 int objcount, struct obd_ioobj *obj, int niocount,
864 struct niobuf_local *res, struct obd_trans_info *oti,
867 struct obd_device *obd = exp->exp_obd;
868 struct niobuf_local *lnb;
869 struct inode *inode = NULL;
870 int rc = 0, i, cleanup_phase = 0, err, entries = 0;
873 LASSERT(objcount == 1);
874 LASSERT(current->journal_info == NULL);
877 inode = res->dentry->d_inode;
879 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
881 struct dir_entry *de;
883 buf = kmap(lnb->page);
884 LASSERT(buf != NULL);
885 end = buf + lnb->len;
886 de = (struct dir_entry *) buf;
887 while ((char *) de < end && de->namelen) {
888 err = fsfilt_add_dir_entry(obd, res->dentry, de->name,
889 de->namelen, de->ino,
890 de->generation, de->mds,
893 CERROR("can't add dir entry %*s->%u/%u/%u"
894 " to %lu/%u: %d\n", de->namelen,
895 de->name, de->mds, (unsigned)de->ino,
896 (unsigned) de->generation,
897 res->dentry->d_inode->i_ino,
898 res->dentry->d_inode->i_generation,
904 de = (struct dir_entry *)
905 ((char *) de + DIR_REC_LEN(de->namelen));
911 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++)
912 __free_page(lnb->page);
918 int mds_choose_mdsnum(struct obd_device *obd, const char *name, int len, int flags)
921 struct mds_obd *mds = &obd->u.mds;
922 int i = mds->mds_num;
924 if (flags & REC_REINT_CREATE) {
926 } else if (mds->mds_md_exp) {
927 lmv = &mds->mds_md_exp->exp_obd->u.lmv;
928 i = raw_name2idx(MEA_MAGIC_LAST_CHAR, lmv->desc.ld_tgt_count, name, len);
933 int mds_lock_slave_objs(struct obd_device *obd, struct dentry *dentry,
934 struct lustre_handle **rlockh)
936 struct mds_obd *mds = &obd->u.mds;
937 struct mdc_op_data *op_data;
938 struct lookup_intent it;
939 struct mea *mea = NULL;
944 LASSERT(rlockh != NULL);
945 LASSERT(dentry != NULL);
946 LASSERT(dentry->d_inode != NULL);
949 if (!mds->mds_md_obd)
952 /* a dir can be splitted only */
953 if (!S_ISDIR(dentry->d_inode->i_mode))
956 rc = mds_md_get_attr(obd, dentry->d_inode,
964 if (mea->mea_count == 0)
965 /* this is slave object */
966 GOTO(cleanup, rc = 0);
968 CDEBUG(D_OTHER, "%s: lock slaves for %lu/%lu\n",
969 obd->obd_name, (unsigned long)dentry->d_inode->i_ino,
970 (unsigned long)dentry->d_inode->i_generation);
972 handle_size = sizeof(struct lustre_handle) *
975 OBD_ALLOC(*rlockh, handle_size);
977 GOTO(cleanup, rc = -ENOMEM);
979 memset(*rlockh, 0, handle_size);
980 OBD_ALLOC(op_data, sizeof(*op_data));
981 if (op_data == NULL) {
982 OBD_FREE(*rlockh, handle_size);
985 memset(op_data, 0, sizeof(*op_data));
988 it.it_op = IT_UNLINK;
990 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
992 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
993 op_data, *rlockh, NULL, 0, ldlm_completion_ast,
994 mds_blocking_ast, NULL);
995 OBD_FREE(op_data, sizeof(*op_data));
996 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
999 OBD_FREE(mea, mea_size);
1003 void mds_unlock_slave_objs(struct obd_device *obd, struct dentry *dentry,
1004 struct lustre_handle *lockh)
1006 struct mds_obd *mds = &obd->u.mds;
1007 struct mea *mea = NULL;
1008 int mea_size, rc, i;
1011 if (lockh == NULL) {
1016 LASSERT(mds->mds_md_obd != NULL);
1017 LASSERT(S_ISDIR(dentry->d_inode->i_mode));
1019 rc = mds_md_get_attr(obd, dentry->d_inode, &mea, &mea_size);
1021 CERROR("locks are leaked\n");
1025 LASSERT(mea_size != 0);
1026 LASSERT(mea != NULL);
1027 LASSERT(mea->mea_count != 0);
1029 CDEBUG(D_OTHER, "%s: unlock slaves for %lu/%lu\n", obd->obd_name,
1030 (unsigned long) dentry->d_inode->i_ino,
1031 (unsigned long) dentry->d_inode->i_generation);
1033 for (i = 0; i < mea->mea_count; i++) {
1034 if (lockh[i].cookie != 0)
1035 ldlm_lock_decref(lockh + i, LCK_EX);
1038 OBD_FREE(lockh, sizeof(struct lustre_handle) * mea->mea_count);
1039 OBD_FREE(mea, mea_size);
1043 int mds_unlink_slave_objs(struct obd_device *obd, struct dentry *dentry)
1045 struct mds_obd *mds = &obd->u.mds;
1046 struct ptlrpc_request *req = NULL;
1047 struct mdc_op_data *op_data;
1048 struct mea *mea = NULL;
1052 /* clustered MD ? */
1053 if (!mds->mds_md_obd)
1056 /* a dir can be splitted only */
1057 if (!S_ISDIR(dentry->d_inode->i_mode))
1060 rc = mds_md_get_attr(obd, dentry->d_inode, &mea, &mea_size);
1067 if (mea->mea_count == 0)
1068 GOTO(cleanup, rc = 0);
1070 CDEBUG(D_OTHER, "%s: unlink slaves for %lu/%lu\n", obd->obd_name,
1071 (unsigned long)dentry->d_inode->i_ino,
1072 (unsigned long)dentry->d_inode->i_generation);
1074 OBD_ALLOC(op_data, sizeof(*op_data));
1075 if (op_data == NULL)
1078 memset(op_data, 0, sizeof(*op_data));
1079 op_data->mea1 = mea;
1080 rc = md_unlink(mds->mds_md_exp, op_data, &req);
1081 OBD_FREE(op_data, sizeof(*op_data));
1082 LASSERT(req == NULL);
1085 OBD_FREE(mea, mea_size);
1089 struct ide_tracking {
1094 int mds_ide_filldir(void *__buf, const char *name, int namelen,
1095 loff_t offset, ino_t ino, unsigned int d_type)
1097 struct ide_tracking *it = __buf;
1103 if (it->entries > 2)
1107 if (name[0] == '.' && namelen == 1)
1109 if (name[0] == '.' && name[1] == '.' && namelen == 2)
1116 /* checks if passed dentry points to empty dir. */
1117 int mds_is_dir_empty(struct obd_device *obd, struct dentry *dentry)
1119 struct ide_tracking it;
1127 nlen = strlen("__iopen__/") + 10 + 1;
1128 OBD_ALLOC(file_name, nlen);
1132 LASSERT(dentry->d_inode != NULL);
1133 i = sprintf(file_name, "__iopen__/0x%lx",
1134 dentry->d_inode->i_ino);
1136 file = filp_open(file_name, O_RDONLY, 0);
1138 CERROR("can't open directory %s: %d\n",
1139 file_name, (int) PTR_ERR(file));
1140 GOTO(cleanup, rc = PTR_ERR(file));
1143 rc = vfs_readdir(file, mds_ide_filldir, &it);
1144 filp_close(file, 0);
1146 if (it.empty && rc == 0)
1152 OBD_FREE(file_name, nlen);
1156 int mds_lock_and_check_slave(int offset, struct ptlrpc_request *req,
1157 struct lustre_handle *lockh)
1159 struct obd_device *obd = req->rq_export->exp_obd;
1160 struct dentry *dentry = NULL;
1161 struct lvfs_run_ctxt saved;
1162 int cleanup_phase = 0;
1163 struct mds_req_sec_desc *rsd;
1164 struct mds_body *body;
1165 struct lvfs_ucred uc;
1166 int rc, update_mode;
1169 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1171 CERROR("Can't unpack security desc\n");
1172 GOTO(cleanup, rc = -EFAULT);
1175 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1176 lustre_swab_mds_body);
1178 CERROR("Can't swab mds_body\n");
1179 GOTO(cleanup, rc = -EFAULT);
1181 CDEBUG(D_OTHER, "%s: check slave "DLID4"\n", obd->obd_name,
1184 dentry = mds_id2locked_dentry(obd, &body->id1, NULL, LCK_EX,
1185 lockh, &update_mode, NULL, 0,
1186 MDS_INODELOCK_UPDATE);
1187 if (IS_ERR(dentry)) {
1188 CERROR("can't find inode: %d\n", (int) PTR_ERR(dentry));
1189 GOTO(cleanup, rc = PTR_ERR(dentry));
1194 * handling the case when remote MDS checks if dir is empty
1195 * before rename. But it also does it for all entries, because
1196 * inode is stored here and remote MDS does not know if rename
1197 * point to dir or to reg file. So we check it here.
1199 if (!S_ISDIR(dentry->d_inode->i_mode))
1200 GOTO(cleanup, rc = 0);
1202 rc = mds_init_ucred(&uc, req, rsd);
1207 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1208 rc = mds_is_dir_empty(obd, dentry) ? 0 : -ENOTEMPTY;
1209 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1211 mds_exit_ucred(&uc);
1214 switch(cleanup_phase) {
1217 ldlm_lock_decref(lockh, LCK_EX);
1225 int mds_convert_mea_ea(struct obd_device *obd, struct inode *inode,
1226 struct lov_mds_md *lmm, int lmm_size)
1228 struct lov_stripe_md *lsm = NULL;
1229 struct mea_old *old;
1235 mea = (struct mea *)lmm;
1236 old = (struct mea_old *)lmm;
1238 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
1239 mea->mea_magic == MEA_MAGIC_ALL_CHARS)
1243 * making MDS try LOV EA converting in the non-LMV configuration
1246 if (!obd->u.mds.mds_md_exp)
1249 CDEBUG(D_INODE, "converting MEA EA on %lu/%u from V0 to V1 (%u/%u)\n",
1250 inode->i_ino, inode->i_generation, old->mea_count,
1253 rc = obd_unpackmd(obd->u.mds.mds_md_exp, &lsm, lmm, lmm_size);
1257 rc = obd_packmd(obd->u.mds.mds_md_exp, &lmm, lsm);
1259 GOTO(conv_free, rc);
1262 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1263 if (IS_ERR(handle)) {
1264 rc = PTR_ERR(handle);
1265 GOTO(conv_free, rc);
1268 rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size, EA_MEA);
1269 err = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 0);
1271 rc = err ? err : lmm_size;
1272 GOTO(conv_free, rc);
1274 obd_free_memmd(obd->u.mds.mds_md_exp, &lsm);