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