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