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