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