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