13a6426bdd9fc586b5f2bb4f2fb5e0a449d8c68c
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / lustre / 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, Intel Corporation.
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 #include <linux/prefetch.h>
49
50 #define DEBUG_SUBSYSTEM S_LLITE
51
52 #include <lustre/lustre_idl.h>
53 #include <obd_support.h>
54 #include <obd_class.h>
55 #include <lustre_lib.h>
56 #include <lustre/lustre_idl.h>
57 #include <lustre_lite.h>
58 #include <lustre_dlm.h>
59 #include <lustre_fid.h>
60 #include "llite_internal.h"
61
62 /*
63  * (new) readdir implementation overview.
64  *
65  * Original lustre readdir implementation cached exact copy of raw directory
66  * pages on the client. These pages were indexed in client page cache by
67  * logical offset in the directory file. This design, while very simple and
68  * intuitive had some inherent problems:
69  *
70  *     . it implies that byte offset to the directory entry serves as a
71  *     telldir(3)/seekdir(3) cookie, but that offset is not stable: in
72  *     ext3/htree directory entries may move due to splits, and more
73  *     importantly,
74  *
75  *     . it is incompatible with the design of split directories for cmd3,
76  *     that assumes that names are distributed across nodes based on their
77  *     hash, and so readdir should be done in hash order.
78  *
79  * New readdir implementation does readdir in hash order, and uses hash of a
80  * file name as a telldir/seekdir cookie. This led to number of complications:
81  *
82  *     . hash is not unique, so it cannot be used to index cached directory
83  *     pages on the client (note, that it requires a whole pageful of hash
84  *     collided entries to cause two pages to have identical hashes);
85  *
86  *     . hash is not unique, so it cannot, strictly speaking, be used as an
87  *     entry cookie. ext3/htree has the same problem and lustre implementation
88  *     mimics their solution: seekdir(hash) positions directory at the first
89  *     entry with the given hash.
90  *
91  * Client side.
92  *
93  * 0. caching
94  *
95  * Client caches directory pages using hash of the first entry as an index. As
96  * noted above hash is not unique, so this solution doesn't work as is:
97  * special processing is needed for "page hash chains" (i.e., sequences of
98  * pages filled with entries all having the same hash value).
99  *
100  * First, such chains have to be detected. To this end, server returns to the
101  * client the hash of the first entry on the page next to one returned. When
102  * client detects that this hash is the same as hash of the first entry on the
103  * returned page, page hash collision has to be handled. Pages in the
104  * hash chain, except first one, are termed "overflow pages".
105  *
106  * Solution to index uniqueness problem is to not cache overflow
107  * pages. Instead, when page hash collision is detected, all overflow pages
108  * from emerging chain are immediately requested from the server and placed in
109  * a special data structure (struct ll_dir_chain). This data structure is used
110  * by ll_readdir() to process entries from overflow pages. When readdir
111  * invocation finishes, overflow pages are discarded. If page hash collision
112  * chain weren't completely processed, next call to readdir will again detect
113  * page hash collision, again read overflow pages in, process next portion of
114  * entries and again discard the pages. This is not as wasteful as it looks,
115  * because, given reasonable hash, page hash collisions are extremely rare.
116  *
117  * 1. directory positioning
118  *
119  * When seekdir(hash) is called, original
120  *
121  *
122  *
123  *
124  *
125  *
126  *
127  *
128  * Server.
129  *
130  * identification of and access to overflow pages
131  *
132  * page format
133  *
134  * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
135  * a header lu_dirpage which describes the start/end hash, and whether this
136  * page is empty (contains no dir entry) or hash collide with next page.
137  * After client receives reply, several pages will be integrated into dir page
138  * in PAGE_CACHE_SIZE (if PAGE_CACHE_SIZE greater than LU_PAGE_SIZE), and the
139  * lu_dirpage for this integrated page will be adjusted. See
140  * lmv_adjust_dirpages().
141  *
142  */
143
144 /* returns the page unlocked, but with a reference */
145 static int ll_dir_filler(void *_hash, struct page *page0)
146 {
147         struct inode *inode = page0->mapping->host;
148         int hash64 = ll_i2sbi(inode)->ll_flags & LL_SBI_64BIT_HASH;
149         struct obd_export *exp = ll_i2sbi(inode)->ll_md_exp;
150         struct ptlrpc_request *request;
151         struct mdt_body *body;
152         struct md_op_data *op_data;
153         __u64 hash = *((__u64 *)_hash);
154         struct page **page_pool;
155         struct page *page;
156         struct lu_dirpage *dp;
157         int max_pages = ll_i2sbi(inode)->ll_md_brw_size >> PAGE_CACHE_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 <= MD_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+PAGE_CACHE_SIZE-1)
198                          >> PAGE_CACHE_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 = kmap(page);
221                 hash = le64_to_cpu(dp->ldp_hash_start);
222                 kunmap(page);
223
224                 offset = hash_x_index(hash, hash64);
225
226                 prefetchw(&page->flags);
227                 ret = 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_locked(page);
299                 if (PageUptodate(page)) {
300                         dp = 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 = {.ei_type = LDLM_IBITS,
360                                                   .ei_mode = mode,
361                                                   .ei_cb_bl =
362                                                   ll_md_blocking_ast,
363                                                   .ei_cb_cp =
364                                                   ldlm_completion_ast,
365                                                   .ei_cb_gl = NULL,
366                                                   .ei_cb_wg = NULL,
367                                                   .ei_cbdata = NULL};
368                 struct lookup_intent it = { .it_op = IT_READDIR };
369                 struct ptlrpc_request *request;
370                 struct md_op_data *op_data;
371
372                 op_data = ll_prep_md_op_data(NULL, dir, NULL, NULL, 0, 0,
373                 LUSTRE_OPC_ANY, NULL);
374                 if (IS_ERR(op_data))
375                         return (void *)op_data;
376
377                 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, &einfo, &it,
378                                 op_data, &lockh, NULL, 0, NULL, 0);
379
380                 ll_finish_md_op_data(op_data);
381
382                 request = (struct ptlrpc_request *)it.d.lustre.it_data;
383                 if (request)
384                         ptlrpc_req_finished(request);
385                 if (rc < 0) {
386                         CERROR("lock enqueue: "DFID" at "LPU64": rc %d\n",
387                                 PFID(ll_inode2fid(dir)), hash, rc);
388                         return ERR_PTR(rc);
389                 }
390
391                 CDEBUG(D_INODE, "setting lr_lvb_inode to inode %p (%lu/%u)\n",
392                        dir, dir->i_ino, dir->i_generation);
393                 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp,
394                                  &it.d.lustre.it_lock_handle, dir, NULL);
395         } else {
396                 /* for cross-ref object, l_ast_data of the lock may not be set,
397                  * we reset it here */
398                 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp, &lockh.cookie,
399                                  dir, NULL);
400         }
401         ldlm_lock_dump_handle(D_OTHER, &lockh);
402
403         mutex_lock(&lli->lli_readdir_mutex);
404         page = ll_dir_page_locate(dir, &lhash, &start, &end);
405         if (IS_ERR(page)) {
406                 CERROR("dir page locate: "DFID" at "LPU64": rc %ld\n",
407                        PFID(ll_inode2fid(dir)), lhash, PTR_ERR(page));
408                 GOTO(out_unlock, page);
409         } else if (page != NULL) {
410                 /*
411                  * XXX nikita: not entirely correct handling of a corner case:
412                  * suppose hash chain of entries with hash value HASH crosses
413                  * border between pages P0 and P1. First both P0 and P1 are
414                  * cached, seekdir() is called for some entry from the P0 part
415                  * of the chain. Later P0 goes out of cache. telldir(HASH)
416                  * happens and finds P1, as it starts with matching hash
417                  * value. Remaining entries from P0 part of the chain are
418                  * skipped. (Is that really a bug?)
419                  *
420                  * Possible solutions: 0. don't cache P1 is such case, handle
421                  * it as an "overflow" page. 1. invalidate all pages at
422                  * once. 2. use HASH|1 as an index for P1.
423                  */
424                 GOTO(hash_collision, page);
425         }
426
427         page = read_cache_page(mapping, hash_x_index(hash, hash64),
428                                ll_dir_filler, &lhash);
429         if (IS_ERR(page)) {
430                 CERROR("read cache page: "DFID" at "LPU64": rc %ld\n",
431                        PFID(ll_inode2fid(dir)), hash, PTR_ERR(page));
432                 GOTO(out_unlock, page);
433         }
434
435         wait_on_page_locked(page);
436         (void)kmap(page);
437         if (!PageUptodate(page)) {
438                 CERROR("page not updated: "DFID" at "LPU64": rc %d\n",
439                        PFID(ll_inode2fid(dir)), hash, -5);
440                 goto fail;
441         }
442         if (!PageChecked(page))
443                 ll_check_page(dir, page);
444         if (PageError(page)) {
445                 CERROR("page error: "DFID" at "LPU64": rc %d\n",
446                        PFID(ll_inode2fid(dir)), hash, -5);
447                 goto fail;
448         }
449 hash_collision:
450         dp = page_address(page);
451         if (BITS_PER_LONG == 32 && hash64) {
452                 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
453                 end   = le64_to_cpu(dp->ldp_hash_end) >> 32;
454                 lhash = hash >> 32;
455         } else {
456                 start = le64_to_cpu(dp->ldp_hash_start);
457                 end   = le64_to_cpu(dp->ldp_hash_end);
458                 lhash = hash;
459         }
460         if (end == start) {
461                 LASSERT(start == lhash);
462                 CWARN("Page-wide hash collision: "LPU64"\n", end);
463                 if (BITS_PER_LONG == 32 && hash64)
464                         CWARN("Real page-wide hash collision at ["LPU64" "LPU64
465                               "] with hash "LPU64"\n",
466                               le64_to_cpu(dp->ldp_hash_start),
467                               le64_to_cpu(dp->ldp_hash_end), hash);
468                 /*
469                  * Fetch whole overflow chain...
470                  *
471                  * XXX not yet.
472                  */
473                 goto fail;
474         }
475 out_unlock:
476         mutex_unlock(&lli->lli_readdir_mutex);
477         ldlm_lock_decref(&lockh, mode);
478         return page;
479
480 fail:
481         ll_release_page(page, 1);
482         page = ERR_PTR(-EIO);
483         goto out_unlock;
484 }
485
486 int ll_dir_read(struct inode *inode, __u64 *_pos, void *cookie,
487                 filldir_t filldir)
488 {
489         struct ll_inode_info *info       = ll_i2info(inode);
490         struct ll_sb_info    *sbi       = ll_i2sbi(inode);
491         __u64            pos    = *_pos;
492         int                api32      = ll_need_32bit_api(sbi);
493         int                hash64     = sbi->ll_flags & LL_SBI_64BIT_HASH;
494         struct page       *page;
495         struct ll_dir_chain   chain;
496         int                done = 0;
497         int                rc = 0;
498         ENTRY;
499
500         ll_dir_chain_init(&chain);
501
502         page = ll_get_dir_page(inode, pos, &chain);
503
504         while (rc == 0 && !done) {
505                 struct lu_dirpage *dp;
506                 struct lu_dirent  *ent;
507
508                 if (!IS_ERR(page)) {
509                         /*
510                          * If page is empty (end of directory is reached),
511                          * use this value.
512                          */
513                         __u64 hash = MDS_DIR_END_OFF;
514                         __u64 next;
515
516                         dp = page_address(page);
517                         for (ent = lu_dirent_start(dp); ent != NULL && !done;
518                              ent = lu_dirent_next(ent)) {
519                                 __u16     type;
520                                 int         namelen;
521                                 struct lu_fid  fid;
522                                 __u64     lhash;
523                                 __u64     ino;
524
525                                 /*
526                                  * XXX: implement correct swabbing here.
527                                  */
528
529                                 hash = le64_to_cpu(ent->lde_hash);
530                                 if (hash < pos)
531                                         /*
532                                          * Skip until we find target hash
533                                          * value.
534                                          */
535                                         continue;
536
537                                 namelen = le16_to_cpu(ent->lde_namelen);
538                                 if (namelen == 0)
539                                         /*
540                                          * Skip dummy record.
541                                          */
542                                         continue;
543
544                                 if (api32 && hash64)
545                                         lhash = hash >> 32;
546                                 else
547                                         lhash = hash;
548                                 fid_le_to_cpu(&fid, &ent->lde_fid);
549                                 ino = cl_fid_build_ino(&fid, api32);
550                                 type = ll_dirent_type_get(ent);
551                                 /* For 'll_nfs_get_name_filldir()', it will try
552                                  * to access the 'ent' through its 'lde_name',
553                                  * so the parameter 'name' for 'filldir()' must
554                                  * be part of the 'ent'. */
555                                 done = filldir(cookie, ent->lde_name, namelen,
556                                                lhash, ino, type);
557                         }
558                         next = le64_to_cpu(dp->ldp_hash_end);
559                         if (!done) {
560                                 pos = next;
561                                 if (pos == MDS_DIR_END_OFF) {
562                                         /*
563                                          * End of directory reached.
564                                          */
565                                         done = 1;
566                                         ll_release_page(page, 0);
567                                 } else if (1 /* chain is exhausted*/) {
568                                         /*
569                                          * Normal case: continue to the next
570                                          * page.
571                                          */
572                                         ll_release_page(page,
573                                             le32_to_cpu(dp->ldp_flags) &
574                                                         LDF_COLLIDE);
575                                         next = pos;
576                                         page = ll_get_dir_page(inode, pos,
577                                                                &chain);
578                                 } else {
579                                         /*
580                                          * go into overflow page.
581                                          */
582                                         LASSERT(le32_to_cpu(dp->ldp_flags) &
583                                                 LDF_COLLIDE);
584                                         ll_release_page(page, 1);
585                                 }
586                         } else {
587                                 pos = hash;
588                                 ll_release_page(page, 0);
589                         }
590                 } else {
591                         rc = PTR_ERR(page);
592                         CERROR("error reading dir "DFID" at %lu: rc %d\n",
593                                PFID(&info->lli_fid), (unsigned long)pos, rc);
594                 }
595         }
596
597         *_pos = pos;
598         ll_dir_chain_fini(&chain);
599         RETURN(rc);
600 }
601
602 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
603 {
604         struct inode            *inode  = filp->f_dentry->d_inode;
605         struct ll_file_data     *lfd    = LUSTRE_FPRIVATE(filp);
606         struct ll_sb_info       *sbi    = ll_i2sbi(inode);
607         __u64                   pos     = lfd->lfd_pos;
608         int                     hash64  = sbi->ll_flags & LL_SBI_64BIT_HASH;
609         int                     api32   = ll_need_32bit_api(sbi);
610         int                     rc;
611         struct path             path;
612         ENTRY;
613
614         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu "
615                " 32bit_api %d\n", inode->i_ino, inode->i_generation,
616                inode, (unsigned long)pos, i_size_read(inode), api32);
617
618         if (pos == MDS_DIR_END_OFF)
619                 /*
620                  * end-of-file.
621                  */
622                 GOTO(out, rc = 0);
623
624         rc = ll_dir_read(inode, &pos, cookie, filldir);
625         lfd->lfd_pos = pos;
626         if (pos == MDS_DIR_END_OFF) {
627                 if (api32)
628                         filp->f_pos = LL_DIR_END_OFF_32BIT;
629                 else
630                         filp->f_pos = LL_DIR_END_OFF;
631         } else {
632                 if (api32 && hash64)
633                         filp->f_pos = pos >> 32;
634                 else
635                         filp->f_pos = pos;
636         }
637         filp->f_version = inode->i_version;
638         path.mnt = filp->f_path.mnt;
639         path.dentry = filp->f_dentry;
640         touch_atime(&path);
641
642 out:
643         if (!rc)
644                 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
645
646         RETURN(rc);
647 }
648
649 int ll_send_mgc_param(struct obd_export *mgc, char *string)
650 {
651         struct mgs_send_param *msp;
652         int rc = 0;
653
654         OBD_ALLOC_PTR(msp);
655         if (!msp)
656                 return -ENOMEM;
657
658         strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
659         rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
660                                 sizeof(struct mgs_send_param), msp, NULL);
661         if (rc)
662                 CERROR("Failed to set parameter: %d\n", rc);
663         OBD_FREE_PTR(msp);
664
665         return rc;
666 }
667
668 int ll_dir_setdirstripe(struct inode *dir, struct lmv_user_md *lump,
669                         char *filename)
670 {
671         struct ptlrpc_request *request = NULL;
672         struct md_op_data *op_data;
673         struct ll_sb_info *sbi = ll_i2sbi(dir);
674         int mode;
675         int err;
676
677         ENTRY;
678
679         mode = (0755 & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
680         op_data = ll_prep_md_op_data(NULL, dir, NULL, filename,
681                                      strlen(filename), mode, LUSTRE_OPC_MKDIR,
682                                      lump);
683         if (IS_ERR(op_data))
684                 GOTO(err_exit, err = PTR_ERR(op_data));
685
686         op_data->op_cli_flags |= CLI_SET_MEA;
687         err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
688                         current_fsuid(), current_fsgid(),
689                         cfs_curproc_cap_pack(), 0, &request);
690         ll_finish_md_op_data(op_data);
691         if (err)
692                 GOTO(err_exit, err);
693 err_exit:
694         ptlrpc_req_finished(request);
695         return err;
696 }
697
698 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
699                      int set_default)
700 {
701         struct ll_sb_info *sbi = ll_i2sbi(inode);
702         struct md_op_data *op_data;
703         struct ptlrpc_request *req = NULL;
704         int rc = 0;
705         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
706         struct obd_device *mgc = lsi->lsi_mgc;
707         int lum_size;
708         ENTRY;
709
710         if (lump != NULL) {
711                 /*
712                  * This is coming from userspace, so should be in
713                  * local endian.  But the MDS would like it in little
714                  * endian, so we swab it before we send it.
715                  */
716                 switch (lump->lmm_magic) {
717                 case LOV_USER_MAGIC_V1: {
718                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
719                                 lustre_swab_lov_user_md_v1(lump);
720                         lum_size = sizeof(struct lov_user_md_v1);
721                         break;
722                 }
723                 case LOV_USER_MAGIC_V3: {
724                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
725                                 lustre_swab_lov_user_md_v3(
726                                         (struct lov_user_md_v3 *)lump);
727                         lum_size = sizeof(struct lov_user_md_v3);
728                         break;
729                 }
730                 default: {
731                         CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
732                                         " %#08x != %#08x nor %#08x\n",
733                                         lump->lmm_magic, LOV_USER_MAGIC_V1,
734                                         LOV_USER_MAGIC_V3);
735                         RETURN(-EINVAL);
736                 }
737                 }
738         } else {
739                 lum_size = sizeof(struct lov_user_md_v1);
740         }
741
742         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
743                                      LUSTRE_OPC_ANY, NULL);
744         if (IS_ERR(op_data))
745                 RETURN(PTR_ERR(op_data));
746
747         if (lump != NULL && lump->lmm_magic == cpu_to_le32(LMV_USER_MAGIC))
748                 op_data->op_cli_flags |= CLI_SET_MEA;
749
750         /* swabbing is done in lov_setstripe() on server side */
751         rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size,
752                         NULL, 0, &req, NULL);
753         ll_finish_md_op_data(op_data);
754         ptlrpc_req_finished(req);
755         if (rc) {
756                 if (rc != -EPERM && rc != -EACCES)
757                         CERROR("mdc_setattr fails: rc = %d\n", rc);
758         }
759
760         /* In the following we use the fact that LOV_USER_MAGIC_V1 and
761          LOV_USER_MAGIC_V3 have the same initial fields so we do not
762          need the make the distiction between the 2 versions */
763         if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
764                 char *param = NULL;
765                 char *buf;
766
767                 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
768                 if (param == NULL)
769                         GOTO(end, rc = -ENOMEM);
770
771                 buf = param;
772                 /* Get fsname and assume devname to be -MDT0000. */
773                 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
774                 strcat(buf, "-MDT0000.lov");
775                 buf += strlen(buf);
776
777                 /* Set root stripesize */
778                 sprintf(buf, ".stripesize=%u",
779                         lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
780                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
781                 if (rc)
782                         GOTO(end, rc);
783
784                 /* Set root stripecount */
785                 sprintf(buf, ".stripecount=%hd",
786                         lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
787                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
788                 if (rc)
789                         GOTO(end, rc);
790
791                 /* Set root stripeoffset */
792                 sprintf(buf, ".stripeoffset=%hd",
793                         lump ? le16_to_cpu(lump->lmm_stripe_offset) :
794                         (typeof(lump->lmm_stripe_offset))(-1));
795                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
796
797 end:
798                 if (param != NULL)
799                         OBD_FREE(param, MGS_PARAM_MAXLEN);
800         }
801         RETURN(rc);
802 }
803
804 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
805                      int *lmm_size, struct ptlrpc_request **request)
806 {
807         struct ll_sb_info *sbi = ll_i2sbi(inode);
808         struct mdt_body   *body;
809         struct lov_mds_md *lmm = NULL;
810         struct ptlrpc_request *req = NULL;
811         int rc, lmmsize;
812         struct md_op_data *op_data;
813
814         rc = ll_get_max_mdsize(sbi, &lmmsize);
815         if (rc)
816                 RETURN(rc);
817
818         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
819                                      0, lmmsize, LUSTRE_OPC_ANY,
820                                      NULL);
821         if (IS_ERR(op_data))
822                 RETURN(PTR_ERR(op_data));
823
824         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
825         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
826         ll_finish_md_op_data(op_data);
827         if (rc < 0) {
828                 CDEBUG(D_INFO, "md_getattr failed on inode "
829                        "%lu/%u: rc %d\n", inode->i_ino,
830                        inode->i_generation, rc);
831                 GOTO(out, rc);
832         }
833
834         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
835         LASSERT(body != NULL);
836
837         lmmsize = body->eadatasize;
838
839         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
840             lmmsize == 0) {
841                 GOTO(out, rc = -ENODATA);
842         }
843
844         lmm = req_capsule_server_sized_get(&req->rq_pill,
845                                            &RMF_MDT_MD, lmmsize);
846         LASSERT(lmm != NULL);
847
848         /*
849          * This is coming from the MDS, so is probably in
850          * little endian.  We convert it to host endian before
851          * passing it to userspace.
852          */
853         /* We don't swab objects for directories */
854         switch (le32_to_cpu(lmm->lmm_magic)) {
855         case LOV_MAGIC_V1:
856                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
857                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
858                 break;
859         case LOV_MAGIC_V3:
860                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
861                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
862                 break;
863         default:
864                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
865                 rc = -EPROTO;
866         }
867 out:
868         *lmmp = lmm;
869         *lmm_size = lmmsize;
870         *request = req;
871         return rc;
872 }
873
874 /*
875  *  Get MDT index for the inode.
876  */
877 int ll_get_mdt_idx(struct inode *inode)
878 {
879         struct ll_sb_info *sbi = ll_i2sbi(inode);
880         struct md_op_data *op_data;
881         int rc, mdtidx;
882         ENTRY;
883
884         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0,
885                                      0, LUSTRE_OPC_ANY, NULL);
886         if (IS_ERR(op_data))
887                 RETURN(PTR_ERR(op_data));
888
889         op_data->op_flags |= MF_GET_MDT_IDX;
890         rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
891         mdtidx = op_data->op_mds;
892         ll_finish_md_op_data(op_data);
893         if (rc < 0) {
894                 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
895                 RETURN(rc);
896         }
897         return mdtidx;
898 }
899
900 /**
901  * Generic handler to do any pre-copy work.
902  *
903  * It send a first hsm_progress (with extent length == 0) to coordinator as a
904  * first information for it that real work has started.
905  *
906  * Moreover, for a ARCHIVE request, it will sample the file data version and
907  * store it in \a copy.
908  *
909  * \return 0 on success.
910  */
911 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
912 {
913         struct ll_sb_info               *sbi = ll_s2sbi(sb);
914         struct hsm_progress_kernel       hpk;
915         int                              rc;
916         ENTRY;
917
918         /* Forge a hsm_progress based on data from copy. */
919         hpk.hpk_fid = copy->hc_hai.hai_fid;
920         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
921         hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
922         hpk.hpk_extent.length = 0;
923         hpk.hpk_flags = 0;
924         hpk.hpk_errval = 0;
925         hpk.hpk_data_version = 0;
926
927
928         /* For archive request, we need to read the current file version. */
929         if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
930                 struct inode    *inode;
931                 __u64            data_version = 0;
932
933                 /* Get inode for this fid */
934                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
935                 if (IS_ERR(inode)) {
936                         hpk.hpk_flags |= HP_FLAG_RETRY;
937                         /* hpk_errval is >= 0 */
938                         hpk.hpk_errval = -PTR_ERR(inode);
939                         GOTO(progress, rc = PTR_ERR(inode));
940                 }
941
942                 /* Read current file data version */
943                 rc = ll_data_version(inode, &data_version, 1);
944                 iput(inode);
945                 if (rc != 0) {
946                         CDEBUG(D_HSM, "Could not read file data version of "
947                                       DFID" (rc = %d). Archive request ("
948                                       LPX64") could not be done.\n",
949                                       PFID(&copy->hc_hai.hai_fid), rc,
950                                       copy->hc_hai.hai_cookie);
951                         hpk.hpk_flags |= HP_FLAG_RETRY;
952                         /* hpk_errval must be >= 0 */
953                         hpk.hpk_errval = -rc;
954                         GOTO(progress, rc);
955                 }
956
957                 /* Store it the hsm_copy for later copytool use.
958                  * Always modified even if no lsm. */
959                 copy->hc_data_version = data_version;
960         }
961
962 progress:
963         rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
964                            &hpk, NULL);
965
966         RETURN(rc);
967 }
968
969 /**
970  * Generic handler to do any post-copy work.
971  *
972  * It will send the last hsm_progress update to coordinator to inform it
973  * that copy is finished and whether it was successful or not.
974  *
975  * Moreover,
976  * - for ARCHIVE request, it will sample the file data version and compare it
977  *   with the version saved in ll_ioc_copy_start(). If they do not match, copy
978  *   will be considered as failed.
979  * - for RESTORE request, it will sample the file data version and send it to
980  *   coordinator which is useful if the file was imported as 'released'.
981  *
982  * \return 0 on success.
983  */
984 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
985 {
986         struct ll_sb_info               *sbi = ll_s2sbi(sb);
987         struct hsm_progress_kernel       hpk;
988         int                              rc;
989         ENTRY;
990
991         /* If you modify the logic here, also check llapi_hsm_copy_end(). */
992         /* Take care: copy->hc_hai.hai_action, len, gid and data are not
993          * initialized if copy_end was called with copy == NULL.
994          */
995
996         /* Forge a hsm_progress based on data from copy. */
997         hpk.hpk_fid = copy->hc_hai.hai_fid;
998         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
999         hpk.hpk_extent = copy->hc_hai.hai_extent;
1000         hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
1001         hpk.hpk_errval = copy->hc_errval;
1002         hpk.hpk_data_version = 0;
1003
1004         /* For archive request, we need to check the file data was not changed.
1005          *
1006          * For restore request, we need to send the file data version, this is
1007          * useful when the file was created using hsm_import.
1008          */
1009         if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
1010              (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
1011             (copy->hc_errval == 0)) {
1012                 struct inode    *inode;
1013                 __u64            data_version = 0;
1014
1015                 /* Get lsm for this fid */
1016                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
1017                 if (IS_ERR(inode)) {
1018                         hpk.hpk_flags |= HP_FLAG_RETRY;
1019                         /* hpk_errval must be >= 0 */
1020                         hpk.hpk_errval = -PTR_ERR(inode);
1021                         GOTO(progress, rc = PTR_ERR(inode));
1022                 }
1023
1024                 rc = ll_data_version(inode, &data_version,
1025                                      copy->hc_hai.hai_action == HSMA_ARCHIVE);
1026                 iput(inode);
1027                 if (rc) {
1028                         CDEBUG(D_HSM, "Could not read file data version. "
1029                                       "Request could not be confirmed.\n");
1030                         if (hpk.hpk_errval == 0)
1031                                 hpk.hpk_errval = -rc;
1032                         GOTO(progress, rc);
1033                 }
1034
1035                 /* Store it the hsm_copy for later copytool use.
1036                  * Always modified even if no lsm. */
1037                 hpk.hpk_data_version = data_version;
1038
1039                 /* File could have been stripped during archiving, so we need
1040                  * to check anyway. */
1041                 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
1042                     (copy->hc_data_version != data_version)) {
1043                         CDEBUG(D_HSM, "File data version mismatched. "
1044                               "File content was changed during archiving. "
1045                                DFID", start:"LPX64" current:"LPX64"\n",
1046                                PFID(&copy->hc_hai.hai_fid),
1047                                copy->hc_data_version, data_version);
1048                         /* File was changed, send error to cdt. Do not ask for
1049                          * retry because if a file is modified frequently,
1050                          * the cdt will loop on retried archive requests.
1051                          * The policy engine will ask for a new archive later
1052                          * when the file will not be modified for some tunable
1053                          * time */
1054                         /* we do not notify caller */
1055                         hpk.hpk_flags &= ~HP_FLAG_RETRY;
1056                         /* hpk_errval must be >= 0 */
1057                         hpk.hpk_errval = EBUSY;
1058                 }
1059
1060         }
1061
1062 progress:
1063         rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1064                            &hpk, NULL);
1065
1066         RETURN(rc);
1067 }
1068
1069
1070 static int copy_and_ioctl(int cmd, struct obd_export *exp, void *data, int len)
1071 {
1072         void *ptr;
1073         int rc;
1074
1075         OBD_ALLOC(ptr, len);
1076         if (ptr == NULL)
1077                 return -ENOMEM;
1078         if (copy_from_user(ptr, data, len)) {
1079                 OBD_FREE(ptr, len);
1080                 return -EFAULT;
1081         }
1082         rc = obd_iocontrol(cmd, exp, len, data, NULL);
1083         OBD_FREE(ptr, len);
1084         return rc;
1085 }
1086
1087 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
1088 {
1089         int cmd = qctl->qc_cmd;
1090         int type = qctl->qc_type;
1091         int id = qctl->qc_id;
1092         int valid = qctl->qc_valid;
1093         int rc = 0;
1094         ENTRY;
1095
1096         switch (cmd) {
1097         case LUSTRE_Q_INVALIDATE:
1098         case LUSTRE_Q_FINVALIDATE:
1099         case Q_QUOTAON:
1100         case Q_QUOTAOFF:
1101         case Q_SETQUOTA:
1102         case Q_SETINFO:
1103                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1104                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1105                         RETURN(-EPERM);
1106                 break;
1107         case Q_GETQUOTA:
1108                 if (((type == USRQUOTA && current_euid() != id) ||
1109                      (type == GRPQUOTA && !in_egroup_p(id))) &&
1110                     (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1111                      sbi->ll_flags & LL_SBI_RMT_CLIENT))
1112                         RETURN(-EPERM);
1113                 break;
1114         case Q_GETINFO:
1115                 break;
1116         default:
1117                 CERROR("unsupported quotactl op: %#x\n", cmd);
1118                 RETURN(-ENOTTY);
1119         }
1120
1121         if (valid != QC_GENERAL) {
1122                 if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
1123                         RETURN(-EOPNOTSUPP);
1124
1125                 if (cmd == Q_GETINFO)
1126                         qctl->qc_cmd = Q_GETOINFO;
1127                 else if (cmd == Q_GETQUOTA)
1128                         qctl->qc_cmd = Q_GETOQUOTA;
1129                 else
1130                         RETURN(-EINVAL);
1131
1132                 switch (valid) {
1133                 case QC_MDTIDX:
1134                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1135                                            sizeof(*qctl), qctl, NULL);
1136                         break;
1137                 case QC_OSTIDX:
1138                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1139                                            sizeof(*qctl), qctl, NULL);
1140                         break;
1141                 case QC_UUID:
1142                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1143                                            sizeof(*qctl), qctl, NULL);
1144                         if (rc == -EAGAIN)
1145                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1146                                                    sbi->ll_dt_exp,
1147                                                    sizeof(*qctl), qctl, NULL);
1148                         break;
1149                 default:
1150                         rc = -EINVAL;
1151                         break;
1152                 }
1153
1154                 if (rc)
1155                         RETURN(rc);
1156
1157                 qctl->qc_cmd = cmd;
1158         } else {
1159                 struct obd_quotactl *oqctl;
1160
1161                 OBD_ALLOC_PTR(oqctl);
1162                 if (oqctl == NULL)
1163                         RETURN(-ENOMEM);
1164
1165                 QCTL_COPY(oqctl, qctl);
1166                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1167                 if (rc) {
1168                         if (rc != -EALREADY && cmd == Q_QUOTAON) {
1169                                 oqctl->qc_cmd = Q_QUOTAOFF;
1170                                 obd_quotactl(sbi->ll_md_exp, oqctl);
1171                         }
1172                         OBD_FREE_PTR(oqctl);
1173                         RETURN(rc);
1174                 }
1175                 /* If QIF_SPACE is not set, client should collect the
1176                  * space usage from OSSs by itself */
1177                 if (cmd == Q_GETQUOTA &&
1178                     !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1179                     !oqctl->qc_dqblk.dqb_curspace) {
1180                         struct obd_quotactl *oqctl_tmp;
1181
1182                         OBD_ALLOC_PTR(oqctl_tmp);
1183                         if (oqctl_tmp == NULL)
1184                                 GOTO(out, rc = -ENOMEM);
1185
1186                         oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1187                         oqctl_tmp->qc_id = oqctl->qc_id;
1188                         oqctl_tmp->qc_type = oqctl->qc_type;
1189
1190                         /* collect space usage from OSTs */
1191                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1192                         rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1193                         if (!rc || rc == -EREMOTEIO) {
1194                                 oqctl->qc_dqblk.dqb_curspace =
1195                                         oqctl_tmp->qc_dqblk.dqb_curspace;
1196                                 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1197                         }
1198
1199                         /* collect space & inode usage from MDTs */
1200                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1201                         oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1202                         rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1203                         if (!rc || rc == -EREMOTEIO) {
1204                                 oqctl->qc_dqblk.dqb_curspace +=
1205                                         oqctl_tmp->qc_dqblk.dqb_curspace;
1206                                 oqctl->qc_dqblk.dqb_curinodes =
1207                                         oqctl_tmp->qc_dqblk.dqb_curinodes;
1208                                 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1209                         } else {
1210                                 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1211                         }
1212
1213                         OBD_FREE_PTR(oqctl_tmp);
1214                 }
1215 out:
1216                 QCTL_COPY(qctl, oqctl);
1217                 OBD_FREE_PTR(oqctl);
1218         }
1219
1220         RETURN(rc);
1221 }
1222
1223 static char *
1224 ll_getname(const char __user *filename)
1225 {
1226         int ret = 0, len;
1227         char *tmp = __getname();
1228
1229         if (!tmp)
1230                 return ERR_PTR(-ENOMEM);
1231
1232         len = strncpy_from_user(tmp, filename, PATH_MAX);
1233         if (len == 0)
1234                 ret = -ENOENT;
1235         else if (len > PATH_MAX)
1236                 ret = -ENAMETOOLONG;
1237
1238         if (ret) {
1239                 __putname(tmp);
1240                 tmp =  ERR_PTR(ret);
1241         }
1242         return tmp;
1243 }
1244
1245 #define ll_putname(filename) __putname(filename)
1246
1247 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1248 {
1249         struct inode *inode = file->f_dentry->d_inode;
1250         struct ll_sb_info *sbi = ll_i2sbi(inode);
1251         struct obd_ioctl_data *data;
1252         int rc = 0;
1253         ENTRY;
1254
1255         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
1256                inode->i_ino, inode->i_generation, inode, cmd);
1257
1258         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1259         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1260                 return -ENOTTY;
1261
1262         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1263         switch(cmd) {
1264         case FSFILT_IOC_GETFLAGS:
1265         case FSFILT_IOC_SETFLAGS:
1266                 RETURN(ll_iocontrol(inode, file, cmd, arg));
1267         case FSFILT_IOC_GETVERSION_OLD:
1268         case FSFILT_IOC_GETVERSION:
1269                 RETURN(put_user(inode->i_generation, (int *)arg));
1270         /* We need to special case any other ioctls we want to handle,
1271          * to send them to the MDS/OST as appropriate and to properly
1272          * network encode the arg field.
1273         case FSFILT_IOC_SETVERSION_OLD:
1274         case FSFILT_IOC_SETVERSION:
1275         */
1276         case LL_IOC_GET_MDTIDX: {
1277                 int mdtidx;
1278
1279                 mdtidx = ll_get_mdt_idx(inode);
1280                 if (mdtidx < 0)
1281                         RETURN(mdtidx);
1282
1283                 if (put_user((int)mdtidx, (int*)arg))
1284                         RETURN(-EFAULT);
1285
1286                 return 0;
1287         }
1288         case IOC_MDC_LOOKUP: {
1289                 struct ptlrpc_request *request = NULL;
1290                 int namelen, len = 0;
1291                 char *buf = NULL;
1292                 char *filename;
1293                 struct md_op_data *op_data;
1294
1295                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1296                 if (rc)
1297                         RETURN(rc);
1298                 data = (void *)buf;
1299
1300                 filename = data->ioc_inlbuf1;
1301                 namelen = strlen(filename);
1302
1303                 if (namelen < 1) {
1304                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1305                         GOTO(out_free, rc = -EINVAL);
1306                 }
1307
1308                 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename, namelen,
1309                                              0, LUSTRE_OPC_ANY, NULL);
1310                 if (IS_ERR(op_data))
1311                         GOTO(out_free, rc = PTR_ERR(op_data));
1312
1313                 op_data->op_valid = OBD_MD_FLID;
1314                 rc = md_getattr_name(sbi->ll_md_exp, op_data, &request);
1315                 ll_finish_md_op_data(op_data);
1316                 if (rc < 0) {
1317                         CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
1318                         GOTO(out_free, rc);
1319                 }
1320                 ptlrpc_req_finished(request);
1321                 EXIT;
1322 out_free:
1323                 obd_ioctl_freedata(buf, len);
1324                 return rc;
1325         }
1326         case LL_IOC_LMV_SETSTRIPE: {
1327                 struct lmv_user_md  *lum;
1328                 char            *buf = NULL;
1329                 char            *filename;
1330                 int              namelen = 0;
1331                 int              lumlen = 0;
1332                 int              len;
1333                 int              rc;
1334
1335                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1336                 if (rc)
1337                         RETURN(rc);
1338
1339                 data = (void *)buf;
1340                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1341                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1342                         GOTO(lmv_out_free, rc = -EINVAL);
1343
1344                 filename = data->ioc_inlbuf1;
1345                 namelen = data->ioc_inllen1;
1346
1347                 if (namelen < 1) {
1348                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1349                         GOTO(lmv_out_free, rc = -EINVAL);
1350                 }
1351                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1352                 lumlen = data->ioc_inllen2;
1353
1354                 if (lum->lum_magic != LMV_USER_MAGIC ||
1355                     lumlen != sizeof(*lum)) {
1356                         CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1357                                filename, lum->lum_magic, lumlen, -EFAULT);
1358                         GOTO(lmv_out_free, rc = -EINVAL);
1359                 }
1360
1361                 /**
1362                  * ll_dir_setdirstripe will be used to set dir stripe
1363                  *  mdc_create--->mdt_reint_create (with dirstripe)
1364                  */
1365                 rc = ll_dir_setdirstripe(inode, lum, filename);
1366 lmv_out_free:
1367                 obd_ioctl_freedata(buf, len);
1368                 RETURN(rc);
1369
1370         }
1371         case LL_IOC_LOV_SETSTRIPE: {
1372                 struct lov_user_md_v3 lumv3;
1373                 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1374                 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1375                 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1376
1377                 int set_default = 0;
1378
1379                 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1380                 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1381                         sizeof(lumv3p->lmm_objects[0]));
1382                 /* first try with v1 which is smaller than v3 */
1383                 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1384                         RETURN(-EFAULT);
1385
1386                 if ((lumv1->lmm_magic == LOV_USER_MAGIC_V3) ) {
1387                         if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1388                                 RETURN(-EFAULT);
1389                 }
1390
1391                 if (inode->i_sb->s_root == file->f_dentry)
1392                         set_default = 1;
1393
1394                 /* in v1 and v3 cases lumv1 points to data */
1395                 rc = ll_dir_setstripe(inode, lumv1, set_default);
1396
1397                 RETURN(rc);
1398         }
1399         case LL_IOC_LMV_GETSTRIPE: {
1400                 struct lmv_user_md *lump = (struct lmv_user_md *)arg;
1401                 struct lmv_user_md lum;
1402                 struct lmv_user_md *tmp;
1403                 int lum_size;
1404                 int rc = 0;
1405                 int mdtindex;
1406
1407                 if (copy_from_user(&lum, lump, sizeof(struct lmv_user_md)))
1408                         RETURN(-EFAULT);
1409
1410                 if (lum.lum_magic != LMV_MAGIC_V1)
1411                         RETURN(-EINVAL);
1412
1413                 lum_size = lmv_user_md_size(1, LMV_MAGIC_V1);
1414                 OBD_ALLOC(tmp, lum_size);
1415                 if (tmp == NULL)
1416                         GOTO(free_lmv, rc = -ENOMEM);
1417
1418                 memcpy(tmp, &lum, sizeof(lum));
1419                 tmp->lum_type = LMV_STRIPE_TYPE;
1420                 tmp->lum_stripe_count = 1;
1421                 mdtindex = ll_get_mdt_idx(inode);
1422                 if (mdtindex < 0)
1423                         GOTO(free_lmv, rc = -ENOMEM);
1424
1425                 tmp->lum_stripe_offset = mdtindex;
1426                 tmp->lum_objects[0].lum_mds = mdtindex;
1427                 memcpy(&tmp->lum_objects[0].lum_fid, ll_inode2fid(inode),
1428                        sizeof(struct lu_fid));
1429                 if (copy_to_user((void *)arg, tmp, lum_size))
1430                         GOTO(free_lmv, rc = -EFAULT);
1431 free_lmv:
1432                 if (tmp)
1433                         OBD_FREE(tmp, lum_size);
1434                 RETURN(rc);
1435         }
1436         case LL_IOC_REMOVE_ENTRY: {
1437                 char            *filename = NULL;
1438                 int              namelen = 0;
1439                 int              rc;
1440
1441                 /* Here is a little hack to avoid sending REINT_RMENTRY to
1442                  * unsupported server, which might crash the server(LU-2730),
1443                  * Because both LVB_TYPE and REINT_RMENTRY will be supported
1444                  * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1445                  * server will support REINT_RMENTRY XXX*/
1446                 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1447                         return -ENOTSUPP;
1448
1449                 filename = ll_getname((const char *)arg);
1450                 if (IS_ERR(filename))
1451                         RETURN(PTR_ERR(filename));
1452
1453                 namelen = strlen(filename);
1454                 if (namelen < 1)
1455                         GOTO(out_rmdir, rc = -EINVAL);
1456
1457                 rc = ll_rmdir_entry(inode, filename, namelen);
1458 out_rmdir:
1459                 if (filename)
1460                         ll_putname(filename);
1461                 RETURN(rc);
1462         }
1463         case LL_IOC_LOV_SWAP_LAYOUTS:
1464                 RETURN(-EPERM);
1465         case LL_IOC_OBD_STATFS:
1466                 RETURN(ll_obd_statfs(inode, (void *)arg));
1467         case LL_IOC_LOV_GETSTRIPE:
1468         case LL_IOC_MDC_GETINFO:
1469         case IOC_MDC_GETFILEINFO:
1470         case IOC_MDC_GETFILESTRIPE: {
1471                 struct ptlrpc_request *request = NULL;
1472                 struct lov_user_md *lump;
1473                 struct lov_mds_md *lmm = NULL;
1474                 struct mdt_body *body;
1475                 char *filename = NULL;
1476                 int lmmsize;
1477
1478                 if (cmd == IOC_MDC_GETFILEINFO ||
1479                     cmd == IOC_MDC_GETFILESTRIPE) {
1480                         filename = ll_getname((const char *)arg);
1481                         if (IS_ERR(filename))
1482                                 RETURN(PTR_ERR(filename));
1483
1484                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1485                                                       &lmmsize, &request);
1486                 } else {
1487                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
1488                 }
1489
1490                 if (request) {
1491                         body = req_capsule_server_get(&request->rq_pill,
1492                                                       &RMF_MDT_BODY);
1493                         LASSERT(body != NULL);
1494                 } else {
1495                         GOTO(out_req, rc);
1496                 }
1497
1498                 if (rc < 0) {
1499                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1500                                                cmd == LL_IOC_MDC_GETINFO))
1501                                 GOTO(skip_lmm, rc = 0);
1502                         else
1503                                 GOTO(out_req, rc);
1504                 }
1505
1506                 if (cmd == IOC_MDC_GETFILESTRIPE ||
1507                     cmd == LL_IOC_LOV_GETSTRIPE) {
1508                         lump = (struct lov_user_md *)arg;
1509                 } else {
1510                         struct lov_user_mds_data *lmdp;
1511                         lmdp = (struct lov_user_mds_data *)arg;
1512                         lump = &lmdp->lmd_lmm;
1513                 }
1514                 if (copy_to_user(lump, lmm, lmmsize)) {
1515                         if (copy_to_user(lump, lmm, sizeof(*lump)))
1516                                 GOTO(out_req, rc = -EFAULT);
1517                         rc = -EOVERFLOW;
1518                 }
1519         skip_lmm:
1520                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1521                         struct lov_user_mds_data *lmdp;
1522                         lstat_t st = { 0 };
1523
1524                         st.st_dev     = inode->i_sb->s_dev;
1525                         st.st_mode    = body->mode;
1526                         st.st_nlink   = body->nlink;
1527                         st.st_uid     = body->uid;
1528                         st.st_gid     = body->gid;
1529                         st.st_rdev    = body->rdev;
1530                         st.st_size    = body->size;
1531                         st.st_blksize = PAGE_CACHE_SIZE;
1532                         st.st_blocks  = body->blocks;
1533                         st.st_atime   = body->atime;
1534                         st.st_mtime   = body->mtime;
1535                         st.st_ctime   = body->ctime;
1536                         st.st_ino     = inode->i_ino;
1537
1538                         lmdp = (struct lov_user_mds_data *)arg;
1539                         if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
1540                                 GOTO(out_req, rc = -EFAULT);
1541                 }
1542
1543                 EXIT;
1544         out_req:
1545                 ptlrpc_req_finished(request);
1546                 if (filename)
1547                         ll_putname(filename);
1548                 return rc;
1549         }
1550         case IOC_LOV_GETINFO: {
1551                 struct lov_user_mds_data *lumd;
1552                 struct lov_stripe_md *lsm;
1553                 struct lov_user_md *lum;
1554                 struct lov_mds_md *lmm;
1555                 int lmmsize;
1556                 lstat_t st;
1557
1558                 lumd = (struct lov_user_mds_data *)arg;
1559                 lum = &lumd->lmd_lmm;
1560
1561                 rc = ll_get_max_mdsize(sbi, &lmmsize);
1562                 if (rc)
1563                         RETURN(rc);
1564
1565                 OBD_ALLOC_LARGE(lmm, lmmsize);
1566                 if (copy_from_user(lmm, lum, lmmsize))
1567                         GOTO(free_lmm, rc = -EFAULT);
1568
1569                 switch (lmm->lmm_magic) {
1570                 case LOV_USER_MAGIC_V1:
1571                         if (LOV_USER_MAGIC_V1 == cpu_to_le32(LOV_USER_MAGIC_V1))
1572                                 break;
1573                         /* swab objects first so that stripes num will be sane */
1574                         lustre_swab_lov_user_md_objects(
1575                                 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1576                                 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1577                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1578                         break;
1579                 case LOV_USER_MAGIC_V3:
1580                         if (LOV_USER_MAGIC_V3 == cpu_to_le32(LOV_USER_MAGIC_V3))
1581                                 break;
1582                         /* swab objects first so that stripes num will be sane */
1583                         lustre_swab_lov_user_md_objects(
1584                                 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1585                                 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1586                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1587                         break;
1588                 default:
1589                         GOTO(free_lmm, rc = -EINVAL);
1590                 }
1591
1592                 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
1593                 if (rc < 0)
1594                         GOTO(free_lmm, rc = -ENOMEM);
1595
1596                 /* Perform glimpse_size operation. */
1597                 memset(&st, 0, sizeof(st));
1598
1599                 rc = ll_glimpse_ioctl(sbi, lsm, &st);
1600                 if (rc)
1601                         GOTO(free_lsm, rc);
1602
1603                 if (copy_to_user(&lumd->lmd_st, &st, sizeof(st)))
1604                         GOTO(free_lsm, rc = -EFAULT);
1605
1606                 EXIT;
1607         free_lsm:
1608                 obd_free_memmd(sbi->ll_dt_exp, &lsm);
1609         free_lmm:
1610                 OBD_FREE_LARGE(lmm, lmmsize);
1611                 return rc;
1612         }
1613         case OBD_IOC_LLOG_CATINFO: {
1614                 RETURN(-EOPNOTSUPP);
1615         }
1616         case OBD_IOC_QUOTACHECK: {
1617                 struct obd_quotactl *oqctl;
1618                 int error = 0;
1619
1620                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1621                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1622                         RETURN(-EPERM);
1623
1624                 OBD_ALLOC_PTR(oqctl);
1625                 if (!oqctl)
1626                         RETURN(-ENOMEM);
1627                 oqctl->qc_type = arg;
1628                 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
1629                 if (rc < 0) {
1630                         CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
1631                         error = rc;
1632                 }
1633
1634                 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
1635                 if (rc < 0)
1636                         CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
1637
1638                 OBD_FREE_PTR(oqctl);
1639                 return error ?: rc;
1640         }
1641         case OBD_IOC_POLL_QUOTACHECK: {
1642                 struct if_quotacheck *check;
1643
1644                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1645                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1646                         RETURN(-EPERM);
1647
1648                 OBD_ALLOC_PTR(check);
1649                 if (!check)
1650                         RETURN(-ENOMEM);
1651
1652                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
1653                                    NULL);
1654                 if (rc) {
1655                         CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
1656                         if (copy_to_user((void *)arg, check,
1657                                              sizeof(*check)))
1658                                 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1659                         GOTO(out_poll, rc);
1660                 }
1661
1662                 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
1663                                    NULL);
1664                 if (rc) {
1665                         CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
1666                         if (copy_to_user((void *)arg, check,
1667                                              sizeof(*check)))
1668                                 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1669                         GOTO(out_poll, rc);
1670                 }
1671         out_poll:
1672                 OBD_FREE_PTR(check);
1673                 RETURN(rc);
1674         }
1675 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
1676         case LL_IOC_QUOTACTL_18: {
1677                 /* copy the old 1.x quota struct for internal use, then copy
1678                  * back into old format struct.  For 1.8 compatibility. */
1679                 struct if_quotactl_18 *qctl_18;
1680                 struct if_quotactl *qctl_20;
1681
1682                 OBD_ALLOC_PTR(qctl_18);
1683                 if (!qctl_18)
1684                         RETURN(-ENOMEM);
1685
1686                 OBD_ALLOC_PTR(qctl_20);
1687                 if (!qctl_20)
1688                         GOTO(out_quotactl_18, rc = -ENOMEM);
1689
1690                 if (copy_from_user(qctl_18, (void *)arg, sizeof(*qctl_18)))
1691                         GOTO(out_quotactl_20, rc = -ENOMEM);
1692
1693                 QCTL_COPY(qctl_20, qctl_18);
1694                 qctl_20->qc_idx = 0;
1695
1696                 /* XXX: dqb_valid was borrowed as a flag to mark that
1697                  *      only mds quota is wanted */
1698                 if (qctl_18->qc_cmd == Q_GETQUOTA &&
1699                     qctl_18->qc_dqblk.dqb_valid) {
1700                         qctl_20->qc_valid = QC_MDTIDX;
1701                         qctl_20->qc_dqblk.dqb_valid = 0;
1702                 } else if (qctl_18->obd_uuid.uuid[0] != '\0') {
1703                         qctl_20->qc_valid = QC_UUID;
1704                         qctl_20->obd_uuid = qctl_18->obd_uuid;
1705                 } else {
1706                         qctl_20->qc_valid = QC_GENERAL;
1707                 }
1708
1709                 rc = quotactl_ioctl(sbi, qctl_20);
1710
1711                 if (rc == 0) {
1712                         QCTL_COPY(qctl_18, qctl_20);
1713                         qctl_18->obd_uuid = qctl_20->obd_uuid;
1714
1715                         if (copy_to_user((void *)arg, qctl_18,
1716                                              sizeof(*qctl_18)))
1717                                 rc = -EFAULT;
1718                 }
1719
1720         out_quotactl_20:
1721                 OBD_FREE_PTR(qctl_20);
1722         out_quotactl_18:
1723                 OBD_FREE_PTR(qctl_18);
1724                 RETURN(rc);
1725         }
1726 #else
1727 #warning "remove old LL_IOC_QUOTACTL_18 compatibility code"
1728 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0) */
1729         case LL_IOC_QUOTACTL: {
1730                 struct if_quotactl *qctl;
1731
1732                 OBD_ALLOC_PTR(qctl);
1733                 if (!qctl)
1734                         RETURN(-ENOMEM);
1735
1736                 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1737                         GOTO(out_quotactl, rc = -EFAULT);
1738
1739                 rc = quotactl_ioctl(sbi, qctl);
1740
1741                 if (rc == 0 && copy_to_user((void *)arg,qctl,sizeof(*qctl)))
1742                         rc = -EFAULT;
1743
1744         out_quotactl:
1745                 OBD_FREE_PTR(qctl);
1746                 RETURN(rc);
1747         }
1748         case OBD_IOC_GETDTNAME:
1749         case OBD_IOC_GETMDNAME:
1750                 RETURN(ll_get_obd_name(inode, cmd, arg));
1751         case LL_IOC_FLUSHCTX:
1752                 RETURN(ll_flush_ctx(inode));
1753 #ifdef CONFIG_FS_POSIX_ACL
1754         case LL_IOC_RMTACL: {
1755             if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1756                 inode == inode->i_sb->s_root->d_inode) {
1757                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1758
1759                 LASSERT(fd != NULL);
1760                 rc = rct_add(&sbi->ll_rct, current_pid(), arg);
1761                 if (!rc)
1762                         fd->fd_flags |= LL_FILE_RMTACL;
1763                 RETURN(rc);
1764             } else
1765                 RETURN(0);
1766         }
1767 #endif
1768         case LL_IOC_GETOBDCOUNT: {
1769                 int count, vallen;
1770                 struct obd_export *exp;
1771
1772                 if (copy_from_user(&count, (int *)arg, sizeof(int)))
1773                         RETURN(-EFAULT);
1774
1775                 /* get ost count when count is zero, get mdt count otherwise */
1776                 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1777                 vallen = sizeof(count);
1778                 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1779                                   KEY_TGT_COUNT, &vallen, &count, NULL);
1780                 if (rc) {
1781                         CERROR("get target count failed: %d\n", rc);
1782                         RETURN(rc);
1783                 }
1784
1785                 if (copy_to_user((int *)arg, &count, sizeof(int)))
1786                         RETURN(-EFAULT);
1787
1788                 RETURN(0);
1789         }
1790         case LL_IOC_PATH2FID:
1791                 if (copy_to_user((void *)arg, ll_inode2fid(inode),
1792                                      sizeof(struct lu_fid)))
1793                         RETURN(-EFAULT);
1794                 RETURN(0);
1795         case LL_IOC_GET_CONNECT_FLAGS: {
1796                 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, (void*)arg));
1797         }
1798         case OBD_IOC_CHANGELOG_SEND:
1799         case OBD_IOC_CHANGELOG_CLEAR:
1800                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1801                                     sizeof(struct ioc_changelog));
1802                 RETURN(rc);
1803         case OBD_IOC_FID2PATH:
1804                 RETURN(ll_fid2path(inode, (void *)arg));
1805         case LL_IOC_HSM_REQUEST: {
1806                 struct hsm_user_request *hur;
1807                 int                      totalsize;
1808
1809                 OBD_ALLOC_PTR(hur);
1810                 if (hur == NULL)
1811                         RETURN(-ENOMEM);
1812
1813                 /* We don't know the true size yet; copy the fixed-size part */
1814                 if (copy_from_user(hur, (void *)arg, sizeof(*hur))) {
1815                         OBD_FREE_PTR(hur);
1816                         RETURN(-EFAULT);
1817                 }
1818
1819                 /* Compute the whole struct size */
1820                 totalsize = hur_len(hur);
1821                 OBD_FREE_PTR(hur);
1822                 OBD_ALLOC_LARGE(hur, totalsize);
1823                 if (hur == NULL)
1824                         RETURN(-ENOMEM);
1825
1826                 /* Copy the whole struct */
1827                 if (copy_from_user(hur, (void *)arg, totalsize)) {
1828                         OBD_FREE_LARGE(hur, totalsize);
1829                         RETURN(-EFAULT);
1830                 }
1831
1832                 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1833                                    hur, NULL);
1834
1835                 OBD_FREE_LARGE(hur, totalsize);
1836
1837                 RETURN(rc);
1838         }
1839         case LL_IOC_HSM_PROGRESS: {
1840                 struct hsm_progress_kernel      hpk;
1841                 struct hsm_progress             hp;
1842
1843                 if (copy_from_user(&hp, (void *)arg, sizeof(hp)))
1844                         RETURN(-EFAULT);
1845
1846                 hpk.hpk_fid = hp.hp_fid;
1847                 hpk.hpk_cookie = hp.hp_cookie;
1848                 hpk.hpk_extent = hp.hp_extent;
1849                 hpk.hpk_flags = hp.hp_flags;
1850                 hpk.hpk_errval = hp.hp_errval;
1851                 hpk.hpk_data_version = 0;
1852
1853                 /* File may not exist in Lustre; all progress
1854                  * reported to Lustre root */
1855                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1856                                    NULL);
1857                 RETURN(rc);
1858         }
1859         case LL_IOC_HSM_CT_START:
1860                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1861                                     sizeof(struct lustre_kernelcomm));
1862                 RETURN(rc);
1863
1864         case LL_IOC_HSM_COPY_START: {
1865                 struct hsm_copy *copy;
1866                 int              rc;
1867
1868                 OBD_ALLOC_PTR(copy);
1869                 if (copy == NULL)
1870                         RETURN(-ENOMEM);
1871                 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1872                         OBD_FREE_PTR(copy);
1873                         RETURN(-EFAULT);
1874                 }
1875
1876                 rc = ll_ioc_copy_start(inode->i_sb, copy);
1877                 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1878                         rc = -EFAULT;
1879
1880                 OBD_FREE_PTR(copy);
1881                 RETURN(rc);
1882         }
1883         case LL_IOC_HSM_COPY_END: {
1884                 struct hsm_copy *copy;
1885                 int              rc;
1886
1887                 OBD_ALLOC_PTR(copy);
1888                 if (copy == NULL)
1889                         RETURN(-ENOMEM);
1890                 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1891                         OBD_FREE_PTR(copy);
1892                         RETURN(-EFAULT);
1893                 }
1894
1895                 rc = ll_ioc_copy_end(inode->i_sb, copy);
1896                 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1897                         rc = -EFAULT;
1898
1899                 OBD_FREE_PTR(copy);
1900                 RETURN(rc);
1901         }
1902         default:
1903                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1904                                      (void *)arg));
1905         }
1906 }
1907
1908 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1909 {
1910         struct inode *inode = file->f_mapping->host;
1911         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1912         struct ll_sb_info *sbi = ll_i2sbi(inode);
1913         int api32 = ll_need_32bit_api(sbi);
1914         loff_t ret = -EINVAL;
1915         ENTRY;
1916
1917         mutex_lock(&inode->i_mutex);
1918         switch (origin) {
1919                 case SEEK_SET:
1920                         break;
1921                 case SEEK_CUR:
1922                         offset += file->f_pos;
1923                         break;
1924                 case SEEK_END:
1925                         if (offset > 0)
1926                                 GOTO(out, ret);
1927                         if (api32)
1928                                 offset += LL_DIR_END_OFF_32BIT;
1929                         else
1930                                 offset += LL_DIR_END_OFF;
1931                         break;
1932                 default:
1933                         GOTO(out, ret);
1934         }
1935
1936         if (offset >= 0 &&
1937             ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1938              (!api32 && offset <= LL_DIR_END_OFF))) {
1939                 if (offset != file->f_pos) {
1940                         if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1941                             (!api32 && offset == LL_DIR_END_OFF))
1942                                 fd->lfd_pos = MDS_DIR_END_OFF;
1943                         else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1944                                 fd->lfd_pos = offset << 32;
1945                         else
1946                                 fd->lfd_pos = offset;
1947                         file->f_pos = offset;
1948                         file->f_version = 0;
1949                 }
1950                 ret = offset;
1951         }
1952         GOTO(out, ret);
1953
1954 out:
1955         mutex_unlock(&inode->i_mutex);
1956         return ret;
1957 }
1958
1959 int ll_dir_open(struct inode *inode, struct file *file)
1960 {
1961         ENTRY;
1962         RETURN(ll_file_open(inode, file));
1963 }
1964
1965 int ll_dir_release(struct inode *inode, struct file *file)
1966 {
1967         ENTRY;
1968         RETURN(ll_file_release(inode, file));
1969 }
1970
1971 struct file_operations ll_dir_operations = {
1972         .llseek   = ll_dir_seek,
1973         .open     = ll_dir_open,
1974         .release  = ll_dir_release,
1975         .read     = generic_read_dir,
1976         .readdir  = ll_readdir,
1977         .unlocked_ioctl   = ll_dir_ioctl,
1978         .fsync    = ll_fsync,
1979 };