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