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