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