Whamcloud - gitweb
342c65f1083c6a12d0a652493643196da2d5a2ad
[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;
1391         size_t size;
1392         unsigned int nr;
1393         int i, rc, *rcs = NULL;
1394         ENTRY;
1395
1396         if (!capable(CAP_DAC_READ_SEARCH) &&
1397             !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
1398                 RETURN(-EPERM);
1399         /* Only need to get the buflen */
1400         if (get_user(nr, &ufa->fa_nr))
1401                 RETURN(-EFAULT);
1402         /* DoS protection */
1403         if (nr > OBD_MAX_FIDS_IN_ARRAY)
1404                 RETURN(-E2BIG);
1405
1406         size = offsetof(struct fid_array, fa_fids[nr]);
1407         OBD_ALLOC(lfa, size);
1408         if (!lfa)
1409                 RETURN(-ENOMEM);
1410         OBD_ALLOC_PTR_ARRAY(rcs, nr);
1411         if (!rcs)
1412                 GOTO(free_lfa, rc = -ENOMEM);
1413
1414         if (copy_from_user(lfa, arg, size))
1415                 GOTO(free_rcs, rc = -EFAULT);
1416
1417         /* In case of subdirectory mount, we need to make sure all the files
1418          * for which we want to remove FID are visible in the namespace.
1419          */
1420         if (!fid_is_root(&sbi->ll_root_fid)) {
1421                 struct fid_array *lfa_new = NULL;
1422                 int path_len = PATH_MAX, linkno;
1423                 struct getinfo_fid2path *gf;
1424                 int idx, last_idx = nr - 1;
1425
1426                 OBD_ALLOC(lfa_new, size);
1427                 if (!lfa_new)
1428                         GOTO(free_rcs, rc = -ENOMEM);
1429                 lfa_new->fa_nr = 0;
1430
1431                 gf = kmalloc(sizeof(*gf) + path_len + 1, GFP_NOFS);
1432                 if (!gf)
1433                         GOTO(free_rcs, rc = -ENOMEM);
1434
1435                 for (idx = 0; idx < nr; idx++) {
1436                         linkno = 0;
1437                         while (1) {
1438                                 memset(gf, 0, sizeof(*gf) + path_len + 1);
1439                                 gf->gf_fid = lfa->fa_fids[idx];
1440                                 gf->gf_pathlen = path_len;
1441                                 gf->gf_linkno = linkno;
1442                                 rc = __ll_fid2path(inode, gf,
1443                                                    sizeof(*gf) + gf->gf_pathlen,
1444                                                    gf->gf_pathlen);
1445                                 if (rc == -ENAMETOOLONG) {
1446                                         struct getinfo_fid2path *tmpgf;
1447
1448                                         path_len += PATH_MAX;
1449                                         tmpgf = krealloc(gf,
1450                                                      sizeof(*gf) + path_len + 1,
1451                                                      GFP_NOFS);
1452                                         if (!tmpgf) {
1453                                                 kfree(gf);
1454                                                 OBD_FREE(lfa_new, size);
1455                                                 GOTO(free_rcs, rc = -ENOMEM);
1456                                         }
1457                                         gf = tmpgf;
1458                                         continue;
1459                                 }
1460                                 if (rc)
1461                                         break;
1462                                 if (gf->gf_linkno == linkno)
1463                                         break;
1464                                 linkno = gf->gf_linkno;
1465                         }
1466
1467                         if (!rc) {
1468                                 /* All the links for this fid are visible in the
1469                                  * mounted subdir. So add it to the list of fids
1470                                  * to remove.
1471                                  */
1472                                 lfa_new->fa_fids[lfa_new->fa_nr++] =
1473                                         lfa->fa_fids[idx];
1474                         } else {
1475                                 /* At least one link for this fid is not visible
1476                                  * in the mounted subdir. So add it at the end
1477                                  * of the list that will be hidden to lower
1478                                  * layers, and set -ENOENT as ret code.
1479                                  */
1480                                 lfa_new->fa_fids[last_idx] = lfa->fa_fids[idx];
1481                                 rcs[last_idx--] = rc;
1482                         }
1483                 }
1484                 kfree(gf);
1485                 OBD_FREE(lfa, size);
1486                 lfa = lfa_new;
1487         }
1488
1489         if (lfa->fa_nr == 0)
1490                 GOTO(free_rcs, rc = rcs[nr - 1]);
1491
1492         /* Call mdc_iocontrol */
1493         rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1494         lfa->fa_nr = nr;
1495         if (!rc) {
1496                 for (i = 0; i < nr; i++)
1497                         if (rcs[i])
1498                                 lfa->fa_fids[i].f_ver = rcs[i];
1499                 if (copy_to_user(arg, lfa, size))
1500                         rc = -EFAULT;
1501         }
1502
1503 free_rcs:
1504         OBD_FREE_PTR_ARRAY(rcs, nr);
1505 free_lfa:
1506         OBD_FREE(lfa, size);
1507
1508         RETURN(rc);
1509 }
1510
1511 /* This function tries to get a single name component,
1512  * to send to the server. No actual path traversal involved,
1513  * so we limit to NAME_MAX */
1514 static char *ll_getname(const char __user *filename)
1515 {
1516         int ret = 0, len;
1517         char *tmp;
1518
1519         OBD_ALLOC(tmp, NAME_MAX + 1);
1520
1521         if (!tmp)
1522                 return ERR_PTR(-ENOMEM);
1523
1524         len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1525         if (len < 0)
1526                 ret = -ENOENT;
1527         else if (len > NAME_MAX)
1528                 ret = -ENAMETOOLONG;
1529
1530         if (ret) {
1531                 OBD_FREE(tmp, NAME_MAX + 1);
1532                 tmp =  ERR_PTR(ret);
1533         }
1534         return tmp;
1535 }
1536
1537 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1538
1539 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1540 {
1541         struct dentry *dentry = file_dentry(file);
1542         struct inode *inode = file_inode(file);
1543         struct ll_sb_info *sbi = ll_i2sbi(inode);
1544         struct obd_ioctl_data *data = NULL;
1545         void __user *uarg = (void __user *)arg;
1546         int rc = 0;
1547         ENTRY;
1548
1549         CDEBUG(D_VFSTRACE|D_IOCTL, "VFS Op:inode="DFID"(%pK) cmd=%x arg=%lx\n",
1550                PFID(ll_inode2fid(inode)), inode, cmd, arg);
1551
1552         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1553         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1554                 return -ENOTTY;
1555
1556         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1557         switch (cmd) {
1558         case IOC_MDC_LOOKUP: {
1559                 int namelen, len = 0;
1560                 char *filename;
1561
1562                 rc = obd_ioctl_getdata(&data, &len, uarg);
1563                 if (rc != 0)
1564                         RETURN(rc);
1565
1566                 filename = data->ioc_inlbuf1;
1567                 namelen = strlen(filename);
1568                 if (namelen < 1) {
1569                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1570                         GOTO(out_free, rc = -EINVAL);
1571                 }
1572
1573                 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1574                 if (rc < 0) {
1575                         CERROR("%s: lookup %.*s failed: rc = %d\n",
1576                                sbi->ll_fsname, namelen, filename, rc);
1577                         GOTO(out_free, rc);
1578                 }
1579 out_free:
1580                 OBD_FREE_LARGE(data, len);
1581                 return rc;
1582         }
1583         case LL_IOC_LMV_SETSTRIPE: {
1584                 struct lmv_user_md  *lum;
1585                 char *filename;
1586                 int namelen = 0;
1587                 int lumlen = 0;
1588                 umode_t mode;
1589                 bool createonly = false;
1590                 int len;
1591                 int rc;
1592
1593                 rc = obd_ioctl_getdata(&data, &len, uarg);
1594                 if (rc)
1595                         RETURN(rc);
1596
1597                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1598                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1599                         GOTO(lmv_out_free, rc = -EINVAL);
1600
1601                 filename = data->ioc_inlbuf1;
1602                 namelen = data->ioc_inllen1;
1603
1604                 if (namelen < 1) {
1605                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1606                         GOTO(lmv_out_free, rc = -EINVAL);
1607                 }
1608                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1609                 lumlen = data->ioc_inllen2;
1610
1611                 if (!lmv_user_magic_supported(lum->lum_magic)) {
1612                         CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1613                                lum->lum_magic, -EINVAL);
1614                         GOTO(lmv_out_free, rc = -EINVAL);
1615                 }
1616
1617                 if ((lum->lum_magic == LMV_USER_MAGIC ||
1618                      lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1619                     lumlen < sizeof(*lum)) {
1620                         CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1621                                filename, lumlen, lum->lum_magic, -EINVAL);
1622                         GOTO(lmv_out_free, rc = -EINVAL);
1623                 }
1624
1625                 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1626                     lumlen < sizeof(struct lmv_foreign_md)) {
1627                         CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1628                                filename, lum->lum_magic, lumlen, -EFAULT);
1629                         GOTO(lmv_out_free, rc = -EINVAL);
1630                 }
1631
1632                 mode = data->ioc_type;
1633                 createonly = data->ioc_obdo1.o_flags & OBD_FL_OBDMDEXISTS;
1634                 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode,
1635                                          createonly);
1636 lmv_out_free:
1637                 OBD_FREE_LARGE(data, len);
1638                 RETURN(rc);
1639
1640         }
1641         case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1642                 struct lmv_user_md lum;
1643                 struct lmv_user_md __user *ulump = uarg;
1644                 int rc;
1645
1646                 if (copy_from_user(&lum, ulump, sizeof(lum)))
1647                         RETURN(-EFAULT);
1648
1649                 if (lum.lum_magic != LMV_USER_MAGIC)
1650                         RETURN(-EINVAL);
1651
1652                 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1653
1654                 RETURN(rc);
1655         }
1656         case LL_IOC_LOV_SETSTRIPE_NEW:
1657         case LL_IOC_LOV_SETSTRIPE: {
1658                 struct lov_user_md_v3 *lumv3 = NULL;
1659                 struct lov_user_md_v1 lumv1;
1660                 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1661                 struct lov_user_md_v1 __user *lumv1p = uarg;
1662                 struct lov_user_md_v3 __user *lumv3p = uarg;
1663                 int lum_size = 0;
1664                 int set_default = 0;
1665
1666                 BUILD_BUG_ON(sizeof(struct lov_user_md_v3) <=
1667                              sizeof(struct lov_comp_md_v1));
1668                 BUILD_BUG_ON(sizeof(*lumv3) != sizeof(*lumv3p));
1669                 /* first try with v1 which is smaller than v3 */
1670                 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1671                         RETURN(-EFAULT);
1672
1673                 if (is_root_inode(inode))
1674                         set_default = 1;
1675
1676                 switch (lumv1.lmm_magic) {
1677                 case LOV_USER_MAGIC_V3:
1678                 case LOV_USER_MAGIC_SPECIFIC:
1679                         lum_size = ll_lov_user_md_size(&lumv1);
1680                         if (lum_size < 0)
1681                                 RETURN(lum_size);
1682                         OBD_ALLOC(lumv3, lum_size);
1683                         if (!lumv3)
1684                                 RETURN(-ENOMEM);
1685                         if (copy_from_user(lumv3, lumv3p, lum_size))
1686                                 GOTO(out, rc = -EFAULT);
1687                         lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1688                         break;
1689                 case LOV_USER_MAGIC_V1:
1690                         break;
1691                 default:
1692                         GOTO(out, rc = -EOPNOTSUPP);
1693                 }
1694
1695                 /* in v1 and v3 cases lumv1 points to data */
1696                 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1697 out:
1698                 if (lumv3)
1699                         OBD_FREE(lumv3, lum_size);
1700                 RETURN(rc);
1701         }
1702         case LL_IOC_LMV_GETSTRIPE: {
1703                 struct lmv_user_md __user *ulmv = uarg;
1704                 struct lmv_user_md lum;
1705                 struct ptlrpc_request *request = NULL;
1706                 union lmv_mds_md *lmm = NULL;
1707                 int lmmsize;
1708                 u64 valid = 0;
1709                 struct lmv_user_md *tmp = NULL;
1710                 int mdt_index;
1711                 int lum_size;
1712                 int stripe_count;
1713                 int max_stripe_count;
1714                 int i;
1715                 int rc;
1716
1717                 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1718                         RETURN(-EFAULT);
1719
1720                 /* get default LMV */
1721                 if (lum.lum_magic == LMV_USER_MAGIC &&
1722                     lum.lum_type != LMV_TYPE_RAW) {
1723                         rc = ll_dir_get_default_lmv(inode, &lum);
1724                         if (rc)
1725                                 RETURN(rc);
1726
1727                         if (copy_to_user(ulmv, &lum, sizeof(lum)))
1728                                 RETURN(-EFAULT);
1729
1730                         RETURN(0);
1731                 }
1732
1733                 max_stripe_count = lum.lum_stripe_count;
1734                 /* lum_magic will indicate which stripe the ioctl will like
1735                  * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1736                  * is for default LMV stripe */
1737                 if (lum.lum_magic == LMV_MAGIC_V1)
1738                         valid |= OBD_MD_MEA;
1739                 else if (lum.lum_magic == LMV_USER_MAGIC)
1740                         valid |= OBD_MD_DEFAULT_MEA;
1741                 else
1742                         RETURN(-EINVAL);
1743
1744                 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
1745                                               &request, NULL, valid);
1746                 if (rc != 0)
1747                         GOTO(finish_req, rc);
1748
1749                 /* get default LMV in raw mode */
1750                 if (lum.lum_magic == LMV_USER_MAGIC) {
1751                         if (copy_to_user(ulmv, lmm, lmmsize))
1752                                 GOTO(finish_req, rc = -EFAULT);
1753                         GOTO(finish_req, rc);
1754                 }
1755
1756                 /* if foreign LMV case, fake stripes number */
1757                 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1758                         struct lmv_foreign_md *lfm;
1759
1760                         lfm = (struct lmv_foreign_md *)lmm;
1761                         if (lfm->lfm_length < XATTR_SIZE_MAX -
1762                             offsetof(typeof(*lfm), lfm_value)) {
1763                                 __u32 size = lfm->lfm_length +
1764                                              offsetof(typeof(*lfm), lfm_value);
1765
1766                                 stripe_count = lmv_foreign_to_md_stripes(size);
1767                         } else {
1768                                 CERROR("%s: invalid %d foreign size returned: rc = %d\n",
1769                                        sbi->ll_fsname, lfm->lfm_length,
1770                                        -EINVAL);
1771                                 return -EINVAL;
1772                         }
1773                 } else {
1774                         stripe_count = lmv_mds_md_stripe_count_get(lmm);
1775                 }
1776                 if (max_stripe_count < stripe_count) {
1777                         lum.lum_stripe_count = stripe_count;
1778                         if (copy_to_user(ulmv, &lum, sizeof(lum)))
1779                                 GOTO(finish_req, rc = -EFAULT);
1780                         GOTO(finish_req, rc = -E2BIG);
1781                 }
1782
1783                 /* enough room on user side and foreign case */
1784                 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1785                         struct lmv_foreign_md *lfm;
1786                         __u32 size;
1787
1788                         lfm = (struct lmv_foreign_md *)lmm;
1789                         size = lfm->lfm_length +
1790                                offsetof(struct lmv_foreign_md, lfm_value);
1791                         if (copy_to_user(ulmv, lfm, size))
1792                                 GOTO(finish_req, rc = -EFAULT);
1793                         GOTO(finish_req, rc);
1794                 }
1795
1796                 lum_size = lmv_user_md_size(stripe_count,
1797                                             LMV_USER_MAGIC_SPECIFIC);
1798                 OBD_ALLOC(tmp, lum_size);
1799                 if (tmp == NULL)
1800                         GOTO(finish_req, rc = -ENOMEM);
1801
1802                 mdt_index = ll_get_mdt_idx(inode);
1803                 if (mdt_index < 0)
1804                         GOTO(out_tmp, rc = -ENOMEM);
1805
1806                 tmp->lum_magic = LMV_MAGIC_V1;
1807                 tmp->lum_stripe_count = 0;
1808                 tmp->lum_stripe_offset = mdt_index;
1809                 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1810                 for (i = 0; i < stripe_count; i++) {
1811                         struct lu_fid   fid;
1812
1813                         fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1814                         if (fid_is_sane(&fid)) {
1815                                 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1816                                 if (mdt_index < 0)
1817                                         GOTO(out_tmp, rc = mdt_index);
1818
1819                                 tmp->lum_objects[i].lum_mds = mdt_index;
1820                                 tmp->lum_objects[i].lum_fid = fid;
1821                         }
1822
1823                         tmp->lum_stripe_count++;
1824                 }
1825
1826                 if (copy_to_user(ulmv, tmp, lum_size))
1827                         GOTO(out_tmp, rc = -EFAULT);
1828 out_tmp:
1829                 OBD_FREE(tmp, lum_size);
1830 finish_req:
1831                 ptlrpc_req_finished(request);
1832                 return rc;
1833         }
1834         case LL_IOC_REMOVE_ENTRY: {
1835                 char            *filename = NULL;
1836                 int              namelen = 0;
1837                 int              rc;
1838
1839                 /* Here is a little hack to avoid sending REINT_RMENTRY to
1840                  * unsupported server, which might crash the server(LU-2730),
1841                  * Because both LVB_TYPE and REINT_RMENTRY will be supported
1842                  * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1843                  * server will support REINT_RMENTRY XXX*/
1844                 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1845                         RETURN(-EOPNOTSUPP);
1846
1847                 filename = ll_getname(uarg);
1848                 if (IS_ERR(filename))
1849                         RETURN(PTR_ERR(filename));
1850
1851                 namelen = strlen(filename);
1852                 if (namelen < 1)
1853                         GOTO(out_rmdir, rc = -EINVAL);
1854
1855                 rc = ll_rmdir_entry(inode, filename, namelen);
1856 out_rmdir:
1857                 if (filename)
1858                         ll_putname(filename);
1859                 RETURN(rc);
1860         }
1861         case LL_IOC_RMFID:
1862                 RETURN(ll_rmfid(file, uarg));
1863         case LL_IOC_LOV_SWAP_LAYOUTS:
1864                 RETURN(-EPERM);
1865         case LL_IOC_LOV_GETSTRIPE:
1866         case LL_IOC_LOV_GETSTRIPE_NEW:
1867         case LL_IOC_MDC_GETINFO_V1:
1868         case LL_IOC_MDC_GETINFO_V2:
1869         case IOC_MDC_GETFILEINFO_V1:
1870         case IOC_MDC_GETFILEINFO_V2:
1871         case IOC_MDC_GETFILESTRIPE: {
1872                 struct ptlrpc_request *request = NULL;
1873                 struct ptlrpc_request *root_request = NULL;
1874                 struct lov_user_md __user *lump;
1875                 struct lov_mds_md *lmm = NULL;
1876                 struct mdt_body *body;
1877                 char *filename = NULL;
1878                 lstat_t __user *statp = NULL;
1879                 lstatx_t __user *stxp = NULL;
1880                 __u64 __user *flagsp = NULL;
1881                 __u32 __user *lmmsizep = NULL;
1882                 struct lu_fid __user *fidp = NULL;
1883                 int lmmsize;
1884                 bool api32;
1885
1886                 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1887                     cmd == IOC_MDC_GETFILEINFO_V2 ||
1888                     cmd == IOC_MDC_GETFILESTRIPE) {
1889                         filename = ll_getname(uarg);
1890                         if (IS_ERR(filename))
1891                                 RETURN(PTR_ERR(filename));
1892
1893                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1894                                                       &lmmsize, &request);
1895                 } else {
1896                         rc = ll_dir_getstripe_default(inode, (void **)&lmm,
1897                                                       &lmmsize, &request,
1898                                                       &root_request, 0);
1899                 }
1900
1901                 if (request) {
1902                         body = req_capsule_server_get(&request->rq_pill,
1903                                                       &RMF_MDT_BODY);
1904                         LASSERT(body != NULL);
1905                 } else {
1906                         GOTO(out_req, rc);
1907                 }
1908
1909                 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO_V1 ||
1910                                        cmd == LL_IOC_MDC_GETINFO_V1 ||
1911                                        cmd == IOC_MDC_GETFILEINFO_V2 ||
1912                                        cmd == LL_IOC_MDC_GETINFO_V2)) {
1913                         lmmsize = 0;
1914                         rc = 0;
1915                 }
1916
1917                 if (rc < 0)
1918                         GOTO(out_req, rc);
1919
1920                 if (cmd == IOC_MDC_GETFILESTRIPE ||
1921                     cmd == LL_IOC_LOV_GETSTRIPE ||
1922                     cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1923                         lump = uarg;
1924                 } else if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1925                            cmd == LL_IOC_MDC_GETINFO_V1){
1926                         struct lov_user_mds_data_v1 __user *lmdp;
1927
1928                         lmdp = uarg;
1929                         statp = &lmdp->lmd_st;
1930                         lump = &lmdp->lmd_lmm;
1931                 } else {
1932                         struct lov_user_mds_data __user *lmdp;
1933
1934                         lmdp = uarg;
1935                         fidp = &lmdp->lmd_fid;
1936                         stxp = &lmdp->lmd_stx;
1937                         flagsp = &lmdp->lmd_flags;
1938                         lmmsizep = &lmdp->lmd_lmmsize;
1939                         lump = &lmdp->lmd_lmm;
1940                 }
1941
1942                 if (lmmsize == 0) {
1943                         /* If the file has no striping then zero out *lump so
1944                          * that the caller isn't confused by garbage. */
1945                         if (clear_user(lump, sizeof(*lump)))
1946                                 GOTO(out_req, rc = -EFAULT);
1947                 } else if (copy_to_user(lump, lmm, lmmsize)) {
1948                         if (copy_to_user(lump, lmm, sizeof(*lump)))
1949                                 GOTO(out_req, rc = -EFAULT);
1950                         rc = -EOVERFLOW;
1951                 }
1952                 api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
1953
1954                 if (cmd == IOC_MDC_GETFILEINFO_V1 ||
1955                     cmd == LL_IOC_MDC_GETINFO_V1) {
1956                         lstat_t st = { 0 };
1957
1958                         st.st_dev       = inode->i_sb->s_dev;
1959                         st.st_mode      = body->mbo_mode;
1960                         st.st_nlink     = body->mbo_nlink;
1961                         st.st_uid       = body->mbo_uid;
1962                         st.st_gid       = body->mbo_gid;
1963                         st.st_rdev      = body->mbo_rdev;
1964                         if (llcrypt_require_key(inode) == -ENOKEY)
1965                                 st.st_size = round_up(st.st_size,
1966                                                    LUSTRE_ENCRYPTION_UNIT_SIZE);
1967                         else
1968                                 st.st_size = body->mbo_size;
1969                         st.st_blksize   = PAGE_SIZE;
1970                         st.st_blocks    = body->mbo_blocks;
1971                         st.st_atime     = body->mbo_atime;
1972                         st.st_mtime     = body->mbo_mtime;
1973                         st.st_ctime     = body->mbo_ctime;
1974                         st.st_ino       = cl_fid_build_ino(&body->mbo_fid1,
1975                                                            api32);
1976
1977                         if (copy_to_user(statp, &st, sizeof(st)))
1978                                 GOTO(out_req, rc = -EFAULT);
1979                 } else if (cmd == IOC_MDC_GETFILEINFO_V2 ||
1980                            cmd == LL_IOC_MDC_GETINFO_V2) {
1981                         lstatx_t stx = { 0 };
1982                         __u64 valid = body->mbo_valid;
1983
1984                         stx.stx_blksize = PAGE_SIZE;
1985                         stx.stx_nlink = body->mbo_nlink;
1986                         stx.stx_uid = body->mbo_uid;
1987                         stx.stx_gid = body->mbo_gid;
1988                         stx.stx_mode = body->mbo_mode;
1989                         stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1990                                                        api32);
1991                         if (llcrypt_require_key(inode) == -ENOKEY)
1992                                 stx.stx_size = round_up(stx.stx_size,
1993                                                    LUSTRE_ENCRYPTION_UNIT_SIZE);
1994                         else
1995                                 stx.stx_size = body->mbo_size;
1996                         stx.stx_blocks = body->mbo_blocks;
1997                         stx.stx_atime.tv_sec = body->mbo_atime;
1998                         stx.stx_ctime.tv_sec = body->mbo_ctime;
1999                         stx.stx_mtime.tv_sec = body->mbo_mtime;
2000                         stx.stx_btime.tv_sec = body->mbo_btime;
2001                         stx.stx_rdev_major = MAJOR(body->mbo_rdev);
2002                         stx.stx_rdev_minor = MINOR(body->mbo_rdev);
2003                         stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
2004                         stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
2005                         stx.stx_mask |= STATX_BASIC_STATS | STATX_BTIME;
2006
2007                         /* For a striped directory, the size and blocks returned
2008                          * from MDT is not correct.
2009                          * The size and blocks are aggregated by client across
2010                          * all stripes.
2011                          * Thus for a striped directory, do not return the valid
2012                          * FLSIZE and FLBLOCKS flags to the caller.
2013                          * However, this whould be better decided by the MDS
2014                          * instead of the client.
2015                          */
2016                         if (cmd == LL_IOC_MDC_GETINFO_V2 &&
2017                             ll_dir_striped(inode))
2018                                 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
2019
2020                         if (flagsp && copy_to_user(flagsp, &valid,
2021                                                    sizeof(*flagsp)))
2022                                 GOTO(out_req, rc = -EFAULT);
2023
2024                         if (fidp && copy_to_user(fidp, &body->mbo_fid1,
2025                                                  sizeof(*fidp)))
2026                                 GOTO(out_req, rc = -EFAULT);
2027
2028                         if (!(valid & OBD_MD_FLSIZE))
2029                                 stx.stx_mask &= ~STATX_SIZE;
2030                         if (!(valid & OBD_MD_FLBLOCKS))
2031                                 stx.stx_mask &= ~STATX_BLOCKS;
2032
2033                         if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
2034                                 GOTO(out_req, rc = -EFAULT);
2035
2036                         if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
2037                                                      sizeof(*lmmsizep)))
2038                                 GOTO(out_req, rc = -EFAULT);
2039                 }
2040
2041                 EXIT;
2042 out_req:
2043                 ptlrpc_req_finished(request);
2044                 ptlrpc_req_finished(root_request);
2045                 if (filename)
2046                         ll_putname(filename);
2047                 return rc;
2048         }
2049         case OBD_IOC_QUOTACTL: {
2050                 struct if_quotactl *qctl;
2051                 int qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
2052
2053                 OBD_ALLOC(qctl, qctl_len);
2054                 if (!qctl)
2055                         RETURN(-ENOMEM);
2056
2057                 if (copy_from_user(qctl, uarg, sizeof(*qctl)))
2058                         GOTO(out_quotactl, rc = -EFAULT);
2059
2060                 if (LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd)) {
2061                         char __user *from = uarg +
2062                                         offsetof(typeof(*qctl), qc_poolname);
2063                         if (copy_from_user(qctl->qc_poolname, from,
2064                                            LOV_MAXPOOLNAME + 1))
2065                                 GOTO(out_quotactl, rc = -EFAULT);
2066                 }
2067
2068                 rc = quotactl_ioctl(inode->i_sb, qctl);
2069                 if ((rc == 0 || rc == -ENODATA) &&
2070                     copy_to_user(uarg, qctl, sizeof(*qctl)))
2071                         rc = -EFAULT;
2072 out_quotactl:
2073                 OBD_FREE(qctl, qctl_len);
2074                 RETURN(rc);
2075         }
2076         case LL_IOC_GETOBDCOUNT: {
2077                 u32 count, vallen;
2078                 struct obd_export *exp;
2079
2080                 if (copy_from_user(&count, uarg, sizeof(count)))
2081                         RETURN(-EFAULT);
2082
2083                 /* get ost count when count is zero, get mdt count otherwise */
2084                 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
2085                 vallen = sizeof(count);
2086                 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
2087                                   KEY_TGT_COUNT, &vallen, &count);
2088                 if (rc) {
2089                         CERROR("%s: get target count failed: rc = %d\n",
2090                                sbi->ll_fsname, rc);
2091                         RETURN(rc);
2092                 }
2093
2094                 if (copy_to_user(uarg, &count, sizeof(count)))
2095                         RETURN(-EFAULT);
2096
2097                 RETURN(0);
2098         }
2099         case LL_IOC_GET_CONNECT_FLAGS:
2100                 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, uarg));
2101         case LL_IOC_FID2MDTIDX: {
2102                 struct obd_export *exp = ll_i2mdexp(inode);
2103                 struct lu_fid fid;
2104                 __u32 index;
2105
2106                 if (copy_from_user(&fid, uarg, sizeof(fid)))
2107                         RETURN(-EFAULT);
2108
2109                 /* Call mdc_iocontrol */
2110                 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
2111                                    (__u32 __user *)&index);
2112                 if (rc != 0)
2113                         RETURN(rc);
2114
2115                 RETURN(index);
2116         }
2117         case LL_IOC_HSM_REQUEST: {
2118                 struct hsm_user_request *hur;
2119                 ssize_t totalsize;
2120
2121                 OBD_ALLOC_PTR(hur);
2122                 if (hur == NULL)
2123                         RETURN(-ENOMEM);
2124
2125                 /* We don't know the true size yet; copy the fixed-size part */
2126                 if (copy_from_user(hur, uarg, sizeof(*hur))) {
2127                         OBD_FREE_PTR(hur);
2128                         RETURN(-EFAULT);
2129                 }
2130
2131                 /* Compute the whole struct size */
2132                 totalsize = hur_len(hur);
2133                 OBD_FREE_PTR(hur);
2134                 if (totalsize < 0)
2135                         RETURN(-E2BIG);
2136
2137                 /* Final size will be more than double totalsize */
2138                 if (totalsize >= MDS_MAXREQSIZE / 3)
2139                         RETURN(-E2BIG);
2140
2141                 OBD_ALLOC_LARGE(hur, totalsize);
2142                 if (hur == NULL)
2143                         RETURN(-ENOMEM);
2144
2145                 /* Copy the whole struct */
2146                 if (copy_from_user(hur, uarg, totalsize))
2147                         GOTO(out_hur, rc = -EFAULT);
2148
2149                 if (hur->hur_request.hr_action == HUA_RELEASE) {
2150                         const struct lu_fid *fid;
2151                         struct inode *f;
2152                         int i;
2153
2154                         for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
2155                                 fid = &hur->hur_user_item[i].hui_fid;
2156                                 f = search_inode_for_lustre(inode->i_sb, fid);
2157                                 if (IS_ERR(f)) {
2158                                         rc = PTR_ERR(f);
2159                                         break;
2160                                 }
2161
2162                                 rc = ll_hsm_release(f);
2163                                 iput(f);
2164                                 if (rc != 0)
2165                                         break;
2166                         }
2167                 } else {
2168                         rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
2169                                            hur, NULL);
2170                 }
2171 out_hur:
2172                 OBD_FREE_LARGE(hur, totalsize);
2173
2174                 RETURN(rc);
2175         }
2176         case LL_IOC_HSM_PROGRESS: {
2177                 struct hsm_progress_kernel hpk;
2178                 struct hsm_progress hp;
2179
2180                 if (copy_from_user(&hp, uarg, sizeof(hp)))
2181                         RETURN(-EFAULT);
2182
2183                 hpk.hpk_fid = hp.hp_fid;
2184                 hpk.hpk_cookie = hp.hp_cookie;
2185                 hpk.hpk_extent = hp.hp_extent;
2186                 hpk.hpk_flags = hp.hp_flags;
2187                 hpk.hpk_errval = hp.hp_errval;
2188                 hpk.hpk_data_version = 0;
2189
2190                 /* File may not exist in Lustre; all progress
2191                  * reported to Lustre root */
2192                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
2193                                    NULL);
2194                 RETURN(rc);
2195         }
2196         case LL_IOC_HSM_CT_START:
2197                 if (!capable(CAP_SYS_ADMIN))
2198                         RETURN(-EPERM);
2199
2200                 rc = copy_and_ct_start(cmd, sbi->ll_md_exp, uarg);
2201                 RETURN(rc);
2202
2203         case LL_IOC_HSM_COPY_START: {
2204                 struct hsm_copy *copy;
2205                 int rc;
2206
2207                 OBD_ALLOC_PTR(copy);
2208                 if (copy == NULL)
2209                         RETURN(-ENOMEM);
2210                 if (copy_from_user(copy, uarg, sizeof(*copy))) {
2211                         OBD_FREE_PTR(copy);
2212                         RETURN(-EFAULT);
2213                 }
2214
2215                 rc = ll_ioc_copy_start(inode->i_sb, copy);
2216                 if (copy_to_user(uarg, copy, sizeof(*copy)))
2217                         rc = -EFAULT;
2218
2219                 OBD_FREE_PTR(copy);
2220                 RETURN(rc);
2221         }
2222         case LL_IOC_HSM_COPY_END: {
2223                 struct hsm_copy *copy;
2224                 int rc;
2225
2226                 OBD_ALLOC_PTR(copy);
2227                 if (copy == NULL)
2228                         RETURN(-ENOMEM);
2229                 if (copy_from_user(copy, uarg, sizeof(*copy))) {
2230                         OBD_FREE_PTR(copy);
2231                         RETURN(-EFAULT);
2232                 }
2233
2234                 rc = ll_ioc_copy_end(inode->i_sb, copy);
2235                 if (copy_to_user(uarg, copy, sizeof(*copy)))
2236                         rc = -EFAULT;
2237
2238                 OBD_FREE_PTR(copy);
2239                 RETURN(rc);
2240         }
2241         case LL_IOC_MIGRATE: {
2242                 struct lmv_user_md *lum;
2243                 int len;
2244                 char *filename;
2245                 int namelen = 0;
2246                 __u32 flags;
2247                 int rc;
2248
2249                 rc = obd_ioctl_getdata(&data, &len, uarg);
2250                 if (rc)
2251                         RETURN(rc);
2252
2253                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2254                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2255                         GOTO(migrate_free, rc = -EINVAL);
2256
2257                 filename = data->ioc_inlbuf1;
2258                 namelen = data->ioc_inllen1;
2259                 flags = data->ioc_type;
2260
2261                 if (namelen < 1 || namelen != strlen(filename) + 1) {
2262                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2263                         GOTO(migrate_free, rc = -EINVAL);
2264                 }
2265
2266                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2267                 if (lum->lum_magic != LMV_USER_MAGIC &&
2268                     lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2269                         rc = -EINVAL;
2270                         CERROR("%s: wrong lum magic %x: rc = %d\n",
2271                                filename, lum->lum_magic, rc);
2272                         GOTO(migrate_free, rc);
2273                 }
2274
2275                 rc = ll_migrate(inode, file, lum, filename, flags);
2276 migrate_free:
2277                 OBD_FREE_LARGE(data, len);
2278
2279                 RETURN(rc);
2280         }
2281         case LL_IOC_PCC_DETACH_BY_FID: {
2282                 struct lu_pcc_detach_fid *detach;
2283                 struct lu_fid *fid;
2284                 struct inode *inode2;
2285                 unsigned long ino;
2286
2287                 OBD_ALLOC_PTR(detach);
2288                 if (detach == NULL)
2289                         RETURN(-ENOMEM);
2290
2291                 if (copy_from_user(detach, uarg, sizeof(*detach)))
2292                         GOTO(out_detach, rc = -EFAULT);
2293
2294                 fid = &detach->pccd_fid;
2295                 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2296                 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2297                 if (inode2 == NULL)
2298                         /* Target inode is not in inode cache, and PCC file
2299                          * has aleady released, return immdiately.
2300                          */
2301                         GOTO(out_detach, rc = 0);
2302
2303                 if (!S_ISREG(inode2->i_mode))
2304                         GOTO(out_iput, rc = -EINVAL);
2305
2306                 if (!inode_owner_or_capable(&nop_mnt_idmap, inode2))
2307                         GOTO(out_iput, rc = -EPERM);
2308
2309                 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2310 out_iput:
2311                 iput(inode2);
2312 out_detach:
2313                 OBD_FREE_PTR(detach);
2314                 RETURN(rc);
2315         }
2316         default:
2317                 rc = ll_iocontrol(inode, file, cmd, uarg);
2318                 if (rc != -ENOTTY)
2319                         RETURN(rc);
2320                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL, uarg));
2321         }
2322 }
2323
2324 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2325 {
2326         struct inode *inode = file->f_mapping->host;
2327         struct ll_file_data *fd = file->private_data;
2328         struct ll_sb_info *sbi = ll_i2sbi(inode);
2329         int api32 = ll_need_32bit_api(sbi);
2330         loff_t ret = -EINVAL;
2331         ENTRY;
2332
2333         ll_inode_lock(inode);
2334         switch (origin) {
2335         case SEEK_SET:
2336                 break;
2337         case SEEK_CUR:
2338                 offset += file->f_pos;
2339                 break;
2340         case SEEK_END:
2341                 if (offset > 0)
2342                         GOTO(out, ret);
2343                 if (api32)
2344                         offset += LL_DIR_END_OFF_32BIT;
2345                 else
2346                         offset += LL_DIR_END_OFF;
2347                 break;
2348         default:
2349                 GOTO(out, ret);
2350         }
2351
2352         if (offset >= 0 &&
2353             ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2354              (!api32 && offset <= LL_DIR_END_OFF))) {
2355                 if (offset != file->f_pos) {
2356                         bool hash64;
2357
2358                         hash64 = test_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
2359                         if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2360                             (!api32 && offset == LL_DIR_END_OFF))
2361                                 fd->lfd_pos = MDS_DIR_END_OFF;
2362                         else if (api32 && hash64)
2363                                 fd->lfd_pos = offset << 32;
2364                         else
2365                                 fd->lfd_pos = offset;
2366                         file->f_pos = offset;
2367                         file->f_version = 0;
2368                 }
2369                 ret = offset;
2370         }
2371         GOTO(out, ret);
2372
2373 out:
2374         ll_inode_unlock(inode);
2375         return ret;
2376 }
2377
2378 static int ll_dir_open(struct inode *inode, struct file *file)
2379 {
2380         ENTRY;
2381         RETURN(ll_file_open(inode, file));
2382 }
2383
2384 static int ll_dir_release(struct inode *inode, struct file *file)
2385 {
2386         ENTRY;
2387         RETURN(ll_file_release(inode, file));
2388 }
2389
2390 /* notify error if partially read striped directory */
2391 static int ll_dir_flush(struct file *file, fl_owner_t id)
2392 {
2393         struct ll_file_data *lfd = file->private_data;
2394         int rc = lfd->fd_partial_readdir_rc;
2395
2396         lfd->fd_partial_readdir_rc = 0;
2397
2398         return rc;
2399 }
2400
2401 const struct file_operations ll_dir_operations = {
2402         .llseek         = ll_dir_seek,
2403         .open           = ll_dir_open,
2404         .release        = ll_dir_release,
2405         .read           = generic_read_dir,
2406 #ifdef HAVE_DIR_CONTEXT
2407         .iterate_shared = ll_iterate,
2408 #else
2409         .readdir        = ll_readdir,
2410 #endif
2411         .unlocked_ioctl = ll_dir_ioctl,
2412         .fsync          = ll_fsync,
2413         .flush          = ll_dir_flush,
2414 };