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>
44 #include <linux/lustre_sec.h>
45 #include <asm/div64.h>
47 #include "mds_internal.h"
51 * - magic in mea struct
53 int mds_md_connect(struct obd_device *obd, char *md_name)
55 struct mds_obd *mds = &obd->u.mds;
56 struct lustre_handle conn = {0};
57 unsigned long sec_flags = PTLRPC_SEC_FL_MDS;
62 if (IS_ERR(mds->mds_md_obd))
63 RETURN(PTR_ERR(mds->mds_md_obd));
65 if (mds->mds_md_connected)
68 down(&mds->mds_md_sem);
69 if (mds->mds_md_connected) {
74 mds->mds_md_obd = class_name2obd(md_name);
75 if (!mds->mds_md_obd) {
76 CERROR("MDS cannot locate MD(LMV) %s\n",
78 mds->mds_md_obd = ERR_PTR(-ENOTCONN);
79 GOTO(err_last, rc = -ENOTCONN);
82 rc = obd_connect(&conn, mds->mds_md_obd, &obd->obd_uuid, NULL,
83 OBD_OPT_MDS_CONNECTION);
85 CERROR("MDS cannot connect to MD(LMV) %s (%d)\n",
87 mds->mds_md_obd = ERR_PTR(rc);
90 mds->mds_md_exp = class_conn2export(&conn);
91 if (mds->mds_md_exp == NULL)
92 CERROR("can't get export!\n");
94 rc = obd_register_observer(mds->mds_md_obd, obd);
96 CERROR("MDS cannot register as observer of MD(LMV) %s, "
97 "rc = %d\n", md_name, rc);
101 /* retrieve size of EA */
102 rc = obd_get_info(mds->mds_md_exp, strlen("mdsize"),
103 "mdsize", &valsize, &value);
107 if (value > mds->mds_max_mdsize)
108 mds->mds_max_mdsize = value;
110 /* find our number in LMV cluster */
111 rc = obd_get_info(mds->mds_md_exp, strlen("mdsnum"),
112 "mdsnum", &valsize, &value);
116 mds->mds_num = value;
118 rc = obd_set_info(mds->mds_md_exp, strlen("inter_mds"),
119 "inter_mds", 0, NULL);
123 if (mds->mds_mds_sec) {
124 rc = obd_set_info(mds->mds_md_exp, strlen("sec"), "sec",
125 strlen(mds->mds_mds_sec), mds->mds_mds_sec);
130 rc = obd_set_info(mds->mds_md_exp, strlen("sec_flags"), "sec_flags",
131 sizeof(sec_flags), &sec_flags);
135 mds->mds_md_connected = 1;
136 up(&mds->mds_md_sem);
140 obd_register_observer(mds->mds_md_obd, NULL);
142 obd_disconnect(mds->mds_md_exp, 0);
143 mds->mds_md_exp = NULL;
144 mds->mds_md_obd = ERR_PTR(rc);
146 up(&mds->mds_md_sem);
150 int mds_md_postsetup(struct obd_device *obd)
152 struct mds_obd *mds = &obd->u.mds;
156 if (mds->mds_md_exp) {
157 rc = obd_init_ea_size(mds->mds_md_exp,
159 mds->mds_max_cookiesize);
165 int mds_md_disconnect(struct obd_device *obd, int flags)
167 struct mds_obd *mds = &obd->u.mds;
171 if (!mds->mds_md_connected)
174 down(&mds->mds_md_sem);
175 if (!IS_ERR(mds->mds_md_obd) && mds->mds_md_exp != NULL) {
176 LASSERT(mds->mds_md_connected);
178 obd_register_observer(mds->mds_md_obd, NULL);
180 if (flags & OBD_OPT_FORCE) {
181 struct obd_device *lmv_obd;
182 struct obd_ioctl_data ioc_data = { 0 };
184 lmv_obd = class_exp2obd(mds->mds_md_exp);
189 * making disconnecting lmv stuff do not send anything
190 * to all remote MDSs from LMV. This is needed to
191 * prevent possible hanging with endless recovery, when
192 * MDS sends disconnect to already disconnected
193 * target. Probably this is wrong, but client does the
194 * same in --force mode and I do not see why can't we do
197 lmv_obd->obd_no_recov = 1;
198 obd_iocontrol(IOC_OSC_SET_ACTIVE, mds->mds_md_exp,
199 sizeof(ioc_data), &ioc_data, NULL);
203 * if obd_disconnect() fails (probably because the export was
204 * disconnected by class_disconnect_exports()) then we just need
207 mds->mds_md_connected = 0;
208 rc = obd_disconnect(mds->mds_md_exp, flags);
210 class_export_put(mds->mds_md_exp);
213 mds->mds_md_exp = NULL;
214 mds->mds_md_obd = NULL;
216 up(&mds->mds_md_sem);
220 int mds_md_reconnect(struct obd_device *obd)
222 struct mds_obd *mds = &obd->u.mds;
223 struct obd_statfs osfs;
227 /* We don't know state of connections to another MDSes
228 * before the failure. If MDS we were connected to before
229 * the failure gets failed, then it will wait for us to
230 * reconnect and will timed recovery out. bug 4920 */
231 if (mds->mds_md_connected == 0)
233 if (mds->mds_md_obd == NULL)
236 err = obd_statfs(mds->mds_md_obd, &osfs, jiffies - HZ);
238 CERROR("can't reconnect to MDSes after recovery: %d\n", err);
242 int mds_md_get_attr(struct obd_device *obd, struct inode *inode,
243 struct mea **mea, int *mea_size)
245 struct mds_obd *mds = &obd->u.mds;
249 if (!mds->mds_md_obd)
251 if (!S_ISDIR(inode->i_mode))
254 /* first calculate mea size */
255 *mea_size = obd_alloc_diskmd(mds->mds_md_exp,
256 (struct lov_mds_md **)mea);
257 if (*mea_size < 0 || *mea == NULL)
258 return *mea_size < 0 ? *mea_size : -EINVAL;
260 rc = mds_get_md(obd, inode, *mea, mea_size, 1, 1);
263 OBD_FREE(*mea, *mea_size);
281 #define DIR_ROUND (DIR_PAD - 1)
282 #define DIR_REC_LEN(name_len) (((name_len) + 16 + DIR_ROUND) & ~DIR_ROUND)
284 /* this struct holds dir entries for particular MDS to be flushed */
286 struct list_head list;
291 struct brw_page brwc;
294 struct dirsplit_control {
295 struct obd_device *obd;
297 struct dentry *dentry;
299 struct dir_cache *cache;
302 static int dc_new_page_to_cache(struct dir_cache * dirc)
306 if (!list_empty(&dirc->list) && dirc->free > sizeof(__u16)) {
307 /* current page became full, mark the end */
308 struct dir_entry *de = dirc->cur;
312 page = alloc_page(GFP_KERNEL);
315 list_add_tail(&page->lru, &dirc->list);
316 dirc->cur = page_address(page);
317 dirc->free = PAGE_SIZE;
321 static int retrieve_generation_numbers(struct dirsplit_control *dc, void *buf)
323 struct mds_obd *mds = &dc->obd->u.mds;
324 struct dir_entry *de;
325 struct dentry *dentry;
328 end = buf + PAGE_SIZE;
329 de = (struct dir_entry *) buf;
330 while ((char *) de < end && de->namelen) {
331 /* lookup an inode */
332 LASSERT(de->namelen <= 255);
333 dentry = ll_lookup_one_len(de->name, dc->dentry,
335 if (IS_ERR(dentry)) {
336 CERROR("can't lookup %*s: %d\n", de->namelen,
337 de->name, (int) PTR_ERR(dentry));
340 if (dentry->d_inode != NULL) {
342 struct lustre_id sid;
344 down(&dentry->d_inode->i_sem);
345 rc = mds_read_inode_sid(dc->obd,
346 dentry->d_inode, &sid);
347 up(&dentry->d_inode->i_sem);
349 CERROR("Can't read inode self id, "
350 "inode %lu, rc %d\n",
351 dentry->d_inode->i_ino, rc);
355 de->fid = id_fid(&sid);
356 de->mds = mds->mds_num;
357 de->ino = dentry->d_inode->i_ino;
358 de->generation = dentry->d_inode->i_generation;
359 } else if (dentry->d_flags & DCACHE_CROSS_REF) {
360 de->fid = dentry->d_fid;
361 de->ino = dentry->d_inum;
362 de->mds = dentry->d_mdsnum;
363 de->generation = dentry->d_generation;
365 CERROR("can't lookup %*s\n", de->namelen, de->name);
371 de = (struct dir_entry *)
372 ((char *) de + DIR_REC_LEN(de->namelen));
377 static int flush_buffer_onto_mds(struct dirsplit_control *dc, int mdsnum)
379 struct mds_obd *mds = &dc->obd->u.mds;
380 struct list_head *cur, *tmp;
381 struct dir_cache *ca;
384 ca = dc->cache + mdsnum;
386 if (ca->free > sizeof(__u16)) {
387 /* current page became full, mark the end */
388 struct dir_entry *de = ca->cur;
392 list_for_each_safe(cur, tmp, &ca->list) {
395 page = list_entry(cur, struct page, lru);
396 LASSERT(page != NULL);
398 retrieve_generation_numbers(dc, page_address(page));
401 ca->brwc.disk_offset = ca->brwc.page_offset = 0;
402 ca->brwc.count = PAGE_SIZE;
404 ca->oa.o_mds = mdsnum;
405 rc = obd_brw(OBD_BRW_WRITE, mds->mds_md_exp, &ca->oa,
406 (struct lov_stripe_md *) dc->mea,
415 static int remove_entries_from_orig_dir(struct dirsplit_control *dc, int mdsnum)
417 struct list_head *cur, *tmp;
418 struct dentry *dentry;
419 struct dir_cache *ca;
420 struct dir_entry *de;
426 ca = dc->cache + mdsnum;
427 list_for_each_safe(cur, tmp, &ca->list) {
428 page = list_entry(cur, struct page, lru);
429 buf = page_address(page);
430 end = buf + PAGE_SIZE;
432 de = (struct dir_entry *) buf;
433 while ((char *) de < end && de->namelen) {
434 /* lookup an inode */
435 LASSERT(de->namelen <= 255);
437 dentry = ll_lookup_one_len(de->name, dc->dentry,
439 if (IS_ERR(dentry)) {
440 CERROR("can't lookup %*s: %d\n", de->namelen,
441 de->name, (int) PTR_ERR(dentry));
444 rc = fsfilt_del_dir_entry(dc->obd, dentry);
447 de = (struct dir_entry *)
448 ((char *) de + DIR_REC_LEN(de->namelen));
454 static int filldir(void * __buf, const char * name, int namlen,
455 loff_t offset, ino_t ino, unsigned int d_type)
457 struct dirsplit_control *dc = __buf;
458 struct mds_obd *mds = &dc->obd->u.mds;
459 struct dir_cache *ca;
460 struct dir_entry *de;
465 if (name[0] == '.' &&
466 (namlen == 1 || (namlen == 2 && name[1] == '.'))) {
467 /* skip special entries */
472 newmds = mea_name2idx(dc->mea, (char *) name, namlen);
474 if (newmds == mds->mds_num) {
475 /* this entry remains on the current MDS, skip moving */
479 OBD_ALLOC(n, namlen + 1);
480 memcpy(n, name, namlen);
481 n[namlen] = (char) 0;
483 OBD_FREE(n, namlen + 1);
485 /* check for space in buffer for new entry */
486 ca = dc->cache + newmds;
487 if (DIR_REC_LEN(namlen) > ca->free) {
488 int err = dc_new_page_to_cache(ca);
492 /* insert found entry into buffer to be flushed later */
493 /* NOTE: we'll fill generations number later, because we
494 * it's stored in inode, thus we need to lookup an entry,
495 * but directory is locked for readdir(), so we delay this */
499 de->namelen = namlen;
500 memcpy(de->name, name, namlen);
501 ca->cur += DIR_REC_LEN(namlen);
502 ca->free -= DIR_REC_LEN(namlen);
508 int scan_and_distribute(struct obd_device *obd, struct dentry *dentry,
511 struct inode *dir = dentry->d_inode;
512 struct dirsplit_control dc;
517 nlen = strlen("__iopen__/") + 10 + 1;
518 OBD_ALLOC(file_name, nlen);
522 i = sprintf(file_name, "__iopen__/0x%lx",
523 dentry->d_inode->i_ino);
525 file = filp_open(file_name, O_RDONLY, 0);
527 CERROR("can't open directory %s: %d\n",
528 file_name, (int) PTR_ERR(file));
529 OBD_FREE(file_name, nlen);
530 RETURN(PTR_ERR(file));
533 memset(&dc, 0, sizeof(dc));
538 OBD_ALLOC(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
539 LASSERT(dc.cache != NULL);
540 for (i = 0; i < mea->mea_count; i++) {
541 INIT_LIST_HEAD(&dc.cache[i].list);
542 dc.cache[i].free = 0;
543 dc.cache[i].cached = 0;
546 err = vfs_readdir(file, filldir, &dc);
551 for (i = 0; i < mea->mea_count; i++) {
552 if (!dc.cache[i].cached)
554 err = flush_buffer_onto_mds(&dc, i);
559 for (i = 0; i < mea->mea_count; i++) {
560 if (!dc.cache[i].cached)
562 err = remove_entries_from_orig_dir(&dc, i);
569 for (i = 0; i < mea->mea_count; i++) {
570 struct list_head *cur, *tmp;
571 if (!dc.cache[i].cached)
573 list_for_each_safe(cur, tmp, &dc.cache[i].list) {
575 page = list_entry(cur, struct page, lru);
576 list_del(&page->lru);
580 OBD_FREE(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
581 OBD_FREE(file_name, nlen);
586 #define MAX_DIR_SIZE (64 * 1024)
587 #define I_NON_SPLITTABLE (256)
589 int mds_splitting_expected(struct obd_device *obd, struct dentry *dentry)
591 struct mds_obd *mds = &obd->u.mds;
592 struct mea *mea = NULL;
596 if (!mds->mds_md_obd)
597 return MDS_NO_SPLITTABLE;
600 if (dentry->d_inode == NULL)
601 return MDS_NO_SPLITTABLE;
603 /* a dir can be splitted only */
604 if (!S_ISDIR(dentry->d_inode->i_mode))
605 return MDS_NO_SPLITTABLE;
607 /* already splittied or slave directory (part of splitted dir) */
608 if (dentry->d_inode->i_flags & I_NON_SPLITTABLE)
609 return MDS_NO_SPLITTABLE;
611 /* don't split root directory */
612 if (dentry->d_inode->i_ino == id_ino(&mds->mds_rootid))
613 return MDS_NO_SPLITTABLE;
615 /* large enough to be splitted? */
616 if (dentry->d_inode->i_size < MAX_DIR_SIZE)
617 return MDS_NO_SPLIT_EXPECTED;
619 mds_md_get_attr(obd, dentry->d_inode, &mea, &size);
621 /* already splitted or slave object: shouldn't be splitted */
622 rc = MDS_NO_SPLITTABLE;
623 /* mark to skip subsequent checks */
624 dentry->d_inode->i_flags |= I_NON_SPLITTABLE;
627 /* may be splitted */
628 rc = MDS_EXPECT_SPLIT;
635 * must not be called on already splitted directories.
637 int mds_try_to_split_dir(struct obd_device *obd, struct dentry *dentry,
638 struct mea **mea, int nstripes, int update_mode)
640 struct inode *dir = dentry->d_inode;
641 struct mds_obd *mds = &obd->u.mds;
642 struct mea *tmea = NULL;
643 struct obdo *oa = NULL;
644 int rc, mea_size = 0;
649 if (update_mode != LCK_EX)
652 /* TODO: optimization possible - we already may have mea here */
653 rc = mds_splitting_expected(obd, dentry);
654 if (rc == MDS_NO_SPLITTABLE)
656 if (rc == MDS_NO_SPLIT_EXPECTED && nstripes == 0)
658 if (nstripes && nstripes == 1)
661 LASSERT(mea == NULL || *mea == NULL);
663 CDEBUG(D_OTHER, "%s: split directory %u/%lu/%lu\n",
664 obd->obd_name, mds->mds_num, dir->i_ino,
665 (unsigned long) dir->i_generation);
669 mea_size = obd_size_diskmd(mds->mds_md_exp, NULL);
671 /* FIXME: Actually we may only want to allocate enough space for
672 * necessary amount of stripes, but on the other hand with this
673 * approach of allocating maximal possible amount of MDS slots,
674 * it would be easier to split the dir over more MDSes */
675 rc = obd_alloc_diskmd(mds->mds_md_exp, (void *)mea);
677 CERROR("obd_alloc_diskmd() failed, error %d.\n", rc);
683 (*mea)->mea_count = nstripes;
685 /* 1) create directory objects on slave MDS'es */
686 /* FIXME: should this be OBD method? */
691 oa->o_generation = dir->i_generation;
693 obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
694 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
695 OBD_MD_FLUID | OBD_MD_FLGID);
697 oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
698 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
699 oa->o_mode = dir->i_mode;
702 * until lmv_obd_create() properly rewritten, it is important to have
703 * here oa->o_id = dir->i_ino, as otherwise master object will have
704 * invalid store cookie (zero inode num), what will lead to -ESTALE in
705 * mds_open() or somewhere else.
707 oa->o_id = dir->i_ino;
709 /* get parent id: ldlm lock on the parent protects ea */
710 rc = mds_read_inode_sid(obd, dir, &id);
712 CERROR("Can't read inode self id, inode %lu, "
713 "rc %d.\n", dir->i_ino, rc);
716 oa->o_fid = id_fid(&id);
717 oa->o_mds = mds->mds_num;
718 LASSERT(oa->o_fid != 0);
720 CDEBUG(D_OTHER, "%s: create subdirs with mode %o, uid %u, gid %u\n",
721 obd->obd_name, dir->i_mode, dir->i_uid, dir->i_gid);
723 rc = obd_create(mds->mds_md_exp, oa, NULL, 0,
724 (struct lov_stripe_md **)mea, NULL);
726 CERROR("Can't create remote inode, rc = %d\n", rc);
730 LASSERT(id_fid(&(*mea)->mea_ids[0]));
731 CDEBUG(D_OTHER, "%d dirobjects created\n", (int)(*mea)->mea_count);
733 /* 2) update dir attribute */
736 handle = fsfilt_start(obd, dir, FSFILT_OP_SETATTR, NULL);
737 if (IS_ERR(handle)) {
739 CERROR("fsfilt_start() failed: %d\n", (int) PTR_ERR(handle));
740 GOTO(err_oa, rc = PTR_ERR(handle));
743 rc = fsfilt_set_md(obd, dir, handle, *mea, mea_size, EA_MEA);
746 CERROR("fsfilt_set_md() failed, error %d.\n", rc);
750 rc = fsfilt_commit(obd, mds->mds_sb, dir, handle, 0);
753 CERROR("fsfilt_commit() failed, error %d.\n", rc);
760 /* 3) read through the dir and distribute it over objects */
761 rc = scan_and_distribute(obd, dentry, *mea);
763 obd_free_diskmd(mds->mds_md_exp, (struct lov_mds_md **)mea);
765 CERROR("scan_and_distribute() failed, error %d.\n", rc);
776 static int filter_start_page_write(struct inode *inode,
777 struct niobuf_local *lnb)
779 struct page *page = alloc_pages(GFP_HIGHUSER, 0);
781 CERROR("no memory for a temp page\n");
782 return lnb->rc = -ENOMEM;
784 POISON_PAGE(page, 0xf1);
785 page->index = lnb->offset >> PAGE_SHIFT;
790 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
791 int objcount, struct obd_ioobj *obj,
792 int niocount, struct niobuf_remote *nb,
793 struct niobuf_local *res,
794 struct obd_trans_info *oti, struct lustre_capa *capa)
796 struct niobuf_remote *rnb;
797 struct niobuf_local *lnb = NULL;
798 int rc = 0, i, tot_bytes = 0;
799 unsigned long now = jiffies;
800 struct dentry *dentry;
804 LASSERT(objcount == 1);
805 LASSERT(obj->ioo_bufcnt > 0);
807 memset(res, 0, niocount * sizeof(*res));
811 id_ino(&id) = obj->ioo_id;
812 id_gen(&id) = obj->ioo_gr;
814 dentry = mds_id2dentry(exp->exp_obd, &id, NULL);
815 if (IS_ERR(dentry)) {
816 CERROR("can't get dentry for "LPU64"/%u: %d\n",
817 id.li_stc.u.e3s.l3s_ino, id.li_stc.u.e3s.l3s_gen,
818 (int)PTR_ERR(dentry));
819 GOTO(cleanup, rc = (int) PTR_ERR(dentry));
822 if (dentry->d_inode == NULL) {
823 CERROR("trying to BRW to non-existent file "LPU64"\n",
826 GOTO(cleanup, rc = -ENOENT);
829 if (time_after(jiffies, now + 15 * HZ))
830 CERROR("slow preprw_write setup %lus\n", (jiffies - now) / HZ);
832 CDEBUG(D_INFO, "preprw_write setup: %lu jiffies\n",
835 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
837 lnb->dentry = dentry;
838 lnb->offset = rnb->offset;
840 lnb->flags = rnb->flags;
842 rc = filter_start_page_write(dentry->d_inode, lnb);
844 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR, "page err %u@"
845 LPU64" %u/%u %p: rc %d\n", lnb->len, lnb->offset,
846 i, obj->ioo_bufcnt, dentry, rc);
848 __free_pages(lnb->page, 0);
852 tot_bytes += lnb->len;
855 if (time_after(jiffies, now + 15 * HZ))
856 CERROR("slow start_page_write %lus\n", (jiffies - now) / HZ);
858 CDEBUG(D_INFO, "start_page_write: %lu jiffies\n",
866 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
867 int objcount, struct obd_ioobj *obj, int niocount,
868 struct niobuf_local *res, struct obd_trans_info *oti,
871 struct obd_device *obd = exp->exp_obd;
872 struct niobuf_local *lnb;
873 struct inode *inode = NULL;
874 int rc = 0, i, cleanup_phase = 0, err, entries = 0;
877 LASSERT(objcount == 1);
878 LASSERT(current->journal_info == NULL);
881 inode = res->dentry->d_inode;
883 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
885 struct dir_entry *de;
887 buf = kmap(lnb->page);
888 LASSERT(buf != NULL);
889 end = buf + lnb->len;
890 de = (struct dir_entry *) buf;
891 while ((char *) de < end && de->namelen) {
892 err = fsfilt_add_dir_entry(obd, res->dentry, de->name,
893 de->namelen, de->ino,
894 de->generation, de->mds,
897 CERROR("can't add dir entry %*s->%u/%u/%u"
898 " to %lu/%u: %d\n", de->namelen,
899 de->name, de->mds, (unsigned)de->ino,
900 (unsigned) de->generation,
901 res->dentry->d_inode->i_ino,
902 res->dentry->d_inode->i_generation,
908 de = (struct dir_entry *)
909 ((char *) de + DIR_REC_LEN(de->namelen));
915 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++)
916 __free_page(lnb->page);
922 int mds_choose_mdsnum(struct obd_device *obd, const char *name, int len, int flags,
923 struct ptlrpc_peer *peer, struct inode *parent, int local)
925 struct mds_obd *mds = &obd->u.mds;
926 char peer_str[PTL_NALFMT_SIZE];
927 int i = mds->mds_num;
932 RETURN(mds->mds_num);
934 if (flags & REC_REINT_CREATE) {
936 } else if (mds->mds_md_exp != NULL && peer != NULL) {
937 LASSERT(parent != NULL);
938 /* distribute only at root level */
939 lmv = &mds->mds_md_exp->exp_obd->u.lmv;
940 if (parent->i_ino != id_ino(&mds->mds_rootid)) {
943 __u64 nid = peer->peer_id.nid;
944 __u64 count = lmv->desc.ld_tgt_count;
945 i = do_div(nid, count);
946 CDEBUG(D_OTHER, "client from %s creates top dir %*s "
948 ptlrpc_peernid2str(peer,peer_str), len, name, i+1);
950 } else if (mds->mds_md_exp) {
951 lmv = &mds->mds_md_exp->exp_obd->u.lmv;
952 i = raw_name2idx(MEA_MAGIC_LAST_CHAR, lmv->desc.ld_tgt_count, name, len);
957 int mds_lock_slave_objs(struct obd_device *obd, struct dentry *dentry,
958 struct lustre_handle **rlockh)
960 struct mds_obd *mds = &obd->u.mds;
961 struct mdc_op_data *op_data;
962 struct lookup_intent it;
963 struct mea *mea = NULL;
968 LASSERT(rlockh != NULL);
969 LASSERT(dentry != NULL);
970 LASSERT(dentry->d_inode != NULL);
973 if (!mds->mds_md_obd)
976 /* a dir can be splitted only */
977 if (!S_ISDIR(dentry->d_inode->i_mode))
980 rc = mds_md_get_attr(obd, dentry->d_inode,
988 if (mea->mea_count == 0)
989 /* this is slave object */
990 GOTO(cleanup, rc = 0);
992 CDEBUG(D_OTHER, "%s: lock slaves for %lu/%lu\n",
993 obd->obd_name, (unsigned long)dentry->d_inode->i_ino,
994 (unsigned long)dentry->d_inode->i_generation);
996 handle_size = sizeof(struct lustre_handle) *
999 OBD_ALLOC(*rlockh, handle_size);
1000 if (*rlockh == NULL)
1001 GOTO(cleanup, rc = -ENOMEM);
1003 memset(*rlockh, 0, handle_size);
1004 OBD_ALLOC(op_data, sizeof(*op_data));
1005 if (op_data == NULL) {
1006 OBD_FREE(*rlockh, handle_size);
1009 memset(op_data, 0, sizeof(*op_data));
1011 op_data->mea1 = mea;
1012 it.it_op = IT_UNLINK;
1014 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
1016 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
1017 op_data, *rlockh, NULL, 0, ldlm_completion_ast,
1018 mds_blocking_ast, NULL);
1019 OBD_FREE(op_data, sizeof(*op_data));
1020 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
1023 OBD_FREE(mea, mea_size);
1027 void mds_unlock_slave_objs(struct obd_device *obd, struct dentry *dentry,
1028 struct lustre_handle *lockh)
1030 struct mds_obd *mds = &obd->u.mds;
1031 struct mea *mea = NULL;
1032 int mea_size, rc, i;
1035 if (lockh == NULL) {
1040 LASSERT(mds->mds_md_obd != NULL);
1041 LASSERT(S_ISDIR(dentry->d_inode->i_mode));
1043 rc = mds_md_get_attr(obd, dentry->d_inode, &mea, &mea_size);
1045 CERROR("locks are leaked\n");
1049 LASSERT(mea_size != 0);
1050 LASSERT(mea != NULL);
1051 LASSERT(mea->mea_count != 0);
1053 CDEBUG(D_OTHER, "%s: unlock slaves for %lu/%lu\n", obd->obd_name,
1054 (unsigned long) dentry->d_inode->i_ino,
1055 (unsigned long) dentry->d_inode->i_generation);
1057 for (i = 0; i < mea->mea_count; i++) {
1058 if (lockh[i].cookie != 0)
1059 ldlm_lock_decref(lockh + i, LCK_EX);
1062 OBD_FREE(lockh, sizeof(struct lustre_handle) * mea->mea_count);
1063 OBD_FREE(mea, mea_size);
1067 int mds_unlink_slave_objs(struct obd_device *obd, struct dentry *dentry)
1069 struct mds_obd *mds = &obd->u.mds;
1070 struct ptlrpc_request *req = NULL;
1071 struct mdc_op_data *op_data;
1072 struct mea *mea = NULL;
1076 /* clustered MD ? */
1077 if (!mds->mds_md_obd)
1080 /* a dir can be splitted only */
1081 if (!S_ISDIR(dentry->d_inode->i_mode))
1084 rc = mds_md_get_attr(obd, dentry->d_inode, &mea, &mea_size);
1091 if (mea->mea_count == 0)
1092 GOTO(cleanup, rc = 0);
1094 CDEBUG(D_OTHER, "%s: unlink slaves for %lu/%lu\n", obd->obd_name,
1095 (unsigned long)dentry->d_inode->i_ino,
1096 (unsigned long)dentry->d_inode->i_generation);
1098 OBD_ALLOC(op_data, sizeof(*op_data));
1099 if (op_data == NULL)
1102 memset(op_data, 0, sizeof(*op_data));
1103 op_data->mea1 = mea;
1104 rc = md_unlink(mds->mds_md_exp, op_data, &req);
1105 OBD_FREE(op_data, sizeof(*op_data));
1106 LASSERT(req == NULL);
1109 OBD_FREE(mea, mea_size);
1113 struct ide_tracking {
1118 int mds_ide_filldir(void *__buf, const char *name, int namelen,
1119 loff_t offset, ino_t ino, unsigned int d_type)
1121 struct ide_tracking *it = __buf;
1127 if (it->entries > 2)
1131 if (name[0] == '.' && namelen == 1)
1133 if (name[0] == '.' && name[1] == '.' && namelen == 2)
1140 /* checks if passed dentry points to empty dir. */
1141 int mds_is_dir_empty(struct obd_device *obd, struct dentry *dentry)
1143 struct ide_tracking it;
1151 nlen = strlen("__iopen__/") + 10 + 1;
1152 OBD_ALLOC(file_name, nlen);
1156 LASSERT(dentry->d_inode != NULL);
1157 i = sprintf(file_name, "__iopen__/0x%lx",
1158 dentry->d_inode->i_ino);
1160 file = filp_open(file_name, O_RDONLY, 0);
1162 CERROR("can't open directory %s: %d\n",
1163 file_name, (int) PTR_ERR(file));
1164 GOTO(cleanup, rc = PTR_ERR(file));
1167 rc = vfs_readdir(file, mds_ide_filldir, &it);
1168 filp_close(file, 0);
1170 if (it.empty && rc == 0)
1176 OBD_FREE(file_name, nlen);
1180 int mds_lock_and_check_slave(int offset, struct ptlrpc_request *req,
1181 struct lustre_handle *lockh)
1183 struct obd_device *obd = req->rq_export->exp_obd;
1184 struct dentry *dentry = NULL;
1185 struct lvfs_run_ctxt saved;
1186 int cleanup_phase = 0;
1187 struct mds_req_sec_desc *rsd;
1188 struct mds_body *body;
1189 struct lvfs_ucred uc;
1190 int rc, update_mode;
1193 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1195 CERROR("Can't unpack security desc\n");
1196 GOTO(cleanup, rc = -EFAULT);
1199 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1200 lustre_swab_mds_body);
1202 CERROR("Can't swab mds_body\n");
1203 GOTO(cleanup, rc = -EFAULT);
1205 CDEBUG(D_OTHER, "%s: check slave "DLID4"\n", obd->obd_name,
1208 dentry = mds_id2locked_dentry(obd, &body->id1, NULL, LCK_EX,
1209 lockh, &update_mode, NULL, 0,
1210 MDS_INODELOCK_UPDATE);
1211 if (IS_ERR(dentry)) {
1212 CERROR("can't find inode: %d\n", (int) PTR_ERR(dentry));
1213 GOTO(cleanup, rc = PTR_ERR(dentry));
1218 * handling the case when remote MDS checks if dir is empty
1219 * before rename. But it also does it for all entries, because
1220 * inode is stored here and remote MDS does not know if rename
1221 * point to dir or to reg file. So we check it here.
1223 if (!S_ISDIR(dentry->d_inode->i_mode))
1224 GOTO(cleanup, rc = 0);
1226 rc = mds_init_ucred(&uc, req, rsd);
1231 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1232 rc = mds_is_dir_empty(obd, dentry) ? 0 : -ENOTEMPTY;
1233 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1235 mds_exit_ucred(&uc);
1238 switch(cleanup_phase) {
1241 ldlm_lock_decref(lockh, LCK_EX);
1249 int mds_convert_mea_ea(struct obd_device *obd, struct inode *inode,
1250 struct lov_mds_md *lmm, int lmm_size)
1252 struct lov_stripe_md *lsm = NULL;
1253 struct mea_old *old;
1259 mea = (struct mea *)lmm;
1260 old = (struct mea_old *)lmm;
1262 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
1263 mea->mea_magic == MEA_MAGIC_ALL_CHARS)
1267 * making MDS try LOV EA converting in the non-LMV configuration
1270 if (!obd->u.mds.mds_md_exp)
1273 CDEBUG(D_INODE, "converting MEA EA on %lu/%u from V0 to V1 (%u/%u)\n",
1274 inode->i_ino, inode->i_generation, old->mea_count,
1277 rc = obd_unpackmd(obd->u.mds.mds_md_exp, &lsm, lmm, lmm_size);
1281 rc = obd_packmd(obd->u.mds.mds_md_exp, &lmm, lsm);
1283 GOTO(conv_free, rc);
1286 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1287 if (IS_ERR(handle)) {
1288 rc = PTR_ERR(handle);
1289 GOTO(conv_free, rc);
1292 rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size, EA_MEA);
1293 err = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 0);
1295 rc = err ? err : lmm_size;
1296 GOTO(conv_free, rc);
1298 obd_free_memmd(obd->u.mds.mds_md_exp, &lsm);