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