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