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