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