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