Whamcloud - gitweb
b=20581 MDS returns full hash for readdir to decrease hash collision
[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 (c) 2002, 2010, Oracle and/or its affiliates. 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 <lustre_fid.h>
58 #include "llite_internal.h"
59
60 #ifndef HAVE_PAGE_CHECKED
61 #ifdef HAVE_PG_FS_MISC
62 #define PageChecked(page)        test_bit(PG_fs_misc, &(page)->flags)
63 #define SetPageChecked(page)     set_bit(PG_fs_misc, &(page)->flags)
64 #else
65 #error PageChecked or PageFsMisc not defined in kernel
66 #endif
67 #endif
68
69 /*
70  * (new) readdir implementation overview.
71  *
72  * Original lustre readdir implementation cached exact copy of raw directory
73  * pages on the client. These pages were indexed in client page cache by
74  * logical offset in the directory file. This design, while very simple and
75  * intuitive had some inherent problems:
76  *
77  *     . it implies that byte offset to the directory entry serves as a
78  *     telldir(3)/seekdir(3) cookie, but that offset is not stable: in
79  *     ext3/htree directory entries may move due to splits, and more
80  *     importantly,
81  *
82  *     . it is incompatible with the design of split directories for cmd3,
83  *     that assumes that names are distributed across nodes based on their
84  *     hash, and so readdir should be done in hash order.
85  *
86  * New readdir implementation does readdir in hash order, and uses hash of a
87  * file name as a telldir/seekdir cookie. This led to number of complications:
88  *
89  *     . hash is not unique, so it cannot be used to index cached directory
90  *     pages on the client (note, that it requires a whole pageful of hash
91  *     collided entries to cause two pages to have identical hashes);
92  *
93  *     . hash is not unique, so it cannot, strictly speaking, be used as an
94  *     entry cookie. ext3/htree has the same problem and lustre implementation
95  *     mimics their solution: seekdir(hash) positions directory at the first
96  *     entry with the given hash.
97  *
98  * Client side.
99  *
100  * 0. caching
101  *
102  * Client caches directory pages using hash of the first entry as an index. As
103  * noted above hash is not unique, so this solution doesn't work as is:
104  * special processing is needed for "page hash chains" (i.e., sequences of
105  * pages filled with entries all having the same hash value).
106  *
107  * First, such chains have to be detected. To this end, server returns to the
108  * client the hash of the first entry on the page next to one returned. When
109  * client detects that this hash is the same as hash of the first entry on the
110  * returned page, page hash collision has to be handled. Pages in the
111  * hash chain, except first one, are termed "overflow pages".
112  *
113  * Solution to index uniqueness problem is to not cache overflow
114  * pages. Instead, when page hash collision is detected, all overflow pages
115  * from emerging chain are immediately requested from the server and placed in
116  * a special data structure (struct ll_dir_chain). This data structure is used
117  * by ll_readdir() to process entries from overflow pages. When readdir
118  * invocation finishes, overflow pages are discarded. If page hash collision
119  * chain weren't completely processed, next call to readdir will again detect
120  * page hash collision, again read overflow pages in, process next portion of
121  * entries and again discard the pages. This is not as wasteful as it looks,
122  * because, given reasonable hash, page hash collisions are extremely rare.
123  *
124  * 1. directory positioning
125  *
126  * When seekdir(hash) is called, original
127  *
128  *
129  *
130  *
131  *
132  *
133  *
134  *
135  * Server.
136  *
137  * identification of and access to overflow pages
138  *
139  * page format
140  *
141  *
142  *
143  *
144  *
145  */
146
147 /* returns the page unlocked, but with a reference */
148 static int ll_dir_readpage(struct file *file, struct page *page)
149 {
150         struct inode *inode = page->mapping->host;
151         struct ptlrpc_request *request;
152         struct mdt_body *body;
153         struct obd_capa *oc;
154         __u64 hash;
155         int rc;
156         ENTRY;
157
158         if (file) {
159                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
160
161                 hash = fd->fd_dir.lfd_next;
162         } else {
163                 struct ll_inode_info *lli = ll_i2info(inode);
164
165                 cfs_spin_lock(&lli->lli_sa_lock);
166                 if (lli->lli_sai)
167                         LASSERT(lli->lli_sai->sai_pid == cfs_curproc_pid());
168                 else
169                         LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
170                 hash = lli->lli_sa_pos;
171                 cfs_spin_unlock(&lli->lli_sa_lock);
172         }
173         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) off %lu\n",
174                inode->i_ino, inode->i_generation, inode, (unsigned long)hash);
175
176         oc = ll_mdscapa_get(inode);
177         rc = md_readpage(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode),
178                          oc, hash, page, &request);
179         capa_put(oc);
180         if (!rc) {
181                 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
182                 /* Checked by mdc_readpage() */
183                 LASSERT(body != NULL);
184
185                 if (body->valid & OBD_MD_FLSIZE)
186                         cl_isize_write(inode, body->size);
187                 SetPageUptodate(page);
188         }
189         ptlrpc_req_finished(request);
190
191         unlock_page(page);
192         EXIT;
193         return rc;
194 }
195
196 struct address_space_operations ll_dir_aops = {
197         .readpage  = ll_dir_readpage,
198 };
199
200 static void ll_check_page(struct inode *dir, struct page *page)
201 {
202         /* XXX: check page format later */
203         SetPageChecked(page);
204 }
205
206 static void ll_release_page(struct page *page, __u64 hash,
207                             __u64 start, __u64 end)
208 {
209         kunmap(page);
210         lock_page(page);
211         if (likely(page->mapping != NULL)) {
212                 ll_truncate_complete_page(page);
213                 unlock_page(page);
214         } else {
215                 unlock_page(page);
216                 CWARN("NULL mapping page %p, truncated by others: "
217                       "hash("LPX64") | start("LPX64") | end("LPX64")\n",
218                       page, hash, start, end);
219         }
220         page_cache_release(page);
221 }
222
223 /*
224  * Find, kmap and return page that contains given hash.
225  */
226 static struct page *ll_dir_page_locate(struct inode *dir, __u64 *hash,
227                                        __u64 *start, __u64 *end)
228 {
229         struct address_space *mapping = dir->i_mapping;
230         /*
231          * Complement of hash is used as an index so that
232          * radix_tree_gang_lookup() can be used to find a page with starting
233          * hash _smaller_ than one we are looking for.
234          */
235         unsigned long offset = hash_x_index(*hash);
236         struct page *page;
237         int found;
238
239         TREE_READ_LOCK_IRQ(mapping);
240         found = radix_tree_gang_lookup(&mapping->page_tree,
241                                        (void **)&page, offset, 1);
242         if (found > 0) {
243                 struct lu_dirpage *dp;
244
245                 page_cache_get(page);
246                 TREE_READ_UNLOCK_IRQ(mapping);
247                 /*
248                  * In contrast to find_lock_page() we are sure that directory
249                  * page cannot be truncated (while DLM lock is held) and,
250                  * hence, can avoid restart.
251                  *
252                  * In fact, page cannot be locked here at all, because
253                  * ll_dir_readpage() does synchronous io.
254                  */
255                 wait_on_page(page);
256                 if (PageUptodate(page)) {
257                         dp = kmap(page);
258 #if BITS_PER_LONG == 32
259                         *start = le64_to_cpu(dp->ldp_hash_start) >> 32;
260                         *end   = le64_to_cpu(dp->ldp_hash_end) >> 32;
261                         *hash  = *hash >> 32;
262 #else
263                         *start = le64_to_cpu(dp->ldp_hash_start);
264                         *end   = le64_to_cpu(dp->ldp_hash_end);
265 #endif
266                         LASSERTF(*start <= *hash, "start = "LPX64",end = "
267                                  LPX64",hash = "LPX64"\n", *start, *end, *hash);
268                         if (*hash > *end || (*end != *start && *hash == *end)) {
269                                 ll_release_page(page, *hash, *start, *end);
270                                 page = NULL;
271                         }
272                 } else {
273                         page_cache_release(page);
274                         page = ERR_PTR(-EIO);
275                 }
276
277         } else {
278                 TREE_READ_UNLOCK_IRQ(mapping);
279                 page = NULL;
280         }
281         return page;
282 }
283
284 struct page *ll_get_dir_page(struct file *filp, struct inode *dir, __u64 hash,
285                              int exact, struct ll_dir_chain *chain)
286 {
287         ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
288         struct address_space *mapping = dir->i_mapping;
289         struct lustre_handle lockh;
290         struct lu_dirpage *dp;
291         struct page *page;
292         ldlm_mode_t mode;
293         int rc;
294         __u64 start = 0;
295         __u64 end = 0;
296         __u64 lhash = hash;
297         struct ll_inode_info *lli = ll_i2info(dir);
298
299         mode = LCK_PR;
300         rc = md_lock_match(ll_i2sbi(dir)->ll_md_exp, LDLM_FL_BLOCK_GRANTED,
301                            ll_inode2fid(dir), LDLM_IBITS, &policy, mode, &lockh);
302         if (!rc) {
303                 struct ldlm_enqueue_info einfo = { LDLM_IBITS, mode,
304                        ll_md_blocking_ast, ldlm_completion_ast,
305                        NULL, NULL, dir };
306                 struct lookup_intent it = { .it_op = IT_READDIR };
307                 struct ptlrpc_request *request;
308                 struct md_op_data *op_data;
309
310                 op_data = ll_prep_md_op_data(NULL, dir, NULL, NULL, 0, 0,
311                                              LUSTRE_OPC_ANY, NULL);
312                 if (IS_ERR(op_data))
313                         return (void *)op_data;
314
315                 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, &einfo, &it,
316                                 op_data, &lockh, NULL, 0, NULL, 0);
317
318                 ll_finish_md_op_data(op_data);
319
320                 request = (struct ptlrpc_request *)it.d.lustre.it_data;
321                 if (request)
322                         ptlrpc_req_finished(request);
323                 if (rc < 0) {
324                         CERROR("lock enqueue: "DFID" at "LPU64": rc %d\n",
325                                PFID(ll_inode2fid(dir)), hash, rc);
326                         return ERR_PTR(rc);
327                 }
328         } else {
329                 /* for cross-ref object, l_ast_data of the lock may not be set,
330                  * we reset it here */
331                 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp, &lockh.cookie,
332                                  dir, NULL);
333         }
334         ldlm_lock_dump_handle(D_OTHER, &lockh);
335
336         cfs_down(&lli->lli_readdir_sem);
337         page = ll_dir_page_locate(dir, &lhash, &start, &end);
338         if (IS_ERR(page)) {
339                 CERROR("dir page locate: "DFID" at "LPU64": rc %ld\n",
340                        PFID(ll_inode2fid(dir)), lhash, PTR_ERR(page));
341                 GOTO(out_unlock, page);
342         }
343
344         if (page != NULL) {
345                 /*
346                  * XXX nikita: not entirely correct handling of a corner case:
347                  * suppose hash chain of entries with hash value HASH crosses
348                  * border between pages P0 and P1. First both P0 and P1 are
349                  * cached, seekdir() is called for some entry from the P0 part
350                  * of the chain. Later P0 goes out of cache. telldir(HASH)
351                  * happens and finds P1, as it starts with matching hash
352                  * value. Remaining entries from P0 part of the chain are
353                  * skipped. (Is that really a bug?)
354                  *
355                  * Possible solutions: 0. don't cache P1 is such case, handle
356                  * it as an "overflow" page. 1. invalidate all pages at
357                  * once. 2. use HASH|1 as an index for P1.
358                  */
359                 if (exact && lhash != start) {
360                         /*
361                          * readdir asked for a page starting _exactly_ from
362                          * given hash, but cache contains stale page, with
363                          * entries with smaller hash values. Stale page should
364                          * be invalidated, and new one fetched.
365                          */
366                         CDEBUG(D_OTHER, "Stale readpage page %p: "
367                                "start = "LPX64",end = "LPX64"hash ="LPX64"\n",
368                                page, start, end, lhash);
369                         ll_release_page(page, lhash, start, end);
370                 } else {
371                         GOTO(hash_collision, page);
372                 }
373         }
374
375         page = read_cache_page(mapping, hash_x_index(hash),
376                                (filler_t*)mapping->a_ops->readpage, filp);
377         if (IS_ERR(page)) {
378                 CERROR("read cache page: "DFID" at "LPU64": rc %ld\n",
379                        PFID(ll_inode2fid(dir)), hash, PTR_ERR(page));
380                 GOTO(out_unlock, page);
381         }
382
383         wait_on_page(page);
384         (void)kmap(page);
385         if (!PageUptodate(page)) {
386                 CERROR("page not updated: "DFID" at "LPU64": rc %d\n",
387                        PFID(ll_inode2fid(dir)), hash, -5);
388                 goto fail;
389         }
390         if (!PageChecked(page))
391                 ll_check_page(dir, page);
392         if (PageError(page)) {
393                 CERROR("page error: "DFID" at "LPU64": rc %d\n",
394                        PFID(ll_inode2fid(dir)), hash, -5);
395                 goto fail;
396         }
397 hash_collision:
398         dp = page_address(page);
399 #if BITS_PER_LONG == 32
400         start = le64_to_cpu(dp->ldp_hash_start) >> 32;
401         end   = le64_to_cpu(dp->ldp_hash_end) >> 32;
402         lhash = hash >> 32;
403 #else
404         start = le64_to_cpu(dp->ldp_hash_start);
405         end   = le64_to_cpu(dp->ldp_hash_end);
406         lhash = hash;
407 #endif
408         if (end == start) {
409                 LASSERT(start == lhash);
410                 CWARN("Page-wide hash collision: "LPU64"\n", end);
411 #if BITS_PER_LONG == 32
412                 CWARN("Real page-wide hash collision at ["LPU64" "LPU64"] with "
413                       "hash "LPU64"\n", le64_to_cpu(dp->ldp_hash_start),
414                       le64_to_cpu(dp->ldp_hash_end), hash);
415 #endif
416                 /*
417                  * Fetch whole overflow chain...
418                  *
419                  * XXX not yet.
420                  */
421                 goto fail;
422         }
423 out_unlock:
424         cfs_up(&lli->lli_readdir_sem);
425         ldlm_lock_decref(&lockh, mode);
426         return page;
427
428 fail:
429         ll_put_page(page);
430         page = ERR_PTR(-EIO);
431         goto out_unlock;
432 }
433
434 int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
435 {
436         struct inode         *inode = filp->f_dentry->d_inode;
437         struct ll_inode_info *info  = ll_i2info(inode);
438         struct ll_sb_info    *sbi   = ll_i2sbi(inode);
439         struct ll_file_data  *fd    = LUSTRE_FPRIVATE(filp);
440         __u64                 pos   = fd->fd_dir.lfd_pos;
441         struct page          *page;
442         struct ll_dir_chain   chain;
443         int rc, need_32bit;
444         int done;
445         int shift;
446         __u16 type;
447         ENTRY;
448
449         need_32bit = ll_need_32bit_api(sbi);
450         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu 32bit_api %d\n",
451                inode->i_ino, inode->i_generation, inode,
452                (unsigned long)pos, i_size_read(inode), need_32bit);
453
454         if (pos == DIR_END_OFF)
455                 /*
456                  * end-of-file.
457                  */
458                 RETURN(0);
459
460         rc    = 0;
461         done  = 0;
462         shift = 0;
463         ll_dir_chain_init(&chain);
464
465         fd->fd_dir.lfd_next = pos;
466         page = ll_get_dir_page(filp, inode, pos, 0, &chain);
467
468         while (rc == 0 && !done) {
469                 struct lu_dirpage *dp;
470                 struct lu_dirent  *ent;
471
472                 if (!IS_ERR(page)) {
473                         /*
474                          * If page is empty (end of directory is reached),
475                          * use this value.
476                          */
477                         __u64 hash = DIR_END_OFF;
478                         __u64 next;
479
480                         dp = page_address(page);
481                         for (ent = lu_dirent_start(dp); ent != NULL && !done;
482                              ent = lu_dirent_next(ent)) {
483                                 char          *name;
484                                 int            namelen;
485                                 struct lu_fid  fid;
486                                 __u64          ino;
487                                 __u64          lhash;
488
489                                 /*
490                                  * XXX: implement correct swabbing here.
491                                  */
492
493                                 hash = le64_to_cpu(ent->lde_hash);
494                                 if (hash < pos)
495                                         /*
496                                          * Skip until we find target hash
497                                          * value.
498                                          */
499                                         continue;
500
501                                 namelen = le16_to_cpu(ent->lde_namelen);
502                                 if (namelen == 0)
503                                         /*
504                                          * Skip dummy record.
505                                          */
506                                         continue;
507
508                                 name = ent->lde_name;
509                                 fid_le_to_cpu(&fid, &ent->lde_fid);
510                                 if (need_32bit) {
511                                         lhash = hash >> 32;
512                                         ino = cl_fid_build_ino32(&fid);
513                                 } else {
514                                         lhash = hash;
515                                         ino = cl_fid_build_ino(&fid);
516                                 }
517                                 type = ll_dirent_type_get(ent);
518                                 done = filldir(cookie, name, namelen,
519                                                lhash, ino, type);
520                         }
521                         next = le64_to_cpu(dp->ldp_hash_end);
522                         ll_put_page(page);
523                         if (!done) {
524                                 pos = next;
525                                 if (pos == DIR_END_OFF) {
526                                         /*
527                                          * End of directory reached.
528                                          */
529                                         done = 1;
530                                 } else if (1 /* chain is exhausted*/) {
531                                         /*
532                                          * Normal case: continue to the next
533                                          * page.
534                                          */
535                                         fd->fd_dir.lfd_next = pos;
536                                         page = ll_get_dir_page(filp, inode, pos,
537                                                                1, &chain);
538                                 } else {
539                                         /*
540                                          * go into overflow page.
541                                          */
542                                 }
543                         } else {
544                                 pos = hash;
545                         }
546                 } else {
547                         rc = PTR_ERR(page);
548                         CERROR("error reading dir "DFID" at %lu: rc %d\n",
549                                PFID(&info->lli_fid), (unsigned long)pos, rc);
550                 }
551         }
552
553         fd->fd_dir.lfd_pos = pos;
554         if (need_32bit)
555                 filp->f_pos = pos >> 32;
556         else
557                 filp->f_pos = pos;
558         filp->f_version = inode->i_version;
559         touch_atime(filp->f_vfsmnt, filp->f_dentry);
560
561         ll_dir_chain_fini(&chain);
562
563         RETURN(rc);
564 }
565
566 int ll_send_mgc_param(struct obd_export *mgc, char *string)
567 {
568         struct mgs_send_param *msp;
569         int rc = 0;
570
571         OBD_ALLOC_PTR(msp);
572         if (!msp)
573                 return -ENOMEM;
574
575         strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
576         rc = obd_set_info_async(mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
577                                 sizeof(struct mgs_send_param), msp, NULL);
578         if (rc)
579                 CERROR("Failed to set parameter: %d\n", rc);
580         OBD_FREE_PTR(msp);
581
582         return rc;
583 }
584
585 char *ll_get_fsname(struct inode *inode)
586 {
587         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
588         char *ptr, *fsname;
589         int len;
590
591         OBD_ALLOC(fsname, MGS_PARAM_MAXLEN);
592         len = strlen(lsi->lsi_lmd->lmd_profile);
593         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
594         if (ptr && (strcmp(ptr, "-client") == 0))
595                 len -= 7;
596         strncpy(fsname, lsi->lsi_lmd->lmd_profile, len);
597         fsname[len] = '\0';
598
599         return fsname;
600 }
601
602 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
603                      int set_default)
604 {
605         struct ll_sb_info *sbi = ll_i2sbi(inode);
606         struct md_op_data *op_data;
607         struct ptlrpc_request *req = NULL;
608         int rc = 0;
609         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
610         struct obd_device *mgc = lsi->lsi_mgc;
611         char *fsname = NULL, *param = NULL;
612         int lum_size;
613
614         if (lump != NULL) {
615                 /*
616                  * This is coming from userspace, so should be in
617                  * local endian.  But the MDS would like it in little
618                  * endian, so we swab it before we send it.
619                  */
620                 switch (lump->lmm_magic) {
621                 case LOV_USER_MAGIC_V1: {
622                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
623                                 lustre_swab_lov_user_md_v1(lump);
624                         lum_size = sizeof(struct lov_user_md_v1);
625                         break;
626                         }
627                 case LOV_USER_MAGIC_V3: {
628                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
629                                 lustre_swab_lov_user_md_v3(
630                                         (struct lov_user_md_v3 *)lump);
631                         lum_size = sizeof(struct lov_user_md_v3);
632                         break;
633                         }
634                 default: {
635                         CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
636                                         " %#08x != %#08x nor %#08x\n",
637                                         lump->lmm_magic, LOV_USER_MAGIC_V1,
638                                         LOV_USER_MAGIC_V3);
639                         RETURN(-EINVAL);
640                         }
641                }
642         } else {
643                 lum_size = sizeof(struct lov_user_md_v1);
644         }
645
646         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
647                                      LUSTRE_OPC_ANY, NULL);
648         if (IS_ERR(op_data))
649                 RETURN(PTR_ERR(op_data));
650
651         /* swabbing is done in lov_setstripe() on server side */
652         rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size,
653                         NULL, 0, &req, NULL);
654         ll_finish_md_op_data(op_data);
655         ptlrpc_req_finished(req);
656         if (rc) {
657                 if (rc != -EPERM && rc != -EACCES)
658                         CERROR("mdc_setattr fails: rc = %d\n", rc);
659         }
660
661         /* In the following we use the fact that LOV_USER_MAGIC_V1 and
662          LOV_USER_MAGIC_V3 have the same initial fields so we do not
663          need the make the distiction between the 2 versions */
664         if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
665                 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
666
667                 /* Get fsname and assume devname to be -MDT0000. */
668                 fsname = ll_get_fsname(inode);
669                 /* Set root stripesize */
670                 sprintf(param, "%s-MDT0000.lov.stripesize=%u", fsname,
671                         lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
672                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
673                 if (rc)
674                         goto end;
675
676                 /* Set root stripecount */
677                 sprintf(param, "%s-MDT0000.lov.stripecount=%hd", fsname,
678                         lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
679                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
680                 if (rc)
681                         goto end;
682
683                 /* Set root stripeoffset */
684                 sprintf(param, "%s-MDT0000.lov.stripeoffset=%hd", fsname,
685                         lump ? le16_to_cpu(lump->lmm_stripe_offset) :
686                         (typeof(lump->lmm_stripe_offset))(-1));
687                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
688                 if (rc)
689                         goto end;
690 end:
691                 if (fsname)
692                         OBD_FREE(fsname, MGS_PARAM_MAXLEN);
693                 if (param)
694                         OBD_FREE(param, MGS_PARAM_MAXLEN);
695         }
696         return rc;
697 }
698
699 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
700                      int *lmm_size, struct ptlrpc_request **request)
701 {
702         struct ll_sb_info *sbi = ll_i2sbi(inode);
703         struct mdt_body   *body;
704         struct lov_mds_md *lmm = NULL;
705         struct ptlrpc_request *req = NULL;
706         int rc, lmmsize;
707         struct md_op_data *op_data;
708
709         rc = ll_get_max_mdsize(sbi, &lmmsize);
710         if (rc)
711                 RETURN(rc);
712
713         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
714                                      0, lmmsize, LUSTRE_OPC_ANY,
715                                      NULL);
716         if (op_data == NULL)
717                 RETURN(-ENOMEM);
718
719         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
720         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
721         ll_finish_md_op_data(op_data);
722         if (rc < 0) {
723                 CDEBUG(D_INFO, "md_getattr failed on inode "
724                        "%lu/%u: rc %d\n", inode->i_ino,
725                        inode->i_generation, rc);
726                 GOTO(out, rc);
727         }
728
729         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
730         LASSERT(body != NULL);
731
732         lmmsize = body->eadatasize;
733
734         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
735             lmmsize == 0) {
736                 GOTO(out, rc = -ENODATA);
737         }
738
739         lmm = req_capsule_server_sized_get(&req->rq_pill,
740                                            &RMF_MDT_MD, lmmsize);
741         LASSERT(lmm != NULL);
742
743         /*
744          * This is coming from the MDS, so is probably in
745          * little endian.  We convert it to host endian before
746          * passing it to userspace.
747          */
748         /* We don't swab objects for directories */
749         switch (le32_to_cpu(lmm->lmm_magic)) {
750         case LOV_MAGIC_V1:
751                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
752                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
753                 break;
754         case LOV_MAGIC_V3:
755                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
756                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
757                 break;
758         default:
759                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
760                 rc = -EPROTO;
761         }
762 out:
763         *lmmp = lmm;
764         *lmm_size = lmmsize;
765         *request = req;
766         return rc;
767 }
768
769 /*
770  *  Get MDT index for the inode.
771  */
772 int ll_get_mdt_idx(struct inode *inode)
773 {
774         struct ll_sb_info *sbi = ll_i2sbi(inode);
775         struct md_op_data *op_data;
776         int rc, mdtidx;
777         ENTRY;
778
779         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0,
780                                      0, LUSTRE_OPC_ANY, NULL);
781         if (op_data == NULL)
782                 RETURN(-ENOMEM);
783
784         op_data->op_valid |= OBD_MD_MDTIDX;
785         rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
786         mdtidx = op_data->op_mds;
787         ll_finish_md_op_data(op_data);
788         if (rc < 0) {
789                 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
790                 RETURN(rc);
791         }
792         return mdtidx;
793 }
794
795 static int copy_and_ioctl(int cmd, struct obd_export *exp, void *data, int len)
796 {
797         void *ptr;
798         int rc;
799
800         OBD_ALLOC(ptr, len);
801         if (ptr == NULL)
802                 return -ENOMEM;
803         if (cfs_copy_from_user(ptr, data, len)) {
804                 OBD_FREE(ptr, len);
805                 return -EFAULT;
806         }
807         rc = obd_iocontrol(cmd, exp, len, data, NULL);
808         OBD_FREE(ptr, len);
809         return rc;
810 }
811
812 static int ll_dir_ioctl(struct inode *inode, struct file *file,
813                         unsigned int cmd, unsigned long arg)
814 {
815         struct ll_sb_info *sbi = ll_i2sbi(inode);
816         struct obd_ioctl_data *data;
817         int rc = 0;
818         ENTRY;
819
820         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
821                inode->i_ino, inode->i_generation, inode, cmd);
822
823         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
824         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
825                 return -ENOTTY;
826
827         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
828         switch(cmd) {
829         case FSFILT_IOC_GETFLAGS:
830         case FSFILT_IOC_SETFLAGS:
831                 RETURN(ll_iocontrol(inode, file, cmd, arg));
832         case FSFILT_IOC_GETVERSION_OLD:
833         case FSFILT_IOC_GETVERSION:
834                 RETURN(put_user(inode->i_generation, (int *)arg));
835         /* We need to special case any other ioctls we want to handle,
836          * to send them to the MDS/OST as appropriate and to properly
837          * network encode the arg field.
838         case FSFILT_IOC_SETVERSION_OLD:
839         case FSFILT_IOC_SETVERSION:
840         */
841         case LL_IOC_GET_MDTIDX: {
842                 int mdtidx;
843
844                 mdtidx = ll_get_mdt_idx(inode);
845                 if (mdtidx < 0)
846                         RETURN(mdtidx);
847
848                 if (put_user((int)mdtidx, (int*)arg))
849                         RETURN(-EFAULT);
850
851                 return 0;
852         }
853         case IOC_MDC_LOOKUP: {
854                 struct ptlrpc_request *request = NULL;
855                 int namelen, len = 0;
856                 char *buf = NULL;
857                 char *filename;
858                 struct md_op_data *op_data;
859
860                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
861                 if (rc)
862                         RETURN(rc);
863                 data = (void *)buf;
864
865                 filename = data->ioc_inlbuf1;
866                 namelen = strlen(filename);
867
868                 if (namelen < 1) {
869                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
870                         GOTO(out_free, rc = -EINVAL);
871                 }
872
873                 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename, namelen,
874                                              0, LUSTRE_OPC_ANY, NULL);
875                 if (op_data == NULL)
876                         GOTO(out_free, rc = -ENOMEM);
877
878                 op_data->op_valid = OBD_MD_FLID;
879                 rc = md_getattr_name(sbi->ll_md_exp, op_data, &request);
880                 ll_finish_md_op_data(op_data);
881                 if (rc < 0) {
882                         CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
883                         GOTO(out_free, rc);
884                 }
885                 ptlrpc_req_finished(request);
886                 EXIT;
887 out_free:
888                 obd_ioctl_freedata(buf, len);
889                 return rc;
890         }
891         case LL_IOC_LOV_SETSTRIPE: {
892                 struct lov_user_md_v3 lumv3;
893                 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
894                 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
895                 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
896
897                 int set_default = 0;
898
899                 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
900                 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
901                         sizeof(lumv3p->lmm_objects[0]));
902                 /* first try with v1 which is smaller than v3 */
903                 if (cfs_copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
904                         RETURN(-EFAULT);
905
906                 if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
907                         if (cfs_copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
908                                 RETURN(-EFAULT);
909                 }
910
911                 if (inode->i_sb->s_root == file->f_dentry)
912                         set_default = 1;
913
914                 /* in v1 and v3 cases lumv1 points to data */
915                 rc = ll_dir_setstripe(inode, lumv1, set_default);
916
917                 RETURN(rc);
918         }
919         case LL_IOC_OBD_STATFS:
920                 RETURN(ll_obd_statfs(inode, (void *)arg));
921         case LL_IOC_LOV_GETSTRIPE:
922         case LL_IOC_MDC_GETINFO:
923         case IOC_MDC_GETFILEINFO:
924         case IOC_MDC_GETFILESTRIPE: {
925                 struct ptlrpc_request *request = NULL;
926                 struct lov_user_md *lump;
927                 struct lov_mds_md *lmm = NULL;
928                 struct mdt_body *body;
929                 char *filename = NULL;
930                 int lmmsize;
931
932                 if (cmd == IOC_MDC_GETFILEINFO ||
933                     cmd == IOC_MDC_GETFILESTRIPE) {
934                         filename = getname((const char *)arg);
935                         if (IS_ERR(filename))
936                                 RETURN(PTR_ERR(filename));
937
938                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
939                                                       &lmmsize, &request);
940                 } else {
941                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
942                 }
943
944                 if (request) {
945                         body = req_capsule_server_get(&request->rq_pill,
946                                                       &RMF_MDT_BODY);
947                         LASSERT(body != NULL);
948                 } else {
949                         GOTO(out_req, rc);
950                 }
951
952                 if (rc < 0) {
953                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
954                                                cmd == LL_IOC_MDC_GETINFO))
955                                 GOTO(skip_lmm, rc = 0);
956                         else
957                                 GOTO(out_req, rc);
958                 }
959
960                 if (cmd == IOC_MDC_GETFILESTRIPE ||
961                     cmd == LL_IOC_LOV_GETSTRIPE) {
962                         lump = (struct lov_user_md *)arg;
963                 } else {
964                         struct lov_user_mds_data *lmdp;
965                         lmdp = (struct lov_user_mds_data *)arg;
966                         lump = &lmdp->lmd_lmm;
967                 }
968                 if (cfs_copy_to_user(lump, lmm, lmmsize)) {
969                         if (cfs_copy_to_user(lump, lmm, sizeof(*lump)))
970                                 GOTO(out_req, rc = -EFAULT);
971                         rc = -EOVERFLOW;
972                 }
973         skip_lmm:
974                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
975                         struct lov_user_mds_data *lmdp;
976                         lstat_t st = { 0 };
977
978                         st.st_dev     = inode->i_sb->s_dev;
979                         st.st_mode    = body->mode;
980                         st.st_nlink   = body->nlink;
981                         st.st_uid     = body->uid;
982                         st.st_gid     = body->gid;
983                         st.st_rdev    = body->rdev;
984                         st.st_size    = body->size;
985                         st.st_blksize = CFS_PAGE_SIZE;
986                         st.st_blocks  = body->blocks;
987                         st.st_atime   = body->atime;
988                         st.st_mtime   = body->mtime;
989                         st.st_ctime   = body->ctime;
990                         st.st_ino     = inode->i_ino;
991
992                         lmdp = (struct lov_user_mds_data *)arg;
993                         if (cfs_copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
994                                 GOTO(out_req, rc = -EFAULT);
995                 }
996
997                 EXIT;
998         out_req:
999                 ptlrpc_req_finished(request);
1000                 if (filename)
1001                         putname(filename);
1002                 return rc;
1003         }
1004         case IOC_LOV_GETINFO: {
1005                 struct lov_user_mds_data *lumd;
1006                 struct lov_stripe_md *lsm;
1007                 struct lov_user_md *lum;
1008                 struct lov_mds_md *lmm;
1009                 int lmmsize;
1010                 lstat_t st;
1011
1012                 lumd = (struct lov_user_mds_data *)arg;
1013                 lum = &lumd->lmd_lmm;
1014
1015                 rc = ll_get_max_mdsize(sbi, &lmmsize);
1016                 if (rc)
1017                         RETURN(rc);
1018
1019                 OBD_ALLOC_LARGE(lmm, lmmsize);
1020                 if (cfs_copy_from_user(lmm, lum, lmmsize))
1021                         GOTO(free_lmm, rc = -EFAULT);
1022
1023                 switch (lmm->lmm_magic) {
1024                 case LOV_USER_MAGIC_V1:
1025                         if (LOV_USER_MAGIC_V1 == cpu_to_le32(LOV_USER_MAGIC_V1))
1026                                 break;
1027                         /* swab objects first so that stripes num will be sane */
1028                         lustre_swab_lov_user_md_objects(
1029                                 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1030                                 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1031                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1032                         break;
1033                 case LOV_USER_MAGIC_V3:
1034                         if (LOV_USER_MAGIC_V3 == cpu_to_le32(LOV_USER_MAGIC_V3))
1035                                 break;
1036                         /* swab objects first so that stripes num will be sane */
1037                         lustre_swab_lov_user_md_objects(
1038                                 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1039                                 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1040                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1041                         break;
1042                 default:
1043                         GOTO(free_lmm, rc = -EINVAL);
1044                 }
1045
1046                 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
1047                 if (rc < 0)
1048                         GOTO(free_lmm, rc = -ENOMEM);
1049
1050                 /* Perform glimpse_size operation. */
1051                 memset(&st, 0, sizeof(st));
1052
1053                 rc = ll_glimpse_ioctl(sbi, lsm, &st);
1054                 if (rc)
1055                         GOTO(free_lsm, rc);
1056
1057                 if (cfs_copy_to_user(&lumd->lmd_st, &st, sizeof(st)))
1058                         GOTO(free_lsm, rc = -EFAULT);
1059
1060                 EXIT;
1061         free_lsm:
1062                 obd_free_memmd(sbi->ll_dt_exp, &lsm);
1063         free_lmm:
1064                 OBD_FREE_LARGE(lmm, lmmsize);
1065                 return rc;
1066         }
1067         case OBD_IOC_LLOG_CATINFO: {
1068                 struct ptlrpc_request *req = NULL;
1069                 char                  *buf = NULL;
1070                 char                  *str;
1071                 int                    len = 0;
1072
1073                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1074                 if (rc)
1075                         RETURN(rc);
1076                 data = (void *)buf;
1077
1078                 if (!data->ioc_inlbuf1) {
1079                         obd_ioctl_freedata(buf, len);
1080                         RETURN(-EINVAL);
1081                 }
1082
1083                 req = ptlrpc_request_alloc(sbi2mdc(sbi)->cl_import,
1084                                            &RQF_LLOG_CATINFO);
1085                 if (req == NULL)
1086                         GOTO(out_catinfo, rc = -ENOMEM);
1087
1088                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
1089                                      data->ioc_inllen1);
1090                 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_CLIENT,
1091                                      data->ioc_inllen2);
1092
1093                 rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION, LLOG_CATINFO);
1094                 if (rc) {
1095                         ptlrpc_request_free(req);
1096                         GOTO(out_catinfo, rc);
1097                 }
1098
1099                 str = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
1100                 memcpy(str, data->ioc_inlbuf1, data->ioc_inllen1);
1101                 if (data->ioc_inllen2) {
1102                         str = req_capsule_client_get(&req->rq_pill,
1103                                                      &RMF_STRING);
1104                         memcpy(str, data->ioc_inlbuf2, data->ioc_inllen2);
1105                 }
1106
1107                 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_SERVER,
1108                                      data->ioc_plen1);
1109                 ptlrpc_request_set_replen(req);
1110
1111                 rc = ptlrpc_queue_wait(req);
1112                 if (!rc) {
1113                         str = req_capsule_server_get(&req->rq_pill,
1114                                                      &RMF_STRING);
1115                         if (cfs_copy_to_user(data->ioc_pbuf1, str,
1116                                              data->ioc_plen1))
1117                                 rc = -EFAULT;
1118                 }
1119                 ptlrpc_req_finished(req);
1120         out_catinfo:
1121                 obd_ioctl_freedata(buf, len);
1122                 RETURN(rc);
1123         }
1124         case OBD_IOC_QUOTACHECK: {
1125                 struct obd_quotactl *oqctl;
1126                 int error = 0;
1127
1128                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1129                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1130                         RETURN(-EPERM);
1131
1132                 OBD_ALLOC_PTR(oqctl);
1133                 if (!oqctl)
1134                         RETURN(-ENOMEM);
1135                 oqctl->qc_type = arg;
1136                 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
1137                 if (rc < 0) {
1138                         CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
1139                         error = rc;
1140                 }
1141
1142                 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
1143                 if (rc < 0)
1144                         CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
1145
1146                 OBD_FREE_PTR(oqctl);
1147                 return error ?: rc;
1148         }
1149         case OBD_IOC_POLL_QUOTACHECK: {
1150                 struct if_quotacheck *check;
1151
1152                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1153                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1154                         RETURN(-EPERM);
1155
1156                 OBD_ALLOC_PTR(check);
1157                 if (!check)
1158                         RETURN(-ENOMEM);
1159
1160                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
1161                                    NULL);
1162                 if (rc) {
1163                         CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
1164                         if (cfs_copy_to_user((void *)arg, check,
1165                                              sizeof(*check)))
1166                                 CDEBUG(D_QUOTA, "cfs_copy_to_user failed\n");
1167                         GOTO(out_poll, rc);
1168                 }
1169
1170                 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
1171                                    NULL);
1172                 if (rc) {
1173                         CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
1174                         if (cfs_copy_to_user((void *)arg, check,
1175                                              sizeof(*check)))
1176                                 CDEBUG(D_QUOTA, "cfs_copy_to_user failed\n");
1177                         GOTO(out_poll, rc);
1178                 }
1179         out_poll:
1180                 OBD_FREE_PTR(check);
1181                 RETURN(rc);
1182         }
1183         case OBD_IOC_QUOTACTL: {
1184                 struct if_quotactl *qctl;
1185                 int cmd, type, id, valid;
1186
1187                 OBD_ALLOC_PTR(qctl);
1188                 if (!qctl)
1189                         RETURN(-ENOMEM);
1190
1191                 if (cfs_copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1192                         GOTO(out_quotactl, rc = -EFAULT);
1193
1194                 cmd = qctl->qc_cmd;
1195                 type = qctl->qc_type;
1196                 id = qctl->qc_id;
1197                 valid = qctl->qc_valid;
1198
1199                 switch (cmd) {
1200                 case LUSTRE_Q_INVALIDATE:
1201                 case LUSTRE_Q_FINVALIDATE:
1202                 case Q_QUOTAON:
1203                 case Q_QUOTAOFF:
1204                 case Q_SETQUOTA:
1205                 case Q_SETINFO:
1206                         if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1207                             sbi->ll_flags & LL_SBI_RMT_CLIENT)
1208                                 GOTO(out_quotactl, rc = -EPERM);
1209                         break;
1210                 case Q_GETQUOTA:
1211                         if (((type == USRQUOTA && cfs_curproc_euid() != id) ||
1212                              (type == GRPQUOTA && !in_egroup_p(id))) &&
1213                             (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1214                              sbi->ll_flags & LL_SBI_RMT_CLIENT))
1215                                 GOTO(out_quotactl, rc = -EPERM);
1216                         break;
1217                 case Q_GETINFO:
1218                         break;
1219                 default:
1220                         CERROR("unsupported quotactl op: %#x\n", cmd);
1221                         GOTO(out_quotactl, rc = -ENOTTY);
1222                 }
1223
1224                 if (valid != QC_GENERAL) {
1225                         if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
1226                                 GOTO(out_quotactl, rc = -EOPNOTSUPP);
1227
1228                         if (cmd == Q_GETINFO)
1229                                 qctl->qc_cmd = Q_GETOINFO;
1230                         else if (cmd == Q_GETQUOTA)
1231                                 qctl->qc_cmd = Q_GETOQUOTA;
1232                         else
1233                                 GOTO(out_quotactl, rc = -EINVAL);
1234
1235                         switch (valid) {
1236                         case QC_MDTIDX:
1237                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1238                                                    sbi->ll_md_exp,
1239                                                    sizeof(*qctl), qctl, NULL);
1240                                 break;
1241                         case QC_OSTIDX:
1242                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1243                                                    sbi->ll_dt_exp,
1244                                                    sizeof(*qctl), qctl, NULL);
1245                                 break;
1246                         case QC_UUID:
1247                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1248                                                    sbi->ll_md_exp,
1249                                                    sizeof(*qctl), qctl, NULL);
1250                                 if (rc == -EAGAIN)
1251                                         rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1252                                                            sbi->ll_dt_exp,
1253                                                            sizeof(*qctl), qctl,
1254                                                            NULL);
1255                                 break;
1256                         default:
1257                                 rc = -EINVAL;
1258                                 break;
1259                         }
1260
1261                         if (rc)
1262                                 GOTO(out_quotactl, rc);
1263                         else
1264                                 qctl->qc_cmd = cmd;
1265                 } else {
1266                         struct obd_quotactl *oqctl;
1267
1268                         OBD_ALLOC_PTR(oqctl);
1269                         if (!oqctl)
1270                                 GOTO(out_quotactl, rc = -ENOMEM);
1271
1272                         QCTL_COPY(oqctl, qctl);
1273                         rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1274                         if (rc) {
1275                                 if (rc != -EALREADY && cmd == Q_QUOTAON) {
1276                                         oqctl->qc_cmd = Q_QUOTAOFF;
1277                                         obd_quotactl(sbi->ll_md_exp, oqctl);
1278                                 }
1279                                 OBD_FREE_PTR(oqctl);
1280                                 GOTO(out_quotactl, rc);
1281                         } else {
1282                                 QCTL_COPY(qctl, oqctl);
1283                                 OBD_FREE_PTR(oqctl);
1284                         }
1285                 }
1286
1287                 if (cfs_copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1288                         rc = -EFAULT;
1289
1290         out_quotactl:
1291                 OBD_FREE_PTR(qctl);
1292                 RETURN(rc);
1293         }
1294         case OBD_IOC_GETNAME: {
1295                 struct obd_device *obd = class_exp2obd(sbi->ll_dt_exp);
1296                 if (!obd)
1297                         RETURN(-EFAULT);
1298                 if (cfs_copy_to_user((void *)arg, obd->obd_name,
1299                                      strlen(obd->obd_name) + 1))
1300                         RETURN (-EFAULT);
1301                 RETURN(0);
1302         }
1303         case LL_IOC_FLUSHCTX:
1304                 RETURN(ll_flush_ctx(inode));
1305 #ifdef CONFIG_FS_POSIX_ACL
1306         case LL_IOC_RMTACL: {
1307             if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1308                 inode == inode->i_sb->s_root->d_inode) {
1309                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1310
1311                 LASSERT(fd != NULL);
1312                 rc = rct_add(&sbi->ll_rct, cfs_curproc_pid(), arg);
1313                 if (!rc)
1314                         fd->fd_flags |= LL_FILE_RMTACL;
1315                 RETURN(rc);
1316             } else
1317                 RETURN(0);
1318         }
1319 #endif
1320         case LL_IOC_GETOBDCOUNT: {
1321                 int count;
1322
1323                 if (cfs_copy_from_user(&count, (int *)arg, sizeof(int)))
1324                         RETURN(-EFAULT);
1325
1326                 if (!count) {
1327                         /* get ost count */
1328                         struct lov_obd *lov = &sbi->ll_dt_exp->exp_obd->u.lov;
1329                         count = lov->desc.ld_tgt_count;
1330                 } else {
1331                         /* get mdt count */
1332                         struct lmv_obd *lmv = &sbi->ll_md_exp->exp_obd->u.lmv;
1333                         count = lmv->desc.ld_tgt_count;
1334                 }
1335
1336                 if (cfs_copy_to_user((int *)arg, &count, sizeof(int)))
1337                         RETURN(-EFAULT);
1338
1339                 RETURN(0);
1340         }
1341         case LL_IOC_PATH2FID:
1342                 if (cfs_copy_to_user((void *)arg, ll_inode2fid(inode),
1343                                      sizeof(struct lu_fid)))
1344                         RETURN(-EFAULT);
1345                 RETURN(0);
1346         case LL_IOC_GET_CONNECT_FLAGS: {
1347                 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, (void*)arg));
1348         }
1349         case OBD_IOC_CHANGELOG_SEND:
1350         case OBD_IOC_CHANGELOG_CLEAR:
1351                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1352                                     sizeof(struct ioc_changelog));
1353                 RETURN(rc);
1354         case OBD_IOC_FID2PATH:
1355                 RETURN(ll_fid2path(ll_i2mdexp(inode), (void *)arg));
1356         case LL_IOC_HSM_CT_START:
1357                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1358                                     sizeof(struct lustre_kernelcomm));
1359                 RETURN(rc);
1360
1361         default:
1362                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp,0,NULL,(void *)arg));
1363         }
1364 }
1365
1366 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1367 {
1368         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1369         loff_t pos = file->f_pos;
1370         loff_t ret;
1371         ENTRY;
1372
1373         if (origin == 1 && offset >= 0 && file->f_pos == DIR_END_OFF) {
1374                 CWARN("end of dir hash, DIR_END_OFF(-2) is returned\n");
1375                 RETURN(DIR_END_OFF);
1376         }
1377
1378         ret = default_llseek(file, offset, origin);
1379         if (ret >= 0) {
1380                 struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
1381
1382                 if (ll_need_32bit_api(sbi)) {
1383                         if (file->f_pos >> 32) {
1384                                 /* hash overflow, simple revert */
1385                                 file->f_pos = pos;
1386                                 RETURN(-EOVERFLOW);
1387                         } else {
1388                                 fd->fd_dir.lfd_pos = file->f_pos << 32;
1389                         }
1390                 } else {
1391                         fd->fd_dir.lfd_pos = file->f_pos;
1392                 }
1393         }
1394         RETURN(ret);
1395 }
1396
1397 int ll_dir_open(struct inode *inode, struct file *file)
1398 {
1399         ENTRY;
1400         RETURN(ll_file_open(inode, file));
1401 }
1402
1403 int ll_dir_release(struct inode *inode, struct file *file)
1404 {
1405         ENTRY;
1406         RETURN(ll_file_release(inode, file));
1407 }
1408
1409 struct file_operations ll_dir_operations = {
1410         .llseek   = ll_dir_seek,
1411         .open     = ll_dir_open,
1412         .release  = ll_dir_release,
1413         .read     = generic_read_dir,
1414         .readdir  = ll_readdir,
1415         .ioctl    = ll_dir_ioctl
1416 };