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