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