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