Whamcloud - gitweb
LU-16052 llog: handle -EBADR for catalog processing
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/llite/dir.c
32  *
33  * Directory code for lustre client.
34  */
35
36 #include <linux/fs.h>
37 #include <linux/pagemap.h>
38 #include <linux/mm.h>
39 #include <linux/version.h>
40 #include <linux/security.h>
41 #include <linux/user_namespace.h>
42 #include <linux/uidgid.h>
43 #include <linux/uaccess.h>
44 #include <linux/buffer_head.h>   // for wait_on_buffer
45 #include <linux/pagevec.h>
46
47 #define DEBUG_SUBSYSTEM S_LLITE
48
49 #include <obd_support.h>
50 #include <obd_class.h>
51 #include <uapi/linux/lustre/lustre_ioctl.h>
52 #include <lustre_lib.h>
53 #include <lustre_dlm.h>
54 #include <lustre_compat.h>
55 #include <lustre_fid.h>
56 #include <lustre_kernelcomm.h>
57 #include <lustre_swab.h>
58 #include <libcfs/libcfs_crypto.h>
59
60 #include "llite_internal.h"
61
62 /*
63  * (new) readdir implementation overview.
64  *
65  * Original lustre readdir implementation cached exact copy of raw directory
66  * pages on the client. These pages were indexed in client page cache by
67  * logical offset in the directory file. This design, while very simple and
68  * intuitive had some inherent problems:
69  *
70  *     . it implies that byte offset to the directory entry serves as a
71  *     telldir(3)/seekdir(3) cookie, but that offset is not stable: in
72  *     ext3/htree directory entries may move due to splits, and more
73  *     importantly,
74  *
75  *     . it is incompatible with the design of split directories for cmd3,
76  *     that assumes that names are distributed across nodes based on their
77  *     hash, and so readdir should be done in hash order.
78  *
79  * New readdir implementation does readdir in hash order, and uses hash of a
80  * file name as a telldir/seekdir cookie. This led to number of complications:
81  *
82  *     . hash is not unique, so it cannot be used to index cached directory
83  *     pages on the client (note, that it requires a whole pageful of hash
84  *     collided entries to cause two pages to have identical hashes);
85  *
86  *     . hash is not unique, so it cannot, strictly speaking, be used as an
87  *     entry cookie. ext3/htree has the same problem and lustre implementation
88  *     mimics their solution: seekdir(hash) positions directory at the first
89  *     entry with the given hash.
90  *
91  * Client side.
92  *
93  * 0. caching
94  *
95  * Client caches directory pages using hash of the first entry as an index. As
96  * noted above hash is not unique, so this solution doesn't work as is:
97  * special processing is needed for "page hash chains" (i.e., sequences of
98  * pages filled with entries all having the same hash value).
99  *
100  * First, such chains have to be detected. To this end, server returns to the
101  * client the hash of the first entry on the page next to one returned. When
102  * client detects that this hash is the same as hash of the first entry on the
103  * returned page, page hash collision has to be handled. Pages in the
104  * hash chain, except first one, are termed "overflow pages".
105  *
106  * Proposed (unimplimented) solution to index uniqueness problem is to
107  * not cache overflow pages.  Instead, when page hash collision is
108  * detected, all overflow pages from emerging chain should be
109  * immediately requested from the server and placed in a special data
110  * structure.  This data structure can be used by ll_readdir() to
111  * process entries from overflow pages.  When readdir invocation
112  * finishes, overflow pages are discarded.  If page hash collision chain
113  * weren't completely processed, next call to readdir will again detect
114  * page hash collision, again read overflow pages in, process next
115  * portion of entries and again discard the pages.  This is not as
116  * wasteful as it looks, because, given reasonable hash, page hash
117  * collisions are extremely rare.
118  *
119  * 1. directory positioning
120  *
121  * When seekdir(hash) is called, original
122  *
123  *
124  *
125  *
126  *
127  *
128  *
129  *
130  * Server.
131  *
132  * identification of and access to overflow pages
133  *
134  * page format
135  *
136  * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
137  * a header lu_dirpage which describes the start/end hash, and whether this
138  * page is empty (contains no dir entry) or hash collide with next page.
139  * After client receives reply, several pages will be integrated into dir page
140  * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
141  * lu_dirpage for this integrated page will be adjusted. See
142  * mdc_adjust_dirpages().
143  *
144  */
145 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
146                              __u64 offset, int *partial_readdir_rc)
147 {
148         struct md_readdir_info mrinfo = {
149                                         .mr_blocking_ast = ll_md_blocking_ast };
150         struct page *page;
151         int rc;
152
153         rc = md_read_page(ll_i2mdexp(dir), op_data, &mrinfo, offset, &page);
154         if (rc != 0)
155                 return ERR_PTR(rc);
156
157         if (partial_readdir_rc && mrinfo.mr_partial_readdir_rc)
158                 *partial_readdir_rc = mrinfo.mr_partial_readdir_rc;
159
160         return page;
161 }
162
163 void ll_release_page(struct inode *inode, struct page *page,
164                      bool remove)
165 {
166         kunmap(page);
167
168         /* Always remove the page for striped dir, because the page is
169          * built from temporarily in LMV layer */
170         if (inode && ll_dir_striped(inode)) {
171                 __free_page(page);
172                 return;
173         }
174
175         if (remove) {
176                 lock_page(page);
177                 if (likely(page->mapping != NULL))
178                         cfs_delete_from_page_cache(page);
179                 unlock_page(page);
180         }
181         put_page(page);
182 }
183
184 #ifdef HAVE_DIR_CONTEXT
185 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
186                 struct dir_context *ctx, int *partial_readdir_rc)
187 {
188 #else
189 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
190                 void *cookie, filldir_t filldir, int *partial_readdir_rc)
191 {
192 #endif
193         struct ll_sb_info *sbi = ll_i2sbi(inode);
194         __u64 pos = *ppos;
195         bool is_api32 = ll_need_32bit_api(sbi);
196         bool is_hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
197         struct page *page;
198         bool done = false;
199         struct llcrypt_str lltr = LLTR_INIT(NULL, 0);
200         int rc = 0;
201         ENTRY;
202
203         if (IS_ENCRYPTED(inode)) {
204                 rc = llcrypt_fname_alloc_buffer(inode, NAME_MAX, &lltr);
205                 if (rc < 0)
206                         RETURN(rc);
207         }
208
209         page = ll_get_dir_page(inode, op_data, pos, partial_readdir_rc);
210
211         while (rc == 0 && !done) {
212                 struct lu_dirpage *dp;
213                 struct lu_dirent  *ent;
214                 __u64 hash;
215                 __u64 next;
216
217                 if (IS_ERR(page)) {
218                         rc = PTR_ERR(page);
219                         break;
220                 }
221
222                 hash = MDS_DIR_END_OFF;
223                 dp = page_address(page);
224                 for (ent = lu_dirent_start(dp); ent != NULL && !done;
225                      ent = lu_dirent_next(ent)) {
226                         __u16          type;
227                         int            namelen;
228                         struct lu_fid  fid;
229                         __u64          lhash;
230                         __u64          ino;
231
232                         hash = le64_to_cpu(ent->lde_hash);
233                         if (hash < pos) /* Skip until we find target hash */
234                                 continue;
235
236                         namelen = le16_to_cpu(ent->lde_namelen);
237                         if (namelen == 0) /* Skip dummy record */
238                                 continue;
239
240                         if (is_api32 && is_hash64)
241                                 lhash = hash >> 32;
242                         else
243                                 lhash = hash;
244                         fid_le_to_cpu(&fid, &ent->lde_fid);
245                         ino = cl_fid_build_ino(&fid, is_api32);
246                         type = S_DT(lu_dirent_type_get(ent));
247                         /* For ll_nfs_get_name_filldir(), it will try to access
248                          * 'ent' through 'lde_name', so the parameter 'name'
249                          * for 'filldir()' must be part of the 'ent'. */
250 #ifdef HAVE_DIR_CONTEXT
251                         ctx->pos = lhash;
252                         if (!IS_ENCRYPTED(inode)) {
253                                 done = !dir_emit(ctx, ent->lde_name, namelen,
254                                                  ino, type);
255                         } else {
256                                 /* Directory is encrypted */
257                                 int save_len = lltr.len;
258                                 struct llcrypt_str de_name =
259                                         LLTR_INIT(ent->lde_name, namelen);
260
261                                 rc = ll_fname_disk_to_usr(inode, 0, 0, &de_name,
262                                                           &lltr, &fid);
263                                 de_name = lltr;
264                                 lltr.len = save_len;
265                                 if (rc) {
266                                         done = 1;
267                                         break;
268                                 }
269                                 done = !dir_emit(ctx, de_name.name, de_name.len,
270                                                  ino, type);
271                         }
272 #else
273                         /* HAVE_DIR_CONTEXT is defined from kernel 3.11, whereas
274                          * IS_ENCRYPTED is brought by kernel 4.14.
275                          * So there is no need to handle encryption case here.
276                          */
277                         done = filldir(cookie, ent->lde_name, namelen, lhash,
278                                        ino, type);
279 #endif
280                 }
281
282                 if (done) {
283                         pos = hash;
284                         ll_release_page(inode, page, false);
285                         break;
286                 }
287
288                 next = le64_to_cpu(dp->ldp_hash_end);
289                 pos = next;
290                 if (pos == MDS_DIR_END_OFF) {
291                         /*
292                          * End of directory reached.
293                          */
294                         done = 1;
295                         ll_release_page(inode, page, false);
296                 } else {
297                         /*
298                          * Normal case: continue to the next
299                          * page.
300                          */
301                         ll_release_page(inode, page,
302                                         le32_to_cpu(dp->ldp_flags) &
303                                         LDF_COLLIDE);
304                         next = pos;
305                         page = ll_get_dir_page(inode, op_data, pos,
306                                                partial_readdir_rc);
307                 }
308         }
309 #ifdef HAVE_DIR_CONTEXT
310         ctx->pos = pos;
311 #else
312         *ppos = pos;
313 #endif
314         llcrypt_fname_free_buffer(&lltr);
315         RETURN(rc);
316 }
317
318 #ifdef HAVE_DIR_CONTEXT
319 static int ll_iterate(struct file *filp, struct dir_context *ctx)
320 #else
321 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
322 #endif
323 {
324         struct inode *inode = file_inode(filp);
325         struct ll_file_data *lfd = filp->private_data;
326         struct ll_sb_info *sbi = ll_i2sbi(inode);
327         bool hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
328         int api32 = ll_need_32bit_api(sbi);
329         struct md_op_data *op_data;
330         struct lu_fid pfid = { 0 };
331         ktime_t kstart = ktime_get();
332         /* result of possible partial readdir */
333         int partial_readdir_rc = 0;
334         __u64 pos;
335         int rc;
336
337         ENTRY;
338
339         LASSERT(lfd != NULL);
340         pos = lfd->lfd_pos;
341
342         CDEBUG(D_VFSTRACE,
343                "VFS Op:inode="DFID"(%p) pos/size%lu/%llu 32bit_api %d\n",
344                PFID(ll_inode2fid(inode)),
345                inode, (unsigned long)pos, i_size_read(inode), api32);
346
347         if (IS_ENCRYPTED(inode)) {
348                 rc = llcrypt_get_encryption_info(inode);
349                 if (rc && rc != -ENOKEY)
350                         GOTO(out, rc);
351         }
352
353         if (pos == MDS_DIR_END_OFF)
354                 /*
355                  * end-of-file.
356                  */
357                 GOTO(out, rc = 0);
358
359         if (unlikely(ll_dir_striped(inode))) {
360                 /*
361                  * This is only needed for striped dir to fill ..,
362                  * see lmv_read_page()
363                  */
364                 if (file_dentry(filp)->d_parent != NULL &&
365                     file_dentry(filp)->d_parent->d_inode != NULL) {
366                         __u64 ibits = MDS_INODELOCK_LOOKUP;
367                         struct inode *parent =
368                                 file_dentry(filp)->d_parent->d_inode;
369
370                         if (ll_have_md_lock(parent, &ibits, LCK_MINMODE))
371                                 pfid = *ll_inode2fid(parent);
372                 }
373
374                 /* If it can not find in cache, do lookup .. on the master
375                  * object */
376                 if (fid_is_zero(&pfid)) {
377                         rc = ll_dir_get_parent_fid(inode, &pfid);
378                         if (rc != 0)
379                                 RETURN(rc);
380                 }
381         }
382
383         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
384                                      LUSTRE_OPC_ANY, inode);
385         if (IS_ERR(op_data))
386                 GOTO(out, rc = PTR_ERR(op_data));
387
388         /* foreign dirs are browsed out of Lustre */
389         if (unlikely(op_data->op_mea1 != NULL &&
390                      op_data->op_mea1->lsm_md_magic == LMV_MAGIC_FOREIGN)) {
391                 ll_finish_md_op_data(op_data);
392                 RETURN(-ENODATA);
393         }
394
395         op_data->op_fid3 = pfid;
396
397 #ifdef HAVE_DIR_CONTEXT
398         ctx->pos = pos;
399         rc = ll_dir_read(inode, &pos, op_data, ctx, &partial_readdir_rc);
400         pos = ctx->pos;
401 #else
402         rc = ll_dir_read(inode, &pos, op_data, cookie, filldir,
403                          &partial_readdir_rc);
404 #endif
405         lfd->lfd_pos = pos;
406         if (!lfd->fd_partial_readdir_rc)
407                 lfd->fd_partial_readdir_rc = partial_readdir_rc;
408
409         if (pos == MDS_DIR_END_OFF) {
410                 if (api32)
411                         pos = LL_DIR_END_OFF_32BIT;
412                 else
413                         pos = LL_DIR_END_OFF;
414         } else {
415                 if (api32 && hash64)
416                         pos = pos >> 32;
417         }
418 #ifdef HAVE_DIR_CONTEXT
419         ctx->pos = pos;
420 #else
421         filp->f_pos = pos;
422 #endif
423         ll_finish_md_op_data(op_data);
424
425 out:
426         if (!rc)
427                 ll_stats_ops_tally(sbi, LPROC_LL_READDIR,
428                                    ktime_us_delta(ktime_get(), kstart));
429
430         RETURN(rc);
431 }
432
433 /**
434  * Create striped directory with specified stripe(@lump)
435  *
436  * \param[in] dparent   the parent of the directory.
437  * \param[in] lump      the specified stripes.
438  * \param[in] dirname   the name of the directory.
439  * \param[in] mode      the specified mode of the directory.
440  *
441  * \retval              =0 if striped directory is being created successfully.
442  *                      <0 if the creation is failed.
443  */
444 static int ll_dir_setdirstripe(struct dentry *dparent, struct lmv_user_md *lump,
445                                size_t len, const char *dirname, umode_t mode,
446                                bool createonly)
447 {
448         struct inode *parent = dparent->d_inode;
449         struct ptlrpc_request *request = NULL;
450         struct md_op_data *op_data;
451         struct ll_sb_info *sbi = ll_i2sbi(parent);
452         struct inode *inode = NULL;
453         struct dentry dentry = {
454                 .d_parent = dparent,
455                 .d_name = {
456                         .name = dirname,
457                         .len = strlen(dirname),
458                         .hash = ll_full_name_hash(dparent, dirname,
459                                                   strlen(dirname)),
460                 },
461                 .d_sb = dparent->d_sb,
462         };
463         bool encrypt = false;
464         int hash_flags;
465         int err;
466
467         ENTRY;
468         if (unlikely(!lmv_user_magic_supported(lump->lum_magic)))
469                 RETURN(-EINVAL);
470
471         if (lump->lum_magic != LMV_MAGIC_FOREIGN) {
472                 CDEBUG(D_VFSTRACE,
473                        "VFS Op:inode="DFID"(%p) name %s stripe_offset %d, stripe_count: %u\n",
474                        PFID(ll_inode2fid(parent)), parent, dirname,
475                        (int)lump->lum_stripe_offset, lump->lum_stripe_count);
476         } else {
477                 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lump;
478
479                 CDEBUG(D_VFSTRACE,
480                        "VFS Op:inode="DFID"(%p) name %s foreign, length %u, value '%.*s'\n",
481                        PFID(ll_inode2fid(parent)), parent, dirname,
482                        lfm->lfm_length, lfm->lfm_length, lfm->lfm_value);
483         }
484
485         if (lump->lum_stripe_count > 1 &&
486             !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
487                 RETURN(-EINVAL);
488
489         if (IS_DEADDIR(parent) &&
490             !OBD_FAIL_CHECK(OBD_FAIL_LLITE_NO_CHECK_DEAD))
491                 RETURN(-ENOENT);
492
493         /* MDS < 2.14 doesn't support 'crush' hash type, and cannot handle
494          * unknown hash if client doesn't set a valid one. switch to fnv_1a_64.
495          */
496         if (!(exp_connect_flags2(sbi->ll_md_exp) & OBD_CONNECT2_CRUSH)) {
497                 enum lmv_hash_type type = lump->lum_hash_type &
498                                           LMV_HASH_TYPE_MASK;
499
500                 if (type >= LMV_HASH_TYPE_CRUSH ||
501                     type == LMV_HASH_TYPE_UNKNOWN)
502                         lump->lum_hash_type = (lump->lum_hash_type ^ type) |
503                                               LMV_HASH_TYPE_FNV_1A_64;
504         }
505
506         hash_flags = lump->lum_hash_type & ~LMV_HASH_TYPE_MASK;
507         if (hash_flags & ~LMV_HASH_FLAG_KNOWN)
508                 RETURN(-EINVAL);
509
510         if (unlikely(!lmv_user_magic_supported(cpu_to_le32(lump->lum_magic))))
511                 lustre_swab_lmv_user_md(lump);
512
513         if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
514                 mode &= ~current_umask();
515         mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
516         op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
517                                      strlen(dirname), mode, LUSTRE_OPC_MKDIR,
518                                      lump);
519         if (IS_ERR(op_data))
520                 RETURN(PTR_ERR(op_data));
521
522         op_data->op_dir_depth = ll_i2info(parent)->lli_dir_depth;
523
524         if (ll_sbi_has_encrypt(sbi) &&
525             (IS_ENCRYPTED(parent) ||
526              unlikely(ll_sb_has_test_dummy_encryption(parent->i_sb)))) {
527                 err = llcrypt_get_encryption_info(parent);
528                 if (err)
529                         GOTO(out_op_data, err);
530                 if (!llcrypt_has_encryption_key(parent))
531                         GOTO(out_op_data, err = -ENOKEY);
532                 encrypt = true;
533         }
534
535         if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags)) {
536                 /* selinux_dentry_init_security() uses dentry->d_parent and name
537                  * to determine the security context for the file. So our fake
538                  * dentry should be real enough for this purpose. */
539                 err = ll_dentry_init_security(&dentry, mode, &dentry.d_name,
540                                               &op_data->op_file_secctx_name,
541                                               &op_data->op_file_secctx_name_size,
542                                               &op_data->op_file_secctx,
543                                               &op_data->op_file_secctx_size);
544                 if (err < 0)
545                         GOTO(out_op_data, err);
546         }
547
548         if (encrypt) {
549                 err = llcrypt_inherit_context(parent, NULL, op_data, false);
550                 if (err)
551                         GOTO(out_op_data, err);
552         }
553
554         op_data->op_cli_flags |= CLI_SET_MEA;
555         if (createonly)
556                 op_data->op_bias |= MDS_SETSTRIPE_CREATE;
557
558         err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
559                         from_kuid(&init_user_ns, current_fsuid()),
560                         from_kgid(&init_user_ns, current_fsgid()),
561                         current_cap(), 0, &request);
562         if (err)
563                 GOTO(out_request, err);
564
565         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
566
567         err = ll_prep_inode(&inode, &request->rq_pill, parent->i_sb, NULL);
568         if (err)
569                 GOTO(out_inode, err);
570
571         dentry.d_inode = inode;
572
573         if (test_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags))
574                 err = ll_inode_notifysecctx(inode, op_data->op_file_secctx,
575                                             op_data->op_file_secctx_size);
576         else
577                 err = ll_inode_init_security(&dentry, inode, parent);
578
579         if (err)
580                 GOTO(out_inode, err);
581
582         if (encrypt) {
583                 err = ll_set_encflags(inode, op_data->op_file_encctx,
584                                       op_data->op_file_encctx_size, false);
585                 if (err)
586                         GOTO(out_inode, err);
587         }
588
589 out_inode:
590         iput(inode);
591 out_request:
592         ptlrpc_req_finished(request);
593 out_op_data:
594         ll_finish_md_op_data(op_data);
595
596         return err;
597 }
598
599 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
600                      int set_default)
601 {
602         struct ll_sb_info *sbi = ll_i2sbi(inode);
603         struct md_op_data *op_data;
604         struct ptlrpc_request *req = NULL;
605         int rc = 0;
606         int lum_size;
607         ENTRY;
608
609         if (lump != NULL) {
610                 switch (lump->lmm_magic) {
611                 case LOV_USER_MAGIC_V1:
612                         lum_size = sizeof(struct lov_user_md_v1);
613                         break;
614                 case LOV_USER_MAGIC_V3:
615                         lum_size = sizeof(struct lov_user_md_v3);
616                         break;
617                 case LOV_USER_MAGIC_COMP_V1:
618                         lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
619                         break;
620                 case LMV_USER_MAGIC: {
621                         struct lmv_user_md *lmv = (struct lmv_user_md *)lump;
622
623                         /* MDS < 2.14 doesn't support 'crush' hash type, and
624                          * cannot handle unknown hash if client doesn't set a
625                          * valid one. switch to fnv_1a_64.
626                          */
627                         if (!(exp_connect_flags2(sbi->ll_md_exp) &
628                               OBD_CONNECT2_CRUSH)) {
629                                 enum lmv_hash_type type = lmv->lum_hash_type &
630                                                           LMV_HASH_TYPE_MASK;
631
632                                 if (type >= LMV_HASH_TYPE_CRUSH ||
633                                     type == LMV_HASH_TYPE_UNKNOWN)
634                                         lmv->lum_hash_type =
635                                                 (lmv->lum_hash_type ^ type) |
636                                                 LMV_HASH_TYPE_FNV_1A_64;
637                         }
638                         if (lmv->lum_magic != cpu_to_le32(LMV_USER_MAGIC))
639                                 lustre_swab_lmv_user_md(lmv);
640                         lum_size = sizeof(*lmv);
641                         break;
642                 }
643                 case LOV_USER_MAGIC_SPECIFIC: {
644                         struct lov_user_md_v3 *v3 =
645                                 (struct lov_user_md_v3 *)lump;
646                         if (v3->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
647                                 RETURN(-EINVAL);
648                         lum_size = lov_user_md_size(v3->lmm_stripe_count,
649                                                     LOV_USER_MAGIC_SPECIFIC);
650                         break;
651                 }
652                 default:
653                         CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
654                                         " %#08x != %#08x nor %#08x\n",
655                                         lump->lmm_magic, LOV_USER_MAGIC_V1,
656                                         LOV_USER_MAGIC_V3);
657                         RETURN(-EINVAL);
658                 }
659
660                 /*
661                  * This is coming from userspace, so should be in
662                  * local endian.  But the MDS would like it in little
663                  * endian, so we swab it before we send it.
664                  */
665                 if ((__swab32(lump->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
666                     le32_to_cpu(LOV_MAGIC_MAGIC))
667                         lustre_swab_lov_user_md(lump, 0);
668         } else {
669                 lum_size = sizeof(struct lov_user_md_v1);
670         }
671
672         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
673                                      LUSTRE_OPC_ANY, NULL);
674         if (IS_ERR(op_data))
675                 RETURN(PTR_ERR(op_data));
676
677         /* swabbing is done in lov_setstripe() on server side */
678         rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
679         ll_finish_md_op_data(op_data);
680         ptlrpc_req_finished(req);
681         if (rc)
682                 RETURN(rc);
683
684         RETURN(rc);
685 }
686
687 static int ll_dir_get_default_layout(struct inode *inode, void **plmm,
688                                      int *plmm_size,
689                                      struct ptlrpc_request **request, u64 valid,
690                                      enum get_default_layout_type type)
691 {
692         struct ll_sb_info *sbi = ll_i2sbi(inode);
693         struct mdt_body   *body;
694         struct lov_mds_md *lmm = NULL;
695         struct ptlrpc_request *req = NULL;
696         int rc, lmm_size;
697         struct md_op_data *op_data;
698         struct lu_fid fid;
699         ENTRY;
700
701         rc = ll_get_default_mdsize(sbi, &lmm_size);
702         if (rc)
703                 RETURN(rc);
704
705         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
706                                      0, lmm_size, LUSTRE_OPC_ANY,
707                                      NULL);
708         if (IS_ERR(op_data))
709                 RETURN(PTR_ERR(op_data));
710
711         op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
712
713         if (type == GET_DEFAULT_LAYOUT_ROOT) {
714                 lu_root_fid(&op_data->op_fid1);
715                 fid = op_data->op_fid1;
716         } else {
717                 fid = *ll_inode2fid(inode);
718         }
719
720         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
721         ll_finish_md_op_data(op_data);
722         if (rc < 0) {
723                 CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
724                        PFID(&fid), rc);
725                 GOTO(out, rc);
726         }
727
728         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
729         LASSERT(body != NULL);
730
731         lmm_size = body->mbo_eadatasize;
732
733         if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
734             lmm_size == 0) {
735                 GOTO(out, rc = -ENODATA);
736         }
737
738         lmm = req_capsule_server_sized_get(&req->rq_pill,
739                                            &RMF_MDT_MD, lmm_size);
740         LASSERT(lmm != NULL);
741
742         /*
743          * This is coming from the MDS, so is probably in
744          * little endian.  We convert it to host endian before
745          * passing it to userspace.
746          */
747         /* We don't swab objects for directories */
748         switch (le32_to_cpu(lmm->lmm_magic)) {
749         case LOV_MAGIC_V1:
750         case LOV_MAGIC_V3:
751         case LOV_MAGIC_COMP_V1:
752         case LOV_USER_MAGIC_SPECIFIC:
753                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
754                         lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
755                 break;
756         case LMV_MAGIC_V1:
757                 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
758                         lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
759                 break;
760         case LMV_USER_MAGIC:
761                 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
762                         lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
763                 break;
764         case LMV_MAGIC_FOREIGN: {
765                 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lmm;
766
767                 if (LMV_MAGIC_FOREIGN != cpu_to_le32(LMV_MAGIC_FOREIGN)) {
768                         __swab32s(&lfm->lfm_magic);
769                         __swab32s(&lfm->lfm_length);
770                         __swab32s(&lfm->lfm_type);
771                         __swab32s(&lfm->lfm_flags);
772                 }
773                 break;
774         }
775         default:
776                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
777                 rc = -EPROTO;
778         }
779 out:
780         *plmm = lmm;
781         *plmm_size = lmm_size;
782         *request = req;
783         return rc;
784 }
785
786 /**
787  * This function will be used to get default LOV/LMV/Default LMV
788  * @valid will be used to indicate which stripe it will retrieve.
789  * If the directory does not have its own default layout, then the
790  * function will request the default layout from root FID.
791  *      OBD_MD_MEA              LMV stripe EA
792  *      OBD_MD_DEFAULT_MEA      Default LMV stripe EA
793  *      otherwise               Default LOV EA.
794  * Each time, it can only retrieve 1 stripe EA
795  **/
796 int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
797                              struct ptlrpc_request **request,
798                              struct ptlrpc_request **root_request,
799                              u64 valid)
800 {
801         struct ptlrpc_request *req = NULL;
802         struct ptlrpc_request *root_req = NULL;
803         struct lov_mds_md *lmm = NULL;
804         int lmm_size = 0;
805         int rc = 0;
806         ENTRY;
807
808         rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
809                                        &req, valid, 0);
810         if (rc == -ENODATA && !fid_is_root(ll_inode2fid(inode)) &&
811             !(valid & OBD_MD_MEA) && root_request != NULL) {
812                 int rc2 = ll_dir_get_default_layout(inode, (void **)&lmm,
813                                                     &lmm_size, &root_req, valid,
814                                                     GET_DEFAULT_LAYOUT_ROOT);
815                 if (rc2 == 0)
816                         rc = 0;
817         }
818
819         *plmm = lmm;
820         *plmm_size = lmm_size;
821         *request = req;
822         if (root_request != NULL)
823                 *root_request = root_req;
824
825         RETURN(rc);
826 }
827
828 /**
829  * This function will be used to get default LOV/LMV/Default LMV
830  * @valid will be used to indicate which stripe it will retrieve
831  *      OBD_MD_MEA              LMV stripe EA
832  *      OBD_MD_DEFAULT_MEA      Default LMV stripe EA
833  *      otherwise               Default LOV EA.
834  * Each time, it can only retrieve 1 stripe EA
835  **/
836 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
837                      struct ptlrpc_request **request, u64 valid)
838 {
839         struct ptlrpc_request *req = NULL;
840         struct lov_mds_md *lmm = NULL;
841         int lmm_size = 0;
842         int rc = 0;
843         ENTRY;
844
845         rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
846                                        &req, valid, 0);
847
848         *plmm = lmm;
849         *plmm_size = lmm_size;
850         *request = req;
851
852         RETURN(rc);
853 }
854
855 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
856 {
857         struct md_op_data       *op_data;
858         int                     rc;
859         int                     mdt_index;
860         ENTRY;
861
862         OBD_ALLOC_PTR(op_data);
863         if (op_data == NULL)
864                 RETURN(-ENOMEM);
865
866         op_data->op_flags |= MF_GET_MDT_IDX;
867         op_data->op_fid1 = *fid;
868         rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
869         mdt_index = op_data->op_mds;
870         OBD_FREE_PTR(op_data);
871         if (rc < 0)
872                 RETURN(rc);
873
874         RETURN(mdt_index);
875 }
876
877 /*
878  *  Get MDT index for the inode.
879  */
880 int ll_get_mdt_idx(struct inode *inode)
881 {
882         return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
883 }
884
885 /**
886  * Generic handler to do any pre-copy work.
887  *
888  * It sends a first hsm_progress (with extent length == 0) to coordinator as a
889  * first information for it that real work has started.
890  *
891  * Moreover, for a ARCHIVE request, it will sample the file data version and
892  * store it in \a copy.
893  *
894  * \return 0 on success.
895  */
896 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
897 {
898         struct ll_sb_info               *sbi = ll_s2sbi(sb);
899         struct hsm_progress_kernel       hpk;
900         int                              rc = 0;
901         int                              rc2;
902         ENTRY;
903
904         /* Forge a hsm_progress based on data from copy. */
905         hpk.hpk_fid = copy->hc_hai.hai_fid;
906         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
907         hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
908         hpk.hpk_extent.length = 0;
909         hpk.hpk_flags = 0;
910         hpk.hpk_errval = 0;
911         hpk.hpk_data_version = 0;
912
913
914         /* For archive request, we need to read the current file version. */
915         if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
916                 struct inode    *inode;
917                 __u64            data_version = 0;
918
919                 /* Get inode for this fid */
920                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
921                 if (IS_ERR(inode)) {
922                         hpk.hpk_flags |= HP_FLAG_RETRY;
923                         /* hpk_errval is >= 0 */
924                         hpk.hpk_errval = -PTR_ERR(inode);
925                         GOTO(progress, rc = PTR_ERR(inode));
926                 }
927
928                 /* Read current file data version */
929                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
930                 iput(inode);
931                 if (rc != 0) {
932                         CDEBUG(D_HSM, "Could not read file data version of "
933                                       DFID" (rc = %d). Archive request ("
934                                       "%#llx) could not be done.\n",
935                                       PFID(&copy->hc_hai.hai_fid), rc,
936                                       copy->hc_hai.hai_cookie);
937                         hpk.hpk_flags |= HP_FLAG_RETRY;
938                         /* hpk_errval must be >= 0 */
939                         hpk.hpk_errval = -rc;
940                         GOTO(progress, rc);
941                 }
942
943                 /* Store in the hsm_copy for later copytool use.
944                  * Always modified even if no lsm. */
945                 copy->hc_data_version = data_version;
946         }
947
948 progress:
949         /* On error, the request should be considered as completed */
950         if (hpk.hpk_errval > 0)
951                 hpk.hpk_flags |= HP_FLAG_COMPLETED;
952
953         rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
954                             &hpk, NULL);
955
956         /* Return first error */
957         RETURN(rc != 0 ? rc : rc2);
958 }
959
960 /**
961  * Generic handler to do any post-copy work.
962  *
963  * It will send the last hsm_progress update to coordinator to inform it
964  * that copy is finished and whether it was successful or not.
965  *
966  * Moreover,
967  * - for ARCHIVE request, it will sample the file data version and compare it
968  *   with the version saved in ll_ioc_copy_start(). If they do not match, copy
969  *   will be considered as failed.
970  * - for RESTORE request, it will sample the file data version and send it to
971  *   coordinator which is useful if the file was imported as 'released'.
972  *
973  * \return 0 on success.
974  */
975 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
976 {
977         struct ll_sb_info               *sbi = ll_s2sbi(sb);
978         struct hsm_progress_kernel       hpk;
979         int                              rc = 0;
980         int                              rc2;
981         ENTRY;
982
983         /* If you modify the logic here, also check llapi_hsm_copy_end(). */
984         /* Take care: copy->hc_hai.hai_action, len, gid and data are not
985          * initialized if copy_end was called with copy == NULL.
986          */
987
988         /* Forge a hsm_progress based on data from copy. */
989         hpk.hpk_fid = copy->hc_hai.hai_fid;
990         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
991         hpk.hpk_extent = copy->hc_hai.hai_extent;
992         hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
993         hpk.hpk_errval = copy->hc_errval;
994         hpk.hpk_data_version = 0;
995
996         /* For archive request, we need to check the file data was not changed.
997          *
998          * For restore request, we need to send the file data version, this is
999          * useful when the file was created using hsm_import.
1000          */
1001         if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
1002              (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
1003             (copy->hc_errval == 0)) {
1004                 struct inode    *inode;
1005                 __u64            data_version = 0;
1006
1007                 /* Get lsm for this fid */
1008                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
1009                 if (IS_ERR(inode)) {
1010                         hpk.hpk_flags |= HP_FLAG_RETRY;
1011                         /* hpk_errval must be >= 0 */
1012                         hpk.hpk_errval = -PTR_ERR(inode);
1013                         GOTO(progress, rc = PTR_ERR(inode));
1014                 }
1015
1016                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
1017                 iput(inode);
1018                 if (rc) {
1019                         CDEBUG(D_HSM, "Could not read file data version. "
1020                                       "Request could not be confirmed.\n");
1021                         if (hpk.hpk_errval == 0)
1022                                 hpk.hpk_errval = -rc;
1023                         GOTO(progress, rc);
1024                 }
1025
1026                 /* Store in the hsm_copy for later copytool use.
1027                  * Always modified even if no lsm. */
1028                 hpk.hpk_data_version = data_version;
1029
1030                 /* File could have been stripped during archiving, so we need
1031                  * to check anyway. */
1032                 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
1033                     (copy->hc_data_version != data_version)) {
1034                         CDEBUG(D_HSM, "File data version mismatched. "
1035                               "File content was changed during archiving. "
1036                                DFID", start:%#llx current:%#llx\n",
1037                                PFID(&copy->hc_hai.hai_fid),
1038                                copy->hc_data_version, data_version);
1039                         /* File was changed, send error to cdt. Do not ask for
1040                          * retry because if a file is modified frequently,
1041                          * the cdt will loop on retried archive requests.
1042                          * The policy engine will ask for a new archive later
1043                          * when the file will not be modified for some tunable
1044                          * time */
1045                         hpk.hpk_flags &= ~HP_FLAG_RETRY;
1046                         rc = -EBUSY;
1047                         /* hpk_errval must be >= 0 */
1048                         hpk.hpk_errval = -rc;
1049                         GOTO(progress, rc);
1050                 }
1051
1052         }
1053
1054 progress:
1055         rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
1056                             &hpk, NULL);
1057
1058         /* Return first error */
1059         RETURN(rc != 0 ? rc : rc2);
1060 }
1061
1062
1063 static int copy_and_ct_start(int cmd, struct obd_export *exp,
1064                              const struct lustre_kernelcomm __user *data)
1065 {
1066         struct lustre_kernelcomm *lk;
1067         struct lustre_kernelcomm *tmp;
1068         size_t size = sizeof(*lk);
1069         size_t new_size;
1070         int i;
1071         int rc;
1072
1073         /* copy data from userspace to get numbers of archive_id */
1074         OBD_ALLOC(lk, size);
1075         if (lk == NULL)
1076                 return -ENOMEM;
1077
1078         if (copy_from_user(lk, data, size))
1079                 GOTO(out_lk, rc = -EFAULT);
1080
1081         if (lk->lk_flags & LK_FLG_STOP)
1082                 goto do_ioctl;
1083
1084         if (!(lk->lk_flags & LK_FLG_DATANR)) {
1085                 __u32 archive_mask = lk->lk_data_count;
1086                 int count;
1087
1088                 /* old hsm agent to old MDS */
1089                 if (!exp_connect_archive_id_array(exp))
1090                         goto do_ioctl;
1091
1092                 /* old hsm agent to new MDS */
1093                 lk->lk_flags |= LK_FLG_DATANR;
1094
1095                 if (archive_mask == 0)
1096                         goto do_ioctl;
1097
1098                 count = hweight32(archive_mask);
1099                 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1100                 OBD_ALLOC(tmp, new_size);
1101                 if (tmp == NULL)
1102                         GOTO(out_lk, rc = -ENOMEM);
1103
1104                 memcpy(tmp, lk, size);
1105                 tmp->lk_data_count = count;
1106                 OBD_FREE(lk, size);
1107                 lk = tmp;
1108                 size = new_size;
1109
1110                 count = 0;
1111                 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1112                         if (BIT(i) & archive_mask) {
1113                                 lk->lk_data[count] = i + 1;
1114                                 count++;
1115                         }
1116                 }
1117                 goto do_ioctl;
1118         }
1119
1120         /* new hsm agent to new mds */
1121         if (lk->lk_data_count > 0) {
1122                 new_size = offsetof(struct lustre_kernelcomm,
1123                                     lk_data[lk->lk_data_count]);
1124                 OBD_ALLOC(tmp, new_size);
1125                 if (tmp == NULL)
1126                         GOTO(out_lk, rc = -ENOMEM);
1127
1128                 OBD_FREE(lk, size);
1129                 lk = tmp;
1130                 size = new_size;
1131
1132                 if (copy_from_user(lk, data, size))
1133                         GOTO(out_lk, rc = -EFAULT);
1134         }
1135
1136         /* new hsm agent to old MDS */
1137         if (!exp_connect_archive_id_array(exp)) {
1138                 __u32 archives = 0;
1139
1140                 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1141                         GOTO(out_lk, rc = -EINVAL);
1142
1143                 for (i = 0; i < lk->lk_data_count; i++) {
1144                         if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1145                                 rc = -EINVAL;
1146                                 CERROR("%s: archive id %d requested but only "
1147                                        "[0 - %zu] supported: rc = %d\n",
1148                                        exp->exp_obd->obd_name, lk->lk_data[i],
1149                                        LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1150                                 GOTO(out_lk, rc);
1151                         }
1152
1153                         if (lk->lk_data[i] == 0) {
1154                                 archives = 0;
1155                                 break;
1156                         }
1157
1158                         archives |= (1 << (lk->lk_data[i] - 1));
1159                 }
1160                 lk->lk_flags &= ~LK_FLG_DATANR;
1161                 lk->lk_data_count = archives;
1162         }
1163 do_ioctl:
1164         rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1165 out_lk:
1166         OBD_FREE(lk, size);
1167         return rc;
1168 }
1169
1170 static int check_owner(int type, int id)
1171 {
1172         switch (type) {
1173         case USRQUOTA:
1174                 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1175                         return -EPERM;
1176                 break;
1177         case GRPQUOTA:
1178                 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1179                         return -EPERM;
1180                 break;
1181         case PRJQUOTA:
1182                 break;
1183         }
1184         return 0;
1185 }
1186
1187 int quotactl_ioctl(struct super_block *sb, struct if_quotactl *qctl)
1188 {
1189         struct ll_sb_info *sbi = ll_s2sbi(sb);
1190         int cmd = qctl->qc_cmd;
1191         int type = qctl->qc_type;
1192         int id = qctl->qc_id;
1193         int valid = qctl->qc_valid;
1194         int rc = 0;
1195
1196         ENTRY;
1197
1198         switch (cmd) {
1199         case Q_SETQUOTA:
1200         case Q_SETINFO:
1201         case LUSTRE_Q_SETDEFAULT:
1202         case LUSTRE_Q_SETQUOTAPOOL:
1203         case LUSTRE_Q_SETINFOPOOL:
1204         case LUSTRE_Q_SETDEFAULT_POOL:
1205         case LUSTRE_Q_DELETEQID:
1206                 if (!capable(CAP_SYS_ADMIN))
1207                         RETURN(-EPERM);
1208
1209                 if (sb->s_flags & SB_RDONLY)
1210                         RETURN(-EROFS);
1211                 break;
1212         case Q_GETQUOTA:
1213         case LUSTRE_Q_GETDEFAULT:
1214         case LUSTRE_Q_GETQUOTAPOOL:
1215         case LUSTRE_Q_GETDEFAULT_POOL:
1216                 if (check_owner(type, id) &&
1217                     (!capable(CAP_SYS_ADMIN)))
1218                         RETURN(-EPERM);
1219                 break;
1220         case Q_GETINFO:
1221         case LUSTRE_Q_GETINFOPOOL:
1222                 break;
1223         default:
1224                 CERROR("unsupported quotactl op: %#x\n", cmd);
1225                 RETURN(-ENOTSUPP);
1226         }
1227
1228         if (valid != QC_GENERAL) {
1229                 if (cmd == Q_GETINFO)
1230                         qctl->qc_cmd = Q_GETOINFO;
1231                 else if (cmd == Q_GETQUOTA ||
1232                          cmd == LUSTRE_Q_GETQUOTAPOOL)
1233                         qctl->qc_cmd = Q_GETOQUOTA;
1234                 else
1235                         RETURN(-EINVAL);
1236
1237                 switch (valid) {
1238                 case QC_MDTIDX:
1239                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1240                                            sizeof(*qctl), qctl, NULL);
1241                         break;
1242                 case QC_OSTIDX:
1243                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1244                                            sizeof(*qctl), qctl, NULL);
1245                         break;
1246                 case QC_UUID:
1247                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1248                                            sizeof(*qctl), qctl, NULL);
1249                         if (rc == -EAGAIN)
1250                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1251                                                    sbi->ll_dt_exp,
1252                                                    sizeof(*qctl), qctl, NULL);
1253                         break;
1254                 default:
1255                         rc = -EINVAL;
1256                         break;
1257                 }
1258
1259                 if (rc)
1260                         RETURN(rc);
1261
1262                 qctl->qc_cmd = cmd;
1263         } else {
1264                 struct obd_quotactl *oqctl;
1265                 int oqctl_len = sizeof(*oqctl);
1266
1267                 if (LUSTRE_Q_CMD_IS_POOL(cmd))
1268                         oqctl_len += LOV_MAXPOOLNAME + 1;
1269
1270                 OBD_ALLOC(oqctl, oqctl_len);
1271                 if (oqctl == NULL)
1272                         RETURN(-ENOMEM);
1273
1274                 QCTL_COPY(oqctl, qctl);
1275                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1276                 if (rc) {
1277                         OBD_FREE(oqctl, oqctl_len);
1278                         RETURN(rc);
1279                 }
1280                 /* If QIF_SPACE is not set, client should collect the
1281                  * space usage from OSSs by itself */
1282                 if ((cmd == Q_GETQUOTA || cmd == LUSTRE_Q_GETQUOTAPOOL) &&
1283                     !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1284                     !oqctl->qc_dqblk.dqb_curspace) {
1285                         struct obd_quotactl *oqctl_tmp;
1286                         int qctl_len = sizeof(*oqctl_tmp) + LOV_MAXPOOLNAME + 1;
1287
1288                         OBD_ALLOC(oqctl_tmp, qctl_len);
1289                         if (oqctl_tmp == NULL)
1290                                 GOTO(out, rc = -ENOMEM);
1291
1292                         if (cmd == LUSTRE_Q_GETQUOTAPOOL) {
1293                                 oqctl_tmp->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
1294                                 memcpy(oqctl_tmp->qc_poolname,
1295                                        qctl->qc_poolname,
1296                                        LOV_MAXPOOLNAME + 1);
1297                         } else {
1298                                 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1299                         }
1300                         oqctl_tmp->qc_id = oqctl->qc_id;
1301                         oqctl_tmp->qc_type = oqctl->qc_type;
1302
1303                         /* collect space usage from OSTs */
1304                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1305                         rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1306                         if (!rc || rc == -EREMOTEIO) {
1307                                 oqctl->qc_dqblk.dqb_curspace =
1308                                         oqctl_tmp->qc_dqblk.dqb_curspace;
1309                                 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1310                         }
1311
1312                         /* collect space & inode usage from MDTs */
1313                         oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1314                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1315                         oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1316                         rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1317                         if (!rc || rc == -EREMOTEIO) {
1318                                 oqctl->qc_dqblk.dqb_curspace +=
1319                                         oqctl_tmp->qc_dqblk.dqb_curspace;
1320                                 oqctl->qc_dqblk.dqb_curinodes =
1321                                         oqctl_tmp->qc_dqblk.dqb_curinodes;
1322                                 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1323                         } else {
1324                                 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1325                         }
1326
1327                         OBD_FREE(oqctl_tmp, qctl_len);
1328                 }
1329 out:
1330                 QCTL_COPY(qctl, oqctl);
1331                 OBD_FREE(oqctl, oqctl_len);
1332         }
1333
1334         RETURN(rc);
1335 }
1336
1337 int ll_rmfid(struct file *file, void __user *arg)
1338 {
1339         const struct fid_array __user *ufa = arg;
1340         struct inode *inode = file_inode(file);
1341         struct fid_array *lfa = NULL;
1342         size_t size;
1343         unsigned nr;
1344         int i, rc, *rcs = NULL;
1345         ENTRY;
1346
1347         if (!capable(CAP_DAC_READ_SEARCH) &&
1348             !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
1349                 RETURN(-EPERM);
1350         /* Only need to get the buflen */
1351         if (get_user(nr, &ufa->fa_nr))
1352                 RETURN(-EFAULT);
1353         /* DoS protection */
1354         if (nr > OBD_MAX_FIDS_IN_ARRAY)
1355                 RETURN(-E2BIG);
1356
1357         size = offsetof(struct fid_array, fa_fids[nr]);
1358         OBD_ALLOC(lfa, size);
1359         if (!lfa)
1360                 RETURN(-ENOMEM);
1361         OBD_ALLOC_PTR_ARRAY(rcs, nr);
1362         if (!rcs)
1363                 GOTO(free_lfa, rc = -ENOMEM);
1364
1365         if (copy_from_user(lfa, arg, size))
1366                 GOTO(free_rcs, rc = -EFAULT);
1367
1368         /* Call mdc_iocontrol */
1369         rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1370         if (!rc) {
1371                 for (i = 0; i < nr; i++)
1372                         if (rcs[i])
1373                                 lfa->fa_fids[i].f_ver = rcs[i];
1374                 if (copy_to_user(arg, lfa, size))
1375                         rc = -EFAULT;
1376         }
1377
1378 free_rcs:
1379         OBD_FREE_PTR_ARRAY(rcs, nr);
1380 free_lfa:
1381         OBD_FREE(lfa, size);
1382
1383         RETURN(rc);
1384 }
1385
1386 /* This function tries to get a single name component,
1387  * to send to the server. No actual path traversal involved,
1388  * so we limit to NAME_MAX */
1389 static char *ll_getname(const char __user *filename)
1390 {
1391         int ret = 0, len;
1392         char *tmp;
1393
1394         OBD_ALLOC(tmp, NAME_MAX + 1);
1395
1396         if (!tmp)
1397                 return ERR_PTR(-ENOMEM);
1398
1399         len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1400         if (len < 0)
1401                 ret = -ENOENT;
1402         else if (len > NAME_MAX)
1403                 ret = -ENAMETOOLONG;
1404
1405         if (ret) {
1406                 OBD_FREE(tmp, NAME_MAX + 1);
1407                 tmp =  ERR_PTR(ret);
1408         }
1409         return tmp;
1410 }
1411
1412 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1413
1414 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1415 {
1416         struct dentry *dentry = file_dentry(file);
1417         struct inode *inode = file_inode(file);
1418         struct ll_sb_info *sbi = ll_i2sbi(inode);
1419         struct obd_ioctl_data *data = NULL;
1420         int rc = 0;
1421         ENTRY;
1422
1423         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1424                PFID(ll_inode2fid(inode)), inode, cmd);
1425
1426         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1427         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1428                 return -ENOTTY;
1429
1430         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1431         switch (cmd) {
1432         case FS_IOC_GETFLAGS:
1433         case FS_IOC_SETFLAGS:
1434                 RETURN(ll_iocontrol(inode, file, cmd, arg));
1435         case FSFILT_IOC_GETVERSION:
1436         case FS_IOC_GETVERSION:
1437                 RETURN(put_user(inode->i_generation, (int __user *)arg));
1438         /* We need to special case any other ioctls we want to handle,
1439          * to send them to the MDS/OST as appropriate and to properly
1440          * network encode the arg field. */
1441         case FS_IOC_SETVERSION:
1442                 RETURN(-ENOTSUPP);
1443
1444         case LL_IOC_GET_MDTIDX: {
1445                 int mdtidx;
1446
1447                 mdtidx = ll_get_mdt_idx(inode);
1448                 if (mdtidx < 0)
1449                         RETURN(mdtidx);
1450
1451                 if (put_user((int)mdtidx, (int __user *)arg))
1452                         RETURN(-EFAULT);
1453
1454                 return 0;
1455         }
1456         case IOC_MDC_LOOKUP: {
1457                 int namelen, len = 0;
1458                 char *filename;
1459
1460                 rc = obd_ioctl_getdata(&data, &len, (void __user *)arg);
1461                 if (rc != 0)
1462                         RETURN(rc);
1463
1464                 filename = data->ioc_inlbuf1;
1465                 namelen = strlen(filename);
1466                 if (namelen < 1) {
1467                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1468                         GOTO(out_free, rc = -EINVAL);
1469                 }
1470
1471                 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1472                 if (rc < 0) {
1473                         CERROR("%s: lookup %.*s failed: rc = %d\n",
1474                                sbi->ll_fsname, namelen, filename, rc);
1475                         GOTO(out_free, rc);
1476                 }
1477 out_free:
1478                 OBD_FREE_LARGE(data, len);
1479                 return rc;
1480         }
1481         case LL_IOC_LMV_SETSTRIPE: {
1482                 struct lmv_user_md  *lum;
1483                 char *filename;
1484                 int namelen = 0;
1485                 int lumlen = 0;
1486                 umode_t mode;
1487                 bool createonly = false;
1488                 int len;
1489                 int rc;
1490
1491                 rc = obd_ioctl_getdata(&data, &len, (void __user *)arg);
1492                 if (rc)
1493                         RETURN(rc);
1494
1495                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1496                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1497                         GOTO(lmv_out_free, rc = -EINVAL);
1498
1499                 filename = data->ioc_inlbuf1;
1500                 namelen = data->ioc_inllen1;
1501
1502                 if (namelen < 1) {
1503                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1504                         GOTO(lmv_out_free, rc = -EINVAL);
1505                 }
1506                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1507                 lumlen = data->ioc_inllen2;
1508
1509                 if (!lmv_user_magic_supported(lum->lum_magic)) {
1510                         CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1511                                lum->lum_magic, -EINVAL);
1512                         GOTO(lmv_out_free, rc = -EINVAL);
1513                 }
1514
1515                 if ((lum->lum_magic == LMV_USER_MAGIC ||
1516                      lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1517                     lumlen < sizeof(*lum)) {
1518                         CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1519                                filename, lumlen, lum->lum_magic, -EINVAL);
1520                         GOTO(lmv_out_free, rc = -EINVAL);
1521                 }
1522
1523                 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1524                     lumlen < sizeof(struct lmv_foreign_md)) {
1525                         CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1526                                filename, lum->lum_magic, lumlen, -EFAULT);
1527                         GOTO(lmv_out_free, rc = -EINVAL);
1528                 }
1529
1530                 mode = data->ioc_type;
1531                 createonly = data->ioc_obdo1.o_flags & OBD_FL_OBDMDEXISTS;
1532                 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode,
1533                                          createonly);
1534 lmv_out_free:
1535                 OBD_FREE_LARGE(data, len);
1536                 RETURN(rc);
1537
1538         }
1539         case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1540                 struct lmv_user_md        lum;
1541                 struct lmv_user_md __user *ulump =
1542                                         (struct lmv_user_md __user *)arg;
1543                 int                       rc;
1544
1545                 if (copy_from_user(&lum, ulump, sizeof(lum)))
1546                         RETURN(-EFAULT);
1547
1548                 if (lum.lum_magic != LMV_USER_MAGIC)
1549                         RETURN(-EINVAL);
1550
1551                 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1552
1553                 RETURN(rc);
1554         }
1555         case LL_IOC_LOV_SETSTRIPE_NEW:
1556         case LL_IOC_LOV_SETSTRIPE: {
1557                 struct lov_user_md_v3 *lumv3 = NULL;
1558                 struct lov_user_md_v1 lumv1;
1559                 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1560                 struct lov_user_md_v1 __user *lumv1p =
1561                         (struct lov_user_md_v1 __user *)arg;
1562                 struct lov_user_md_v3 __user *lumv3p =
1563                         (struct lov_user_md_v3 __user *)arg;
1564                 int lum_size = 0;
1565
1566                 int set_default = 0;
1567
1568                 BUILD_BUG_ON(sizeof(struct lov_user_md_v3) <=
1569                              sizeof(struct lov_comp_md_v1));
1570                 BUILD_BUG_ON(sizeof(*lumv3) != sizeof(*lumv3p));
1571                 /* first try with v1 which is smaller than v3 */
1572                 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1573                         RETURN(-EFAULT);
1574
1575                 if (is_root_inode(inode))
1576                         set_default = 1;
1577
1578                 switch (lumv1.lmm_magic) {
1579                 case LOV_USER_MAGIC_V3:
1580                 case LOV_USER_MAGIC_SPECIFIC:
1581                         lum_size = ll_lov_user_md_size(&lumv1);
1582                         if (lum_size < 0)
1583                                 RETURN(lum_size);
1584                         OBD_ALLOC(lumv3, lum_size);
1585                         if (!lumv3)
1586                                 RETURN(-ENOMEM);
1587                         if (copy_from_user(lumv3, lumv3p, lum_size))
1588                                 GOTO(out, rc = -EFAULT);
1589                         lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1590                         break;
1591                 case LOV_USER_MAGIC_V1:
1592                         break;
1593                 default:
1594                         GOTO(out, rc = -ENOTSUPP);
1595                 }
1596
1597                 /* in v1 and v3 cases lumv1 points to data */
1598                 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1599 out:
1600                 if (lumv3)
1601                         OBD_FREE(lumv3, lum_size);
1602                 RETURN(rc);
1603         }
1604         case LL_IOC_LMV_GETSTRIPE: {
1605                 struct lmv_user_md __user *ulmv =
1606                                         (struct lmv_user_md __user *)arg;
1607                 struct lmv_user_md      lum;
1608                 struct ptlrpc_request   *request = NULL;
1609                 struct ptlrpc_request   *root_request = NULL;
1610                 union lmv_mds_md        *lmm = NULL;
1611                 int                     lmmsize;
1612                 u64                     valid = 0;
1613                 struct lmv_user_md      *tmp = NULL;
1614                 int                     mdt_index;
1615                 int                     lum_size;
1616                 int                     stripe_count;
1617                 int                     max_stripe_count;
1618                 int                     i;
1619                 int                     rc;
1620
1621                 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1622                         RETURN(-EFAULT);
1623
1624                 max_stripe_count = lum.lum_stripe_count;
1625                 /* lum_magic will indicate which stripe the ioctl will like
1626                  * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1627                  * is for default LMV stripe */
1628                 if (lum.lum_magic == LMV_MAGIC_V1)
1629                         valid |= OBD_MD_MEA;
1630                 else if (lum.lum_magic == LMV_USER_MAGIC)
1631                         valid |= OBD_MD_DEFAULT_MEA;
1632                 else
1633                         RETURN(-EINVAL);
1634
1635                 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
1636                                               &request, &root_request, valid);
1637                 if (rc != 0)
1638                         GOTO(finish_req, rc);
1639
1640                 /* Get default LMV EA */
1641                 if (lum.lum_magic == LMV_USER_MAGIC) {
1642                         if (lmmsize > sizeof(*ulmv))
1643                                 GOTO(finish_req, rc = -EINVAL);
1644
1645                         if (root_request != NULL) {
1646                                 struct lmv_user_md *lum;
1647                                 struct ll_inode_info *lli;
1648
1649                                 lum = (struct lmv_user_md *)lmm;
1650                                 lli = ll_i2info(inode);
1651                                 if (lum->lum_max_inherit == LMV_INHERIT_NONE ||
1652                                     (lum->lum_max_inherit > 0 &&
1653                                      lum->lum_max_inherit < lli->lli_dir_depth))
1654                                         GOTO(finish_req, rc = -ENODATA);
1655
1656                                 if (lum->lum_max_inherit ==
1657                                     lli->lli_dir_depth) {
1658                                         lum->lum_max_inherit = LMV_INHERIT_NONE;
1659                                         lum->lum_max_inherit_rr =
1660                                                 LMV_INHERIT_RR_NONE;
1661                                         goto out_copy;
1662                                 }
1663                                 if (lum->lum_max_inherit > lli->lli_dir_depth &&
1664                                     lum->lum_max_inherit <= LMV_INHERIT_MAX)
1665                                         lum->lum_max_inherit -=
1666                                                 lli->lli_dir_depth;
1667
1668                                 if (lum->lum_max_inherit_rr >
1669                                         lli->lli_dir_depth &&
1670                                     lum->lum_max_inherit_rr <=
1671                                         LMV_INHERIT_RR_MAX)
1672                                         lum->lum_max_inherit_rr -=
1673                                                 lli->lli_dir_depth;
1674                                 else if (lum->lum_max_inherit_rr ==
1675                                                 lli->lli_dir_depth)
1676                                         lum->lum_max_inherit_rr =
1677                                                 LMV_INHERIT_RR_NONE;
1678                         }
1679 out_copy:
1680                         if (copy_to_user(ulmv, lmm, lmmsize))
1681                                 GOTO(finish_req, rc = -EFAULT);
1682
1683                         GOTO(finish_req, rc);
1684                 }
1685
1686                 /* if foreign LMV case, fake stripes number */
1687                 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1688                         struct lmv_foreign_md *lfm;
1689
1690                         lfm = (struct lmv_foreign_md *)lmm;
1691                         if (lfm->lfm_length < XATTR_SIZE_MAX -
1692                             offsetof(typeof(*lfm), lfm_value)) {
1693                                 __u32 size = lfm->lfm_length +
1694                                              offsetof(typeof(*lfm), lfm_value);
1695
1696                                 stripe_count = lmv_foreign_to_md_stripes(size);
1697                         } else {
1698                                 CERROR("invalid %d foreign size returned\n",
1699                                             lfm->lfm_length);
1700                                 return -EINVAL;
1701                         }
1702                 } else {
1703                         stripe_count = lmv_mds_md_stripe_count_get(lmm);
1704                 }
1705                 if (max_stripe_count < stripe_count) {
1706                         lum.lum_stripe_count = stripe_count;
1707                         if (copy_to_user(ulmv, &lum, sizeof(lum)))
1708                                 GOTO(finish_req, rc = -EFAULT);
1709                         GOTO(finish_req, rc = -E2BIG);
1710                 }
1711
1712                 /* enough room on user side and foreign case */
1713                 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1714                         struct lmv_foreign_md *lfm;
1715                         __u32 size;
1716
1717                         lfm = (struct lmv_foreign_md *)lmm;
1718                         size = lfm->lfm_length +
1719                                offsetof(struct lmv_foreign_md, lfm_value);
1720                         if (copy_to_user(ulmv, lfm, size))
1721                                 GOTO(finish_req, rc = -EFAULT);
1722                         GOTO(finish_req, rc);
1723                 }
1724
1725                 lum_size = lmv_user_md_size(stripe_count,
1726                                             LMV_USER_MAGIC_SPECIFIC);
1727                 OBD_ALLOC(tmp, lum_size);
1728                 if (tmp == NULL)
1729                         GOTO(finish_req, rc = -ENOMEM);
1730
1731                 mdt_index = ll_get_mdt_idx(inode);
1732                 if (mdt_index < 0)
1733                         GOTO(out_tmp, rc = -ENOMEM);
1734
1735                 tmp->lum_magic = LMV_MAGIC_V1;
1736                 tmp->lum_stripe_count = 0;
1737                 tmp->lum_stripe_offset = mdt_index;
1738                 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1739                 for (i = 0; i < stripe_count; i++) {
1740                         struct lu_fid   fid;
1741
1742                         fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1743                         if (fid_is_sane(&fid)) {
1744                                 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1745                                 if (mdt_index < 0)
1746                                         GOTO(out_tmp, rc = mdt_index);
1747
1748                                 tmp->lum_objects[i].lum_mds = mdt_index;
1749                                 tmp->lum_objects[i].lum_fid = fid;
1750                         }
1751
1752                         tmp->lum_stripe_count++;
1753                 }
1754
1755                 if (copy_to_user(ulmv, tmp, lum_size))
1756                         GOTO(out_tmp, rc = -EFAULT);
1757 out_tmp:
1758                 OBD_FREE(tmp, lum_size);
1759 finish_req:
1760                 ptlrpc_req_finished(request);
1761                 ptlrpc_req_finished(root_request);
1762                 return rc;
1763         }
1764
1765         case LL_IOC_UNLOCK_FOREIGN:
1766                 /* if not a foreign symlink do nothing */
1767                 if (ll_foreign_is_removable(dentry, true)) {
1768                         CDEBUG(D_INFO,
1769                                "prevent rmdir of non-foreign dir ("DFID")\n",
1770                                PFID(ll_inode2fid(inode)));
1771                         RETURN(-EOPNOTSUPP);
1772                 }
1773                 RETURN(0);
1774
1775         case LL_IOC_REMOVE_ENTRY: {
1776                 char            *filename = NULL;
1777                 int              namelen = 0;
1778                 int              rc;
1779
1780                 /* Here is a little hack to avoid sending REINT_RMENTRY to
1781                  * unsupported server, which might crash the server(LU-2730),
1782                  * Because both LVB_TYPE and REINT_RMENTRY will be supported
1783                  * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1784                  * server will support REINT_RMENTRY XXX*/
1785                 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1786                         RETURN(-ENOTSUPP);
1787
1788                 filename = ll_getname((const char __user *)arg);
1789                 if (IS_ERR(filename))
1790                         RETURN(PTR_ERR(filename));
1791
1792                 namelen = strlen(filename);
1793                 if (namelen < 1)
1794                         GOTO(out_rmdir, rc = -EINVAL);
1795
1796                 rc = ll_rmdir_entry(inode, filename, namelen);
1797 out_rmdir:
1798                 if (filename)
1799                         ll_putname(filename);
1800                 RETURN(rc);
1801         }
1802         case LL_IOC_RMFID:
1803                 RETURN(ll_rmfid(file, (void __user *)arg));
1804         case LL_IOC_LOV_SWAP_LAYOUTS:
1805                 RETURN(-EPERM);
1806         case IOC_OBD_STATFS:
1807                 RETURN(ll_obd_statfs(inode, (void __user *)arg));
1808         case LL_IOC_LOV_GETSTRIPE:
1809         case LL_IOC_LOV_GETSTRIPE_NEW:
1810         case LL_IOC_MDC_GETINFO_V1:
1811         case LL_IOC_MDC_GETINFO_V2:
1812         case IOC_MDC_GETFILEINFO_V1:
1813         case IOC_MDC_GETFILEINFO_V2:
1814         case IOC_MDC_GETFILESTRIPE: {
1815                 struct ptlrpc_request *request = NULL;
1816                 struct ptlrpc_request *root_request = NULL;
1817                 struct lov_user_md __user *lump;
1818                 struct lov_mds_md *lmm = NULL;
1819                 struct mdt_body *body;
1820                 char *filename = NULL;
1821                 lstat_t __user *statp = NULL;
1822                 lstatx_t __user *stxp = NULL;
1823                 __u64 __user *flagsp = NULL;
1824                 __u32 __user *lmmsizep = NULL;
1825                 struct lu_fid __user *fidp = NULL;
1826                 int lmmsize;
1827                 bool api32;
1828
1829                 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1830                     cmd == IOC_MDC_GETFILEINFO_V2 ||
1831                     cmd == IOC_MDC_GETFILESTRIPE) {
1832                         filename = ll_getname((const char __user *)arg);
1833                         if (IS_ERR(filename))
1834                                 RETURN(PTR_ERR(filename));
1835
1836                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1837                                                       &lmmsize, &request);
1838                 } else {
1839                         rc = ll_dir_getstripe_default(inode, (void **)&lmm,
1840                                                       &lmmsize, &request,
1841                                                       &root_request, 0);
1842                 }
1843
1844                 if (request) {
1845                         body = req_capsule_server_get(&request->rq_pill,
1846                                                       &RMF_MDT_BODY);
1847                         LASSERT(body != NULL);
1848                 } else {
1849                         GOTO(out_req, rc);
1850                 }
1851
1852                 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO_V1 ||
1853                                        cmd == LL_IOC_MDC_GETINFO_V1 ||
1854                                        cmd == IOC_MDC_GETFILEINFO_V2 ||
1855                                        cmd == LL_IOC_MDC_GETINFO_V2)) {
1856                         lmmsize = 0;
1857                         rc = 0;
1858                 }
1859
1860                 if (rc < 0)
1861                         GOTO(out_req, rc);
1862
1863                 if (cmd == IOC_MDC_GETFILESTRIPE ||
1864                     cmd == LL_IOC_LOV_GETSTRIPE ||
1865                     cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1866                         lump = (struct lov_user_md __user *)arg;
1867                 } else if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1868                            cmd == LL_IOC_MDC_GETINFO_V1){
1869                         struct lov_user_mds_data_v1 __user *lmdp;
1870
1871                         lmdp = (struct lov_user_mds_data_v1 __user *)arg;
1872                         statp = &lmdp->lmd_st;
1873                         lump = &lmdp->lmd_lmm;
1874                 } else {
1875                         struct lov_user_mds_data __user *lmdp;
1876
1877                         lmdp = (struct lov_user_mds_data __user *)arg;
1878                         fidp = &lmdp->lmd_fid;
1879                         stxp = &lmdp->lmd_stx;
1880                         flagsp = &lmdp->lmd_flags;
1881                         lmmsizep = &lmdp->lmd_lmmsize;
1882                         lump = &lmdp->lmd_lmm;
1883                 }
1884
1885                 if (lmmsize == 0) {
1886                         /* If the file has no striping then zero out *lump so
1887                          * that the caller isn't confused by garbage. */
1888                         if (clear_user(lump, sizeof(*lump)))
1889                                 GOTO(out_req, rc = -EFAULT);
1890                 } else if (copy_to_user(lump, lmm, lmmsize)) {
1891                         if (copy_to_user(lump, lmm, sizeof(*lump)))
1892                                 GOTO(out_req, rc = -EFAULT);
1893                         rc = -EOVERFLOW;
1894                 }
1895                 api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
1896
1897                 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1898                     cmd == LL_IOC_MDC_GETINFO_V1) {
1899                         lstat_t st = { 0 };
1900
1901                         st.st_dev       = inode->i_sb->s_dev;
1902                         st.st_mode      = body->mbo_mode;
1903                         st.st_nlink     = body->mbo_nlink;
1904                         st.st_uid       = body->mbo_uid;
1905                         st.st_gid       = body->mbo_gid;
1906                         st.st_rdev      = body->mbo_rdev;
1907                         if (llcrypt_require_key(inode) == -ENOKEY)
1908                                 st.st_size = round_up(st.st_size,
1909                                                    LUSTRE_ENCRYPTION_UNIT_SIZE);
1910                         else
1911                                 st.st_size = body->mbo_size;
1912                         st.st_blksize   = PAGE_SIZE;
1913                         st.st_blocks    = body->mbo_blocks;
1914                         st.st_atime     = body->mbo_atime;
1915                         st.st_mtime     = body->mbo_mtime;
1916                         st.st_ctime     = body->mbo_ctime;
1917                         st.st_ino       = cl_fid_build_ino(&body->mbo_fid1,
1918                                                            api32);
1919
1920                         if (copy_to_user(statp, &st, sizeof(st)))
1921                                 GOTO(out_req, rc = -EFAULT);
1922                 } else if (cmd == IOC_MDC_GETFILEINFO_V2 ||
1923                            cmd == LL_IOC_MDC_GETINFO_V2) {
1924                         lstatx_t stx = { 0 };
1925                         __u64 valid = body->mbo_valid;
1926
1927                         stx.stx_blksize = PAGE_SIZE;
1928                         stx.stx_nlink = body->mbo_nlink;
1929                         stx.stx_uid = body->mbo_uid;
1930                         stx.stx_gid = body->mbo_gid;
1931                         stx.stx_mode = body->mbo_mode;
1932                         stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1933                                                        api32);
1934                         if (llcrypt_require_key(inode) == -ENOKEY)
1935                                 stx.stx_size = round_up(stx.stx_size,
1936                                                    LUSTRE_ENCRYPTION_UNIT_SIZE);
1937                         else
1938                                 stx.stx_size = body->mbo_size;
1939                         stx.stx_blocks = body->mbo_blocks;
1940                         stx.stx_atime.tv_sec = body->mbo_atime;
1941                         stx.stx_ctime.tv_sec = body->mbo_ctime;
1942                         stx.stx_mtime.tv_sec = body->mbo_mtime;
1943                         stx.stx_btime.tv_sec = body->mbo_btime;
1944                         stx.stx_rdev_major = MAJOR(body->mbo_rdev);
1945                         stx.stx_rdev_minor = MINOR(body->mbo_rdev);
1946                         stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
1947                         stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
1948                         stx.stx_mask |= STATX_BASIC_STATS | STATX_BTIME;
1949
1950                         /*
1951                          * For a striped directory, the size and blocks returned
1952                          * from MDT is not correct.
1953                          * The size and blocks are aggregated by client across
1954                          * all stripes.
1955                          * Thus for a striped directory, do not return the valid
1956                          * FLSIZE and FLBLOCKS flags to the caller.
1957                          * However, this whould be better decided by the MDS
1958                          * instead of the client.
1959                          */
1960                         if (cmd == LL_IOC_MDC_GETINFO_V2 &&
1961                             ll_i2info(inode)->lli_lsm_md != NULL)
1962                                 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
1963
1964                         if (flagsp && copy_to_user(flagsp, &valid,
1965                                                    sizeof(*flagsp)))
1966                                 GOTO(out_req, rc = -EFAULT);
1967
1968                         if (fidp && copy_to_user(fidp, &body->mbo_fid1,
1969                                                  sizeof(*fidp)))
1970                                 GOTO(out_req, rc = -EFAULT);
1971
1972                         if (!(valid & OBD_MD_FLSIZE))
1973                                 stx.stx_mask &= ~STATX_SIZE;
1974                         if (!(valid & OBD_MD_FLBLOCKS))
1975                                 stx.stx_mask &= ~STATX_BLOCKS;
1976
1977                         if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
1978                                 GOTO(out_req, rc = -EFAULT);
1979
1980                         if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
1981                                                      sizeof(*lmmsizep)))
1982                                 GOTO(out_req, rc = -EFAULT);
1983                 }
1984
1985                 EXIT;
1986 out_req:
1987                 ptlrpc_req_finished(request);
1988                 ptlrpc_req_finished(root_request);
1989                 if (filename)
1990                         ll_putname(filename);
1991                 return rc;
1992         }
1993         case OBD_IOC_QUOTACTL: {
1994                 struct if_quotactl *qctl;
1995                 int qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
1996
1997                 OBD_ALLOC(qctl, qctl_len);
1998                 if (!qctl)
1999                         RETURN(-ENOMEM);
2000
2001                 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl)))
2002                         GOTO(out_quotactl, rc = -EFAULT);
2003
2004                 if (LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd)) {
2005                         char __user *from = (char __user *)arg +
2006                                         offsetof(typeof(*qctl), qc_poolname);
2007                         if (copy_from_user(qctl->qc_poolname, from,
2008                                            LOV_MAXPOOLNAME + 1))
2009                                 GOTO(out_quotactl, rc = -EFAULT);
2010                 }
2011
2012                 rc = quotactl_ioctl(inode->i_sb, qctl);
2013                 if (rc == 0 &&
2014                     copy_to_user((void __user *)arg, qctl, sizeof(*qctl)))
2015                         rc = -EFAULT;
2016
2017 out_quotactl:
2018                 OBD_FREE(qctl, qctl_len);
2019                 RETURN(rc);
2020         }
2021         case OBD_IOC_GETNAME_OLD:
2022         case OBD_IOC_GETDTNAME:
2023         case OBD_IOC_GETMDNAME:
2024                 RETURN(ll_get_obd_name(inode, cmd, arg));
2025         case LL_IOC_FLUSHCTX:
2026                 RETURN(ll_flush_ctx(inode));
2027         case LL_IOC_GETOBDCOUNT: {
2028                 u32 count, vallen;
2029                 struct obd_export *exp;
2030
2031                 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
2032                         RETURN(-EFAULT);
2033
2034                 /* get ost count when count is zero, get mdt count otherwise */
2035                 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
2036                 vallen = sizeof(count);
2037                 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
2038                                   KEY_TGT_COUNT, &vallen, &count);
2039                 if (rc) {
2040                         CERROR("get target count failed: %d\n", rc);
2041                         RETURN(rc);
2042                 }
2043
2044                 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
2045                         RETURN(-EFAULT);
2046
2047                 RETURN(0);
2048         }
2049         case LL_IOC_PATH2FID:
2050                 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
2051                                      sizeof(struct lu_fid)))
2052                         RETURN(-EFAULT);
2053                 RETURN(0);
2054         case LL_IOC_GET_CONNECT_FLAGS: {
2055                 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
2056                                      (void __user *)arg));
2057         }
2058         case OBD_IOC_FID2PATH:
2059                 RETURN(ll_fid2path(inode, (void __user *)arg));
2060         case LL_IOC_GETPARENT:
2061                 RETURN(ll_getparent(file, (void __user *)arg));
2062         case LL_IOC_FID2MDTIDX: {
2063                 struct obd_export *exp = ll_i2mdexp(inode);
2064                 struct lu_fid     fid;
2065                 __u32             index;
2066
2067                 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
2068                                    sizeof(fid)))
2069                         RETURN(-EFAULT);
2070
2071                 /* Call mdc_iocontrol */
2072                 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
2073                                    (__u32 __user *)&index);
2074                 if (rc != 0)
2075                         RETURN(rc);
2076
2077                 RETURN(index);
2078         }
2079         case LL_IOC_HSM_REQUEST: {
2080                 struct hsm_user_request *hur;
2081                 ssize_t                  totalsize;
2082
2083                 OBD_ALLOC_PTR(hur);
2084                 if (hur == NULL)
2085                         RETURN(-ENOMEM);
2086
2087                 /* We don't know the true size yet; copy the fixed-size part */
2088                 if (copy_from_user(hur, (void __user *)arg, sizeof(*hur))) {
2089                         OBD_FREE_PTR(hur);
2090                         RETURN(-EFAULT);
2091                 }
2092
2093                 /* Compute the whole struct size */
2094                 totalsize = hur_len(hur);
2095                 OBD_FREE_PTR(hur);
2096                 if (totalsize < 0)
2097                         RETURN(-E2BIG);
2098
2099                 /* Final size will be more than double totalsize */
2100                 if (totalsize >= MDS_MAXREQSIZE / 3)
2101                         RETURN(-E2BIG);
2102
2103                 OBD_ALLOC_LARGE(hur, totalsize);
2104                 if (hur == NULL)
2105                         RETURN(-ENOMEM);
2106
2107                 /* Copy the whole struct */
2108                 if (copy_from_user(hur, (void __user *)arg, totalsize))
2109                         GOTO(out_hur, rc = -EFAULT);
2110
2111                 if (hur->hur_request.hr_action == HUA_RELEASE) {
2112                         const struct lu_fid *fid;
2113                         struct inode *f;
2114                         int i;
2115
2116                         for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
2117                                 fid = &hur->hur_user_item[i].hui_fid;
2118                                 f = search_inode_for_lustre(inode->i_sb, fid);
2119                                 if (IS_ERR(f)) {
2120                                         rc = PTR_ERR(f);
2121                                         break;
2122                                 }
2123
2124                                 rc = ll_hsm_release(f);
2125                                 iput(f);
2126                                 if (rc != 0)
2127                                         break;
2128                         }
2129                 } else {
2130                         rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
2131                                            hur, NULL);
2132                 }
2133
2134 out_hur:
2135                 OBD_FREE_LARGE(hur, totalsize);
2136
2137                 RETURN(rc);
2138         }
2139         case LL_IOC_HSM_PROGRESS: {
2140                 struct hsm_progress_kernel      hpk;
2141                 struct hsm_progress             hp;
2142
2143                 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
2144                         RETURN(-EFAULT);
2145
2146                 hpk.hpk_fid = hp.hp_fid;
2147                 hpk.hpk_cookie = hp.hp_cookie;
2148                 hpk.hpk_extent = hp.hp_extent;
2149                 hpk.hpk_flags = hp.hp_flags;
2150                 hpk.hpk_errval = hp.hp_errval;
2151                 hpk.hpk_data_version = 0;
2152
2153                 /* File may not exist in Lustre; all progress
2154                  * reported to Lustre root */
2155                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
2156                                    NULL);
2157                 RETURN(rc);
2158         }
2159         case LL_IOC_HSM_CT_START:
2160                 if (!capable(CAP_SYS_ADMIN))
2161                         RETURN(-EPERM);
2162
2163                 rc = copy_and_ct_start(cmd, sbi->ll_md_exp,
2164                                        (struct lustre_kernelcomm __user *)arg);
2165                 RETURN(rc);
2166
2167         case LL_IOC_HSM_COPY_START: {
2168                 struct hsm_copy *copy;
2169                 int              rc;
2170
2171                 OBD_ALLOC_PTR(copy);
2172                 if (copy == NULL)
2173                         RETURN(-ENOMEM);
2174                 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2175                         OBD_FREE_PTR(copy);
2176                         RETURN(-EFAULT);
2177                 }
2178
2179                 rc = ll_ioc_copy_start(inode->i_sb, copy);
2180                 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2181                         rc = -EFAULT;
2182
2183                 OBD_FREE_PTR(copy);
2184                 RETURN(rc);
2185         }
2186         case LL_IOC_HSM_COPY_END: {
2187                 struct hsm_copy *copy;
2188                 int              rc;
2189
2190                 OBD_ALLOC_PTR(copy);
2191                 if (copy == NULL)
2192                         RETURN(-ENOMEM);
2193                 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2194                         OBD_FREE_PTR(copy);
2195                         RETURN(-EFAULT);
2196                 }
2197
2198                 rc = ll_ioc_copy_end(inode->i_sb, copy);
2199                 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2200                         rc = -EFAULT;
2201
2202                 OBD_FREE_PTR(copy);
2203                 RETURN(rc);
2204         }
2205         case LL_IOC_MIGRATE: {
2206                 struct lmv_user_md *lum;
2207                 int len;
2208                 char *filename;
2209                 int namelen = 0;
2210                 __u32 flags;
2211                 int rc;
2212
2213                 rc = obd_ioctl_getdata(&data, &len, (void __user *)arg);
2214                 if (rc)
2215                         RETURN(rc);
2216
2217                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2218                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2219                         GOTO(migrate_free, rc = -EINVAL);
2220
2221                 filename = data->ioc_inlbuf1;
2222                 namelen = data->ioc_inllen1;
2223                 flags = data->ioc_type;
2224
2225                 if (namelen < 1 || namelen != strlen(filename) + 1) {
2226                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2227                         GOTO(migrate_free, rc = -EINVAL);
2228                 }
2229
2230                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2231                 if (lum->lum_magic != LMV_USER_MAGIC &&
2232                     lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2233                         rc = -EINVAL;
2234                         CERROR("%s: wrong lum magic %x: rc = %d\n",
2235                                filename, lum->lum_magic, rc);
2236                         GOTO(migrate_free, rc);
2237                 }
2238
2239                 rc = ll_migrate(inode, file, lum, filename, flags);
2240 migrate_free:
2241                 OBD_FREE_LARGE(data, len);
2242
2243                 RETURN(rc);
2244         }
2245         case FS_IOC_FSGETXATTR:
2246                 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
2247         case FS_IOC_FSSETXATTR:
2248                 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
2249         case LL_IOC_PROJECT:
2250                 RETURN(ll_ioctl_project(file, cmd, arg));
2251         case LL_IOC_PCC_DETACH_BY_FID: {
2252                 struct lu_pcc_detach_fid *detach;
2253                 struct lu_fid *fid;
2254                 struct inode *inode2;
2255                 unsigned long ino;
2256
2257                 OBD_ALLOC_PTR(detach);
2258                 if (detach == NULL)
2259                         RETURN(-ENOMEM);
2260
2261                 if (copy_from_user(detach,
2262                                    (const struct lu_pcc_detach_fid __user *)arg,
2263                                    sizeof(*detach)))
2264                         GOTO(out_detach, rc = -EFAULT);
2265
2266                 fid = &detach->pccd_fid;
2267                 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2268                 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2269                 if (inode2 == NULL)
2270                         /* Target inode is not in inode cache, and PCC file
2271                          * has aleady released, return immdiately.
2272                          */
2273                         GOTO(out_detach, rc = 0);
2274
2275                 if (!S_ISREG(inode2->i_mode))
2276                         GOTO(out_iput, rc = -EINVAL);
2277
2278                 if (!inode_owner_or_capable(&init_user_ns, inode2))
2279                         GOTO(out_iput, rc = -EPERM);
2280
2281                 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2282 out_iput:
2283                 iput(inode2);
2284 out_detach:
2285                 OBD_FREE_PTR(detach);
2286                 RETURN(rc);
2287         }
2288 #ifdef HAVE_LUSTRE_CRYPTO
2289         case LL_IOC_SET_ENCRYPTION_POLICY:
2290                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2291                         return -EOPNOTSUPP;
2292                 return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
2293         case LL_IOC_GET_ENCRYPTION_POLICY_EX:
2294                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2295                         return -EOPNOTSUPP;
2296                 return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
2297         case LL_IOC_ADD_ENCRYPTION_KEY:
2298                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2299                         return -EOPNOTSUPP;
2300                 rc = llcrypt_ioctl_add_key(file, (void __user *)arg);
2301 #ifdef CONFIG_LL_ENCRYPTION
2302                 if (!rc)
2303                         sptlrpc_enc_pool_add_user();
2304 #endif
2305                 return rc;
2306         case LL_IOC_REMOVE_ENCRYPTION_KEY:
2307                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2308                         return -EOPNOTSUPP;
2309                 rc = llcrypt_ioctl_remove_key(file, (void __user *)arg);
2310 #ifdef CONFIG_LL_ENCRYPTION
2311                 if (!rc)
2312                         sptlrpc_enc_pool_del_user();
2313 #endif
2314                 return rc;
2315         case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
2316                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2317                         return -EOPNOTSUPP;
2318                 rc = llcrypt_ioctl_remove_key_all_users(file,
2319                                                         (void __user *)arg);
2320 #ifdef CONFIG_LL_ENCRYPTION
2321                 if (!rc)
2322                         sptlrpc_enc_pool_del_user();
2323 #endif
2324                 return rc;
2325         case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
2326                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2327                         return -EOPNOTSUPP;
2328                 return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
2329 #endif
2330         default:
2331                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
2332                                      (void __user *)arg));
2333         }
2334 }
2335
2336 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2337 {
2338         struct inode *inode = file->f_mapping->host;
2339         struct ll_file_data *fd = file->private_data;
2340         struct ll_sb_info *sbi = ll_i2sbi(inode);
2341         int api32 = ll_need_32bit_api(sbi);
2342         loff_t ret = -EINVAL;
2343         ENTRY;
2344
2345         inode_lock(inode);
2346         switch (origin) {
2347         case SEEK_SET:
2348                 break;
2349         case SEEK_CUR:
2350                 offset += file->f_pos;
2351                 break;
2352         case SEEK_END:
2353                 if (offset > 0)
2354                         GOTO(out, ret);
2355                 if (api32)
2356                         offset += LL_DIR_END_OFF_32BIT;
2357                 else
2358                         offset += LL_DIR_END_OFF;
2359                 break;
2360         default:
2361                 GOTO(out, ret);
2362         }
2363
2364         if (offset >= 0 &&
2365             ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2366              (!api32 && offset <= LL_DIR_END_OFF))) {
2367                 if (offset != file->f_pos) {
2368                         bool hash64;
2369
2370                         hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
2371                         if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2372                             (!api32 && offset == LL_DIR_END_OFF))
2373                                 fd->lfd_pos = MDS_DIR_END_OFF;
2374                         else if (api32 && hash64)
2375                                 fd->lfd_pos = offset << 32;
2376                         else
2377                                 fd->lfd_pos = offset;
2378                         file->f_pos = offset;
2379                         file->f_version = 0;
2380                 }
2381                 ret = offset;
2382         }
2383         GOTO(out, ret);
2384
2385 out:
2386         inode_unlock(inode);
2387         return ret;
2388 }
2389
2390 static int ll_dir_open(struct inode *inode, struct file *file)
2391 {
2392         ENTRY;
2393         RETURN(ll_file_open(inode, file));
2394 }
2395
2396 static int ll_dir_release(struct inode *inode, struct file *file)
2397 {
2398         ENTRY;
2399         RETURN(ll_file_release(inode, file));
2400 }
2401
2402 /* notify error if partially read striped directory */
2403 static int ll_dir_flush(struct file *file, fl_owner_t id)
2404 {
2405         struct ll_file_data *lfd = file->private_data;
2406         int rc = lfd->fd_partial_readdir_rc;
2407
2408         lfd->fd_partial_readdir_rc = 0;
2409
2410         return rc;
2411 }
2412
2413 const struct file_operations ll_dir_operations = {
2414         .llseek         = ll_dir_seek,
2415         .open           = ll_dir_open,
2416         .release        = ll_dir_release,
2417         .read           = generic_read_dir,
2418 #ifdef HAVE_DIR_CONTEXT
2419         .iterate_shared = ll_iterate,
2420 #else
2421         .readdir        = ll_readdir,
2422 #endif
2423         .unlocked_ioctl = ll_dir_ioctl,
2424         .fsync          = ll_fsync,
2425         .flush          = ll_dir_flush,
2426 };