Whamcloud - gitweb
LU-2800 autoconf: remove obsolete autoconf options
[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 PAGE_CACHE_SIZE (if PAGE_CACHE_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         struct lu_dirpage *dp;
156         int max_pages = ll_i2sbi(inode)->ll_md_brw_size >> PAGE_CACHE_SHIFT;
157         int nrdpgs = 0; /* number of pages read actually */
158         int npages;
159         int i;
160         int rc;
161         ENTRY;
162
163         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) hash "LPU64"\n",
164                inode->i_ino, inode->i_generation, inode, hash);
165
166         LASSERT(max_pages > 0 && max_pages <= MD_MAX_BRW_PAGES);
167
168         OBD_ALLOC(page_pool, sizeof(page) * max_pages);
169         if (page_pool != NULL) {
170                 page_pool[0] = page0;
171         } else {
172                 page_pool = &page0;
173                 max_pages = 1;
174         }
175         for (npages = 1; npages < max_pages; npages++) {
176                 page = page_cache_alloc_cold(inode->i_mapping);
177                 if (!page)
178                         break;
179                 page_pool[npages] = page;
180         }
181
182         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
183                                      LUSTRE_OPC_ANY, NULL);
184         op_data->op_npages = npages;
185         op_data->op_offset = hash;
186         rc = md_readpage(exp, op_data, page_pool, &request);
187         ll_finish_md_op_data(op_data);
188         if (rc == 0) {
189                 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
190                 /* Checked by mdc_readpage() */
191                 LASSERT(body != NULL);
192
193                 if (body->valid & OBD_MD_FLSIZE)
194                         cl_isize_write(inode, body->size);
195
196                 nrdpgs = (request->rq_bulk->bd_nob_transferred +
197                           PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
198                 SetPageUptodate(page0);
199         }
200         unlock_page(page0);
201         ptlrpc_req_finished(request);
202
203         CDEBUG(D_VFSTRACE, "read %d/%d pages\n", nrdpgs, npages);
204
205         for (i = 1; i < npages; i++) {
206                 unsigned long offset;
207                 int ret;
208
209                 page = page_pool[i];
210
211                 if (rc < 0 || i >= nrdpgs) {
212                         page_cache_release(page);
213                         continue;
214                 }
215
216                 SetPageUptodate(page);
217
218                 dp = kmap(page);
219                 hash = le64_to_cpu(dp->ldp_hash_start);
220                 kunmap(page);
221
222                 offset = hash_x_index(hash, hash64);
223
224                 prefetchw(&page->flags);
225                 ret = add_to_page_cache_lru(page, inode->i_mapping, offset,
226                                             GFP_KERNEL);
227                 if (ret == 0)
228                         unlock_page(page);
229                 else
230                         CDEBUG(D_VFSTRACE, "page %lu add to page cache failed:"
231                                " %d\n", offset, ret);
232                 page_cache_release(page);
233         }
234
235         if (page_pool != &page0)
236                 OBD_FREE(page_pool, sizeof(struct page *) * max_pages);
237         EXIT;
238         return rc;
239 }
240
241 static void ll_check_page(struct inode *dir, struct page *page)
242 {
243         /* XXX: check page format later */
244         SetPageChecked(page);
245 }
246
247 void ll_release_page(struct page *page, int remove)
248 {
249         kunmap(page);
250         if (remove) {
251                 lock_page(page);
252                 if (likely(page->mapping != NULL))
253                         truncate_complete_page(page->mapping, page);
254                 unlock_page(page);
255         }
256         page_cache_release(page);
257 }
258
259 /*
260  * Find, kmap and return page that contains given hash.
261  */
262 static struct page *ll_dir_page_locate(struct inode *dir, __u64 *hash,
263                                        __u64 *start, __u64 *end)
264 {
265         int hash64 = ll_i2sbi(dir)->ll_flags & LL_SBI_64BIT_HASH;
266         struct address_space *mapping = dir->i_mapping;
267         /*
268          * Complement of hash is used as an index so that
269          * radix_tree_gang_lookup() can be used to find a page with starting
270          * hash _smaller_ than one we are looking for.
271          */
272         unsigned long offset = hash_x_index(*hash, hash64);
273         struct page *page;
274         int found;
275
276         TREE_READ_LOCK_IRQ(mapping);
277         found = radix_tree_gang_lookup(&mapping->page_tree,
278                                        (void **)&page, offset, 1);
279         if (found > 0) {
280                 struct lu_dirpage *dp;
281
282                 page_cache_get(page);
283                 TREE_READ_UNLOCK_IRQ(mapping);
284                 /*
285                  * In contrast to find_lock_page() we are sure that directory
286                  * page cannot be truncated (while DLM lock is held) and,
287                  * hence, can avoid restart.
288                  *
289                  * In fact, page cannot be locked here at all, because
290                  * ll_dir_filler() does synchronous io.
291                  */
292                 wait_on_page(page);
293                 if (PageUptodate(page)) {
294                         dp = kmap(page);
295                         if (BITS_PER_LONG == 32 && hash64) {
296                                 *start = le64_to_cpu(dp->ldp_hash_start) >> 32;
297                                 *end   = le64_to_cpu(dp->ldp_hash_end) >> 32;
298                                 *hash  = *hash >> 32;
299                         } else {
300                                 *start = le64_to_cpu(dp->ldp_hash_start);
301                                 *end   = le64_to_cpu(dp->ldp_hash_end);
302                         }
303                         LASSERTF(*start <= *hash, "start = "LPX64",end = "
304                                  LPX64",hash = "LPX64"\n", *start, *end, *hash);
305                         CDEBUG(D_VFSTRACE, "page %lu [%llu %llu], hash "LPU64"\n",
306                                offset, *start, *end, *hash);
307                         if (*hash > *end) {
308                                 ll_release_page(page, 0);
309                                 page = NULL;
310                         } else if (*end != *start && *hash == *end) {
311                                 /*
312                                  * upon hash collision, remove this page,
313                                  * otherwise put page reference, and
314                                  * ll_get_dir_page() will issue RPC to fetch
315                                  * the page we want.
316                                  */
317                                 ll_release_page(page,
318                                     le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
319                                 page = NULL;
320                         }
321                 } else {
322                         page_cache_release(page);
323                         page = ERR_PTR(-EIO);
324                 }
325
326         } else {
327                 TREE_READ_UNLOCK_IRQ(mapping);
328                 page = NULL;
329         }
330         return page;
331 }
332
333 struct page *ll_get_dir_page(struct inode *dir, __u64 hash,
334                              struct ll_dir_chain *chain)
335 {
336         ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
337         struct address_space *mapping = dir->i_mapping;
338         struct lustre_handle lockh;
339         struct lu_dirpage *dp;
340         struct page *page;
341         ldlm_mode_t mode;
342         int rc;
343         __u64 start = 0;
344         __u64 end = 0;
345         __u64 lhash = hash;
346         struct ll_inode_info *lli = ll_i2info(dir);
347         int hash64 = ll_i2sbi(dir)->ll_flags & LL_SBI_64BIT_HASH;
348
349         mode = LCK_PR;
350         rc = md_lock_match(ll_i2sbi(dir)->ll_md_exp, LDLM_FL_BLOCK_GRANTED,
351                            ll_inode2fid(dir), LDLM_IBITS, &policy, mode, &lockh);
352         if (!rc) {
353                 struct ldlm_enqueue_info einfo = {
354                         .ei_type = LDLM_IBITS,
355                         .ei_mode = mode,
356                         .ei_cb_bl = ll_md_blocking_ast,
357                         .ei_cb_cp = ldlm_completion_ast,
358                 };
359                 struct lookup_intent it = { .it_op = IT_READDIR };
360                 struct ptlrpc_request *request;
361                 struct md_op_data *op_data;
362
363                 op_data = ll_prep_md_op_data(NULL, dir, NULL, NULL, 0, 0,
364                 LUSTRE_OPC_ANY, NULL);
365                 if (IS_ERR(op_data))
366                         return (void *)op_data;
367
368                 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, &einfo, &it,
369                                 op_data, &lockh, NULL, 0, NULL, 0);
370
371                 ll_finish_md_op_data(op_data);
372
373                 request = (struct ptlrpc_request *)it.d.lustre.it_data;
374                 if (request)
375                         ptlrpc_req_finished(request);
376                 if (rc < 0) {
377                         CERROR("lock enqueue: "DFID" at "LPU64": rc %d\n",
378                                 PFID(ll_inode2fid(dir)), hash, rc);
379                         return ERR_PTR(rc);
380                 }
381
382                 CDEBUG(D_INODE, "setting lr_lvb_inode to inode %p (%lu/%u)\n",
383                        dir, dir->i_ino, dir->i_generation);
384                 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp,
385                                  &it.d.lustre.it_lock_handle, dir, NULL);
386         } else {
387                 /* for cross-ref object, l_ast_data of the lock may not be set,
388                  * we reset it here */
389                 md_set_lock_data(ll_i2sbi(dir)->ll_md_exp, &lockh.cookie,
390                                  dir, NULL);
391         }
392         ldlm_lock_dump_handle(D_OTHER, &lockh);
393
394         mutex_lock(&lli->lli_readdir_mutex);
395         page = ll_dir_page_locate(dir, &lhash, &start, &end);
396         if (IS_ERR(page)) {
397                 CERROR("dir page locate: "DFID" at "LPU64": rc %ld\n",
398                        PFID(ll_inode2fid(dir)), lhash, PTR_ERR(page));
399                 GOTO(out_unlock, page);
400         } else if (page != NULL) {
401                 /*
402                  * XXX nikita: not entirely correct handling of a corner case:
403                  * suppose hash chain of entries with hash value HASH crosses
404                  * border between pages P0 and P1. First both P0 and P1 are
405                  * cached, seekdir() is called for some entry from the P0 part
406                  * of the chain. Later P0 goes out of cache. telldir(HASH)
407                  * happens and finds P1, as it starts with matching hash
408                  * value. Remaining entries from P0 part of the chain are
409                  * skipped. (Is that really a bug?)
410                  *
411                  * Possible solutions: 0. don't cache P1 is such case, handle
412                  * it as an "overflow" page. 1. invalidate all pages at
413                  * once. 2. use HASH|1 as an index for P1.
414                  */
415                 GOTO(hash_collision, page);
416         }
417
418         page = read_cache_page(mapping, hash_x_index(hash, hash64),
419                                ll_dir_filler, &lhash);
420         if (IS_ERR(page)) {
421                 CERROR("read cache page: "DFID" at "LPU64": rc %ld\n",
422                        PFID(ll_inode2fid(dir)), hash, PTR_ERR(page));
423                 GOTO(out_unlock, page);
424         }
425
426         wait_on_page(page);
427         (void)kmap(page);
428         if (!PageUptodate(page)) {
429                 CERROR("page not updated: "DFID" at "LPU64": rc %d\n",
430                        PFID(ll_inode2fid(dir)), hash, -5);
431                 goto fail;
432         }
433         if (!PageChecked(page))
434                 ll_check_page(dir, page);
435         if (PageError(page)) {
436                 CERROR("page error: "DFID" at "LPU64": rc %d\n",
437                        PFID(ll_inode2fid(dir)), hash, -5);
438                 goto fail;
439         }
440 hash_collision:
441         dp = page_address(page);
442         if (BITS_PER_LONG == 32 && hash64) {
443                 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
444                 end   = le64_to_cpu(dp->ldp_hash_end) >> 32;
445                 lhash = hash >> 32;
446         } else {
447                 start = le64_to_cpu(dp->ldp_hash_start);
448                 end   = le64_to_cpu(dp->ldp_hash_end);
449                 lhash = hash;
450         }
451         if (end == start) {
452                 LASSERT(start == lhash);
453                 CWARN("Page-wide hash collision: "LPU64"\n", end);
454                 if (BITS_PER_LONG == 32 && hash64)
455                         CWARN("Real page-wide hash collision at ["LPU64" "LPU64
456                               "] with hash "LPU64"\n",
457                               le64_to_cpu(dp->ldp_hash_start),
458                               le64_to_cpu(dp->ldp_hash_end), hash);
459                 /*
460                  * Fetch whole overflow chain...
461                  *
462                  * XXX not yet.
463                  */
464                 goto fail;
465         }
466 out_unlock:
467         mutex_unlock(&lli->lli_readdir_mutex);
468         ldlm_lock_decref(&lockh, mode);
469         return page;
470
471 fail:
472         ll_release_page(page, 1);
473         page = ERR_PTR(-EIO);
474         goto out_unlock;
475 }
476
477 int ll_dir_read(struct inode *inode, __u64 *_pos, void *cookie,
478                 filldir_t filldir)
479 {
480         struct ll_inode_info *info       = ll_i2info(inode);
481         struct ll_sb_info    *sbi        = ll_i2sbi(inode);
482         __u64                 pos        = *_pos;
483         int                   api32      = ll_need_32bit_api(sbi);
484         int                   hash64     = sbi->ll_flags & LL_SBI_64BIT_HASH;
485         struct page          *page;
486         struct ll_dir_chain   chain;
487         int                   done = 0;
488         int                   rc = 0;
489         ENTRY;
490
491         ll_dir_chain_init(&chain);
492
493         page = ll_get_dir_page(inode, pos, &chain);
494
495         while (rc == 0 && !done) {
496                 struct lu_dirpage *dp;
497                 struct lu_dirent  *ent;
498
499                 if (!IS_ERR(page)) {
500                         /*
501                          * If page is empty (end of directory is reached),
502                          * use this value.
503                          */
504                         __u64 hash = MDS_DIR_END_OFF;
505                         __u64 next;
506
507                         dp = page_address(page);
508                         for (ent = lu_dirent_start(dp); ent != NULL && !done;
509                              ent = lu_dirent_next(ent)) {
510                                 __u16          type;
511                                 int            namelen;
512                                 struct lu_fid  fid;
513                                 __u64          lhash;
514                                 __u64          ino;
515
516                                 /*
517                                  * XXX: implement correct swabbing here.
518                                  */
519
520                                 hash = le64_to_cpu(ent->lde_hash);
521                                 if (hash < pos)
522                                         /*
523                                          * Skip until we find target hash
524                                          * value.
525                                          */
526                                         continue;
527
528                                 namelen = le16_to_cpu(ent->lde_namelen);
529                                 if (namelen == 0)
530                                         /*
531                                          * Skip dummy record.
532                                          */
533                                         continue;
534
535                                 if (api32 && hash64)
536                                         lhash = hash >> 32;
537                                 else
538                                         lhash = hash;
539                                 fid_le_to_cpu(&fid, &ent->lde_fid);
540                                 ino = cl_fid_build_ino(&fid, api32);
541                                 type = ll_dirent_type_get(ent);
542                                 /* For 'll_nfs_get_name_filldir()', it will try
543                                  * to access the 'ent' through its 'lde_name',
544                                  * so the parameter 'name' for 'filldir()' must
545                                  * be part of the 'ent'. */
546                                 done = filldir(cookie, ent->lde_name, namelen,
547                                                lhash, ino, type);
548                         }
549                         next = le64_to_cpu(dp->ldp_hash_end);
550                         if (!done) {
551                                 pos = next;
552                                 if (pos == MDS_DIR_END_OFF) {
553                                         /*
554                                          * End of directory reached.
555                                          */
556                                         done = 1;
557                                         ll_release_page(page, 0);
558                                 } else if (1 /* chain is exhausted*/) {
559                                         /*
560                                          * Normal case: continue to the next
561                                          * page.
562                                          */
563                                         ll_release_page(page,
564                                             le32_to_cpu(dp->ldp_flags) &
565                                                         LDF_COLLIDE);
566                                         next = pos;
567                                         page = ll_get_dir_page(inode, pos,
568                                                                &chain);
569                                 } else {
570                                         /*
571                                          * go into overflow page.
572                                          */
573                                         LASSERT(le32_to_cpu(dp->ldp_flags) &
574                                                 LDF_COLLIDE);
575                                         ll_release_page(page, 1);
576                                 }
577                         } else {
578                                 pos = hash;
579                                 ll_release_page(page, 0);
580                         }
581                 } else {
582                         rc = PTR_ERR(page);
583                         CERROR("error reading dir "DFID" at %lu: rc %d\n",
584                                PFID(&info->lli_fid), (unsigned long)pos, rc);
585                 }
586         }
587
588         *_pos = pos;
589         ll_dir_chain_fini(&chain);
590         RETURN(rc);
591 }
592
593 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
594 {
595         struct inode            *inode  = filp->f_dentry->d_inode;
596         struct ll_file_data     *lfd    = LUSTRE_FPRIVATE(filp);
597         struct ll_sb_info       *sbi    = ll_i2sbi(inode);
598         __u64                   pos     = lfd->lfd_pos;
599         int                     hash64  = sbi->ll_flags & LL_SBI_64BIT_HASH;
600         int                     api32   = ll_need_32bit_api(sbi);
601         int                     rc;
602 #ifdef HAVE_TOUCH_ATIME_1ARG
603         struct path             path;
604 #endif
605         ENTRY;
606
607         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %lu/%llu "
608                " 32bit_api %d\n", inode->i_ino, inode->i_generation,
609                inode, (unsigned long)pos, i_size_read(inode), api32);
610
611         if (pos == MDS_DIR_END_OFF)
612                 /*
613                  * end-of-file.
614                  */
615                 GOTO(out, rc = 0);
616
617         rc = ll_dir_read(inode, &pos, cookie, filldir);
618         lfd->lfd_pos = pos;
619         if (pos == MDS_DIR_END_OFF) {
620                 if (api32)
621                         filp->f_pos = LL_DIR_END_OFF_32BIT;
622                 else
623                         filp->f_pos = LL_DIR_END_OFF;
624         } else {
625                 if (api32 && hash64)
626                         filp->f_pos = pos >> 32;
627                 else
628                         filp->f_pos = pos;
629         }
630         filp->f_version = inode->i_version;
631 #ifdef HAVE_TOUCH_ATIME_1ARG
632         path.mnt = filp->f_vfsmnt;
633         path.dentry = filp->f_dentry;
634         touch_atime(&path);
635 #else
636         touch_atime(filp->f_vfsmnt, filp->f_dentry);
637 #endif
638
639 out:
640         if (!rc)
641                 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
642
643         RETURN(rc);
644 }
645
646 int ll_send_mgc_param(struct obd_export *mgc, char *string)
647 {
648         struct mgs_send_param *msp;
649         int rc = 0;
650
651         OBD_ALLOC_PTR(msp);
652         if (!msp)
653                 return -ENOMEM;
654
655         strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
656         rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
657                                 sizeof(struct mgs_send_param), msp, NULL);
658         if (rc)
659                 CERROR("Failed to set parameter: %d\n", rc);
660         OBD_FREE_PTR(msp);
661
662         return rc;
663 }
664
665 int ll_dir_setdirstripe(struct inode *dir, struct lmv_user_md *lump,
666                         char *filename)
667 {
668         struct ptlrpc_request *request = NULL;
669         struct md_op_data *op_data;
670         struct ll_sb_info *sbi = ll_i2sbi(dir);
671         int mode;
672         int err;
673
674         ENTRY;
675
676         mode = (0755 & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
677         op_data = ll_prep_md_op_data(NULL, dir, NULL, filename,
678                                      strlen(filename), mode, LUSTRE_OPC_MKDIR,
679                                      lump);
680         if (IS_ERR(op_data))
681                 GOTO(err_exit, err = PTR_ERR(op_data));
682
683         op_data->op_cli_flags |= CLI_SET_MEA;
684         err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
685                         cfs_curproc_fsuid(), cfs_curproc_fsgid(),
686                         cfs_curproc_cap_pack(), 0, &request);
687         ll_finish_md_op_data(op_data);
688         if (err)
689                 GOTO(err_exit, err);
690 err_exit:
691         ptlrpc_req_finished(request);
692         return err;
693 }
694
695 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
696                      int set_default)
697 {
698         struct ll_sb_info *sbi = ll_i2sbi(inode);
699         struct md_op_data *op_data;
700         struct ptlrpc_request *req = NULL;
701         int rc = 0;
702         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
703         struct obd_device *mgc = lsi->lsi_mgc;
704         int lum_size;
705         ENTRY;
706
707         if (lump != NULL) {
708                 /*
709                  * This is coming from userspace, so should be in
710                  * local endian.  But the MDS would like it in little
711                  * endian, so we swab it before we send it.
712                  */
713                 switch (lump->lmm_magic) {
714                 case LOV_USER_MAGIC_V1: {
715                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
716                                 lustre_swab_lov_user_md_v1(lump);
717                         lum_size = sizeof(struct lov_user_md_v1);
718                         break;
719                 }
720                 case LOV_USER_MAGIC_V3: {
721                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
722                                 lustre_swab_lov_user_md_v3(
723                                         (struct lov_user_md_v3 *)lump);
724                         lum_size = sizeof(struct lov_user_md_v3);
725                         break;
726                 }
727                 default: {
728                         CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
729                                         " %#08x != %#08x nor %#08x\n",
730                                         lump->lmm_magic, LOV_USER_MAGIC_V1,
731                                         LOV_USER_MAGIC_V3);
732                         RETURN(-EINVAL);
733                 }
734                 }
735         } else {
736                 lum_size = sizeof(struct lov_user_md_v1);
737         }
738
739         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
740                                      LUSTRE_OPC_ANY, NULL);
741         if (IS_ERR(op_data))
742                 RETURN(PTR_ERR(op_data));
743
744         if (lump != NULL && lump->lmm_magic == cpu_to_le32(LMV_USER_MAGIC))
745                 op_data->op_cli_flags |= CLI_SET_MEA;
746
747         /* swabbing is done in lov_setstripe() on server side */
748         rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size,
749                         NULL, 0, &req, NULL);
750         ll_finish_md_op_data(op_data);
751         ptlrpc_req_finished(req);
752         if (rc) {
753                 if (rc != -EPERM && rc != -EACCES)
754                         CERROR("mdc_setattr fails: rc = %d\n", rc);
755         }
756
757         /* In the following we use the fact that LOV_USER_MAGIC_V1 and
758          LOV_USER_MAGIC_V3 have the same initial fields so we do not
759          need the make the distiction between the 2 versions */
760         if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
761                 char *param = NULL;
762                 char *buf;
763
764                 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
765                 if (param == NULL)
766                         GOTO(end, rc = -ENOMEM);
767
768                 buf = param;
769                 /* Get fsname and assume devname to be -MDT0000. */
770                 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
771                 strcat(buf, "-MDT0000.lov");
772                 buf += strlen(buf);
773
774                 /* Set root stripesize */
775                 sprintf(buf, ".stripesize=%u",
776                         lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
777                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
778                 if (rc)
779                         GOTO(end, rc);
780
781                 /* Set root stripecount */
782                 sprintf(buf, ".stripecount=%hd",
783                         lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
784                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
785                 if (rc)
786                         GOTO(end, rc);
787
788                 /* Set root stripeoffset */
789                 sprintf(buf, ".stripeoffset=%hd",
790                         lump ? le16_to_cpu(lump->lmm_stripe_offset) :
791                         (typeof(lump->lmm_stripe_offset))(-1));
792                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
793
794 end:
795                 if (param != NULL)
796                         OBD_FREE(param, MGS_PARAM_MAXLEN);
797         }
798         RETURN(rc);
799 }
800
801 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
802                      int *lmm_size, struct ptlrpc_request **request)
803 {
804         struct ll_sb_info *sbi = ll_i2sbi(inode);
805         struct mdt_body   *body;
806         struct lov_mds_md *lmm = NULL;
807         struct ptlrpc_request *req = NULL;
808         int rc, lmmsize;
809         struct md_op_data *op_data;
810
811         rc = ll_get_max_mdsize(sbi, &lmmsize);
812         if (rc)
813                 RETURN(rc);
814
815         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
816                                      0, lmmsize, LUSTRE_OPC_ANY,
817                                      NULL);
818         if (IS_ERR(op_data))
819                 RETURN(PTR_ERR(op_data));
820
821         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
822         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
823         ll_finish_md_op_data(op_data);
824         if (rc < 0) {
825                 CDEBUG(D_INFO, "md_getattr failed on inode "
826                        "%lu/%u: rc %d\n", inode->i_ino,
827                        inode->i_generation, rc);
828                 GOTO(out, rc);
829         }
830
831         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
832         LASSERT(body != NULL);
833
834         lmmsize = body->eadatasize;
835
836         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
837             lmmsize == 0) {
838                 GOTO(out, rc = -ENODATA);
839         }
840
841         lmm = req_capsule_server_sized_get(&req->rq_pill,
842                                            &RMF_MDT_MD, lmmsize);
843         LASSERT(lmm != NULL);
844
845         /*
846          * This is coming from the MDS, so is probably in
847          * little endian.  We convert it to host endian before
848          * passing it to userspace.
849          */
850         /* We don't swab objects for directories */
851         switch (le32_to_cpu(lmm->lmm_magic)) {
852         case LOV_MAGIC_V1:
853                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
854                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
855                 break;
856         case LOV_MAGIC_V3:
857                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
858                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
859                 break;
860         default:
861                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
862                 rc = -EPROTO;
863         }
864 out:
865         *lmmp = lmm;
866         *lmm_size = lmmsize;
867         *request = req;
868         return rc;
869 }
870
871 /*
872  *  Get MDT index for the inode.
873  */
874 int ll_get_mdt_idx(struct inode *inode)
875 {
876         struct ll_sb_info *sbi = ll_i2sbi(inode);
877         struct md_op_data *op_data;
878         int rc, mdtidx;
879         ENTRY;
880
881         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0,
882                                      0, LUSTRE_OPC_ANY, NULL);
883         if (IS_ERR(op_data))
884                 RETURN(PTR_ERR(op_data));
885
886         op_data->op_flags |= MF_GET_MDT_IDX;
887         rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
888         mdtidx = op_data->op_mds;
889         ll_finish_md_op_data(op_data);
890         if (rc < 0) {
891                 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
892                 RETURN(rc);
893         }
894         return mdtidx;
895 }
896
897 /**
898  * Generic handler to do any pre-copy work.
899  *
900  * It send a first hsm_progress (with extent length == 0) to coordinator as a
901  * first information for it that real work has started.
902  *
903  * Moreover, for a ARCHIVE request, it will sample the file data version and
904  * store it in \a copy.
905  *
906  * \return 0 on success.
907  */
908 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
909 {
910         struct ll_sb_info               *sbi = ll_s2sbi(sb);
911         struct hsm_progress_kernel       hpk;
912         int                              rc;
913         ENTRY;
914
915         /* Forge a hsm_progress based on data from copy. */
916         hpk.hpk_fid = copy->hc_hai.hai_fid;
917         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
918         hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
919         hpk.hpk_extent.length = 0;
920         hpk.hpk_flags = 0;
921         hpk.hpk_errval = 0;
922         hpk.hpk_data_version = 0;
923
924
925         /* For archive request, we need to read the current file version. */
926         if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
927                 struct inode    *inode;
928                 __u64            data_version = 0;
929
930                 /* Get inode for this fid */
931                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
932                 if (IS_ERR(inode)) {
933                         hpk.hpk_flags |= HP_FLAG_RETRY;
934                         /* hpk_errval is >= 0 */
935                         hpk.hpk_errval = -PTR_ERR(inode);
936                         GOTO(progress, rc = PTR_ERR(inode));
937                 }
938
939                 /* Read current file data version */
940                 rc = ll_data_version(inode, &data_version, 1);
941                 iput(inode);
942                 if (rc != 0) {
943                         CDEBUG(D_HSM, "Could not read file data version of "
944                                       DFID" (rc = %d). Archive request ("
945                                       LPX64") could not be done.\n",
946                                       PFID(&copy->hc_hai.hai_fid), rc,
947                                       copy->hc_hai.hai_cookie);
948                         hpk.hpk_flags |= HP_FLAG_RETRY;
949                         /* hpk_errval must be >= 0 */
950                         hpk.hpk_errval = -rc;
951                         GOTO(progress, rc);
952                 }
953
954                 /* Store it the hsm_copy for later copytool use.
955                  * Always modified even if no lsm. */
956                 copy->hc_data_version = data_version;
957         }
958
959 progress:
960         rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
961                            &hpk, NULL);
962
963         RETURN(rc);
964 }
965
966 /**
967  * Generic handler to do any post-copy work.
968  *
969  * It will send the last hsm_progress update to coordinator to inform it
970  * that copy is finished and whether it was successful or not.
971  *
972  * Moreover,
973  * - for ARCHIVE request, it will sample the file data version and compare it
974  *   with the version saved in ll_ioc_copy_start(). If they do not match, copy
975  *   will be considered as failed.
976  * - for RESTORE request, it will sample the file data version and send it to
977  *   coordinator which is useful if the file was imported as 'released'.
978  *
979  * \return 0 on success.
980  */
981 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
982 {
983         struct ll_sb_info               *sbi = ll_s2sbi(sb);
984         struct hsm_progress_kernel       hpk;
985         int                              rc;
986         ENTRY;
987
988         /* If you modify the logic here, also check llapi_hsm_copy_end(). */
989         /* Take care: copy->hc_hai.hai_action, len, gid and data are not
990          * initialized if copy_end was called with copy == NULL.
991          */
992
993         /* Forge a hsm_progress based on data from copy. */
994         hpk.hpk_fid = copy->hc_hai.hai_fid;
995         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
996         hpk.hpk_extent = copy->hc_hai.hai_extent;
997         hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
998         hpk.hpk_errval = copy->hc_errval;
999         hpk.hpk_data_version = 0;
1000
1001         /* For archive request, we need to check the file data was not changed.
1002          *
1003          * For restore request, we need to send the file data version, this is
1004          * useful when the file was created using hsm_import.
1005          */
1006         if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
1007              (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
1008             (copy->hc_errval == 0)) {
1009                 struct inode    *inode;
1010                 __u64            data_version = 0;
1011
1012                 /* Get lsm for this fid */
1013                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
1014                 if (IS_ERR(inode)) {
1015                         hpk.hpk_flags |= HP_FLAG_RETRY;
1016                         /* hpk_errval must be >= 0 */
1017                         hpk.hpk_errval = -PTR_ERR(inode);
1018                         GOTO(progress, rc = PTR_ERR(inode));
1019                 }
1020
1021                 rc = ll_data_version(inode, &data_version,
1022                                      copy->hc_hai.hai_action == HSMA_ARCHIVE);
1023                 iput(inode);
1024                 if (rc) {
1025                         CDEBUG(D_HSM, "Could not read file data version. "
1026                                       "Request could not be confirmed.\n");
1027                         if (hpk.hpk_errval == 0)
1028                                 hpk.hpk_errval = -rc;
1029                         GOTO(progress, rc);
1030                 }
1031
1032                 /* Store it the hsm_copy for later copytool use.
1033                  * Always modified even if no lsm. */
1034                 hpk.hpk_data_version = data_version;
1035
1036                 /* File could have been stripped during archiving, so we need
1037                  * to check anyway. */
1038                 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
1039                     (copy->hc_data_version != data_version)) {
1040                         CDEBUG(D_HSM, "File data version mismatched. "
1041                               "File content was changed during archiving. "
1042                                DFID", start:"LPX64" current:"LPX64"\n",
1043                                PFID(&copy->hc_hai.hai_fid),
1044                                copy->hc_data_version, data_version);
1045                         /* File was changed, send error to cdt. Do not ask for
1046                          * retry because if a file is modified frequently,
1047                          * the cdt will loop on retried archive requests.
1048                          * The policy engine will ask for a new archive later
1049                          * when the file will not be modified for some tunable
1050                          * time */
1051                         /* we do not notify caller */
1052                         hpk.hpk_flags &= ~HP_FLAG_RETRY;
1053                         /* hpk_errval must be >= 0 */
1054                         hpk.hpk_errval = EBUSY;
1055                 }
1056
1057         }
1058
1059 progress:
1060         rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1061                            &hpk, NULL);
1062
1063         RETURN(rc);
1064 }
1065
1066
1067 static int copy_and_ioctl(int cmd, struct obd_export *exp, void *data, int len)
1068 {
1069         void *ptr;
1070         int rc;
1071
1072         OBD_ALLOC(ptr, len);
1073         if (ptr == NULL)
1074                 return -ENOMEM;
1075         if (copy_from_user(ptr, data, len)) {
1076                 OBD_FREE(ptr, len);
1077                 return -EFAULT;
1078         }
1079         rc = obd_iocontrol(cmd, exp, len, data, NULL);
1080         OBD_FREE(ptr, len);
1081         return rc;
1082 }
1083
1084 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
1085 {
1086         int cmd = qctl->qc_cmd;
1087         int type = qctl->qc_type;
1088         int id = qctl->qc_id;
1089         int valid = qctl->qc_valid;
1090         int rc = 0;
1091         ENTRY;
1092
1093         switch (cmd) {
1094         case LUSTRE_Q_INVALIDATE:
1095         case LUSTRE_Q_FINVALIDATE:
1096         case Q_QUOTAON:
1097         case Q_QUOTAOFF:
1098         case Q_SETQUOTA:
1099         case Q_SETINFO:
1100                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1101                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1102                         RETURN(-EPERM);
1103                 break;
1104         case Q_GETQUOTA:
1105                 if (((type == USRQUOTA && cfs_curproc_euid() != id) ||
1106                      (type == GRPQUOTA && !in_egroup_p(id))) &&
1107                     (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1108                      sbi->ll_flags & LL_SBI_RMT_CLIENT))
1109                         RETURN(-EPERM);
1110                 break;
1111         case Q_GETINFO:
1112                 break;
1113         default:
1114                 CERROR("unsupported quotactl op: %#x\n", cmd);
1115                 RETURN(-ENOTTY);
1116         }
1117
1118         if (valid != QC_GENERAL) {
1119                 if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
1120                         RETURN(-EOPNOTSUPP);
1121
1122                 if (cmd == Q_GETINFO)
1123                         qctl->qc_cmd = Q_GETOINFO;
1124                 else if (cmd == Q_GETQUOTA)
1125                         qctl->qc_cmd = Q_GETOQUOTA;
1126                 else
1127                         RETURN(-EINVAL);
1128
1129                 switch (valid) {
1130                 case QC_MDTIDX:
1131                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1132                                            sizeof(*qctl), qctl, NULL);
1133                         break;
1134                 case QC_OSTIDX:
1135                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1136                                            sizeof(*qctl), qctl, NULL);
1137                         break;
1138                 case QC_UUID:
1139                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1140                                            sizeof(*qctl), qctl, NULL);
1141                         if (rc == -EAGAIN)
1142                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1143                                                    sbi->ll_dt_exp,
1144                                                    sizeof(*qctl), qctl, NULL);
1145                         break;
1146                 default:
1147                         rc = -EINVAL;
1148                         break;
1149                 }
1150
1151                 if (rc)
1152                         RETURN(rc);
1153
1154                 qctl->qc_cmd = cmd;
1155         } else {
1156                 struct obd_quotactl *oqctl;
1157
1158                 OBD_ALLOC_PTR(oqctl);
1159                 if (oqctl == NULL)
1160                         RETURN(-ENOMEM);
1161
1162                 QCTL_COPY(oqctl, qctl);
1163                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1164                 if (rc) {
1165                         if (rc != -EALREADY && cmd == Q_QUOTAON) {
1166                                 oqctl->qc_cmd = Q_QUOTAOFF;
1167                                 obd_quotactl(sbi->ll_md_exp, oqctl);
1168                         }
1169                         OBD_FREE_PTR(oqctl);
1170                         RETURN(rc);
1171                 }
1172                 /* If QIF_SPACE is not set, client should collect the
1173                  * space usage from OSSs by itself */
1174                 if (cmd == Q_GETQUOTA &&
1175                     !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1176                     !oqctl->qc_dqblk.dqb_curspace) {
1177                         struct obd_quotactl *oqctl_tmp;
1178
1179                         OBD_ALLOC_PTR(oqctl_tmp);
1180                         if (oqctl_tmp == NULL)
1181                                 GOTO(out, rc = -ENOMEM);
1182
1183                         oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1184                         oqctl_tmp->qc_id = oqctl->qc_id;
1185                         oqctl_tmp->qc_type = oqctl->qc_type;
1186
1187                         /* collect space usage from OSTs */
1188                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1189                         rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1190                         if (!rc || rc == -EREMOTEIO) {
1191                                 oqctl->qc_dqblk.dqb_curspace =
1192                                         oqctl_tmp->qc_dqblk.dqb_curspace;
1193                                 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1194                         }
1195
1196                         /* collect space & inode usage from MDTs */
1197                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1198                         oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1199                         rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1200                         if (!rc || rc == -EREMOTEIO) {
1201                                 oqctl->qc_dqblk.dqb_curspace +=
1202                                         oqctl_tmp->qc_dqblk.dqb_curspace;
1203                                 oqctl->qc_dqblk.dqb_curinodes =
1204                                         oqctl_tmp->qc_dqblk.dqb_curinodes;
1205                                 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1206                         } else {
1207                                 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1208                         }
1209
1210                         OBD_FREE_PTR(oqctl_tmp);
1211                 }
1212 out:
1213                 QCTL_COPY(qctl, oqctl);
1214                 OBD_FREE_PTR(oqctl);
1215         }
1216
1217         RETURN(rc);
1218 }
1219
1220 static char *
1221 ll_getname(const char __user *filename)
1222 {
1223         int ret = 0, len;
1224         char *tmp = __getname();
1225
1226         if (!tmp)
1227                 return ERR_PTR(-ENOMEM);
1228
1229         len = strncpy_from_user(tmp, filename, PATH_MAX);
1230         if (len == 0)
1231                 ret = -ENOENT;
1232         else if (len > PATH_MAX)
1233                 ret = -ENAMETOOLONG;
1234
1235         if (ret) {
1236                 __putname(tmp);
1237                 tmp =  ERR_PTR(ret);
1238         }
1239         return tmp;
1240 }
1241
1242 #define ll_putname(filename) __putname(filename)
1243
1244 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1245 {
1246         struct inode *inode = file->f_dentry->d_inode;
1247         struct ll_sb_info *sbi = ll_i2sbi(inode);
1248         struct obd_ioctl_data *data;
1249         int rc = 0;
1250         ENTRY;
1251
1252         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
1253                inode->i_ino, inode->i_generation, inode, cmd);
1254
1255         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1256         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1257                 return -ENOTTY;
1258
1259         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1260         switch(cmd) {
1261         case FSFILT_IOC_GETFLAGS:
1262         case FSFILT_IOC_SETFLAGS:
1263                 RETURN(ll_iocontrol(inode, file, cmd, arg));
1264         case FSFILT_IOC_GETVERSION_OLD:
1265         case FSFILT_IOC_GETVERSION:
1266                 RETURN(put_user(inode->i_generation, (int *)arg));
1267         /* We need to special case any other ioctls we want to handle,
1268          * to send them to the MDS/OST as appropriate and to properly
1269          * network encode the arg field.
1270         case FSFILT_IOC_SETVERSION_OLD:
1271         case FSFILT_IOC_SETVERSION:
1272         */
1273         case LL_IOC_GET_MDTIDX: {
1274                 int mdtidx;
1275
1276                 mdtidx = ll_get_mdt_idx(inode);
1277                 if (mdtidx < 0)
1278                         RETURN(mdtidx);
1279
1280                 if (put_user((int)mdtidx, (int*)arg))
1281                         RETURN(-EFAULT);
1282
1283                 return 0;
1284         }
1285         case IOC_MDC_LOOKUP: {
1286                 struct ptlrpc_request *request = NULL;
1287                 int namelen, len = 0;
1288                 char *buf = NULL;
1289                 char *filename;
1290                 struct md_op_data *op_data;
1291
1292                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1293                 if (rc)
1294                         RETURN(rc);
1295                 data = (void *)buf;
1296
1297                 filename = data->ioc_inlbuf1;
1298                 namelen = strlen(filename);
1299
1300                 if (namelen < 1) {
1301                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1302                         GOTO(out_free, rc = -EINVAL);
1303                 }
1304
1305                 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename, namelen,
1306                                              0, LUSTRE_OPC_ANY, NULL);
1307                 if (IS_ERR(op_data))
1308                         GOTO(out_free, rc = PTR_ERR(op_data));
1309
1310                 op_data->op_valid = OBD_MD_FLID;
1311                 rc = md_getattr_name(sbi->ll_md_exp, op_data, &request);
1312                 ll_finish_md_op_data(op_data);
1313                 if (rc < 0) {
1314                         CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
1315                         GOTO(out_free, rc);
1316                 }
1317                 ptlrpc_req_finished(request);
1318                 EXIT;
1319 out_free:
1320                 obd_ioctl_freedata(buf, len);
1321                 return rc;
1322         }
1323         case LL_IOC_LMV_SETSTRIPE: {
1324                 struct lmv_user_md  *lum;
1325                 char            *buf = NULL;
1326                 char            *filename;
1327                 int              namelen = 0;
1328                 int              lumlen = 0;
1329                 int              len;
1330                 int              rc;
1331
1332                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1333                 if (rc)
1334                         RETURN(rc);
1335
1336                 data = (void *)buf;
1337                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1338                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1339                         GOTO(lmv_out_free, rc = -EINVAL);
1340
1341                 filename = data->ioc_inlbuf1;
1342                 namelen = data->ioc_inllen1;
1343
1344                 if (namelen < 1) {
1345                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1346                         GOTO(lmv_out_free, rc = -EINVAL);
1347                 }
1348                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1349                 lumlen = data->ioc_inllen2;
1350
1351                 if (lum->lum_magic != LMV_USER_MAGIC ||
1352                     lumlen != sizeof(*lum)) {
1353                         CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1354                                filename, lum->lum_magic, lumlen, -EFAULT);
1355                         GOTO(lmv_out_free, rc = -EINVAL);
1356                 }
1357
1358                 /**
1359                  * ll_dir_setdirstripe will be used to set dir stripe
1360                  *  mdc_create--->mdt_reint_create (with dirstripe)
1361                  */
1362                 rc = ll_dir_setdirstripe(inode, lum, filename);
1363 lmv_out_free:
1364                 obd_ioctl_freedata(buf, len);
1365                 RETURN(rc);
1366
1367         }
1368         case LL_IOC_LOV_SETSTRIPE: {
1369                 struct lov_user_md_v3 lumv3;
1370                 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1371                 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1372                 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1373
1374                 int set_default = 0;
1375
1376                 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1377                 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1378                         sizeof(lumv3p->lmm_objects[0]));
1379                 /* first try with v1 which is smaller than v3 */
1380                 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1381                         RETURN(-EFAULT);
1382
1383                 if ((lumv1->lmm_magic == LOV_USER_MAGIC_V3) ) {
1384                         if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1385                                 RETURN(-EFAULT);
1386                 }
1387
1388                 if (inode->i_sb->s_root == file->f_dentry)
1389                         set_default = 1;
1390
1391                 /* in v1 and v3 cases lumv1 points to data */
1392                 rc = ll_dir_setstripe(inode, lumv1, set_default);
1393
1394                 RETURN(rc);
1395         }
1396         case LL_IOC_LMV_GETSTRIPE: {
1397                 struct lmv_user_md *lump = (struct lmv_user_md *)arg;
1398                 struct lmv_user_md lum;
1399                 struct lmv_user_md *tmp;
1400                 int lum_size;
1401                 int rc = 0;
1402                 int mdtindex;
1403
1404                 if (copy_from_user(&lum, lump, sizeof(struct lmv_user_md)))
1405                         RETURN(-EFAULT);
1406
1407                 if (lum.lum_magic != LMV_MAGIC_V1)
1408                         RETURN(-EINVAL);
1409
1410                 lum_size = lmv_user_md_size(1, LMV_MAGIC_V1);
1411                 OBD_ALLOC(tmp, lum_size);
1412                 if (tmp == NULL)
1413                         GOTO(free_lmv, rc = -ENOMEM);
1414
1415                 memcpy(tmp, &lum, sizeof(lum));
1416                 tmp->lum_type = LMV_STRIPE_TYPE;
1417                 tmp->lum_stripe_count = 1;
1418                 mdtindex = ll_get_mdt_idx(inode);
1419                 if (mdtindex < 0)
1420                         GOTO(free_lmv, rc = -ENOMEM);
1421
1422                 tmp->lum_stripe_offset = mdtindex;
1423                 tmp->lum_objects[0].lum_mds = mdtindex;
1424                 memcpy(&tmp->lum_objects[0].lum_fid, ll_inode2fid(inode),
1425                        sizeof(struct lu_fid));
1426                 if (copy_to_user((void *)arg, tmp, lum_size))
1427                         GOTO(free_lmv, rc = -EFAULT);
1428 free_lmv:
1429                 if (tmp)
1430                         OBD_FREE(tmp, lum_size);
1431                 RETURN(rc);
1432         }
1433         case LL_IOC_REMOVE_ENTRY: {
1434                 char            *filename = NULL;
1435                 int              namelen = 0;
1436                 int              rc;
1437
1438                 /* Here is a little hack to avoid sending REINT_RMENTRY to
1439                  * unsupported server, which might crash the server(LU-2730),
1440                  * Because both LVB_TYPE and REINT_RMENTRY will be supported
1441                  * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1442                  * server will support REINT_RMENTRY XXX*/
1443                 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1444                         return -ENOTSUPP;
1445
1446                 filename = ll_getname((const char *)arg);
1447                 if (IS_ERR(filename))
1448                         RETURN(PTR_ERR(filename));
1449
1450                 namelen = strlen(filename);
1451                 if (namelen < 1)
1452                         GOTO(out_rmdir, rc = -EINVAL);
1453
1454                 rc = ll_rmdir_entry(inode, filename, namelen);
1455 out_rmdir:
1456                 if (filename)
1457                         ll_putname(filename);
1458                 RETURN(rc);
1459         }
1460         case LL_IOC_LOV_SWAP_LAYOUTS:
1461                 RETURN(-EPERM);
1462         case LL_IOC_OBD_STATFS:
1463                 RETURN(ll_obd_statfs(inode, (void *)arg));
1464         case LL_IOC_LOV_GETSTRIPE:
1465         case LL_IOC_MDC_GETINFO:
1466         case IOC_MDC_GETFILEINFO:
1467         case IOC_MDC_GETFILESTRIPE: {
1468                 struct ptlrpc_request *request = NULL;
1469                 struct lov_user_md *lump;
1470                 struct lov_mds_md *lmm = NULL;
1471                 struct mdt_body *body;
1472                 char *filename = NULL;
1473                 int lmmsize;
1474
1475                 if (cmd == IOC_MDC_GETFILEINFO ||
1476                     cmd == IOC_MDC_GETFILESTRIPE) {
1477                         filename = ll_getname((const char *)arg);
1478                         if (IS_ERR(filename))
1479                                 RETURN(PTR_ERR(filename));
1480
1481                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1482                                                       &lmmsize, &request);
1483                 } else {
1484                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
1485                 }
1486
1487                 if (request) {
1488                         body = req_capsule_server_get(&request->rq_pill,
1489                                                       &RMF_MDT_BODY);
1490                         LASSERT(body != NULL);
1491                 } else {
1492                         GOTO(out_req, rc);
1493                 }
1494
1495                 if (rc < 0) {
1496                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1497                                                cmd == LL_IOC_MDC_GETINFO))
1498                                 GOTO(skip_lmm, rc = 0);
1499                         else
1500                                 GOTO(out_req, rc);
1501                 }
1502
1503                 if (cmd == IOC_MDC_GETFILESTRIPE ||
1504                     cmd == LL_IOC_LOV_GETSTRIPE) {
1505                         lump = (struct lov_user_md *)arg;
1506                 } else {
1507                         struct lov_user_mds_data *lmdp;
1508                         lmdp = (struct lov_user_mds_data *)arg;
1509                         lump = &lmdp->lmd_lmm;
1510                 }
1511                 if (copy_to_user(lump, lmm, lmmsize)) {
1512                         if (copy_to_user(lump, lmm, sizeof(*lump)))
1513                                 GOTO(out_req, rc = -EFAULT);
1514                         rc = -EOVERFLOW;
1515                 }
1516         skip_lmm:
1517                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1518                         struct lov_user_mds_data *lmdp;
1519                         lstat_t st = { 0 };
1520
1521                         st.st_dev     = inode->i_sb->s_dev;
1522                         st.st_mode    = body->mode;
1523                         st.st_nlink   = body->nlink;
1524                         st.st_uid     = body->uid;
1525                         st.st_gid     = body->gid;
1526                         st.st_rdev    = body->rdev;
1527                         st.st_size    = body->size;
1528                         st.st_blksize = PAGE_CACHE_SIZE;
1529                         st.st_blocks  = body->blocks;
1530                         st.st_atime   = body->atime;
1531                         st.st_mtime   = body->mtime;
1532                         st.st_ctime   = body->ctime;
1533                         st.st_ino     = inode->i_ino;
1534
1535                         lmdp = (struct lov_user_mds_data *)arg;
1536                         if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
1537                                 GOTO(out_req, rc = -EFAULT);
1538                 }
1539
1540                 EXIT;
1541         out_req:
1542                 ptlrpc_req_finished(request);
1543                 if (filename)
1544                         ll_putname(filename);
1545                 return rc;
1546         }
1547         case IOC_LOV_GETINFO: {
1548                 struct lov_user_mds_data *lumd;
1549                 struct lov_stripe_md *lsm;
1550                 struct lov_user_md *lum;
1551                 struct lov_mds_md *lmm;
1552                 int lmmsize;
1553                 lstat_t st;
1554
1555                 lumd = (struct lov_user_mds_data *)arg;
1556                 lum = &lumd->lmd_lmm;
1557
1558                 rc = ll_get_max_mdsize(sbi, &lmmsize);
1559                 if (rc)
1560                         RETURN(rc);
1561
1562                 OBD_ALLOC_LARGE(lmm, lmmsize);
1563                 if (lmm == NULL)
1564                         RETURN(-ENOMEM);
1565
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, cfs_curproc_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 };