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