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