Whamcloud - gitweb
Land b1_8_gate onto b1_8 (20081218_1708)
[fs/lustre-release.git] / lustre / llite / dir.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/llite/dir.c
37  *
38  * Directory code for lustre client.
39  */
40
41 #include <linux/fs.h>
42 #include <linux/pagemap.h>
43 #include <linux/mm.h>
44 #include <linux/version.h>
45 #include <linux/smp_lock.h>
46 #include <asm/uaccess.h>
47 #include <linux/buffer_head.h>   // for wait_on_buffer
48
49 #define DEBUG_SUBSYSTEM S_LLITE
50
51 #include <obd_support.h>
52 #include <obd_class.h>
53 #include <lustre_lib.h>
54 #include <lustre/lustre_idl.h>
55 #include <lustre_lite.h>
56 #include <lustre_dlm.h>
57 #include "llite_internal.h"
58
59 #ifndef HAVE_PAGE_CHECKED
60 #ifdef HAVE_PG_FS_MISC
61 #define PageChecked(page)        test_bit(PG_fs_misc, &(page)->flags)
62 #define SetPageChecked(page)     set_bit(PG_fs_misc, &(page)->flags)
63 #else
64 #error PageChecked or PageFsMisc not defined in kernel
65 #endif
66 #endif
67
68 /* returns the page unlocked, but with a reference */
69 static int ll_dir_readpage(struct file *file, struct page *page)
70 {
71         struct inode *inode = page->mapping->host;
72         struct ll_fid mdc_fid;
73         __u64 offset;
74         struct ptlrpc_request *request;
75         struct mds_body *body;
76         int rc = 0;
77         ENTRY;
78
79         offset = (__u64)page->index << CFS_PAGE_SHIFT;
80         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) off "LPU64"\n",
81                inode->i_ino, inode->i_generation, inode, offset);
82
83         ll_pack_fid(&mdc_fid, inode->i_ino, inode->i_generation, S_IFDIR);
84
85         rc = mdc_readpage(ll_i2sbi(inode)->ll_mdc_exp, &mdc_fid,
86                           offset, page, &request);
87         if (!rc) {
88                 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF,
89                                       sizeof(*body));
90                 LASSERT(body != NULL); /* checked by mdc_readpage() */
91                 /* swabbed by mdc_readpage() */
92                 LASSERT(lustre_rep_swabbed(request, REPLY_REC_OFF));
93
94                 if (body->size != i_size_read(inode)) {
95                         ll_inode_size_lock(inode, 0);
96                         i_size_write(inode, body->size);
97                         ll_inode_size_unlock(inode, 0);
98                 }
99
100                 SetPageUptodate(page);
101         }
102         ptlrpc_req_finished(request);
103
104         unlock_page(page);
105         EXIT;
106         return rc;
107 }
108
109 struct address_space_operations ll_dir_aops = {
110         .readpage  = ll_dir_readpage,
111 };
112
113 static inline unsigned ll_dir_page_mask(struct inode *inode)
114 {
115         return ~(inode->i_sb->s_blocksize - 1);
116 }
117
118 /*
119  * Check consistency of a single entry.
120  */
121 static int ll_dir_check_entry(struct inode *dir, struct ll_dir_entry *ent,
122                               unsigned offset, unsigned rec_len, pgoff_t index)
123 {
124         const char *msg;
125
126         /*
127          * Consider adding more checks.
128          */
129
130         if (unlikely(rec_len < ll_dir_rec_len(1)))
131                 msg = "entry is too short";
132         else if (unlikely(rec_len & 3))
133                 msg = "wrong alignment";
134         else if (unlikely(rec_len < ll_dir_rec_len(ent->lde_name_len)))
135                 msg = "rec_len doesn't match name_len";
136         else if (unlikely(((offset + rec_len - 1) ^ offset) &
137                           ll_dir_page_mask(dir)))
138                 msg = "directory entry across blocks";
139         else
140                 return 0;
141         CERROR("%s: bad entry in directory %lu/%u: %s - "
142                "offset=%lu+%u, inode=%lu, rec_len=%d,"
143                " name_len=%d\n", ll_i2mdcexp(dir)->exp_obd->obd_name,
144                dir->i_ino, dir->i_generation, msg,
145                index << CFS_PAGE_SHIFT,
146                offset, (unsigned long)le32_to_cpu(ent->lde_inode),
147                rec_len, ent->lde_name_len);
148         return -EIO;
149 }
150
151 static void ll_dir_check_page(struct inode *dir, struct page *page)
152 {
153         int      err;
154         unsigned size = dir->i_sb->s_blocksize;
155         char    *addr = page_address(page);
156         unsigned off;
157         unsigned limit;
158         unsigned reclen;
159
160         struct ll_dir_entry *ent;
161
162         err = 0;
163         if ((i_size_read(dir) >> CFS_PAGE_SHIFT) == (__u64)page->index) {
164                 /*
165                  * Last page.
166                  */
167                 limit = i_size_read(dir) & ~CFS_PAGE_MASK;
168                 if (limit & (size - 1)) {
169                         CERROR("%s: dir %lu/%u size %llu doesn't match %u\n",
170                                ll_i2mdcexp(dir)->exp_obd->obd_name, dir->i_ino,
171                                dir->i_generation, i_size_read(dir), size);
172                         err++;
173                 } else {
174                         /*
175                          * Place dummy forwarding entries to streamline
176                          * ll_readdir().
177                          */
178                         for (off = limit; off < CFS_PAGE_SIZE; off += size) {
179                                 ent = ll_entry_at(addr, off);
180                                 ent->lde_rec_len = cpu_to_le16(size);
181                                 ent->lde_name_len = 0;
182                                 ent->lde_inode = 0;
183                         }
184                 }
185         } else
186                 limit = CFS_PAGE_SIZE;
187
188         for (off = 0;
189              !err && off <= limit - ll_dir_rec_len(1); off += reclen) {
190                 ent    = ll_entry_at(addr, off);
191                 reclen = le16_to_cpu(ent->lde_rec_len);
192                 err    = ll_dir_check_entry(dir, ent, off, reclen, page->index);
193         }
194
195         if (!err && off != limit) {
196                 ent = ll_entry_at(addr, off);
197                 CERROR("%s: entry in directory %lu/%u spans the page boundary "
198                        "offset="LPU64"+%u, inode=%lu\n",
199                        ll_i2mdcexp(dir)->exp_obd->obd_name,
200                        dir->i_ino, dir->i_generation,
201                        (__u64)page->index << CFS_PAGE_SHIFT,
202                        off, (unsigned long)le32_to_cpu(ent->lde_inode));
203                 err++;
204         }
205         if (err)
206                 SetPageError(page);
207         SetPageChecked(page);
208 }
209
210 struct page *ll_get_dir_page(struct inode *dir, unsigned long n)
211 {
212         struct ldlm_res_id res_id;
213         struct lustre_handle lockh;
214         struct obd_device *obddev = class_exp2obd(ll_i2sbi(dir)->ll_mdc_exp);
215         struct address_space *mapping = dir->i_mapping;
216         struct page *page;
217         ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
218         int rc;
219
220         fid_build_reg_res_name(ll_inode_lu_fid(dir), &res_id);
221         rc = ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
222                              &res_id, LDLM_IBITS, &policy, LCK_CR, &lockh);
223         if (!rc) {
224                 struct lookup_intent it = { .it_op = IT_READDIR };
225                 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
226                        ll_mdc_blocking_ast, ldlm_completion_ast, NULL, dir };
227                 struct ptlrpc_request *request;
228                 struct mdc_op_data data = { { 0 } };
229
230                 ll_prepare_mdc_op_data(&data, dir, NULL, NULL, 0, 0, NULL);
231
232                 rc = mdc_enqueue(ll_i2sbi(dir)->ll_mdc_exp, &einfo, &it,
233                                  &data, &lockh, NULL, 0, 0);
234
235                 request = (struct ptlrpc_request *)it.d.lustre.it_data;
236                 if (request)
237                         ptlrpc_req_finished(request);
238                 if (rc < 0) {
239                         CERROR("lock enqueue: rc: %d\n", rc);
240                         return ERR_PTR(rc);
241                 }
242         }
243         ldlm_lock_dump_handle(D_OTHER, &lockh);
244
245         page = read_cache_page(mapping, n,
246                                (filler_t*)mapping->a_ops->readpage, NULL);
247         if (IS_ERR(page))
248                 GOTO(out_unlock, page);
249
250         wait_on_page(page);
251         (void)kmap(page);
252         if (!PageUptodate(page))
253                 goto fail;
254         if (!PageChecked(page))
255                 ll_dir_check_page(dir, page);
256         if (PageError(page))
257                 goto fail;
258
259 out_unlock:
260         ldlm_lock_decref(&lockh, LCK_CR);
261         return page;
262
263 fail:
264         ll_put_page(page);
265         page = ERR_PTR(-EIO);
266         goto out_unlock;
267 }
268
269 static inline unsigned ll_dir_validate_entry(char *base, unsigned offset,
270                                              unsigned mask)
271 {
272         struct ll_dir_entry *de = ll_entry_at(base, offset);
273         struct ll_dir_entry *p  = ll_entry_at(base, offset & mask);
274         while (p < de && p->lde_rec_len > 0)
275                 p = ll_dir_next_entry(p);
276         return (char *)p - base;
277 }
278
279 /*
280  * File type constants. The same as in ext2 for compatibility.
281  */
282
283 enum {
284         LL_DIR_FT_UNKNOWN,
285         LL_DIR_FT_REG_FILE,
286         LL_DIR_FT_DIR,
287         LL_DIR_FT_CHRDEV,
288         LL_DIR_FT_BLKDEV,
289         LL_DIR_FT_FIFO,
290         LL_DIR_FT_SOCK,
291         LL_DIR_FT_SYMLINK,
292         LL_DIR_FT_MAX
293 };
294
295 static unsigned char ll_dir_filetype_table[LL_DIR_FT_MAX] = {
296         [LL_DIR_FT_UNKNOWN]  = DT_UNKNOWN,
297         [LL_DIR_FT_REG_FILE] = DT_REG,
298         [LL_DIR_FT_DIR]      = DT_DIR,
299         [LL_DIR_FT_CHRDEV]   = DT_CHR,
300         [LL_DIR_FT_BLKDEV]   = DT_BLK,
301         [LL_DIR_FT_FIFO]     = DT_FIFO,
302         [LL_DIR_FT_SOCK]     = DT_SOCK,
303         [LL_DIR_FT_SYMLINK]  = DT_LNK,
304 };
305
306 /*
307  * Process one page. Returns:
308  *
309  *     -ve: filldir commands readdir to stop.
310  *     +ve: number of entries submitted to filldir.
311  *       0: no live entries on this page.
312  */
313
314 static int ll_readdir_page(char *addr, __u64 base, unsigned *offset,
315                            filldir_t filldir, void *cookie)
316 {
317         struct ll_dir_entry *de;
318         char *end;
319         int nr;
320
321         de = ll_entry_at(addr, *offset);
322         end = addr + CFS_PAGE_SIZE - ll_dir_rec_len(1);
323         for (nr = 0 ;(char*)de <= end; de = ll_dir_next_entry(de)) {
324                 if (de->lde_inode != 0) {
325                         nr++;
326                         *offset = (char *)de - addr;
327                         if (filldir(cookie, de->lde_name, de->lde_name_len,
328                                     base | *offset, le32_to_cpu(de->lde_inode),
329                                     ll_dir_filetype_table[de->lde_file_type &
330                                                           (LL_DIR_FT_MAX - 1)]))
331                                 return -1;
332                 }
333         }
334         return nr;
335 }
336
337 static int ll_readdir_18(struct file *filp, void *dirent, filldir_t filldir)
338 {
339         struct inode *inode = filp->f_dentry->d_inode;
340         loff_t pos          = filp->f_pos;
341         unsigned offset     = pos & ~CFS_PAGE_MASK;
342         pgoff_t idx         = pos >> CFS_PAGE_SHIFT;
343         pgoff_t npages      = dir_pages(inode);
344         unsigned chunk_mask = ll_dir_page_mask(inode);
345         int need_revalidate = (filp->f_version != inode->i_version);
346         int rc              = 0;
347         int done; /* when this becomes negative --- stop iterating */
348
349         ENTRY;
350
351         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %llu/%llu\n",
352                inode->i_ino, inode->i_generation, inode,
353                pos, i_size_read(inode));
354
355         /*
356          * Checking ->i_size without the lock. Should be harmless, as server
357          * re-checks.
358          */
359         if (pos > i_size_read(inode) - ll_dir_rec_len(1))
360                 RETURN(0);
361
362         for (done = 0; idx < npages; idx++, offset = 0) {
363                 /*
364                  * We can assume that all blocks on this page are filled with
365                  * entries, because ll_dir_check_page() placed special dummy
366                  * entries for us.
367                  */
368
369                 char *kaddr;
370                 struct page *page;
371
372                 CDEBUG(D_EXT2,"read %lu of dir %lu/%u page %lu/%lu "
373                        "size %llu\n",
374                        CFS_PAGE_SIZE, inode->i_ino, inode->i_generation,
375                        idx, npages, i_size_read(inode));
376                 page = ll_get_dir_page(inode, idx);
377
378                 /* size might have been updated by mdc_readpage */
379                 npages = dir_pages(inode);
380
381                 if (IS_ERR(page)) {
382                         rc = PTR_ERR(page);
383                         CERROR("error reading dir %lu/%u page %lu: rc %d\n",
384                                inode->i_ino, inode->i_generation, idx, rc);
385                         continue;
386                 }
387
388                 kaddr = page_address(page);
389                 if (need_revalidate) {
390                         /*
391                          * File offset was changed by lseek() and possibly
392                          * points in the middle of an entry. Re-scan from the
393                          * beginning of the chunk.
394                          */
395                         offset = ll_dir_validate_entry(kaddr, offset,
396                                                        chunk_mask);
397                         need_revalidate = 0;
398                 }
399                 done = ll_readdir_page(kaddr, idx << CFS_PAGE_SHIFT,
400                                        &offset, filldir, dirent);
401                 ll_put_page(page);
402                 if (done > 0)
403                         /*
404                          * Some entries were sent to the user space, return
405                          * success.
406                          */
407                         rc = 0;
408                 else if (done < 0)
409                         /*
410                          * filldir is satisfied.
411                          */
412                         break;
413         }
414
415         filp->f_pos = (idx << CFS_PAGE_SHIFT) | offset;
416         filp->f_version = inode->i_version;
417         touch_atime(filp->f_vfsmnt, filp->f_dentry);
418
419         RETURN(rc);
420 }
421
422 /*      
423  * Chain of hash overflow pages.
424  */            
425 struct ll_dir_chain {
426         /* XXX something. Later */
427 };
428   
429 static inline void ll_dir_chain_init(struct ll_dir_chain *chain)
430 {  
431 }
432
433 static inline void ll_dir_chain_fini(struct ll_dir_chain *chain)
434 {
435 }
436
437 static inline __u32 hash_x_index(__u32 value)
438 {
439         return ((__u32)~0) - value;
440 }
441
442 /*
443  * Layout of readdir pages, as transmitted on wire.
444  */     
445 struct lu_dirent {
446         struct lu_fid lde_fid;
447         __u64         lde_hash;
448         __u16         lde_reclen;
449         __u16         lde_namelen;
450         __u32         lde_padding;
451         char          lde_name[0];
452 };
453
454 struct lu_dirpage {
455         __u64            ldp_hash_start;
456         __u64            ldp_hash_end;
457         __u16            ldp_flags;
458         __u16            ldp_pad0;
459         __u32            ldp_pad1;
460         struct lu_dirent ldp_entries[0];
461 };
462
463 enum lu_dirpage_flags {
464         LDF_EMPTY = 1 << 0
465 };
466
467 static inline struct lu_dirent *lu_dirent_start(struct lu_dirpage *dp)
468 {
469         if (le16_to_cpu(dp->ldp_flags) & LDF_EMPTY)
470                 return NULL;
471         else
472                 return dp->ldp_entries;
473 }
474
475 static inline struct lu_dirent *lu_dirent_next(struct lu_dirent *ent)
476 {
477         struct lu_dirent *next;
478
479         if (le16_to_cpu(ent->lde_reclen) != 0)
480                 next = ((void *)ent) + le16_to_cpu(ent->lde_reclen);
481         else
482                 next = NULL;
483
484         return next;
485 }
486
487 static inline int lu_dirent_size(struct lu_dirent *ent)
488 {
489         if (le16_to_cpu(ent->lde_reclen) == 0) {
490                 return (sizeof(*ent) +
491                         le16_to_cpu(ent->lde_namelen) + 3) & ~3;
492         }
493         return le16_to_cpu(ent->lde_reclen);
494 }
495
496 #define DIR_END_OFF              0xfffffffffffffffeULL
497
498 #ifdef HAVE_RW_TREE_LOCK
499 #define TREE_READ_LOCK_IRQ(mapping)     read_lock_irq(&(mapping)->tree_lock)
500 #define TREE_READ_UNLOCK_IRQ(mapping) read_unlock_irq(&(mapping)->tree_lock)
501 #else
502 #define TREE_READ_LOCK_IRQ(mapping) spin_lock_irq(&(mapping)->tree_lock)
503 #define TREE_READ_UNLOCK_IRQ(mapping) spin_unlock_irq(&(mapping)->tree_lock)
504 #endif
505
506 /* returns the page unlocked, but with a reference */
507 static int ll_dir_readpage_20(struct file *file, struct page *page)
508 {
509         struct inode *inode = page->mapping->host;
510         struct ptlrpc_request *request;
511         struct mdt_body *body;
512         struct ll_fid fid;
513         __u64 hash;
514         int rc;
515         ENTRY;
516
517         hash = hash_x_index(page->index);
518         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) off %lu\n",
519                inode->i_ino, inode->i_generation, inode, (unsigned long)hash);
520
521         ll_inode2fid(&fid, inode);
522         rc = mdc_readpage(ll_i2sbi(inode)->ll_mdc_exp, &fid,
523                           hash, page, &request);
524         if (!rc) {
525                 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF,
526                                       sizeof(*body));
527                 /* Checked by mdc_readpage() */
528                 LASSERT(body != NULL);
529
530                 if (body->valid & OBD_MD_FLSIZE) {
531                         ll_inode_size_lock(inode, 0);
532                         i_size_write(inode, body->size);
533                         ll_inode_size_unlock(inode, 0);
534                 }
535                 SetPageUptodate(page);
536         }
537         ptlrpc_req_finished(request);
538
539         unlock_page(page);
540         EXIT;
541         return rc;
542 }
543
544
545 static void ll_check_page(struct inode *dir, struct page *page)
546 {
547         /* XXX: check page format later */
548         SetPageChecked(page);
549 }
550
551
552 /*
553  * Find, kmap and return page that contains given hash.
554  */
555 static struct page *ll_dir_page_locate(struct inode *dir, unsigned long hash,
556                                        __u64 *start, __u64 *end)
557 {
558         struct address_space *mapping = dir->i_mapping;
559         /*
560          * Complement of hash is used as an index so that
561          * radix_tree_gang_lookup() can be used to find a page with starting
562          * hash _smaller_ than one we are looking for.
563          */
564         unsigned long offset = hash_x_index(hash);
565         struct page *page;
566         int found;
567         ENTRY;
568
569         TREE_READ_LOCK_IRQ(mapping);
570         found = radix_tree_gang_lookup(&mapping->page_tree,
571                                        (void **)&page, offset, 1);
572         if (found > 0) {
573                 struct lu_dirpage *dp;
574
575                 page_cache_get(page);
576                 TREE_READ_UNLOCK_IRQ(mapping);
577                 /*
578                  * In contrast to find_lock_page() we are sure that directory
579                  * page cannot be truncated (while DLM lock is held) and,
580                  * hence, can avoid restart.
581                  *
582                  * In fact, page cannot be locked here at all, because
583                  * ll_dir_readpage() does synchronous io.
584                  */
585                 wait_on_page(page);
586                 if (PageUptodate(page)) {
587                         dp = kmap(page);
588                         *start = le64_to_cpu(dp->ldp_hash_start);
589                         *end   = le64_to_cpu(dp->ldp_hash_end);
590                         LASSERT(*start <= hash);
591                         if (hash > *end || (*end != *start && hash == *end)) {
592                                 kunmap(page);
593                                 lock_page(page);
594                                 ll_truncate_complete_page(page);
595                                 unlock_page(page);
596                                 page_cache_release(page);
597                                 page = NULL;
598                         }
599                 } else {
600                         page_cache_release(page);
601                         page = ERR_PTR(-EIO);
602                 }
603
604         } else {
605                 TREE_READ_UNLOCK_IRQ(mapping);
606                 page = NULL;
607         }
608         RETURN(page);
609 }
610
611 static struct page *ll_get_dir_page_20(struct inode *dir, __u64 hash, int exact,
612                                        struct ll_dir_chain *chain)
613 {
614         struct ldlm_res_id res_id;
615         struct lustre_handle lockh;
616         struct obd_device *obddev = class_exp2obd(ll_i2sbi(dir)->ll_mdc_exp);
617         struct address_space *mapping = dir->i_mapping;
618         struct lu_dirpage *dp;
619         struct page *page;
620         ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
621         ldlm_mode_t mode;
622         int rc;
623         __u64 start = 0;
624         __u64 end = 0;
625         ENTRY;
626  
627         fid_build_reg_res_name(ll_inode_lu_fid(dir), &res_id);
628         mode = LCK_PR;
629         rc = ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
630                              &res_id, LDLM_IBITS, &policy, mode, &lockh);
631         if (!rc) {
632                 struct lookup_intent it = { .it_op = IT_READDIR };
633                 struct ldlm_enqueue_info einfo = { LDLM_IBITS, mode,
634                        ll_mdc_blocking_ast, ldlm_completion_ast, NULL, dir };
635                 struct ptlrpc_request *request;
636                 struct mdc_op_data op_data = { { 0 } };
637
638                 ll_prepare_mdc_op_data(&op_data, dir, NULL, NULL, 0, 0, NULL);
639
640                 rc = mdc_enqueue(ll_i2sbi(dir)->ll_mdc_exp, &einfo, &it,
641                                  &op_data, &lockh, NULL, 0, 0);
642
643                 request = (struct ptlrpc_request *)it.d.lustre.it_data;
644                 if (request)
645                         ptlrpc_req_finished(request);
646                 if (rc < 0) {
647                         CERROR("lock enqueue: rc: %d\n", rc);
648                         RETURN(ERR_PTR(rc));
649                 }
650         }
651         ldlm_lock_dump_handle(D_OTHER, &lockh);
652
653         page = ll_dir_page_locate(dir, hash, &start, &end);
654         if (IS_ERR(page))
655                 GOTO(out_unlock, page);
656
657         if (page != NULL) {
658                 /*
659                  * XXX nikita: not entirely correct handling of a corner case:
660                  * suppose hash chain of entries with hash value HASH crosses
661                  * border between pages P0 and P1. First both P0 and P1 are
662                  * cached, seekdir() is called for some entry from the P0 part
663                  * of the chain. Later P0 goes out of cache. telldir(HASH)
664                  * happens and finds P1, as it starts with matching hash
665                  * value. Remaining entries from P0 part of the chain are
666                  * skipped. (Is that really a bug?)
667                  *
668                  * Possible solutions: 0. don't cache P1 is such case, handle
669                  * it as an "overflow" page. 1. invalidate all pages at
670                  * once. 2. use HASH|1 as an index for P1.
671                  */
672                 if (exact && hash != start) {
673                         /*
674                          * readdir asked for a page starting _exactly_ from
675                          * given hash, but cache contains stale page, with
676                          * entries with smaller hash values. Stale page should
677                          * be invalidated, and new one fetched.
678                          */
679                         CDEBUG(D_INFO, "Stale readpage page %p: %#lx != %#lx\n",
680                               page, (unsigned long)hash, (unsigned long)start);
681                         lock_page(page);
682                         ll_truncate_complete_page(page);
683                         unlock_page(page);
684                         page_cache_release(page);
685                 } else {
686                         GOTO(hash_collision, page);
687                 }
688         }
689
690         page = read_cache_page(mapping, hash_x_index(hash),
691                                (filler_t*)ll_dir_readpage_20, NULL);
692         if (IS_ERR(page))
693                 GOTO(out_unlock, page);
694
695         wait_on_page(page);
696         (void)kmap(page);
697         if (!PageUptodate(page))
698                 goto fail;
699         if (!PageChecked(page))
700                 ll_check_page(dir, page);
701         if (PageError(page))
702                 goto fail;
703 hash_collision:
704         dp = page_address(page);
705
706         start = le64_to_cpu(dp->ldp_hash_start);
707         end   = le64_to_cpu(dp->ldp_hash_end);
708         if (end == start) {
709                 LASSERT(start == hash);
710                 CWARN("Page-wide hash collision: %#lx\n", (unsigned long)end);
711                 /*
712                  * Fetch whole overflow chain...
713                  *
714                  * XXX not yet.
715                  */
716                 goto fail;
717         }
718 out_unlock:
719         ldlm_lock_decref(&lockh, mode);
720         RETURN(page);
721
722 fail:
723         ll_put_page(page);
724         page = ERR_PTR(-EIO);
725         goto out_unlock;
726 }
727
728 static int ll_readdir_20(struct file *filp, void *cookie, filldir_t filldir)
729 {
730         struct inode         *inode = filp->f_dentry->d_inode;
731         struct ll_sb_info    *sbi   = ll_i2sbi(inode);
732         __u64                 pos   = filp->f_pos;
733         struct page          *page;
734         struct ll_dir_chain   chain;
735         int rc;
736         int done;
737         int shift;
738         ENTRY;
739
740         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu\n",
741                inode->i_ino, inode->i_generation, inode,
742                (unsigned long)pos, i_size_read(inode));
743
744         if (pos == DIR_END_OFF)
745                 /*
746                  * end-of-file.
747                  */
748                 RETURN(0);
749
750         rc    = 0;
751         done  = 0;
752         shift = 0;
753         ll_dir_chain_init(&chain);
754
755         page = ll_get_dir_page_20(inode, pos, 0, &chain);
756
757         while (rc == 0 && !done) {
758                 struct lu_dirpage *dp;
759                 struct lu_dirent  *ent;
760
761                 if (!IS_ERR(page)) {
762                         /* 
763                          * If page is empty (end of directoryis reached),
764                          * use this value. 
765                          */
766                         __u64 hash = DIR_END_OFF;
767                         __u64 next;
768
769                         dp = page_address(page);
770                         for (ent = lu_dirent_start(dp); ent != NULL && !done;
771                              ent = lu_dirent_next(ent)) {
772                                 char          *name;
773                                 int            namelen;
774                                 struct lu_fid  fid;
775                                 ino_t          ino;
776
777                                 hash    = le64_to_cpu(ent->lde_hash);
778                                 namelen = le16_to_cpu(ent->lde_namelen);
779
780                                 if (hash < pos)
781                                         /*
782                                          * Skip until we find target hash
783                                          * value.
784                                          */
785                                         continue;
786
787                                 if (namelen == 0)
788                                         /*
789                                          * Skip dummy record.
790                                          */
791                                         continue;
792
793                                 fid  = ent->lde_fid;
794                                 name = ent->lde_name;
795                                 fid_le_to_cpu(&fid, &fid);
796                                 ino  = ll_fid_build_ino(sbi, (struct ll_fid*)&fid);
797
798                                 done = filldir(cookie, name, namelen,
799                                                (loff_t)hash, ino, DT_UNKNOWN);
800                         }
801                         next = le64_to_cpu(dp->ldp_hash_end);
802                         ll_put_page(page);
803                         if (!done) {
804                                 pos = next;
805                                 if (pos == DIR_END_OFF)
806                                         /*
807                                          * End of directory reached.
808                                          */
809                                         done = 1;
810                                 else if (1 /* chain is exhausted*/)
811                                         /*
812                                          * Normal case: continue to the next
813                                          * page.
814                                          */
815                                         page = ll_get_dir_page_20(inode, pos, 1,
816                                                                   &chain);
817                                 else {
818                                         /*
819                                          * go into overflow page.
820                                          */
821                                 }
822                         } else {
823                                 pos = hash;
824                         }
825                 } else {
826                         rc = PTR_ERR(page);
827                         CERROR("error reading dir "DFID" at %lu: rc %d\n",
828                                PFID(ll_inode_lu_fid(inode)),
829                                (unsigned long)pos, rc);
830                 }
831         }
832
833         filp->f_pos = (loff_t)(__s32)pos;
834         filp->f_version = inode->i_version;
835         touch_atime(filp->f_vfsmnt, filp->f_dentry);
836
837         ll_dir_chain_fini(&chain);
838
839         RETURN(rc);
840 }
841
842 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
843 {
844         struct inode      *inode = filp->f_dentry->d_inode;
845         struct ll_sb_info *sbi = ll_i2sbi(inode);
846
847         if (sbi->ll_mdc_exp->exp_connect_flags & OBD_CONNECT_FID) {
848                 return ll_readdir_20(filp, cookie, filldir);
849         } else {
850                 return ll_readdir_18(filp, cookie, filldir);
851         }
852 }
853
854 #define QCTL_COPY(out, in)              \
855 do {                                    \
856         Q_COPY(out, in, qc_cmd);        \
857         Q_COPY(out, in, qc_type);       \
858         Q_COPY(out, in, qc_id);         \
859         Q_COPY(out, in, qc_stat);       \
860         Q_COPY(out, in, qc_dqinfo);     \
861         Q_COPY(out, in, qc_dqblk);      \
862 } while (0)
863
864 static int ll_send_mgc_param(struct obd_export *mgc, char *string)
865 {
866         struct mgs_send_param *msp;
867         int rc = 0;
868
869         OBD_ALLOC_PTR(msp);
870         if (!msp)
871                 return -ENOMEM;
872
873         strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
874         rc = obd_set_info_async(mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
875                                 sizeof(struct mgs_send_param), msp, NULL);
876         if (rc)
877                 CERROR("Failed to set parameter: %d\n", rc);
878
879         OBD_FREE_PTR(msp);
880         return rc;
881 }
882
883 static char *ll_get_fsname(struct inode *inode)
884 {
885         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
886         char *ptr, *fsname;
887         int len;
888
889         OBD_ALLOC(fsname, MGS_PARAM_MAXLEN);
890         len = strlen(lsi->lsi_lmd->lmd_profile);
891         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
892         if (ptr && (strcmp(ptr, "-client") == 0))
893                 len -= 7;
894         strncpy(fsname, lsi->lsi_lmd->lmd_profile, len);
895         fsname[len] = '\0';
896
897         return fsname;
898 }
899
900 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
901                      int set_default)
902 {
903         struct ll_sb_info *sbi = ll_i2sbi(inode);
904         struct mdc_op_data data = { { 0 } };
905         struct ptlrpc_request *req = NULL;
906         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
907         struct obd_device *mgc = lsi->lsi_mgc;
908         char *fsname = NULL, *param = NULL;
909         int lum_size;
910
911         struct iattr attr = { 0 };
912         int rc = 0;
913
914         /*
915          * This is coming from userspace, so should be in
916          * local endian.  But the MDS would like it in little
917          * endian, so we swab it before we send it.
918          */
919         switch (lump->lmm_magic) {
920         case LOV_USER_MAGIC_V1: {
921                 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
922                         lustre_swab_lov_user_md_v1(lump);
923                 lum_size = sizeof(struct lov_user_md_v1);
924                 break;
925                 }
926         case LOV_USER_MAGIC_V3: {
927                 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
928                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lump);
929                 lum_size = sizeof(struct lov_user_md_v3);
930                 break;
931                 }
932         default: {
933                 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
934                                 " %#08x != %#08x nor %#08x\n",
935                                 lump->lmm_magic, LOV_USER_MAGIC_V1,
936                                 LOV_USER_MAGIC_V3);
937                 RETURN(-EINVAL);
938                 }
939         }
940
941         ll_prepare_mdc_op_data(&data, inode, NULL, NULL, 0, 0, NULL);
942
943         /* swabbing is done in lov_setstripe() on server side */
944         rc = mdc_setattr(sbi->ll_mdc_exp, &data,
945                          &attr, lump, lum_size, NULL, 0, &req);
946         if (rc) {
947                 ptlrpc_req_finished(req);
948                 if (rc != -EPERM && rc != -EACCES)
949                         CERROR("mdc_setattr fails: rc = %d\n", rc);
950                 return rc;
951         }
952         ptlrpc_req_finished(req);
953
954         /* In the following we use the fact that LOV_USER_MAGIC_V1 and
955          LOV_USER_MAGIC_V3 have the same initial fields so we do not
956          need the make the distiction between the 2 versions */
957         if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
958                 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
959
960                 /* Get fsname and assume devname to be -MDT0000. */
961                 fsname = ll_get_fsname(inode);
962                 /* Set root stripesize */
963                 sprintf(param, "%s-MDT0000.lov.stripesize=%u", fsname,
964                         le32_to_cpu(lump->lmm_stripe_size));
965                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
966                 if (rc)
967                         goto end;
968
969                 /* Set root stripecount */
970                 sprintf(param, "%s-MDT0000.lov.stripecount=%u", fsname,
971                         le16_to_cpu(lump->lmm_stripe_count));
972                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
973                 if (rc)
974                         goto end;
975
976                 /* Set root stripeoffset */
977                 sprintf(param, "%s-MDT0000.lov.stripeoffset=%u", fsname,
978                         le16_to_cpu(lump->lmm_stripe_offset));
979                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
980                 if (rc)
981                         goto end;
982 end:
983                 if (fsname)
984                         OBD_FREE(fsname, MGS_PARAM_MAXLEN);
985                 if (param)
986                         OBD_FREE(param, MGS_PARAM_MAXLEN);
987         }
988         return rc;
989 }
990
991 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
992                      int *lmm_size, struct ptlrpc_request **request)
993 {
994         struct ll_sb_info *sbi = ll_i2sbi(inode);
995         struct ll_fid     fid;
996         struct mds_body   *body;
997         struct lov_mds_md *lmm = NULL;
998         struct ptlrpc_request *req = NULL;
999         int rc, lmmsize;
1000
1001         ll_inode2fid(&fid, inode);
1002
1003         rc = ll_get_max_mdsize(sbi, &lmmsize);
1004         if (rc)
1005                 RETURN(rc);
1006
1007         rc = mdc_getattr(sbi->ll_mdc_exp, &fid,
1008                         OBD_MD_FLEASIZE|OBD_MD_FLDIREA,
1009                         lmmsize, &req);
1010         if (rc < 0) {
1011                 CDEBUG(D_INFO, "mdc_getattr failed on inode "
1012                        "%lu/%u: rc %d\n", inode->i_ino,
1013                        inode->i_generation, rc);
1014                 GOTO(out, rc);
1015         }
1016         body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1017                         sizeof(*body));
1018         LASSERT(body != NULL); /* checked by mdc_getattr_name */
1019         /* swabbed by mdc_getattr_name */
1020         LASSERT(lustre_rep_swabbed(req, REPLY_REC_OFF));
1021
1022         lmmsize = body->eadatasize;
1023
1024         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
1025             lmmsize == 0) {
1026                 GOTO(out, rc = -ENODATA);
1027         }
1028
1029         lmm = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, lmmsize);
1030         LASSERT(lmm != NULL);
1031         LASSERT(lustre_rep_swabbed(req, REPLY_REC_OFF + 1));
1032
1033         /*
1034          * This is coming from the MDS, so is probably in
1035          * little endian.  We convert it to host endian before
1036          * passing it to userspace.
1037          */
1038         /* We don't swab objects for directories */
1039         switch (le32_to_cpu(lmm->lmm_magic)) {
1040         case LOV_MAGIC_V1:
1041                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
1042                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1043                 break;
1044         case LOV_MAGIC_V3:
1045                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
1046                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1047                 break;
1048         default:
1049                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
1050                 rc = -EPROTO;
1051         }
1052
1053 out:
1054         *lmmp = lmm;
1055         *lmm_size = lmmsize;
1056         *request = req;
1057         return rc;
1058 }
1059
1060 static int ll_dir_ioctl(struct inode *inode, struct file *file,
1061                         unsigned int cmd, unsigned long arg)
1062 {
1063         struct ll_sb_info *sbi = ll_i2sbi(inode);
1064         struct obd_ioctl_data *data;
1065         ENTRY;
1066
1067         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
1068                inode->i_ino, inode->i_generation, inode, cmd);
1069
1070         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1071         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1072                 return -ENOTTY;
1073
1074         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1075         switch(cmd) {
1076         case EXT3_IOC_GETFLAGS:
1077         case EXT3_IOC_SETFLAGS:
1078                 RETURN(ll_iocontrol(inode, file, cmd, arg));
1079         case EXT3_IOC_GETVERSION_OLD:
1080         case EXT3_IOC_GETVERSION:
1081                 RETURN(put_user(inode->i_generation, (int *)arg));
1082         /* We need to special case any other ioctls we want to handle,
1083          * to send them to the MDS/OST as appropriate and to properly
1084          * network encode the arg field.
1085         case EXT3_IOC_SETVERSION_OLD:
1086         case EXT3_IOC_SETVERSION:
1087         */
1088         case IOC_MDC_LOOKUP: {
1089                 struct ptlrpc_request *request = NULL;
1090                 struct ll_fid fid;
1091                 char *buf = NULL;
1092                 char *filename;
1093                 int namelen, rc, len = 0;
1094
1095                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1096                 if (rc)
1097                         RETURN(rc);
1098                 data = (void *)buf;
1099
1100                 filename = data->ioc_inlbuf1;
1101                 namelen = data->ioc_inllen1;
1102
1103                 if (namelen < 1) {
1104                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1105                         GOTO(out, rc = -EINVAL);
1106                 }
1107
1108                 ll_inode2fid(&fid, inode);
1109                 rc = mdc_getattr_name(sbi->ll_mdc_exp, &fid, filename, namelen,
1110                                       OBD_MD_FLID, 0, &request);
1111                 if (rc < 0) {
1112                         CDEBUG(D_INFO, "mdc_getattr_name: %d\n", rc);
1113                         GOTO(out, rc);
1114                 }
1115
1116                 ptlrpc_req_finished(request);
1117
1118                 EXIT;
1119         out:
1120                 obd_ioctl_freedata(buf, len);
1121                 return rc;
1122         }
1123         case LL_IOC_LOV_SETSTRIPE: {
1124                 struct lov_user_md_v3 lumv3;
1125                 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1126                 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1127                 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1128
1129                 int rc = 0;
1130                 int set_default = 0;
1131
1132                 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1133                 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1134                         sizeof(lumv3p->lmm_objects[0]));
1135
1136                 /* first try with v1 which is smaller than v3 */
1137                 rc = copy_from_user(lumv1, lumv1p, sizeof(*lumv1));
1138                 if (rc)
1139                         return(-EFAULT);
1140
1141                 if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
1142                         rc = copy_from_user(&lumv3, lumv3p, sizeof(lumv3));
1143                         if (rc)
1144                                 RETURN(-EFAULT);
1145                 }
1146
1147                 if (inode->i_sb->s_root == file->f_dentry)
1148                         set_default = 1;
1149
1150                 /* in v1 and v3 cases lumv1 points to data */
1151                 rc = ll_dir_setstripe(inode, lumv1, set_default);
1152
1153                 return rc;
1154         }
1155         case LL_IOC_OBD_STATFS:
1156                 RETURN(ll_obd_statfs(inode, (void *)arg));
1157         case LL_IOC_LOV_GETSTRIPE:
1158         case LL_IOC_MDC_GETINFO:
1159         case IOC_MDC_GETFILEINFO:
1160         case IOC_MDC_GETFILESTRIPE: {
1161                 struct ptlrpc_request *request = NULL;
1162                 struct mds_body *body;
1163                 struct lov_user_md *lump;
1164                 struct lov_mds_md *lmm = NULL;
1165                 char *filename = NULL;
1166                 int rc, lmmsize;
1167
1168                 if (cmd == IOC_MDC_GETFILEINFO ||
1169                     cmd == IOC_MDC_GETFILESTRIPE) {
1170                         filename = getname((const char *)arg);
1171                         if (IS_ERR(filename))
1172                                 RETURN(PTR_ERR(filename));
1173
1174                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1175                                                       &lmmsize, &request);
1176                 } else {
1177                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
1178                 }
1179
1180                 if (request) {
1181                         body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF,
1182                                               sizeof(*body));
1183                         LASSERT(body != NULL); /* checked by mdc_getattr_name */
1184                         /* swabbed by mdc_getattr_name */
1185                         LASSERT(lustre_rep_swabbed(request, REPLY_REC_OFF));
1186                 } else {
1187                         GOTO(out_req, rc);
1188                 }
1189
1190                 if (rc < 0) {
1191                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1192                                                cmd == LL_IOC_MDC_GETINFO))
1193                                 GOTO(skip_lmm, rc = 0);
1194                         else
1195                                 GOTO(out_req, rc);
1196                 }
1197
1198                 if (cmd == IOC_MDC_GETFILESTRIPE ||
1199                     cmd == LL_IOC_LOV_GETSTRIPE) {
1200                         lump = (struct lov_user_md *)arg;
1201                 } else {
1202                         struct lov_user_mds_data *lmdp;
1203                         lmdp = (struct lov_user_mds_data *)arg;
1204                         lump = &lmdp->lmd_lmm;
1205                 }
1206                 rc = copy_to_user(lump, lmm, lmmsize);
1207                 if (rc)
1208                         GOTO(out_lmm, rc = -EFAULT);
1209         skip_lmm:
1210                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1211                         struct lov_user_mds_data *lmdp;
1212                         lstat_t st = { 0 };
1213
1214                         st.st_dev     = inode->i_sb->s_dev;
1215                         st.st_mode    = body->mode;
1216                         st.st_nlink   = body->nlink;
1217                         st.st_uid     = body->uid;
1218                         st.st_gid     = body->gid;
1219                         st.st_rdev    = body->rdev;
1220                         st.st_size    = body->size;
1221                         st.st_blksize = CFS_PAGE_SIZE;
1222                         st.st_blocks  = body->blocks;
1223                         st.st_atime   = body->atime;
1224                         st.st_mtime   = body->mtime;
1225                         st.st_ctime   = body->ctime;
1226                         st.st_ino     = body->ino;
1227
1228                         lmdp = (struct lov_user_mds_data *)arg;
1229                         rc = copy_to_user(&lmdp->lmd_st, &st, sizeof(st));
1230                         if (rc)
1231                                 GOTO(out_lmm, rc = -EFAULT);
1232                 }
1233
1234                 EXIT;
1235         out_lmm:
1236                 if (lmm && lmm->lmm_magic == LOV_MAGIC_JOIN)
1237                         OBD_FREE(lmm, lmmsize);
1238         out_req:
1239                 ptlrpc_req_finished(request);
1240                 if (filename)
1241                         putname(filename);
1242                 return rc;
1243         }
1244         case IOC_LOV_GETINFO: {
1245                 struct lov_user_mds_data *lumd;
1246                 struct lov_stripe_md *lsm;
1247                 struct lov_user_md *lum;
1248                 struct lov_mds_md *lmm;
1249                 int lmmsize;
1250                 lstat_t st;
1251                 int rc;
1252
1253                 lumd = (struct lov_user_mds_data *)arg;
1254                 lum = &lumd->lmd_lmm;
1255
1256                 rc = ll_get_max_mdsize(sbi, &lmmsize);
1257                 if (rc)
1258                         RETURN(rc);
1259
1260                 OBD_ALLOC(lmm, lmmsize);
1261                 rc = copy_from_user(lmm, lum, lmmsize);
1262                 if (rc)
1263                         GOTO(free_lmm, rc = -EFAULT);
1264
1265                 switch (lmm->lmm_magic) {
1266                 case LOV_USER_MAGIC_V1:
1267                         if (LOV_USER_MAGIC == cpu_to_le32(LOV_USER_MAGIC))
1268                                 break;
1269                         /* swab objects first so that stripes num will be sane */
1270                         lustre_swab_lov_user_md_objects(
1271                                 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1272                                 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1273                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1274                         break;
1275                 case LOV_USER_MAGIC_V3:
1276                         if (LOV_USER_MAGIC == cpu_to_le32(LOV_USER_MAGIC))
1277                                 break;
1278                         /* swab objects first so that stripes num will be sane */
1279                         lustre_swab_lov_user_md_objects(
1280                                 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1281                                 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1282                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1283                         break;
1284                 default:
1285                         GOTO(free_lmm, rc = -EINVAL);
1286                 }
1287
1288                 rc = obd_unpackmd(sbi->ll_osc_exp, &lsm, lmm, lmmsize);
1289                 if (rc < 0)
1290                         GOTO(free_lmm, rc = -ENOMEM);
1291
1292                 rc = obd_checkmd(sbi->ll_osc_exp, sbi->ll_mdc_exp, lsm);
1293                 if (rc)
1294                         GOTO(free_lsm, rc);
1295
1296                 /* Perform glimpse_size operation. */
1297                 memset(&st, 0, sizeof(st));
1298
1299                 rc = ll_glimpse_ioctl(sbi, lsm, &st);
1300                 if (rc)
1301                         GOTO(free_lsm, rc);
1302
1303                 rc = copy_to_user(&lumd->lmd_st, &st, sizeof(st));
1304                 if (rc)
1305                         GOTO(free_lsm, rc = -EFAULT);
1306
1307                 EXIT;
1308         free_lsm:
1309                 obd_free_memmd(sbi->ll_osc_exp, &lsm);
1310         free_lmm:
1311                 OBD_FREE(lmm, lmmsize);
1312                 return rc;
1313         }
1314         case OBD_IOC_LLOG_CATINFO: {
1315                 struct ptlrpc_request *req = NULL;
1316                 char *buf = NULL;
1317                 int rc, len = 0;
1318                 char *bufs[3] = { NULL }, *str;
1319                 int lens[3] = { sizeof(struct ptlrpc_body) };
1320                 int size[2] = { sizeof(struct ptlrpc_body) };
1321
1322                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1323                 if (rc)
1324                         RETURN(rc);
1325                 data = (void *)buf;
1326
1327                 if (!data->ioc_inlbuf1) {
1328                         obd_ioctl_freedata(buf, len);
1329                         RETURN(-EINVAL);
1330                 }
1331
1332                 lens[REQ_REC_OFF] = data->ioc_inllen1;
1333                 bufs[REQ_REC_OFF] = data->ioc_inlbuf1;
1334                 if (data->ioc_inllen2) {
1335                         lens[REQ_REC_OFF + 1] = data->ioc_inllen2;
1336                         bufs[REQ_REC_OFF + 1] = data->ioc_inlbuf2;
1337                 } else {
1338                         lens[REQ_REC_OFF + 1] = 0;
1339                         bufs[REQ_REC_OFF + 1] = NULL;
1340                 }
1341
1342                 req = ptlrpc_prep_req(sbi2mdc(sbi)->cl_import,
1343                                       LUSTRE_LOG_VERSION, LLOG_CATINFO, 3, lens,
1344                                       bufs);
1345                 if (!req)
1346                         GOTO(out_catinfo, rc = -ENOMEM);
1347
1348                 size[REPLY_REC_OFF] = data->ioc_plen1;
1349                 ptlrpc_req_set_repsize(req, 2, size);
1350
1351                 rc = ptlrpc_queue_wait(req);
1352                 str = lustre_msg_string(req->rq_repmsg, REPLY_REC_OFF,
1353                                         data->ioc_plen1);
1354                 if (!rc)
1355                         rc = copy_to_user(data->ioc_pbuf1, str,data->ioc_plen1);
1356                 ptlrpc_req_finished(req);
1357         out_catinfo:
1358                 obd_ioctl_freedata(buf, len);
1359                 RETURN(rc);
1360         }
1361         case OBD_IOC_QUOTACHECK: {
1362                 struct obd_quotactl *oqctl;
1363                 int rc, error = 0;
1364
1365                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1366                         RETURN(-EPERM);
1367
1368                 OBD_ALLOC_PTR(oqctl);
1369                 if (!oqctl)
1370                         RETURN(-ENOMEM);
1371                 oqctl->qc_type = arg;
1372                 rc = obd_quotacheck(sbi->ll_mdc_exp, oqctl);
1373                 if (rc < 0) {
1374                         CDEBUG(D_INFO, "mdc_quotacheck failed: rc %d\n", rc);
1375                         error = rc;
1376                 }
1377
1378                 rc = obd_quotacheck(sbi->ll_osc_exp, oqctl);
1379                 if (rc < 0)
1380                         CDEBUG(D_INFO, "osc_quotacheck failed: rc %d\n", rc);
1381
1382                 OBD_FREE_PTR(oqctl);
1383                 return error ?: rc;
1384         }
1385         case OBD_IOC_POLL_QUOTACHECK: {
1386                 struct if_quotacheck *check;
1387                 int rc;
1388
1389                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1390                         RETURN(-EPERM);
1391
1392                 OBD_ALLOC_PTR(check);
1393                 if (!check)
1394                         RETURN(-ENOMEM);
1395
1396                 rc = obd_iocontrol(cmd, sbi->ll_mdc_exp, 0, (void *)check,
1397                                    NULL);
1398                 if (rc) {
1399                         CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
1400                         if (copy_to_user((void *)arg, check, sizeof(*check)))
1401                                 rc = -EFAULT;
1402                         GOTO(out_poll, rc);
1403                 }
1404
1405                 rc = obd_iocontrol(cmd, sbi->ll_osc_exp, 0, (void *)check,
1406                                    NULL);
1407                 if (rc) {
1408                         CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
1409                         if (copy_to_user((void *)arg, check, sizeof(*check)))
1410                                 rc = -EFAULT;
1411                         GOTO(out_poll, rc);
1412                 }
1413         out_poll:
1414                 OBD_FREE_PTR(check);
1415                 RETURN(rc);
1416         }
1417         case OBD_IOC_QUOTACTL: {
1418                 struct if_quotactl *qctl;
1419                 struct obd_quotactl *oqctl;
1420
1421                 int cmd, type, id, rc = 0;
1422
1423                 OBD_ALLOC_PTR(qctl);
1424                 if (!qctl)
1425                         RETURN(-ENOMEM);
1426
1427                 OBD_ALLOC_PTR(oqctl);
1428                 if (!oqctl) {
1429                         OBD_FREE_PTR(qctl);
1430                         RETURN(-ENOMEM);
1431                 }
1432                 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1433                         GOTO(out_quotactl, rc = -EFAULT);
1434
1435                 cmd = qctl->qc_cmd;
1436                 type = qctl->qc_type;
1437                 id = qctl->qc_id;
1438                 switch (cmd) {
1439                 case LUSTRE_Q_INVALIDATE:
1440                 case LUSTRE_Q_FINVALIDATE:
1441                 case Q_QUOTAON:
1442                 case Q_QUOTAOFF:
1443                 case Q_SETQUOTA:
1444                 case Q_SETINFO:
1445                         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1446                                 GOTO(out_quotactl, rc = -EPERM);
1447                         break;
1448                 case Q_GETQUOTA:
1449                         if (((type == USRQUOTA && current->euid != id) ||
1450                              (type == GRPQUOTA && !in_egroup_p(id))) &&
1451                             !cfs_capable(CFS_CAP_SYS_ADMIN))
1452                                 GOTO(out_quotactl, rc = -EPERM);
1453
1454                         /* XXX: dqb_valid is borrowed as a flag to mark that
1455                          *      only mds quota is wanted */
1456                         if (qctl->qc_dqblk.dqb_valid) {
1457                                 qctl->obd_uuid = sbi->ll_mdc_exp->exp_obd->
1458                                                         u.cli.cl_target_uuid;
1459                                 qctl->qc_dqblk.dqb_valid = 0;
1460                         }
1461
1462                         break;
1463                 case Q_GETINFO:
1464                         break;
1465                 default:
1466                         CERROR("unsupported quotactl op: %#x\n", cmd);
1467                         GOTO(out_quotactl, -ENOTTY);
1468                 }
1469
1470                 QCTL_COPY(oqctl, qctl);
1471
1472                 if (qctl->obd_uuid.uuid[0]) {
1473                         struct obd_device *obd;
1474                         struct obd_uuid *uuid = &qctl->obd_uuid;
1475
1476                         obd = class_find_client_notype(uuid,
1477                                          &sbi->ll_osc_exp->exp_obd->obd_uuid);
1478                         if (!obd)
1479                                 GOTO(out_quotactl, rc = -ENOENT);
1480
1481                         if (cmd == Q_GETINFO)
1482                                 oqctl->qc_cmd = Q_GETOINFO;
1483                         else if (cmd == Q_GETQUOTA)
1484                                 oqctl->qc_cmd = Q_GETOQUOTA;
1485                         else
1486                                 GOTO(out_quotactl, rc = -EINVAL);
1487
1488                         if (sbi->ll_mdc_exp->exp_obd == obd) {
1489                                 rc = obd_quotactl(sbi->ll_mdc_exp, oqctl);
1490                         } else {
1491                                 int i;
1492                                 struct obd_export *exp;
1493                                 struct lov_obd *lov = &sbi->ll_osc_exp->
1494                                                             exp_obd->u.lov;
1495
1496                                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1497                                         if (!lov->lov_tgts[i] ||
1498                                             !lov->lov_tgts[i]->ltd_active)
1499                                                 continue;
1500                                         exp = lov->lov_tgts[i]->ltd_exp;
1501                                         if (exp->exp_obd == obd) {
1502                                                 rc = obd_quotactl(exp, oqctl);
1503                                                 break;
1504                                         }
1505                                 }
1506                         }
1507
1508                         oqctl->qc_cmd = cmd;
1509                         QCTL_COPY(qctl, oqctl);
1510
1511                         if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1512                                 rc = -EFAULT;
1513
1514                         GOTO(out_quotactl, rc);
1515                 }
1516
1517                 rc = obd_quotactl(sbi->ll_mdc_exp, oqctl);
1518                 if (rc && rc != -EBUSY && cmd == Q_QUOTAON) {
1519                         oqctl->qc_cmd = Q_QUOTAOFF;
1520                         obd_quotactl(sbi->ll_mdc_exp, oqctl);
1521                 }
1522
1523                 QCTL_COPY(qctl, oqctl);
1524
1525                 if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1526                         rc = -EFAULT;
1527         out_quotactl:
1528                 OBD_FREE_PTR(qctl);
1529                 OBD_FREE_PTR(oqctl);
1530                 RETURN(rc);
1531         }
1532         case OBD_IOC_GETNAME_OLD:
1533         case OBD_IOC_GETNAME: {
1534                 struct obd_device *obd = class_exp2obd(sbi->ll_osc_exp);
1535                 if (!obd)
1536                         RETURN(-EFAULT);
1537                 if (copy_to_user((void *)arg, obd->obd_name,
1538                                 strlen(obd->obd_name) + 1))
1539                         RETURN (-EFAULT);
1540                 RETURN(0);
1541         }
1542         default:
1543                 RETURN(obd_iocontrol(cmd, sbi->ll_osc_exp,0,NULL,(void *)arg));
1544         }
1545 }
1546
1547 struct file_operations ll_dir_operations = {
1548         .open     = ll_file_open,
1549         .release  = ll_file_release,
1550         .read     = generic_read_dir,
1551         .readdir  = ll_readdir,
1552         .ioctl    = ll_dir_ioctl
1553 };