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