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