Whamcloud - gitweb
b=6358
[fs/lustre-release.git] / lustre / mds / mds_lmv.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/mds/mds_lov.c
5  *  Lustre Metadata Server (mds) handling of striped file data
6  *
7  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_MDS
29
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>
45
46 #include "mds_internal.h"
47
48 /*
49  * TODO:
50  *   - magic in mea struct
51  */
52 int mds_md_connect(struct obd_device *obd, char *md_name)
53 {
54         struct mds_obd *mds = &obd->u.mds;
55         struct lustre_handle conn = {0};
56         int rc, valsize, value;
57         ENTRY;
58
59         if (IS_ERR(mds->mds_md_obd))
60                 RETURN(PTR_ERR(mds->mds_md_obd));
61
62         if (mds->mds_md_connected)
63                 RETURN(0);
64
65         down(&mds->mds_md_sem);
66         if (mds->mds_md_connected) {
67                 up(&mds->mds_md_sem);
68                 RETURN(0);
69         }
70
71         mds->mds_md_obd = class_name2obd(md_name);
72         if (!mds->mds_md_obd) {
73                 CERROR("MDS cannot locate MD(LMV) %s\n",
74                        md_name);
75                 mds->mds_md_obd = ERR_PTR(-ENOTCONN);
76                 GOTO(err_last, rc = -ENOTCONN);
77         }
78
79         rc = obd_connect(&conn, mds->mds_md_obd, &obd->obd_uuid, NULL,
80                          OBD_OPT_MDS_CONNECTION);
81         if (rc) {
82                 CERROR("MDS cannot connect to MD(LMV) %s (%d)\n",
83                        md_name, rc);
84                 mds->mds_md_obd = ERR_PTR(rc);
85                 GOTO(err_last, rc);
86         }
87         mds->mds_md_exp = class_conn2export(&conn);
88         if (mds->mds_md_exp == NULL)
89                 CERROR("can't get export!\n");
90
91         rc = obd_register_observer(mds->mds_md_obd, obd);
92         if (rc) {
93                 CERROR("MDS cannot register as observer of MD(LMV) %s, "
94                        "rc = %d\n", md_name, rc);
95                 GOTO(err_discon, rc);
96         }
97
98         /* retrieve size of EA */
99         rc = obd_get_info(mds->mds_md_exp, strlen("mdsize"),
100                           "mdsize", &valsize, &value);
101         if (rc) 
102                 GOTO(err_reg, rc);
103
104         if (value > mds->mds_max_mdsize)
105                 mds->mds_max_mdsize = value;
106
107         /* find our number in LMV cluster */
108         rc = obd_get_info(mds->mds_md_exp, strlen("mdsnum"),
109                           "mdsnum", &valsize, &value);
110         if (rc) 
111                 GOTO(err_reg, rc);
112         
113         mds->mds_num = value;
114
115         rc = obd_set_info(mds->mds_md_exp, strlen("inter_mds"),
116                           "inter_mds", 0, NULL);
117         if (rc)
118                 GOTO(err_reg, rc);
119
120         if (mds->mds_mds_sec) {
121                 rc = obd_set_info(mds->mds_md_exp, strlen("sec"), "sec",
122                                   strlen(mds->mds_mds_sec), mds->mds_mds_sec);
123                 if (rc)
124                         GOTO(err_reg, rc);
125         }
126
127         mds->mds_md_connected = 1;
128         up(&mds->mds_md_sem);
129         RETURN(0);
130
131 err_reg:
132         obd_register_observer(mds->mds_md_obd, NULL);
133 err_discon:
134         obd_disconnect(mds->mds_md_exp, 0);
135         mds->mds_md_exp = NULL;
136         mds->mds_md_obd = ERR_PTR(rc);
137 err_last:
138         up(&mds->mds_md_sem);
139         return rc;
140 }
141
142 int mds_md_postsetup(struct obd_device *obd)
143 {
144         struct mds_obd *mds = &obd->u.mds;
145         int rc = 0;
146         ENTRY;
147
148         if (mds->mds_md_exp) {
149                 rc = obd_init_ea_size(mds->mds_md_exp,
150                                       mds->mds_max_mdsize,
151                                       mds->mds_max_cookiesize);
152         }
153         
154         RETURN(rc);
155 }
156
157 int mds_md_disconnect(struct obd_device *obd, int flags)
158 {
159         struct mds_obd *mds = &obd->u.mds;
160         int rc = 0;
161         ENTRY;
162
163         if (!mds->mds_md_connected)
164                 RETURN(0);
165
166         down(&mds->mds_md_sem);
167         if (!IS_ERR(mds->mds_md_obd) && mds->mds_md_exp != NULL) {
168                 LASSERT(mds->mds_md_connected);
169                 
170                 obd_register_observer(mds->mds_md_obd, NULL);
171
172                 if (flags & OBD_OPT_FORCE) {
173                         struct obd_device *lmv_obd;
174                         struct obd_ioctl_data ioc_data = { 0 };
175                         
176                         lmv_obd = class_exp2obd(mds->mds_md_exp);
177                         if (lmv_obd == NULL)
178                                 GOTO(out, rc = 0);
179
180                         /* 
181                          * making disconnecting lmv stuff do not send anything
182                          * to all remote MDSs from LMV. This is needed to
183                          * prevent possible hanging with endless recovery, when
184                          * MDS sends disconnect to already disconnected
185                          * target. Probably this is wrong, but client does the
186                          * same in --force mode and I do not see why can't we do
187                          * it here. --umka.
188                          */
189                         lmv_obd->obd_no_recov = 1;
190                         obd_iocontrol(IOC_OSC_SET_ACTIVE, mds->mds_md_exp,
191                                       sizeof(ioc_data), &ioc_data, NULL);
192                 }
193
194                 /*
195                  * if obd_disconnect() fails (probably because the export was
196                  * disconnected by class_disconnect_exports()) then we just need
197                  * to drop our ref.
198                  */
199                 mds->mds_md_connected = 0;
200                 rc = obd_disconnect(mds->mds_md_exp, flags);
201                 if (rc)
202                         class_export_put(mds->mds_md_exp);
203
204         out:
205                 mds->mds_md_exp = NULL;
206                 mds->mds_md_obd = NULL;
207         }
208         up(&mds->mds_md_sem);
209         RETURN(rc);
210 }
211
212 int mds_md_reconnect(struct obd_device *obd)
213 {
214         struct mds_obd *mds = &obd->u.mds;
215         struct obd_statfs osfs;
216         int err;
217         ENTRY;
218
219         /* We don't know state of connections to another MDSes
220          * before the failure. If MDS we were connected to before
221          * the failure gets failed, then it will wait for us to
222          * reconnect and will timed recovery out. bug 4920 */
223         if (mds->mds_md_connected == 0)
224                 RETURN(0);
225         if (mds->mds_md_obd == NULL)
226                 RETURN(0);
227
228         err = obd_statfs(mds->mds_md_obd, &osfs, jiffies - HZ);
229         if (err)
230                 CERROR("can't reconnect to MDSes after recovery: %d\n", err);
231         RETURN(0);
232 }
233
234 int mds_md_get_attr(struct obd_device *obd, struct inode *inode,
235                     struct mea **mea, int *mea_size)
236 {
237         struct mds_obd *mds = &obd->u.mds;
238         int rc;
239         ENTRY;
240
241         if (!mds->mds_md_obd)
242                 RETURN(0);
243         if (!S_ISDIR(inode->i_mode))
244                 RETURN(0);
245
246         /* first calculate mea size */
247         *mea_size = obd_alloc_diskmd(mds->mds_md_exp,
248                                      (struct lov_mds_md **)mea);
249         if (*mea_size < 0 || *mea == NULL)
250                 return *mea_size < 0 ? *mea_size : -EINVAL;
251
252         rc = mds_get_md(obd, inode, *mea, mea_size, 1, 1);
253
254         if (rc <= 0) {
255                 OBD_FREE(*mea, *mea_size);
256                 *mea = NULL;
257         } else
258                 rc = 0;
259
260         RETURN(rc);
261 }
262
263 struct dir_entry {
264         __u16   namelen;
265         __u16   mds;
266         __u32   ino;
267         __u32   generation;
268         __u32   fid;
269         char    name[0];
270 };
271
272 #define DIR_PAD                 4
273 #define DIR_ROUND               (DIR_PAD - 1)
274 #define DIR_REC_LEN(name_len)   (((name_len) + 16 + DIR_ROUND) & ~DIR_ROUND)
275
276 /* this struct holds dir entries for particular MDS to be flushed */
277 struct dir_cache {
278         struct list_head list;
279         void *cur;
280         int free;
281         int cached;
282         struct obdo oa;
283         struct brw_page brwc;
284 };
285
286 struct dirsplit_control {
287         struct obd_device *obd;
288         struct inode *dir;
289         struct dentry *dentry;
290         struct mea *mea;
291         struct dir_cache *cache;
292 };
293
294 static int dc_new_page_to_cache(struct dir_cache * dirc)
295 {
296         struct page *page;
297
298         if (!list_empty(&dirc->list) && dirc->free > sizeof(__u16)) {
299                 /* current page became full, mark the end */
300                 struct dir_entry *de = dirc->cur;
301                 de->namelen = 0;
302         }
303
304         page = alloc_page(GFP_KERNEL);
305         if (page == NULL)
306                 return -ENOMEM;
307         list_add_tail(&page->lru, &dirc->list);
308         dirc->cur = page_address(page);
309         dirc->free = PAGE_SIZE;
310         return 0;
311 }
312
313 static int retrieve_generation_numbers(struct dirsplit_control *dc, void *buf)
314 {
315         struct mds_obd *mds = &dc->obd->u.mds;
316         struct dir_entry *de;
317         struct dentry *dentry;
318         char *end;
319         
320         end = buf + PAGE_SIZE;
321         de = (struct dir_entry *) buf;
322         while ((char *) de < end && de->namelen) {
323                 /* lookup an inode */
324                 LASSERT(de->namelen <= 255);
325                 dentry = ll_lookup_one_len(de->name, dc->dentry, 
326                                            de->namelen);
327                 if (IS_ERR(dentry)) {
328                         CERROR("can't lookup %*s: %d\n", de->namelen,
329                                de->name, (int) PTR_ERR(dentry));
330                         goto next;
331                 }
332                 if (dentry->d_inode != NULL) {
333                         int rc;
334                         struct lustre_id sid;
335
336                         down(&dentry->d_inode->i_sem);
337                         rc = mds_read_inode_sid(dc->obd,
338                                                 dentry->d_inode, &sid);
339                         up(&dentry->d_inode->i_sem);
340                         if (rc) {
341                                 CERROR("Can't read inode self id, "
342                                        "inode %lu, rc %d\n",
343                                        dentry->d_inode->i_ino, rc);
344                                 goto next;
345                         }
346
347                         de->fid = id_fid(&sid);
348                         de->mds = mds->mds_num;
349                         de->ino = dentry->d_inode->i_ino;
350                         de->generation = dentry->d_inode->i_generation;
351                 } else if (dentry->d_flags & DCACHE_CROSS_REF) {
352                         de->fid = dentry->d_fid;
353                         de->ino = dentry->d_inum;
354                         de->mds = dentry->d_mdsnum;
355                         de->generation = dentry->d_generation;
356                 } else {
357                         CERROR("can't lookup %*s\n", de->namelen, de->name);
358                         goto next;
359                 }
360                 l_dput(dentry);
361
362 next:
363                 de = (struct dir_entry *)
364                         ((char *) de + DIR_REC_LEN(de->namelen));
365         }
366         return 0;
367 }
368
369 static int flush_buffer_onto_mds(struct dirsplit_control *dc, int mdsnum)
370 {
371         struct mds_obd *mds = &dc->obd->u.mds;
372         struct list_head *cur, *tmp;
373         struct dir_cache *ca;
374         int rc;
375         ENTRY; 
376         ca = dc->cache + mdsnum;
377
378         if (ca->free > sizeof(__u16)) {
379                 /* current page became full, mark the end */
380                 struct dir_entry *de = ca->cur;
381                 de->namelen = 0;
382         }
383
384         list_for_each_safe(cur, tmp, &ca->list) {
385                 struct page *page;
386
387                 page = list_entry(cur, struct page, lru);
388                 LASSERT(page != NULL);
389
390                 retrieve_generation_numbers(dc, page_address(page));
391
392                 ca->brwc.pg = page;
393                 ca->brwc.disk_offset = ca->brwc.page_offset = 0;
394                 ca->brwc.count = PAGE_SIZE;
395                 ca->brwc.flag = 0;
396                 ca->oa.o_mds = mdsnum;
397                 rc = obd_brw(OBD_BRW_WRITE, mds->mds_md_exp, &ca->oa,
398                              (struct lov_stripe_md *) dc->mea,
399                              1, &ca->brwc, NULL);
400                 if (rc)
401                         RETURN(rc);
402
403         }
404         RETURN(0);
405 }
406
407 static int remove_entries_from_orig_dir(struct dirsplit_control *dc, int mdsnum)
408 {
409         struct list_head *cur, *tmp;
410         struct dentry *dentry;
411         struct dir_cache *ca;
412         struct dir_entry *de;
413         struct page *page;
414         char *buf, *end;
415         int rc;
416         ENTRY; 
417
418         ca = dc->cache + mdsnum;
419         list_for_each_safe(cur, tmp, &ca->list) {
420                 page = list_entry(cur, struct page, lru);
421                 buf = page_address(page);
422                 end = buf + PAGE_SIZE;
423
424                 de = (struct dir_entry *) buf;
425                 while ((char *) de < end && de->namelen) {
426                         /* lookup an inode */
427                         LASSERT(de->namelen <= 255);
428
429                         dentry = ll_lookup_one_len(de->name, dc->dentry, 
430                                                    de->namelen);
431                         if (IS_ERR(dentry)) {
432                                 CERROR("can't lookup %*s: %d\n", de->namelen,
433                                        de->name, (int) PTR_ERR(dentry));
434                                 goto next;
435                         }
436                         rc = fsfilt_del_dir_entry(dc->obd, dentry);
437                         l_dput(dentry);
438 next:
439                         de = (struct dir_entry *)
440                                 ((char *) de + DIR_REC_LEN(de->namelen));
441                 }
442         }
443         RETURN(0);
444 }
445
446 static int filldir(void * __buf, const char * name, int namlen,
447                    loff_t offset, ino_t ino, unsigned int d_type)
448 {
449         struct dirsplit_control *dc = __buf;
450         struct mds_obd *mds = &dc->obd->u.mds;
451         struct dir_cache *ca;
452         struct dir_entry *de;
453         int newmds;
454         char *n;
455         ENTRY;
456
457         if (name[0] == '.' &&
458             (namlen == 1 || (namlen == 2 && name[1] == '.'))) {
459                 /* skip special entries */
460                 RETURN(0);
461         }
462
463         LASSERT(dc != NULL);
464         newmds = mea_name2idx(dc->mea, (char *) name, namlen);
465
466         if (newmds == mds->mds_num) {
467                 /* this entry remains on the current MDS, skip moving */
468                 RETURN(0);
469         }
470         
471         OBD_ALLOC(n, namlen + 1);
472         memcpy(n, name, namlen);
473         n[namlen] = (char) 0;
474         
475         OBD_FREE(n, namlen + 1);
476
477         /* check for space in buffer for new entry */
478         ca = dc->cache + newmds;
479         if (DIR_REC_LEN(namlen) > ca->free) {
480                 int err = dc_new_page_to_cache(ca);
481                 LASSERT(err == 0);
482         }
483         
484         /* insert found entry into buffer to be flushed later */
485         /* NOTE: we'll fill generations number later, because we
486          * it's stored in inode, thus we need to lookup an entry,
487          * but directory is locked for readdir(), so we delay this */
488         de = ca->cur;
489         de->ino = ino;
490         de->mds = d_type;
491         de->namelen = namlen;
492         memcpy(de->name, name, namlen);
493         ca->cur += DIR_REC_LEN(namlen);
494         ca->free -= DIR_REC_LEN(namlen);
495         ca->cached++;
496
497         RETURN(0);
498 }
499
500 int scan_and_distribute(struct obd_device *obd, struct dentry *dentry,
501                         struct mea *mea)
502 {
503         struct inode *dir = dentry->d_inode;
504         struct dirsplit_control dc;
505         struct file * file;
506         int err, i, nlen;
507         char *file_name;
508
509         nlen = strlen("__iopen__/") + 10 + 1;
510         OBD_ALLOC(file_name, nlen);
511         if (!file_name)
512                 RETURN(-ENOMEM);
513         
514         i = sprintf(file_name, "__iopen__/0x%lx",
515                     dentry->d_inode->i_ino);
516
517         file = filp_open(file_name, O_RDONLY, 0);
518         if (IS_ERR(file)) {
519                 CERROR("can't open directory %s: %d\n",
520                                 file_name, (int) PTR_ERR(file));
521                 OBD_FREE(file_name, nlen);
522                 RETURN(PTR_ERR(file));
523         }
524
525         memset(&dc, 0, sizeof(dc));
526         dc.obd = obd;
527         dc.dir = dir;
528         dc.dentry = dentry;
529         dc.mea = mea;
530         OBD_ALLOC(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
531         LASSERT(dc.cache != NULL);
532         for (i = 0; i < mea->mea_count; i++) {
533                 INIT_LIST_HEAD(&dc.cache[i].list);
534                 dc.cache[i].free = 0;
535                 dc.cache[i].cached = 0;
536         }
537
538         err = vfs_readdir(file, filldir, &dc);
539         filp_close(file, 0);
540         if (err)
541                 GOTO(cleanup, err);
542
543         for (i = 0; i < mea->mea_count; i++) {
544                 if (!dc.cache[i].cached)
545                         continue;
546                 err = flush_buffer_onto_mds(&dc, i);
547                 if (err)
548                         GOTO(cleanup, err);
549         }
550
551         for (i = 0; i < mea->mea_count; i++) {
552                 if (!dc.cache[i].cached)
553                         continue;
554                 err = remove_entries_from_orig_dir(&dc, i);
555                 if (err)
556                         GOTO(cleanup, err);
557         }
558
559         EXIT;
560 cleanup:
561         for (i = 0; i < mea->mea_count; i++) {
562                 struct list_head *cur, *tmp;
563                 if (!dc.cache[i].cached)
564                         continue;
565                 list_for_each_safe(cur, tmp, &dc.cache[i].list) {
566                         struct page *page;
567                         page = list_entry(cur, struct page, lru);
568                         list_del(&page->lru);
569                         __free_page(page);
570                 }
571         }
572         OBD_FREE(dc.cache, sizeof(struct dir_cache) * mea->mea_count);
573         OBD_FREE(file_name, nlen);
574
575         return err;
576 }
577
578 #define MAX_DIR_SIZE      (64 * 1024)
579 #define I_NON_SPLITTABLE  (256)
580
581 int mds_splitting_expected(struct obd_device *obd, struct dentry *dentry)
582 {
583         struct mds_obd *mds = &obd->u.mds;
584         struct mea *mea = NULL;
585         int rc, size;
586
587         /* clustered MD ? */
588         if (!mds->mds_md_obd)
589                 return MDS_NO_SPLITTABLE;
590
591         /* inode exist? */
592         if (dentry->d_inode == NULL)
593                 return MDS_NO_SPLITTABLE;
594
595         /* a dir can be splitted only */
596         if (!S_ISDIR(dentry->d_inode->i_mode))
597                 return MDS_NO_SPLITTABLE;
598
599         /* already splittied or slave directory (part of splitted dir) */
600         if (dentry->d_inode->i_flags & I_NON_SPLITTABLE)
601                 return MDS_NO_SPLITTABLE;
602
603         /* don't split root directory */
604         if (dentry->d_inode->i_ino == id_ino(&mds->mds_rootid))
605                 return MDS_NO_SPLITTABLE;
606
607         /* large enough to be splitted? */
608         if (dentry->d_inode->i_size < MAX_DIR_SIZE)
609                 return MDS_NO_SPLIT_EXPECTED;
610
611         mds_md_get_attr(obd, dentry->d_inode, &mea, &size);
612         if (mea) {
613                 /* already splitted or slave object: shouldn't be splitted */
614                 rc = MDS_NO_SPLITTABLE;
615                 /* mark to skip subsequent checks */
616                 dentry->d_inode->i_flags |= I_NON_SPLITTABLE;
617                 OBD_FREE(mea, size);
618         } else {
619                 /* may be splitted */
620                 rc = MDS_EXPECT_SPLIT;
621         }
622
623         return rc;
624 }
625
626 /*
627  * must not be called on already splitted directories.
628  */
629 int mds_try_to_split_dir(struct obd_device *obd, struct dentry *dentry,
630                          struct mea **mea, int nstripes, int update_mode)
631 {
632         struct inode *dir = dentry->d_inode;
633         struct mds_obd *mds = &obd->u.mds;
634         struct mea *tmea = NULL;
635         struct obdo *oa = NULL;
636         int rc, mea_size = 0;
637         struct lustre_id id;
638         void *handle;
639         ENTRY;
640
641         if (update_mode != LCK_EX)
642                 return 0;
643         
644         /* TODO: optimization possible - we already may have mea here */
645         rc = mds_splitting_expected(obd, dentry);
646         if (rc == MDS_NO_SPLITTABLE)
647                 return 0;
648         if (rc == MDS_NO_SPLIT_EXPECTED && nstripes == 0)
649                 return 0;
650         if (nstripes && nstripes == 1)
651                 return 0;
652         
653         LASSERT(mea == NULL || *mea == NULL);
654
655         CDEBUG(D_OTHER, "%s: split directory %u/%lu/%lu\n",
656                obd->obd_name, mds->mds_num, dir->i_ino,
657                (unsigned long) dir->i_generation);
658
659         if (mea == NULL)
660                 mea = &tmea;
661         mea_size = obd_size_diskmd(mds->mds_md_exp, NULL);
662
663         /* FIXME: Actually we may only want to allocate enough space for
664          * necessary amount of stripes, but on the other hand with this
665          * approach of allocating maximal possible amount of MDS slots,
666          * it would be easier to split the dir over more MDSes */
667         rc = obd_alloc_diskmd(mds->mds_md_exp, (void *)mea);
668         if (rc < 0) {
669                 CERROR("obd_alloc_diskmd() failed, error %d.\n", rc);
670                 RETURN(rc);
671         }
672         if (*mea == NULL)
673                 RETURN(-ENOMEM);
674
675         (*mea)->mea_count = nstripes;
676        
677         /* 1) create directory objects on slave MDS'es */
678         /* FIXME: should this be OBD method? */
679         oa = obdo_alloc();
680         if (!oa)
681                 RETURN(-ENOMEM);
682
683         oa->o_generation = dir->i_generation;
684
685         obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
686                         OBD_MD_FLMTIME | OBD_MD_FLCTIME |
687                         OBD_MD_FLUID | OBD_MD_FLGID);
688
689         oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
690         oa->o_valid |= OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
691         oa->o_mode = dir->i_mode;
692
693         /* 
694          * until lmv_obd_create() properly rewritten, it is important to have
695          * here oa->o_id = dir->i_ino, as otherwise master object will have
696          * invalid store cookie (zero inode num), what will lead to -ESTALE in
697          * mds_open() or somewhere else.
698          */
699         oa->o_id = dir->i_ino;
700
701         down(&dir->i_sem);
702         rc = mds_read_inode_sid(obd, dir, &id);
703         up(&dir->i_sem);
704         if (rc) {
705                 CERROR("Can't read inode self id, inode %lu, "
706                        "rc %d.\n", dir->i_ino, rc);
707                 GOTO(err_oa, rc);
708         }
709         oa->o_fid = id_fid(&id);
710         oa->o_mds = mds->mds_num;
711         LASSERT(oa->o_fid != 0);
712
713         CDEBUG(D_OTHER, "%s: create subdirs with mode %o, uid %u, gid %u\n",
714                obd->obd_name, dir->i_mode, dir->i_uid, dir->i_gid);
715                         
716         rc = obd_create(mds->mds_md_exp, oa, NULL, 0,
717                         (struct lov_stripe_md **)mea, NULL);
718         if (rc) {
719                 CERROR("Can't create remote inode, rc = %d\n", rc);
720                 GOTO(err_oa, rc);
721         }
722
723         LASSERT(id_fid(&(*mea)->mea_ids[0]));
724         CDEBUG(D_OTHER, "%d dirobjects created\n", (int)(*mea)->mea_count);
725
726         /* 2) update dir attribute */
727         down(&dir->i_sem);
728         
729         handle = fsfilt_start(obd, dir, FSFILT_OP_SETATTR, NULL);
730         if (IS_ERR(handle)) {
731                 up(&dir->i_sem);
732                 CERROR("fsfilt_start() failed: %d\n", (int) PTR_ERR(handle));
733                 GOTO(err_oa, rc = PTR_ERR(handle));
734         }
735
736         rc = fsfilt_set_md(obd, dir, handle, *mea, mea_size, EA_MEA);
737         if (rc) {
738                 up(&dir->i_sem);
739                 CERROR("fsfilt_set_md() failed, error %d.\n", rc);
740                 GOTO(err_oa, rc);
741         }
742         
743         rc = fsfilt_commit(obd, mds->mds_sb, dir, handle, 0);
744         if (rc) {
745                 up(&dir->i_sem);
746                 CERROR("fsfilt_commit() failed, error %d.\n", rc);
747                 GOTO(err_oa, rc);
748         }
749         
750         up(&dir->i_sem);
751         obdo_free(oa);
752
753         /* 3) read through the dir and distribute it over objects */
754         rc = scan_and_distribute(obd, dentry, *mea);
755         if (mea == &tmea)
756                 obd_free_diskmd(mds->mds_md_exp, (struct lov_mds_md **)mea);
757         if (rc) {
758                 CERROR("scan_and_distribute() failed, error %d.\n", rc);
759                 RETURN(rc);
760         }
761
762         RETURN(1);
763
764 err_oa:
765         obdo_free(oa);
766         return rc;
767 }
768
769 static int filter_start_page_write(struct inode *inode,
770                                    struct niobuf_local *lnb)
771 {
772         struct page *page = alloc_pages(GFP_HIGHUSER, 0);
773         if (page == NULL) {
774                 CERROR("no memory for a temp page\n");
775                 return lnb->rc = -ENOMEM;
776         }
777         POISON_PAGE(page, 0xf1);
778         page->index = lnb->offset >> PAGE_SHIFT;
779         lnb->page = page;
780         return 0;
781 }
782
783 struct dentry *filter_id2dentry(struct obd_device *obd,
784                                 struct dentry *dir_dentry,
785                                 obd_gr group, obd_id id);
786
787 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
788                 int objcount, struct obd_ioobj *obj,
789                 int niocount, struct niobuf_remote *nb,
790                 struct niobuf_local *res,
791                 struct obd_trans_info *oti)
792 {
793         struct niobuf_remote *rnb;
794         struct niobuf_local *lnb = NULL;
795         int rc = 0, i, tot_bytes = 0;
796         unsigned long now = jiffies;
797         struct dentry *dentry;
798         struct lustre_id id;
799         ENTRY;
800
801         LASSERT(objcount == 1);
802         LASSERT(obj->ioo_bufcnt > 0);
803
804         memset(res, 0, niocount * sizeof(*res));
805
806         id_fid(&id) = 0;
807         id_group(&id) = 0;
808         id_ino(&id) = obj->ioo_id;
809         id_gen(&id) = obj->ioo_gr;
810         
811         dentry = mds_id2dentry(exp->exp_obd, &id, NULL);
812         if (IS_ERR(dentry)) {
813                 CERROR("can't get dentry for "LPU64"/%u: %d\n",
814                        id.li_stc.u.e3s.l3s_ino, id.li_stc.u.e3s.l3s_gen,
815                        (int)PTR_ERR(dentry));
816                 GOTO(cleanup, rc = (int) PTR_ERR(dentry));
817         }
818
819         if (dentry->d_inode == NULL) {
820                 CERROR("trying to BRW to non-existent file "LPU64"\n",
821                        obj->ioo_id);
822                 l_dput(dentry);
823                 GOTO(cleanup, rc = -ENOENT);
824         }
825
826         if (time_after(jiffies, now + 15 * HZ))
827                 CERROR("slow preprw_write setup %lus\n", (jiffies - now) / HZ);
828         else
829                 CDEBUG(D_INFO, "preprw_write setup: %lu jiffies\n",
830                        (jiffies - now));
831
832         for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
833              i++, lnb++, rnb++) {
834                 lnb->dentry = dentry;
835                 lnb->offset = rnb->offset;
836                 lnb->len    = rnb->len;
837                 lnb->flags  = rnb->flags;
838
839                 rc = filter_start_page_write(dentry->d_inode, lnb);
840                 if (rc) {
841                         CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR, "page err %u@"
842                                LPU64" %u/%u %p: rc %d\n", lnb->len, lnb->offset,
843                                i, obj->ioo_bufcnt, dentry, rc);
844                         while (lnb-- > res)
845                                 __free_pages(lnb->page, 0);
846                         l_dput(dentry);
847                         GOTO(cleanup, rc);
848                 }
849                 tot_bytes += lnb->len;
850         }
851
852         if (time_after(jiffies, now + 15 * HZ))
853                 CERROR("slow start_page_write %lus\n", (jiffies - now) / HZ);
854         else
855                 CDEBUG(D_INFO, "start_page_write: %lu jiffies\n",
856                        (jiffies - now));
857
858         EXIT;
859 cleanup:
860         return rc;
861 }
862
863 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
864                  int objcount, struct obd_ioobj *obj, int niocount,
865                  struct niobuf_local *res, struct obd_trans_info *oti,
866                  int retcode)
867 {
868         struct obd_device *obd = exp->exp_obd;
869         struct niobuf_local *lnb;
870         struct inode *inode = NULL;
871         int rc = 0, i, cleanup_phase = 0, err, entries = 0;
872         ENTRY;
873
874         LASSERT(objcount == 1);
875         LASSERT(current->journal_info == NULL);
876
877         cleanup_phase = 1;
878         inode = res->dentry->d_inode;
879
880         for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
881                 char *end, *buf;
882                 struct dir_entry *de;
883
884                 buf = kmap(lnb->page);
885                 LASSERT(buf != NULL);
886                 end = buf + lnb->len;
887                 de = (struct dir_entry *) buf;
888                 while ((char *) de < end && de->namelen) {
889                         err = fsfilt_add_dir_entry(obd, res->dentry, de->name,
890                                                    de->namelen, de->ino,
891                                                    de->generation, de->mds,
892                                                    de->fid);
893                         if (err) {
894                                 CERROR("can't add dir entry %*s->%u/%u/%u"
895                                        " to %lu/%u: %d\n", de->namelen,
896                                        de->name, de->mds, (unsigned)de->ino,
897                                        (unsigned) de->generation,
898                                        res->dentry->d_inode->i_ino,
899                                        res->dentry->d_inode->i_generation,
900                                        err);
901                                 rc = err;
902                                 break;
903                         }
904                         LASSERT(err == 0);
905                         de = (struct dir_entry *)
906                                 ((char *) de + DIR_REC_LEN(de->namelen));
907                         entries++;
908                 }
909                 kunmap(lnb->page);
910         }
911
912         for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++)
913                 __free_page(lnb->page);
914         l_dput(res->dentry);
915
916         RETURN(rc);
917 }
918
919 int mds_choose_mdsnum(struct obd_device *obd, const char *name, int len, int flags,
920                         struct ptlrpc_peer *peer, struct inode *parent)
921 {
922         struct mds_obd *mds = &obd->u.mds;
923         struct lmv_obd *lmv;
924         int i = mds->mds_num;
925         char peer_str[PTL_NALFMT_SIZE];
926         if (flags & REC_REINT_CREATE) { 
927                 i = mds->mds_num;
928         } else if (mds->mds_md_exp != NULL && peer != NULL) {
929                 LASSERT(parent != NULL);
930                 /* distribute only at root level */
931                 lmv = &mds->mds_md_exp->exp_obd->u.lmv;
932                 if (parent->i_ino != id_ino(&mds->mds_rootid)) {
933                         i = mds->mds_num;
934                 } else {
935                         __u64 nid = peer->peer_id.nid;
936                         __u64 count = lmv->desc.ld_tgt_count;
937                         i = do_div(nid, count);
938                         CWARN("client from %s creates top dir %*s on mds #%d\n",
939                               ptlrpc_peernid2str(peer, peer_str), len, name, i);
940                 }
941         } else if (mds->mds_md_exp) {
942                 lmv = &mds->mds_md_exp->exp_obd->u.lmv;
943                 i = raw_name2idx(MEA_MAGIC_LAST_CHAR, lmv->desc.ld_tgt_count, name, len);
944         }
945         RETURN(i);
946 }
947
948 int mds_lock_slave_objs(struct obd_device *obd, struct dentry *dentry,
949                         struct lustre_handle **rlockh)
950 {
951         struct mds_obd *mds = &obd->u.mds;
952         struct mdc_op_data *op_data;
953         struct lookup_intent it;
954         struct mea *mea = NULL;
955         int mea_size, rc;
956         int handle_size;
957         ENTRY;
958
959         LASSERT(rlockh != NULL);
960         LASSERT(dentry != NULL);
961         LASSERT(dentry->d_inode != NULL);
962
963         /* clustered MD ? */
964         if (!mds->mds_md_obd)
965                 RETURN(0);
966
967         /* a dir can be splitted only */
968         if (!S_ISDIR(dentry->d_inode->i_mode))
969                 RETURN(0);
970
971         rc = mds_md_get_attr(obd, dentry->d_inode,
972                              &mea, &mea_size);
973         if (rc)
974                 RETURN(rc);
975
976         if (mea == NULL)
977                 RETURN(0);
978         
979         if (mea->mea_count == 0)
980                 /* this is slave object */
981                 GOTO(cleanup, rc = 0);
982                 
983         CDEBUG(D_OTHER, "%s: lock slaves for %lu/%lu\n",
984                obd->obd_name, (unsigned long)dentry->d_inode->i_ino,
985                (unsigned long)dentry->d_inode->i_generation);
986
987         handle_size = sizeof(struct lustre_handle) *
988                 mea->mea_count;
989         
990         OBD_ALLOC(*rlockh, handle_size);
991         if (*rlockh == NULL)
992                 GOTO(cleanup, rc = -ENOMEM);
993
994         memset(*rlockh, 0, handle_size);
995         OBD_ALLOC(op_data, sizeof(*op_data));
996         if (op_data == NULL) {
997                 OBD_FREE(*rlockh, handle_size);
998                 RETURN(-ENOMEM);
999         }
1000         memset(op_data, 0, sizeof(*op_data));
1001
1002         op_data->mea1 = mea;
1003         it.it_op = IT_UNLINK;
1004
1005         OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
1006
1007         rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
1008                         op_data, *rlockh, NULL, 0, ldlm_completion_ast,
1009                         mds_blocking_ast, NULL);
1010         OBD_FREE(op_data, sizeof(*op_data));
1011         OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
1012         EXIT;
1013 cleanup:
1014         OBD_FREE(mea, mea_size);
1015         return rc;
1016 }
1017
1018 void mds_unlock_slave_objs(struct obd_device *obd, struct dentry *dentry,
1019                            struct lustre_handle *lockh)
1020 {
1021         struct mds_obd *mds = &obd->u.mds;
1022         struct mea *mea = NULL;
1023         int mea_size, rc, i;
1024         ENTRY;
1025
1026         if (lockh == NULL) {
1027                 EXIT;
1028                 return;
1029         }
1030
1031         LASSERT(mds->mds_md_obd != NULL);
1032         LASSERT(S_ISDIR(dentry->d_inode->i_mode));
1033
1034         rc = mds_md_get_attr(obd, dentry->d_inode, &mea, &mea_size);
1035         if (rc) {
1036                 CERROR("locks are leaked\n");
1037                 EXIT;
1038                 return;
1039         }
1040         LASSERT(mea_size != 0);
1041         LASSERT(mea != NULL);
1042         LASSERT(mea->mea_count != 0);
1043
1044         CDEBUG(D_OTHER, "%s: unlock slaves for %lu/%lu\n", obd->obd_name,
1045                (unsigned long) dentry->d_inode->i_ino,
1046                (unsigned long) dentry->d_inode->i_generation);
1047
1048         for (i = 0; i < mea->mea_count; i++) {
1049                 if (lockh[i].cookie != 0)
1050                         ldlm_lock_decref(lockh + i, LCK_EX);
1051         }
1052
1053         OBD_FREE(lockh, sizeof(struct lustre_handle) * mea->mea_count);
1054         OBD_FREE(mea, mea_size);
1055         EXIT;
1056 }
1057
1058 int mds_unlink_slave_objs(struct obd_device *obd, struct dentry *dentry)
1059 {
1060         struct mds_obd *mds = &obd->u.mds;
1061         struct ptlrpc_request *req = NULL;
1062         struct mdc_op_data *op_data;
1063         struct mea *mea = NULL;
1064         int mea_size, rc;
1065         ENTRY;
1066
1067         /* clustered MD ? */
1068         if (!mds->mds_md_obd)
1069                 RETURN(0);
1070
1071         /* a dir can be splitted only */
1072         if (!S_ISDIR(dentry->d_inode->i_mode))
1073                 RETURN(0);
1074
1075         rc = mds_md_get_attr(obd, dentry->d_inode, &mea, &mea_size);
1076         if (rc)
1077                 RETURN(rc);
1078
1079         if (mea == NULL)
1080                 RETURN(0);
1081                        
1082         if (mea->mea_count == 0)
1083                 GOTO(cleanup, rc = 0);
1084
1085         CDEBUG(D_OTHER, "%s: unlink slaves for %lu/%lu\n", obd->obd_name,
1086                (unsigned long)dentry->d_inode->i_ino,
1087                (unsigned long)dentry->d_inode->i_generation);
1088
1089         OBD_ALLOC(op_data, sizeof(*op_data));
1090         if (op_data == NULL)
1091                 RETURN(-ENOMEM);
1092         
1093         memset(op_data, 0, sizeof(*op_data));
1094         op_data->mea1 = mea;
1095         rc = md_unlink(mds->mds_md_exp, op_data, &req);
1096         OBD_FREE(op_data, sizeof(*op_data));
1097         LASSERT(req == NULL);
1098         EXIT;
1099 cleanup:
1100         OBD_FREE(mea, mea_size);
1101         return rc;
1102 }
1103
1104 struct ide_tracking {
1105         int entries;
1106         int empty;
1107 };
1108
1109 int mds_ide_filldir(void *__buf, const char *name, int namelen,
1110                     loff_t offset, ino_t ino, unsigned int d_type)
1111 {
1112         struct ide_tracking *it = __buf;
1113
1114         if (ino == 0)
1115                 return 0;
1116
1117         it->entries++;
1118         if (it->entries > 2)
1119                 goto noempty;
1120         if (namelen > 2)
1121                 goto noempty;
1122         if (name[0] == '.' && namelen == 1)
1123                 return 0;
1124         if (name[0] == '.' && name[1] == '.' && namelen == 2)
1125                 return 0;
1126 noempty:
1127         it->empty = 0;
1128         return -ENOTEMPTY;
1129 }
1130
1131 /* checks if passed dentry points to empty dir. */
1132 int mds_is_dir_empty(struct obd_device *obd, struct dentry *dentry)
1133 {
1134         struct ide_tracking it;
1135         struct file * file;
1136         char *file_name;
1137         int nlen, i, rc;
1138         
1139         it.entries = 0;
1140         it.empty = 1;
1141
1142         nlen = strlen("__iopen__/") + 10 + 1;
1143         OBD_ALLOC(file_name, nlen);
1144         if (!file_name)
1145                 RETURN(-ENOMEM);
1146         
1147         LASSERT(dentry->d_inode != NULL);
1148         i = sprintf(file_name, "__iopen__/0x%lx",
1149                     dentry->d_inode->i_ino);
1150
1151         file = filp_open(file_name, O_RDONLY, 0);
1152         if (IS_ERR(file)) {
1153                 CERROR("can't open directory %s: %d\n",
1154                        file_name, (int) PTR_ERR(file));
1155                 GOTO(cleanup, rc = PTR_ERR(file));
1156         }
1157
1158         rc = vfs_readdir(file, mds_ide_filldir, &it);
1159         filp_close(file, 0);
1160
1161         if (it.empty && rc == 0)
1162                 rc = 1;
1163         else
1164                 rc = 0;
1165
1166 cleanup:
1167         OBD_FREE(file_name, nlen);
1168         return rc;
1169 }
1170
1171 int mds_lock_and_check_slave(int offset, struct ptlrpc_request *req,
1172                              struct lustre_handle *lockh)
1173 {
1174         struct obd_device *obd = req->rq_export->exp_obd;
1175         struct dentry *dentry = NULL;
1176         struct lvfs_run_ctxt saved;
1177         int cleanup_phase = 0;
1178         struct mds_req_sec_desc *rsd;
1179         struct mds_body *body;
1180         struct lvfs_ucred uc;
1181         int rc, update_mode;
1182         ENTRY;
1183
1184         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1185         if (!rsd) {
1186                 CERROR("Can't unpack security desc\n");
1187                 GOTO(cleanup, rc = -EFAULT);
1188         }
1189
1190         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1191                                   lustre_swab_mds_body);
1192         if (body == NULL) {
1193                 CERROR("Can't swab mds_body\n");
1194                 GOTO(cleanup, rc = -EFAULT);
1195         }
1196         CDEBUG(D_OTHER, "%s: check slave "DLID4"\n", obd->obd_name,
1197                OLID4(&body->id1));
1198         
1199         dentry = mds_id2locked_dentry(obd, &body->id1, NULL, LCK_EX,
1200                                       lockh, &update_mode, NULL, 0,
1201                                       MDS_INODELOCK_UPDATE);
1202         if (IS_ERR(dentry)) {
1203                 CERROR("can't find inode: %d\n", (int) PTR_ERR(dentry));
1204                 GOTO(cleanup, rc = PTR_ERR(dentry));
1205         }
1206         cleanup_phase = 1;
1207
1208         /* 
1209          * handling the case when remote MDS checks if dir is empty 
1210          * before rename. But it also does it for all entries, because
1211          * inode is stored here and remote MDS does not know if rename
1212          * point to dir or to reg file. So we check it here. 
1213          */
1214         if (!S_ISDIR(dentry->d_inode->i_mode))
1215                 GOTO(cleanup, rc = 0);
1216
1217         rc = mds_init_ucred(&uc, req, rsd);
1218         if (rc) {
1219                 GOTO(cleanup, rc);
1220         }
1221
1222         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1223         rc = mds_is_dir_empty(obd, dentry) ? 0 : -ENOTEMPTY;
1224         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1225
1226         mds_exit_ucred(&uc);
1227         EXIT;
1228 cleanup:
1229         switch(cleanup_phase) {
1230         case 1:
1231                 if (rc)
1232                         ldlm_lock_decref(lockh, LCK_EX);
1233                 l_dput(dentry);
1234         default:
1235                 break;
1236         }
1237         return rc;
1238 }
1239
1240 int mds_convert_mea_ea(struct obd_device *obd, struct inode *inode,
1241                        struct lov_mds_md *lmm, int lmm_size)
1242 {
1243         struct lov_stripe_md *lsm = NULL;
1244         struct mea_old *old;
1245         struct mea *mea;
1246         void *handle;
1247         int rc, err;
1248         ENTRY;
1249
1250         mea = (struct mea *)lmm;
1251         old = (struct mea_old *)lmm;
1252
1253         if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
1254             mea->mea_magic == MEA_MAGIC_ALL_CHARS)
1255                 RETURN(0);
1256
1257         /*
1258          * making MDS try LOV EA converting in the non-LMV configuration
1259          * cases.
1260          */
1261         if (!obd->u.mds.mds_md_exp)
1262                 RETURN(-EINVAL);
1263
1264         CDEBUG(D_INODE, "converting MEA EA on %lu/%u from V0 to V1 (%u/%u)\n",
1265                inode->i_ino, inode->i_generation, old->mea_count, 
1266                old->mea_master);
1267
1268         rc = obd_unpackmd(obd->u.mds.mds_md_exp, &lsm, lmm, lmm_size);
1269         if (rc < 0)
1270                 GOTO(conv_end, rc);
1271
1272         rc = obd_packmd(obd->u.mds.mds_md_exp, &lmm, lsm);
1273         if (rc < 0)
1274                 GOTO(conv_free, rc);
1275         lmm_size = rc;
1276
1277         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1278         if (IS_ERR(handle)) {
1279                 rc = PTR_ERR(handle);
1280                 GOTO(conv_free, rc);
1281         }
1282
1283         rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size, EA_MEA);
1284         err = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 0);
1285         if (!rc)
1286                 rc = err ? err : lmm_size;
1287         GOTO(conv_free, rc);
1288 conv_free:
1289         obd_free_memmd(obd->u.mds.mds_md_exp, &lsm);
1290 conv_end:
1291         return rc;
1292 }
1293