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