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