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