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 <asm/div64.h>
46 #include "mds_internal.h"
50 * - magic in mea struct
52 int mds_md_connect(struct obd_device *obd, char *md_name)
54 struct mds_obd *mds = &obd->u.mds;
55 struct lustre_handle conn = {0};
60 if (IS_ERR(mds->mds_md_obd))
61 RETURN(PTR_ERR(mds->mds_md_obd));
63 if (mds->mds_md_connected)
66 down(&mds->mds_md_sem);
67 if (mds->mds_md_connected) {
72 mds->mds_md_obd = class_name2obd(md_name);
73 if (!mds->mds_md_obd) {
74 CERROR("MDS cannot locate MD(LMV) %s\n",
76 mds->mds_md_obd = ERR_PTR(-ENOTCONN);
77 GOTO(err_last, rc = -ENOTCONN);
80 rc = obd_connect(&conn, mds->mds_md_obd, &obd->obd_uuid, NULL,
81 OBD_OPT_MDS_CONNECTION);
83 CERROR("MDS cannot connect to MD(LMV) %s (%d)\n",
85 mds->mds_md_obd = ERR_PTR(rc);
88 mds->mds_md_exp = class_conn2export(&conn);
89 if (mds->mds_md_exp == NULL)
90 CERROR("can't get export!\n");
92 rc = obd_register_observer(mds->mds_md_obd, obd);
94 CERROR("MDS cannot register as observer of MD(LMV) %s, "
95 "rc = %d\n", md_name, rc);
99 /* retrieve size of EA */
100 rc = obd_get_info(mds->mds_md_exp, strlen("mdsize"),
101 "mdsize", &valsize, &value);
105 if (value > mds->mds_max_mdsize)
106 mds->mds_max_mdsize = value;
108 /* find our number in LMV cluster */
109 rc = obd_get_info(mds->mds_md_exp, strlen("mdsnum"),
110 "mdsnum", &valsize, &value);
114 mds->mds_num = value;
116 rc = obd_set_info(mds->mds_md_exp, strlen("inter_mds"),
117 "inter_mds", 0, NULL);
121 if (mds->mds_mds_sec) {
122 rc = obd_set_info(mds->mds_md_exp, strlen("sec"), "sec",
123 strlen(mds->mds_mds_sec), mds->mds_mds_sec);
128 mds->mds_md_connected = 1;
129 up(&mds->mds_md_sem);
133 obd_register_observer(mds->mds_md_obd, NULL);
135 obd_disconnect(mds->mds_md_exp, 0);
136 mds->mds_md_exp = NULL;
137 mds->mds_md_obd = ERR_PTR(rc);
139 up(&mds->mds_md_sem);
143 int mds_md_postsetup(struct obd_device *obd)
145 struct mds_obd *mds = &obd->u.mds;
149 if (mds->mds_md_exp) {
150 rc = obd_init_ea_size(mds->mds_md_exp,
152 mds->mds_max_cookiesize);
158 int mds_md_disconnect(struct obd_device *obd, int flags)
160 struct mds_obd *mds = &obd->u.mds;
164 if (!mds->mds_md_connected)
167 down(&mds->mds_md_sem);
168 if (!IS_ERR(mds->mds_md_obd) && mds->mds_md_exp != NULL) {
169 LASSERT(mds->mds_md_connected);
171 obd_register_observer(mds->mds_md_obd, NULL);
173 if (flags & OBD_OPT_FORCE) {
174 struct obd_device *lmv_obd;
175 struct obd_ioctl_data ioc_data = { 0 };
177 lmv_obd = class_exp2obd(mds->mds_md_exp);
182 * making disconnecting lmv stuff do not send anything
183 * to all remote MDSs from LMV. This is needed to
184 * prevent possible hanging with endless recovery, when
185 * MDS sends disconnect to already disconnected
186 * target. Probably this is wrong, but client does the
187 * same in --force mode and I do not see why can't we do
190 lmv_obd->obd_no_recov = 1;
191 obd_iocontrol(IOC_OSC_SET_ACTIVE, mds->mds_md_exp,
192 sizeof(ioc_data), &ioc_data, NULL);
196 * if obd_disconnect() fails (probably because the export was
197 * disconnected by class_disconnect_exports()) then we just need
200 mds->mds_md_connected = 0;
201 rc = obd_disconnect(mds->mds_md_exp, flags);
203 class_export_put(mds->mds_md_exp);
206 mds->mds_md_exp = NULL;
207 mds->mds_md_obd = NULL;
209 up(&mds->mds_md_sem);
213 int mds_md_reconnect(struct obd_device *obd)
215 struct mds_obd *mds = &obd->u.mds;
216 struct obd_statfs osfs;
220 /* We don't know state of connections to another MDSes
221 * before the failure. If MDS we were connected to before
222 * the failure gets failed, then it will wait for us to
223 * reconnect and will timed recovery out. bug 4920 */
224 if (mds->mds_md_connected == 0)
226 if (mds->mds_md_obd == NULL)
229 err = obd_statfs(mds->mds_md_obd, &osfs, jiffies - HZ);
231 CERROR("can't reconnect to MDSes after recovery: %d\n", err);
235 int mds_md_get_attr(struct obd_device *obd, struct inode *inode,
236 struct mea **mea, int *mea_size)
238 struct mds_obd *mds = &obd->u.mds;
242 if (!mds->mds_md_obd)
244 if (!S_ISDIR(inode->i_mode))
247 /* first calculate mea size */
248 *mea_size = obd_alloc_diskmd(mds->mds_md_exp,
249 (struct lov_mds_md **)mea);
250 if (*mea_size < 0 || *mea == NULL)
251 return *mea_size < 0 ? *mea_size : -EINVAL;
253 rc = mds_get_md(obd, inode, *mea, mea_size, 1, 1);
256 OBD_FREE(*mea, *mea_size);
274 #define DIR_ROUND (DIR_PAD - 1)
275 #define DIR_REC_LEN(name_len) (((name_len) + 16 + DIR_ROUND) & ~DIR_ROUND)
277 /* this struct holds dir entries for particular MDS to be flushed */
279 struct list_head list;
284 struct brw_page brwc;
287 struct dirsplit_control {
288 struct obd_device *obd;
290 struct dentry *dentry;
292 struct dir_cache *cache;
295 static int dc_new_page_to_cache(struct dir_cache * dirc)
299 if (!list_empty(&dirc->list) && dirc->free > sizeof(__u16)) {
300 /* current page became full, mark the end */
301 struct dir_entry *de = dirc->cur;
305 page = alloc_page(GFP_KERNEL);
308 list_add_tail(&page->lru, &dirc->list);
309 dirc->cur = page_address(page);
310 dirc->free = PAGE_SIZE;
314 static int retrieve_generation_numbers(struct dirsplit_control *dc, void *buf)
316 struct mds_obd *mds = &dc->obd->u.mds;
317 struct dir_entry *de;
318 struct dentry *dentry;
321 end = buf + PAGE_SIZE;
322 de = (struct dir_entry *) buf;
323 while ((char *) de < end && de->namelen) {
324 /* lookup an inode */
325 LASSERT(de->namelen <= 255);
326 dentry = ll_lookup_one_len(de->name, dc->dentry,
328 if (IS_ERR(dentry)) {
329 CERROR("can't lookup %*s: %d\n", de->namelen,
330 de->name, (int) PTR_ERR(dentry));
333 if (dentry->d_inode != NULL) {
335 struct lustre_id sid;
337 down(&dentry->d_inode->i_sem);
338 rc = mds_read_inode_sid(dc->obd,
339 dentry->d_inode, &sid);
340 up(&dentry->d_inode->i_sem);
342 CERROR("Can't read inode self id, "
343 "inode %lu, rc %d\n",
344 dentry->d_inode->i_ino, rc);
348 de->fid = id_fid(&sid);
349 de->mds = mds->mds_num;
350 de->ino = dentry->d_inode->i_ino;
351 de->generation = dentry->d_inode->i_generation;
352 } else if (dentry->d_flags & DCACHE_CROSS_REF) {
353 de->fid = dentry->d_fid;
354 de->ino = dentry->d_inum;
355 de->mds = dentry->d_mdsnum;
356 de->generation = dentry->d_generation;
358 CERROR("can't lookup %*s\n", de->namelen, de->name);
364 de = (struct dir_entry *)
365 ((char *) de + DIR_REC_LEN(de->namelen));
370 static int flush_buffer_onto_mds(struct dirsplit_control *dc, int mdsnum)
372 struct mds_obd *mds = &dc->obd->u.mds;
373 struct list_head *cur, *tmp;
374 struct dir_cache *ca;
377 ca = dc->cache + mdsnum;
379 if (ca->free > sizeof(__u16)) {
380 /* current page became full, mark the end */
381 struct dir_entry *de = ca->cur;
385 list_for_each_safe(cur, tmp, &ca->list) {
388 page = list_entry(cur, struct page, lru);
389 LASSERT(page != NULL);
391 retrieve_generation_numbers(dc, page_address(page));
394 ca->brwc.disk_offset = ca->brwc.page_offset = 0;
395 ca->brwc.count = PAGE_SIZE;
397 ca->oa.o_mds = mdsnum;
398 rc = obd_brw(OBD_BRW_WRITE, mds->mds_md_exp, &ca->oa,
399 (struct lov_stripe_md *) dc->mea,
408 static int remove_entries_from_orig_dir(struct dirsplit_control *dc, int mdsnum)
410 struct list_head *cur, *tmp;
411 struct dentry *dentry;
412 struct dir_cache *ca;
413 struct dir_entry *de;
419 ca = dc->cache + mdsnum;
420 list_for_each_safe(cur, tmp, &ca->list) {
421 page = list_entry(cur, struct page, lru);
422 buf = page_address(page);
423 end = buf + PAGE_SIZE;
425 de = (struct dir_entry *) buf;
426 while ((char *) de < end && de->namelen) {
427 /* lookup an inode */
428 LASSERT(de->namelen <= 255);
430 dentry = ll_lookup_one_len(de->name, dc->dentry,
432 if (IS_ERR(dentry)) {
433 CERROR("can't lookup %*s: %d\n", de->namelen,
434 de->name, (int) PTR_ERR(dentry));
437 rc = fsfilt_del_dir_entry(dc->obd, dentry);
440 de = (struct dir_entry *)
441 ((char *) de + DIR_REC_LEN(de->namelen));
447 static int filldir(void * __buf, const char * name, int namlen,
448 loff_t offset, ino_t ino, unsigned int d_type)
450 struct dirsplit_control *dc = __buf;
451 struct mds_obd *mds = &dc->obd->u.mds;
452 struct dir_cache *ca;
453 struct dir_entry *de;
458 if (name[0] == '.' &&
459 (namlen == 1 || (namlen == 2 && name[1] == '.'))) {
460 /* skip special entries */
465 newmds = mea_name2idx(dc->mea, (char *) name, namlen);
467 if (newmds == mds->mds_num) {
468 /* this entry remains on the current MDS, skip moving */
472 OBD_ALLOC(n, namlen + 1);
473 memcpy(n, name, namlen);
474 n[namlen] = (char) 0;
476 OBD_FREE(n, namlen + 1);
478 /* check for space in buffer for new entry */
479 ca = dc->cache + newmds;
480 if (DIR_REC_LEN(namlen) > ca->free) {
481 int err = dc_new_page_to_cache(ca);
485 /* insert found entry into buffer to be flushed later */
486 /* NOTE: we'll fill generations number later, because we
487 * it's stored in inode, thus we need to lookup an entry,
488 * but directory is locked for readdir(), so we delay this */
492 de->namelen = namlen;
493 memcpy(de->name, name, namlen);
494 ca->cur += DIR_REC_LEN(namlen);
495 ca->free -= DIR_REC_LEN(namlen);
501 int scan_and_distribute(struct obd_device *obd, struct dentry *dentry,
504 struct inode *dir = dentry->d_inode;
505 struct dirsplit_control dc;
510 nlen = strlen("__iopen__/") + 10 + 1;
511 OBD_ALLOC(file_name, nlen);
515 i = sprintf(file_name, "__iopen__/0x%lx",
516 dentry->d_inode->i_ino);
518 file = filp_open(file_name, O_RDONLY, 0);
520 CERROR("can't open directory %s: %d\n",
521 file_name, (int) PTR_ERR(file));
522 OBD_FREE(file_name, nlen);
523 RETURN(PTR_ERR(file));
526 memset(&dc, 0, sizeof(dc));
531 OBD_ALLOC(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
532 LASSERT(dc.cache != NULL);
533 for (i = 0; i < mea->mea_count; i++) {
534 INIT_LIST_HEAD(&dc.cache[i].list);
535 dc.cache[i].free = 0;
536 dc.cache[i].cached = 0;
539 err = vfs_readdir(file, filldir, &dc);
544 for (i = 0; i < mea->mea_count; i++) {
545 if (!dc.cache[i].cached)
547 err = flush_buffer_onto_mds(&dc, i);
552 for (i = 0; i < mea->mea_count; i++) {
553 if (!dc.cache[i].cached)
555 err = remove_entries_from_orig_dir(&dc, i);
562 for (i = 0; i < mea->mea_count; i++) {
563 struct list_head *cur, *tmp;
564 if (!dc.cache[i].cached)
566 list_for_each_safe(cur, tmp, &dc.cache[i].list) {
568 page = list_entry(cur, struct page, lru);
569 list_del(&page->lru);
573 OBD_FREE(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
574 OBD_FREE(file_name, nlen);
579 #define MAX_DIR_SIZE (64 * 1024)
580 #define I_NON_SPLITTABLE (256)
582 int mds_splitting_expected(struct obd_device *obd, struct dentry *dentry)
584 struct mds_obd *mds = &obd->u.mds;
585 struct mea *mea = NULL;
589 if (!mds->mds_md_obd)
590 return MDS_NO_SPLITTABLE;
593 if (dentry->d_inode == NULL)
594 return MDS_NO_SPLITTABLE;
596 /* a dir can be splitted only */
597 if (!S_ISDIR(dentry->d_inode->i_mode))
598 return MDS_NO_SPLITTABLE;
600 /* already splittied or slave directory (part of splitted dir) */
601 if (dentry->d_inode->i_flags & I_NON_SPLITTABLE)
602 return MDS_NO_SPLITTABLE;
604 /* don't split root directory */
605 if (dentry->d_inode->i_ino == id_ino(&mds->mds_rootid))
606 return MDS_NO_SPLITTABLE;
608 /* large enough to be splitted? */
609 if (dentry->d_inode->i_size < MAX_DIR_SIZE)
610 return MDS_NO_SPLIT_EXPECTED;
612 mds_md_get_attr(obd, dentry->d_inode, &mea, &size);
614 /* already splitted or slave object: shouldn't be splitted */
615 rc = MDS_NO_SPLITTABLE;
616 /* mark to skip subsequent checks */
617 dentry->d_inode->i_flags |= I_NON_SPLITTABLE;
620 /* may be splitted */
621 rc = MDS_EXPECT_SPLIT;
628 * must not be called on already splitted directories.
630 int mds_try_to_split_dir(struct obd_device *obd, struct dentry *dentry,
631 struct mea **mea, int nstripes, int update_mode)
633 struct inode *dir = dentry->d_inode;
634 struct mds_obd *mds = &obd->u.mds;
635 struct mea *tmea = NULL;
636 struct obdo *oa = NULL;
637 int rc, mea_size = 0;
642 if (update_mode != LCK_EX)
645 /* TODO: optimization possible - we already may have mea here */
646 rc = mds_splitting_expected(obd, dentry);
647 if (rc == MDS_NO_SPLITTABLE)
649 if (rc == MDS_NO_SPLIT_EXPECTED && nstripes == 0)
651 if (nstripes && nstripes == 1)
654 LASSERT(mea == NULL || *mea == NULL);
656 CDEBUG(D_OTHER, "%s: split directory %u/%lu/%lu\n",
657 obd->obd_name, mds->mds_num, dir->i_ino,
658 (unsigned long) dir->i_generation);
662 mea_size = obd_size_diskmd(mds->mds_md_exp, NULL);
664 /* FIXME: Actually we may only want to allocate enough space for
665 * necessary amount of stripes, but on the other hand with this
666 * approach of allocating maximal possible amount of MDS slots,
667 * it would be easier to split the dir over more MDSes */
668 rc = obd_alloc_diskmd(mds->mds_md_exp, (void *)mea);
670 CERROR("obd_alloc_diskmd() failed, error %d.\n", rc);
676 (*mea)->mea_count = nstripes;
678 /* 1) create directory objects on slave MDS'es */
679 /* FIXME: should this be OBD method? */
684 oa->o_generation = dir->i_generation;
686 obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
687 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
688 OBD_MD_FLUID | OBD_MD_FLGID);
690 oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
691 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
692 oa->o_mode = dir->i_mode;
695 * until lmv_obd_create() properly rewritten, it is important to have
696 * here oa->o_id = dir->i_ino, as otherwise master object will have
697 * invalid store cookie (zero inode num), what will lead to -ESTALE in
698 * mds_open() or somewhere else.
700 oa->o_id = dir->i_ino;
703 rc = mds_read_inode_sid(obd, dir, &id);
706 CERROR("Can't read inode self id, inode %lu, "
707 "rc %d.\n", dir->i_ino, rc);
710 oa->o_fid = id_fid(&id);
711 oa->o_mds = mds->mds_num;
712 LASSERT(oa->o_fid != 0);
714 CDEBUG(D_OTHER, "%s: create subdirs with mode %o, uid %u, gid %u\n",
715 obd->obd_name, dir->i_mode, dir->i_uid, dir->i_gid);
717 rc = obd_create(mds->mds_md_exp, oa, NULL, 0,
718 (struct lov_stripe_md **)mea, NULL);
720 CERROR("Can't create remote inode, rc = %d\n", rc);
724 LASSERT(id_fid(&(*mea)->mea_ids[0]));
725 CDEBUG(D_OTHER, "%d dirobjects created\n", (int)(*mea)->mea_count);
727 /* 2) update dir attribute */
730 handle = fsfilt_start(obd, dir, FSFILT_OP_SETATTR, NULL);
731 if (IS_ERR(handle)) {
733 CERROR("fsfilt_start() failed: %d\n", (int) PTR_ERR(handle));
734 GOTO(err_oa, rc = PTR_ERR(handle));
737 rc = fsfilt_set_md(obd, dir, handle, *mea, mea_size, EA_MEA);
740 CERROR("fsfilt_set_md() failed, error %d.\n", rc);
744 rc = fsfilt_commit(obd, mds->mds_sb, dir, handle, 0);
747 CERROR("fsfilt_commit() failed, error %d.\n", rc);
754 /* 3) read through the dir and distribute it over objects */
755 rc = scan_and_distribute(obd, dentry, *mea);
757 obd_free_diskmd(mds->mds_md_exp, (struct lov_mds_md **)mea);
759 CERROR("scan_and_distribute() failed, error %d.\n", rc);
770 static int filter_start_page_write(struct inode *inode,
771 struct niobuf_local *lnb)
773 struct page *page = alloc_pages(GFP_HIGHUSER, 0);
775 CERROR("no memory for a temp page\n");
776 return lnb->rc = -ENOMEM;
778 POISON_PAGE(page, 0xf1);
779 page->index = lnb->offset >> PAGE_SHIFT;
784 struct dentry *filter_id2dentry(struct obd_device *obd,
785 struct dentry *dir_dentry,
786 obd_gr group, obd_id id);
788 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
789 int objcount, struct obd_ioobj *obj,
790 int niocount, struct niobuf_remote *nb,
791 struct niobuf_local *res,
792 struct obd_trans_info *oti)
794 struct niobuf_remote *rnb;
795 struct niobuf_local *lnb = NULL;
796 int rc = 0, i, tot_bytes = 0;
797 unsigned long now = jiffies;
798 struct dentry *dentry;
802 LASSERT(objcount == 1);
803 LASSERT(obj->ioo_bufcnt > 0);
805 memset(res, 0, niocount * sizeof(*res));
809 id_ino(&id) = obj->ioo_id;
810 id_gen(&id) = obj->ioo_gr;
812 dentry = mds_id2dentry(exp->exp_obd, &id, NULL);
813 if (IS_ERR(dentry)) {
814 CERROR("can't get dentry for "LPU64"/%u: %d\n",
815 id.li_stc.u.e3s.l3s_ino, id.li_stc.u.e3s.l3s_gen,
816 (int)PTR_ERR(dentry));
817 GOTO(cleanup, rc = (int) PTR_ERR(dentry));
820 if (dentry->d_inode == NULL) {
821 CERROR("trying to BRW to non-existent file "LPU64"\n",
824 GOTO(cleanup, rc = -ENOENT);
827 if (time_after(jiffies, now + 15 * HZ))
828 CERROR("slow preprw_write setup %lus\n", (jiffies - now) / HZ);
830 CDEBUG(D_INFO, "preprw_write setup: %lu jiffies\n",
833 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
835 lnb->dentry = dentry;
836 lnb->offset = rnb->offset;
838 lnb->flags = rnb->flags;
840 rc = filter_start_page_write(dentry->d_inode, lnb);
842 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR, "page err %u@"
843 LPU64" %u/%u %p: rc %d\n", lnb->len, lnb->offset,
844 i, obj->ioo_bufcnt, dentry, rc);
846 __free_pages(lnb->page, 0);
850 tot_bytes += lnb->len;
853 if (time_after(jiffies, now + 15 * HZ))
854 CERROR("slow start_page_write %lus\n", (jiffies - now) / HZ);
856 CDEBUG(D_INFO, "start_page_write: %lu jiffies\n",
864 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
865 int objcount, struct obd_ioobj *obj, int niocount,
866 struct niobuf_local *res, struct obd_trans_info *oti,
869 struct obd_device *obd = exp->exp_obd;
870 struct niobuf_local *lnb;
871 struct inode *inode = NULL;
872 int rc = 0, i, cleanup_phase = 0, err, entries = 0;
875 LASSERT(objcount == 1);
876 LASSERT(current->journal_info == NULL);
879 inode = res->dentry->d_inode;
881 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
883 struct dir_entry *de;
885 buf = kmap(lnb->page);
886 LASSERT(buf != NULL);
887 end = buf + lnb->len;
888 de = (struct dir_entry *) buf;
889 while ((char *) de < end && de->namelen) {
890 err = fsfilt_add_dir_entry(obd, res->dentry, de->name,
891 de->namelen, de->ino,
892 de->generation, de->mds,
895 CERROR("can't add dir entry %*s->%u/%u/%u"
896 " to %lu/%u: %d\n", de->namelen,
897 de->name, de->mds, (unsigned)de->ino,
898 (unsigned) de->generation,
899 res->dentry->d_inode->i_ino,
900 res->dentry->d_inode->i_generation,
906 de = (struct dir_entry *)
907 ((char *) de + DIR_REC_LEN(de->namelen));
913 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++)
914 __free_page(lnb->page);
920 int mds_choose_mdsnum(struct obd_device *obd, const char *name, int len, int flags,
921 struct ptlrpc_peer *peer, struct inode *parent)
923 struct mds_obd *mds = &obd->u.mds;
925 int i = mds->mds_num;
926 char peer_str[PTL_NALFMT_SIZE];
927 if (flags & REC_REINT_CREATE) {
929 } else if (mds->mds_md_exp != NULL && peer != NULL) {
930 LASSERT(parent != NULL);
931 /* distribute only at root level */
932 lmv = &mds->mds_md_exp->exp_obd->u.lmv;
933 if (parent->i_ino != id_ino(&mds->mds_rootid)) {
936 __u64 nid = peer->peer_id.nid;
937 __u64 count = lmv->desc.ld_tgt_count;
938 i = do_div(nid, count);
939 CWARN("client from %s creates top dir %*s on mds #%d\n",
940 ptlrpc_peernid2str(peer,peer_str), len, name, i+1);
942 } else if (mds->mds_md_exp) {
943 lmv = &mds->mds_md_exp->exp_obd->u.lmv;
944 i = raw_name2idx(MEA_MAGIC_LAST_CHAR, lmv->desc.ld_tgt_count, name, len);
949 int mds_lock_slave_objs(struct obd_device *obd, struct dentry *dentry,
950 struct lustre_handle **rlockh)
952 struct mds_obd *mds = &obd->u.mds;
953 struct mdc_op_data *op_data;
954 struct lookup_intent it;
955 struct mea *mea = NULL;
960 LASSERT(rlockh != NULL);
961 LASSERT(dentry != NULL);
962 LASSERT(dentry->d_inode != NULL);
965 if (!mds->mds_md_obd)
968 /* a dir can be splitted only */
969 if (!S_ISDIR(dentry->d_inode->i_mode))
972 rc = mds_md_get_attr(obd, dentry->d_inode,
980 if (mea->mea_count == 0)
981 /* this is slave object */
982 GOTO(cleanup, rc = 0);
984 CDEBUG(D_OTHER, "%s: lock slaves for %lu/%lu\n",
985 obd->obd_name, (unsigned long)dentry->d_inode->i_ino,
986 (unsigned long)dentry->d_inode->i_generation);
988 handle_size = sizeof(struct lustre_handle) *
991 OBD_ALLOC(*rlockh, handle_size);
993 GOTO(cleanup, rc = -ENOMEM);
995 memset(*rlockh, 0, handle_size);
996 OBD_ALLOC(op_data, sizeof(*op_data));
997 if (op_data == NULL) {
998 OBD_FREE(*rlockh, handle_size);
1001 memset(op_data, 0, sizeof(*op_data));
1003 op_data->mea1 = mea;
1004 it.it_op = IT_UNLINK;
1006 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
1008 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
1009 op_data, *rlockh, NULL, 0, ldlm_completion_ast,
1010 mds_blocking_ast, NULL);
1011 OBD_FREE(op_data, sizeof(*op_data));
1012 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
1015 OBD_FREE(mea, mea_size);
1019 void mds_unlock_slave_objs(struct obd_device *obd, struct dentry *dentry,
1020 struct lustre_handle *lockh)
1022 struct mds_obd *mds = &obd->u.mds;
1023 struct mea *mea = NULL;
1024 int mea_size, rc, i;
1027 if (lockh == NULL) {
1032 LASSERT(mds->mds_md_obd != NULL);
1033 LASSERT(S_ISDIR(dentry->d_inode->i_mode));
1035 rc = mds_md_get_attr(obd, dentry->d_inode, &mea, &mea_size);
1037 CERROR("locks are leaked\n");
1041 LASSERT(mea_size != 0);
1042 LASSERT(mea != NULL);
1043 LASSERT(mea->mea_count != 0);
1045 CDEBUG(D_OTHER, "%s: unlock slaves for %lu/%lu\n", obd->obd_name,
1046 (unsigned long) dentry->d_inode->i_ino,
1047 (unsigned long) dentry->d_inode->i_generation);
1049 for (i = 0; i < mea->mea_count; i++) {
1050 if (lockh[i].cookie != 0)
1051 ldlm_lock_decref(lockh + i, LCK_EX);
1054 OBD_FREE(lockh, sizeof(struct lustre_handle) * mea->mea_count);
1055 OBD_FREE(mea, mea_size);
1059 int mds_unlink_slave_objs(struct obd_device *obd, struct dentry *dentry)
1061 struct mds_obd *mds = &obd->u.mds;
1062 struct ptlrpc_request *req = NULL;
1063 struct mdc_op_data *op_data;
1064 struct mea *mea = NULL;
1068 /* clustered MD ? */
1069 if (!mds->mds_md_obd)
1072 /* a dir can be splitted only */
1073 if (!S_ISDIR(dentry->d_inode->i_mode))
1076 rc = mds_md_get_attr(obd, dentry->d_inode, &mea, &mea_size);
1083 if (mea->mea_count == 0)
1084 GOTO(cleanup, rc = 0);
1086 CDEBUG(D_OTHER, "%s: unlink slaves for %lu/%lu\n", obd->obd_name,
1087 (unsigned long)dentry->d_inode->i_ino,
1088 (unsigned long)dentry->d_inode->i_generation);
1090 OBD_ALLOC(op_data, sizeof(*op_data));
1091 if (op_data == NULL)
1094 memset(op_data, 0, sizeof(*op_data));
1095 op_data->mea1 = mea;
1096 rc = md_unlink(mds->mds_md_exp, op_data, &req);
1097 OBD_FREE(op_data, sizeof(*op_data));
1098 LASSERT(req == NULL);
1101 OBD_FREE(mea, mea_size);
1105 struct ide_tracking {
1110 int mds_ide_filldir(void *__buf, const char *name, int namelen,
1111 loff_t offset, ino_t ino, unsigned int d_type)
1113 struct ide_tracking *it = __buf;
1119 if (it->entries > 2)
1123 if (name[0] == '.' && namelen == 1)
1125 if (name[0] == '.' && name[1] == '.' && namelen == 2)
1132 /* checks if passed dentry points to empty dir. */
1133 int mds_is_dir_empty(struct obd_device *obd, struct dentry *dentry)
1135 struct ide_tracking it;
1143 nlen = strlen("__iopen__/") + 10 + 1;
1144 OBD_ALLOC(file_name, nlen);
1148 LASSERT(dentry->d_inode != NULL);
1149 i = sprintf(file_name, "__iopen__/0x%lx",
1150 dentry->d_inode->i_ino);
1152 file = filp_open(file_name, O_RDONLY, 0);
1154 CERROR("can't open directory %s: %d\n",
1155 file_name, (int) PTR_ERR(file));
1156 GOTO(cleanup, rc = PTR_ERR(file));
1159 rc = vfs_readdir(file, mds_ide_filldir, &it);
1160 filp_close(file, 0);
1162 if (it.empty && rc == 0)
1168 OBD_FREE(file_name, nlen);
1172 int mds_lock_and_check_slave(int offset, struct ptlrpc_request *req,
1173 struct lustre_handle *lockh)
1175 struct obd_device *obd = req->rq_export->exp_obd;
1176 struct dentry *dentry = NULL;
1177 struct lvfs_run_ctxt saved;
1178 int cleanup_phase = 0;
1179 struct mds_req_sec_desc *rsd;
1180 struct mds_body *body;
1181 struct lvfs_ucred uc;
1182 int rc, update_mode;
1185 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1187 CERROR("Can't unpack security desc\n");
1188 GOTO(cleanup, rc = -EFAULT);
1191 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1192 lustre_swab_mds_body);
1194 CERROR("Can't swab mds_body\n");
1195 GOTO(cleanup, rc = -EFAULT);
1197 CDEBUG(D_OTHER, "%s: check slave "DLID4"\n", obd->obd_name,
1200 dentry = mds_id2locked_dentry(obd, &body->id1, NULL, LCK_EX,
1201 lockh, &update_mode, NULL, 0,
1202 MDS_INODELOCK_UPDATE);
1203 if (IS_ERR(dentry)) {
1204 CERROR("can't find inode: %d\n", (int) PTR_ERR(dentry));
1205 GOTO(cleanup, rc = PTR_ERR(dentry));
1210 * handling the case when remote MDS checks if dir is empty
1211 * before rename. But it also does it for all entries, because
1212 * inode is stored here and remote MDS does not know if rename
1213 * point to dir or to reg file. So we check it here.
1215 if (!S_ISDIR(dentry->d_inode->i_mode))
1216 GOTO(cleanup, rc = 0);
1218 rc = mds_init_ucred(&uc, req, rsd);
1223 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1224 rc = mds_is_dir_empty(obd, dentry) ? 0 : -ENOTEMPTY;
1225 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1227 mds_exit_ucred(&uc);
1230 switch(cleanup_phase) {
1233 ldlm_lock_decref(lockh, LCK_EX);
1241 int mds_convert_mea_ea(struct obd_device *obd, struct inode *inode,
1242 struct lov_mds_md *lmm, int lmm_size)
1244 struct lov_stripe_md *lsm = NULL;
1245 struct mea_old *old;
1251 mea = (struct mea *)lmm;
1252 old = (struct mea_old *)lmm;
1254 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
1255 mea->mea_magic == MEA_MAGIC_ALL_CHARS)
1259 * making MDS try LOV EA converting in the non-LMV configuration
1262 if (!obd->u.mds.mds_md_exp)
1265 CDEBUG(D_INODE, "converting MEA EA on %lu/%u from V0 to V1 (%u/%u)\n",
1266 inode->i_ino, inode->i_generation, old->mea_count,
1269 rc = obd_unpackmd(obd->u.mds.mds_md_exp, &lsm, lmm, lmm_size);
1273 rc = obd_packmd(obd->u.mds.mds_md_exp, &lmm, lsm);
1275 GOTO(conv_free, rc);
1278 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1279 if (IS_ERR(handle)) {
1280 rc = PTR_ERR(handle);
1281 GOTO(conv_free, rc);
1284 rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size, EA_MEA);
1285 err = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 0);
1287 rc = err ? err : lmm_size;
1288 GOTO(conv_free, rc);
1290 obd_free_memmd(obd->u.mds.mds_md_exp, &lsm);