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