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