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