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