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