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