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