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