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