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