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