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