Whamcloud - gitweb
efc1dbd73f15918d1edcd0d43aa9e34a2b88346f
[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 (ll_sbi_has_encrypt(sbi) &&
482             (IS_ENCRYPTED(parent) ||
483             unlikely(llcrypt_dummy_context_enabled(parent)))) {
484                 err = llcrypt_get_encryption_info(parent);
485                 if (err)
486                         GOTO(out_op_data, err);
487                 if (!llcrypt_has_encryption_key(parent))
488                         GOTO(out_op_data, err = -ENOKEY);
489                 encrypt = true;
490         }
491
492         if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
493                 /* selinux_dentry_init_security() uses dentry->d_parent and name
494                  * to determine the security context for the file. So our fake
495                  * dentry should be real enough for this purpose. */
496                 err = ll_dentry_init_security(&dentry, mode, &dentry.d_name,
497                                               &op_data->op_file_secctx_name,
498                                               &op_data->op_file_secctx,
499                                               &op_data->op_file_secctx_size);
500                 if (err < 0)
501                         GOTO(out_op_data, err);
502         }
503
504         if (encrypt) {
505                 err = llcrypt_inherit_context(parent, NULL, op_data, false);
506                 if (err)
507                         GOTO(out_op_data, err);
508         }
509
510         op_data->op_cli_flags |= CLI_SET_MEA;
511         err = md_create(sbi->ll_md_exp, op_data, lump, len, mode,
512                         from_kuid(&init_user_ns, current_fsuid()),
513                         from_kgid(&init_user_ns, current_fsgid()),
514                         cfs_curproc_cap_pack(), 0, &request);
515         if (err)
516                 GOTO(out_request, err);
517
518         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE, cfs_fail_val);
519
520         err = ll_prep_inode(&inode, request, parent->i_sb, NULL);
521         if (err)
522                 GOTO(out_inode, err);
523
524         dentry.d_inode = inode;
525
526         if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
527                 /* no need to protect selinux_inode_setsecurity() by
528                  * inode_lock. Taking it would lead to a client deadlock
529                  * LU-13617
530                  */
531                 err = security_inode_notifysecctx(inode,
532                                                   op_data->op_file_secctx,
533                                                   op_data->op_file_secctx_size);
534         } else {
535                 err = ll_inode_init_security(&dentry, inode, parent);
536         }
537         if (err)
538                 GOTO(out_inode, err);
539
540         if (encrypt) {
541                 err = ll_set_encflags(inode, op_data->op_file_encctx,
542                                       op_data->op_file_encctx_size, false);
543                 if (err)
544                         GOTO(out_inode, err);
545         }
546
547 out_inode:
548         if (inode != NULL)
549                 iput(inode);
550 out_request:
551         ptlrpc_req_finished(request);
552 out_op_data:
553         ll_finish_md_op_data(op_data);
554
555         return err;
556 }
557
558 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
559                      int set_default)
560 {
561         struct ll_sb_info *sbi = ll_i2sbi(inode);
562         struct md_op_data *op_data;
563         struct ptlrpc_request *req = NULL;
564         int rc = 0;
565         int lum_size;
566         ENTRY;
567
568         if (lump != NULL) {
569                 switch (lump->lmm_magic) {
570                 case LOV_USER_MAGIC_V1:
571                         lum_size = sizeof(struct lov_user_md_v1);
572                         break;
573                 case LOV_USER_MAGIC_V3:
574                         lum_size = sizeof(struct lov_user_md_v3);
575                         break;
576                 case LOV_USER_MAGIC_COMP_V1:
577                         lum_size = ((struct lov_comp_md_v1 *)lump)->lcm_size;
578                         break;
579                 case LMV_USER_MAGIC:
580                         if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
581                                 lustre_swab_lmv_user_md(
582                                         (struct lmv_user_md *)lump);
583                         lum_size = sizeof(struct lmv_user_md);
584                         break;
585                 case LOV_USER_MAGIC_SPECIFIC: {
586                         struct lov_user_md_v3 *v3 =
587                                 (struct lov_user_md_v3 *)lump;
588                         if (v3->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
589                                 RETURN(-EINVAL);
590                         lum_size = lov_user_md_size(v3->lmm_stripe_count,
591                                                     LOV_USER_MAGIC_SPECIFIC);
592                         break;
593                 }
594                 default:
595                         CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
596                                         " %#08x != %#08x nor %#08x\n",
597                                         lump->lmm_magic, LOV_USER_MAGIC_V1,
598                                         LOV_USER_MAGIC_V3);
599                         RETURN(-EINVAL);
600                 }
601
602                 /*
603                  * This is coming from userspace, so should be in
604                  * local endian.  But the MDS would like it in little
605                  * endian, so we swab it before we send it.
606                  */
607                 if ((__swab32(lump->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
608                     le32_to_cpu(LOV_MAGIC_MAGIC))
609                         lustre_swab_lov_user_md(lump, 0);
610         } else {
611                 lum_size = sizeof(struct lov_user_md_v1);
612         }
613
614         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
615                                      LUSTRE_OPC_ANY, NULL);
616         if (IS_ERR(op_data))
617                 RETURN(PTR_ERR(op_data));
618
619         /* swabbing is done in lov_setstripe() on server side */
620         rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
621         ll_finish_md_op_data(op_data);
622         ptlrpc_req_finished(req);
623         if (rc)
624                 RETURN(rc);
625
626         RETURN(rc);
627 }
628
629 static int ll_dir_get_default_layout(struct inode *inode, void **plmm,
630                                      int *plmm_size,
631                                      struct ptlrpc_request **request, u64 valid,
632                                      enum get_default_layout_type type)
633 {
634         struct ll_sb_info *sbi = ll_i2sbi(inode);
635         struct mdt_body   *body;
636         struct lov_mds_md *lmm = NULL;
637         struct ptlrpc_request *req = NULL;
638         int rc, lmm_size;
639         struct md_op_data *op_data;
640         struct lu_fid fid;
641         ENTRY;
642
643         rc = ll_get_default_mdsize(sbi, &lmm_size);
644         if (rc)
645                 RETURN(rc);
646
647         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
648                                      0, lmm_size, LUSTRE_OPC_ANY,
649                                      NULL);
650         if (IS_ERR(op_data))
651                 RETURN(PTR_ERR(op_data));
652
653         op_data->op_valid = valid | OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
654
655         if (type == GET_DEFAULT_LAYOUT_ROOT) {
656                 lu_root_fid(&op_data->op_fid1);
657                 fid = op_data->op_fid1;
658         } else {
659                 fid = *ll_inode2fid(inode);
660         }
661
662         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
663         ll_finish_md_op_data(op_data);
664         if (rc < 0) {
665                 CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
666                        PFID(&fid), rc);
667                 GOTO(out, rc);
668         }
669
670         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
671         LASSERT(body != NULL);
672
673         lmm_size = body->mbo_eadatasize;
674
675         if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
676             lmm_size == 0) {
677                 GOTO(out, rc = -ENODATA);
678         }
679
680         lmm = req_capsule_server_sized_get(&req->rq_pill,
681                                            &RMF_MDT_MD, lmm_size);
682         LASSERT(lmm != NULL);
683
684         /*
685          * This is coming from the MDS, so is probably in
686          * little endian.  We convert it to host endian before
687          * passing it to userspace.
688          */
689         /* We don't swab objects for directories */
690         switch (le32_to_cpu(lmm->lmm_magic)) {
691         case LOV_MAGIC_V1:
692         case LOV_MAGIC_V3:
693         case LOV_MAGIC_COMP_V1:
694         case LOV_USER_MAGIC_SPECIFIC:
695                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
696                         lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
697                 break;
698         case LMV_MAGIC_V1:
699                 if (LMV_MAGIC != cpu_to_le32(LMV_MAGIC))
700                         lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
701                 break;
702         case LMV_USER_MAGIC:
703                 if (LMV_USER_MAGIC != cpu_to_le32(LMV_USER_MAGIC))
704                         lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
705                 break;
706         case LMV_MAGIC_FOREIGN: {
707                 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lmm;
708
709                 if (LMV_MAGIC_FOREIGN != cpu_to_le32(LMV_MAGIC_FOREIGN)) {
710                         __swab32s(&lfm->lfm_magic);
711                         __swab32s(&lfm->lfm_length);
712                         __swab32s(&lfm->lfm_type);
713                         __swab32s(&lfm->lfm_flags);
714                 }
715                 break;
716         }
717         default:
718                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
719                 rc = -EPROTO;
720         }
721 out:
722         *plmm = lmm;
723         *plmm_size = lmm_size;
724         *request = req;
725         return rc;
726 }
727
728 /**
729  * This function will be used to get default LOV/LMV/Default LMV
730  * @valid will be used to indicate which stripe it will retrieve.
731  * If the directory does not have its own default layout, then the
732  * function will request the default layout from root FID.
733  *      OBD_MD_MEA              LMV stripe EA
734  *      OBD_MD_DEFAULT_MEA      Default LMV stripe EA
735  *      otherwise               Default LOV EA.
736  * Each time, it can only retrieve 1 stripe EA
737  **/
738 int ll_dir_getstripe_default(struct inode *inode, void **plmm, int *plmm_size,
739                              struct ptlrpc_request **request,
740                              struct ptlrpc_request **root_request,
741                              u64 valid)
742 {
743         struct ptlrpc_request *req = NULL;
744         struct ptlrpc_request *root_req = NULL;
745         struct lov_mds_md *lmm = NULL;
746         int lmm_size = 0;
747         int rc = 0;
748         ENTRY;
749
750         rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
751                                        &req, valid, 0);
752         if (rc == -ENODATA && !fid_is_root(ll_inode2fid(inode)) &&
753             !(valid & (OBD_MD_MEA|OBD_MD_DEFAULT_MEA)) && root_request != NULL)
754                 rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
755                                                &root_req, valid,
756                                                GET_DEFAULT_LAYOUT_ROOT);
757
758         *plmm = lmm;
759         *plmm_size = lmm_size;
760         *request = req;
761         if (root_request != NULL)
762                 *root_request = root_req;
763
764         RETURN(rc);
765 }
766
767 /**
768  * This function will be used to get default LOV/LMV/Default LMV
769  * @valid will be used to indicate which stripe it will retrieve
770  *      OBD_MD_MEA              LMV stripe EA
771  *      OBD_MD_DEFAULT_MEA      Default LMV stripe EA
772  *      otherwise               Default LOV EA.
773  * Each time, it can only retrieve 1 stripe EA
774  **/
775 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
776                      struct ptlrpc_request **request, u64 valid)
777 {
778         struct ptlrpc_request *req = NULL;
779         struct lov_mds_md *lmm = NULL;
780         int lmm_size = 0;
781         int rc = 0;
782         ENTRY;
783
784         rc = ll_dir_get_default_layout(inode, (void **)&lmm, &lmm_size,
785                                        &req, valid, 0);
786
787         *plmm = lmm;
788         *plmm_size = lmm_size;
789         *request = req;
790
791         RETURN(rc);
792 }
793
794 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
795 {
796         struct md_op_data       *op_data;
797         int                     rc;
798         int                     mdt_index;
799         ENTRY;
800
801         OBD_ALLOC_PTR(op_data);
802         if (op_data == NULL)
803                 RETURN(-ENOMEM);
804
805         op_data->op_flags |= MF_GET_MDT_IDX;
806         op_data->op_fid1 = *fid;
807         rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
808         mdt_index = op_data->op_mds;
809         OBD_FREE_PTR(op_data);
810         if (rc < 0)
811                 RETURN(rc);
812
813         RETURN(mdt_index);
814 }
815
816 /*
817  *  Get MDT index for the inode.
818  */
819 int ll_get_mdt_idx(struct inode *inode)
820 {
821         return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
822 }
823
824 /**
825  * Generic handler to do any pre-copy work.
826  *
827  * It sends a first hsm_progress (with extent length == 0) to coordinator as a
828  * first information for it that real work has started.
829  *
830  * Moreover, for a ARCHIVE request, it will sample the file data version and
831  * store it in \a copy.
832  *
833  * \return 0 on success.
834  */
835 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
836 {
837         struct ll_sb_info               *sbi = ll_s2sbi(sb);
838         struct hsm_progress_kernel       hpk;
839         int                              rc = 0;
840         int                              rc2;
841         ENTRY;
842
843         /* Forge a hsm_progress based on data from copy. */
844         hpk.hpk_fid = copy->hc_hai.hai_fid;
845         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
846         hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
847         hpk.hpk_extent.length = 0;
848         hpk.hpk_flags = 0;
849         hpk.hpk_errval = 0;
850         hpk.hpk_data_version = 0;
851
852
853         /* For archive request, we need to read the current file version. */
854         if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
855                 struct inode    *inode;
856                 __u64            data_version = 0;
857
858                 /* Get inode for this fid */
859                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
860                 if (IS_ERR(inode)) {
861                         hpk.hpk_flags |= HP_FLAG_RETRY;
862                         /* hpk_errval is >= 0 */
863                         hpk.hpk_errval = -PTR_ERR(inode);
864                         GOTO(progress, rc = PTR_ERR(inode));
865                 }
866
867                 /* Read current file data version */
868                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
869                 iput(inode);
870                 if (rc != 0) {
871                         CDEBUG(D_HSM, "Could not read file data version of "
872                                       DFID" (rc = %d). Archive request ("
873                                       "%#llx) could not be done.\n",
874                                       PFID(&copy->hc_hai.hai_fid), rc,
875                                       copy->hc_hai.hai_cookie);
876                         hpk.hpk_flags |= HP_FLAG_RETRY;
877                         /* hpk_errval must be >= 0 */
878                         hpk.hpk_errval = -rc;
879                         GOTO(progress, rc);
880                 }
881
882                 /* Store in the hsm_copy for later copytool use.
883                  * Always modified even if no lsm. */
884                 copy->hc_data_version = data_version;
885         }
886
887 progress:
888         /* On error, the request should be considered as completed */
889         if (hpk.hpk_errval > 0)
890                 hpk.hpk_flags |= HP_FLAG_COMPLETED;
891
892         rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
893                             &hpk, NULL);
894
895         /* Return first error */
896         RETURN(rc != 0 ? rc : rc2);
897 }
898
899 /**
900  * Generic handler to do any post-copy work.
901  *
902  * It will send the last hsm_progress update to coordinator to inform it
903  * that copy is finished and whether it was successful or not.
904  *
905  * Moreover,
906  * - for ARCHIVE request, it will sample the file data version and compare it
907  *   with the version saved in ll_ioc_copy_start(). If they do not match, copy
908  *   will be considered as failed.
909  * - for RESTORE request, it will sample the file data version and send it to
910  *   coordinator which is useful if the file was imported as 'released'.
911  *
912  * \return 0 on success.
913  */
914 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
915 {
916         struct ll_sb_info               *sbi = ll_s2sbi(sb);
917         struct hsm_progress_kernel       hpk;
918         int                              rc = 0;
919         int                              rc2;
920         ENTRY;
921
922         /* If you modify the logic here, also check llapi_hsm_copy_end(). */
923         /* Take care: copy->hc_hai.hai_action, len, gid and data are not
924          * initialized if copy_end was called with copy == NULL.
925          */
926
927         /* Forge a hsm_progress based on data from copy. */
928         hpk.hpk_fid = copy->hc_hai.hai_fid;
929         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
930         hpk.hpk_extent = copy->hc_hai.hai_extent;
931         hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
932         hpk.hpk_errval = copy->hc_errval;
933         hpk.hpk_data_version = 0;
934
935         /* For archive request, we need to check the file data was not changed.
936          *
937          * For restore request, we need to send the file data version, this is
938          * useful when the file was created using hsm_import.
939          */
940         if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
941              (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
942             (copy->hc_errval == 0)) {
943                 struct inode    *inode;
944                 __u64            data_version = 0;
945
946                 /* Get lsm for this fid */
947                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
948                 if (IS_ERR(inode)) {
949                         hpk.hpk_flags |= HP_FLAG_RETRY;
950                         /* hpk_errval must be >= 0 */
951                         hpk.hpk_errval = -PTR_ERR(inode);
952                         GOTO(progress, rc = PTR_ERR(inode));
953                 }
954
955                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
956                 iput(inode);
957                 if (rc) {
958                         CDEBUG(D_HSM, "Could not read file data version. "
959                                       "Request could not be confirmed.\n");
960                         if (hpk.hpk_errval == 0)
961                                 hpk.hpk_errval = -rc;
962                         GOTO(progress, rc);
963                 }
964
965                 /* Store in the hsm_copy for later copytool use.
966                  * Always modified even if no lsm. */
967                 hpk.hpk_data_version = data_version;
968
969                 /* File could have been stripped during archiving, so we need
970                  * to check anyway. */
971                 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
972                     (copy->hc_data_version != data_version)) {
973                         CDEBUG(D_HSM, "File data version mismatched. "
974                               "File content was changed during archiving. "
975                                DFID", start:%#llx current:%#llx\n",
976                                PFID(&copy->hc_hai.hai_fid),
977                                copy->hc_data_version, data_version);
978                         /* File was changed, send error to cdt. Do not ask for
979                          * retry because if a file is modified frequently,
980                          * the cdt will loop on retried archive requests.
981                          * The policy engine will ask for a new archive later
982                          * when the file will not be modified for some tunable
983                          * time */
984                         hpk.hpk_flags &= ~HP_FLAG_RETRY;
985                         rc = -EBUSY;
986                         /* hpk_errval must be >= 0 */
987                         hpk.hpk_errval = -rc;
988                         GOTO(progress, rc);
989                 }
990
991         }
992
993 progress:
994         rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
995                             &hpk, NULL);
996
997         /* Return first error */
998         RETURN(rc != 0 ? rc : rc2);
999 }
1000
1001
1002 static int copy_and_ct_start(int cmd, struct obd_export *exp,
1003                              const struct lustre_kernelcomm __user *data)
1004 {
1005         struct lustre_kernelcomm *lk;
1006         struct lustre_kernelcomm *tmp;
1007         size_t size = sizeof(*lk);
1008         size_t new_size;
1009         int i;
1010         int rc;
1011
1012         /* copy data from userspace to get numbers of archive_id */
1013         OBD_ALLOC(lk, size);
1014         if (lk == NULL)
1015                 return -ENOMEM;
1016
1017         if (copy_from_user(lk, data, size))
1018                 GOTO(out_lk, rc = -EFAULT);
1019
1020         if (lk->lk_flags & LK_FLG_STOP)
1021                 goto do_ioctl;
1022
1023         if (!(lk->lk_flags & LK_FLG_DATANR)) {
1024                 __u32 archive_mask = lk->lk_data_count;
1025                 int count;
1026
1027                 /* old hsm agent to old MDS */
1028                 if (!exp_connect_archive_id_array(exp))
1029                         goto do_ioctl;
1030
1031                 /* old hsm agent to new MDS */
1032                 lk->lk_flags |= LK_FLG_DATANR;
1033
1034                 if (archive_mask == 0)
1035                         goto do_ioctl;
1036
1037                 count = hweight32(archive_mask);
1038                 new_size = offsetof(struct lustre_kernelcomm, lk_data[count]);
1039                 OBD_ALLOC(tmp, new_size);
1040                 if (tmp == NULL)
1041                         GOTO(out_lk, rc = -ENOMEM);
1042
1043                 memcpy(tmp, lk, size);
1044                 tmp->lk_data_count = count;
1045                 OBD_FREE(lk, size);
1046                 lk = tmp;
1047                 size = new_size;
1048
1049                 count = 0;
1050                 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
1051                         if (BIT(i) & archive_mask) {
1052                                 lk->lk_data[count] = i + 1;
1053                                 count++;
1054                         }
1055                 }
1056                 goto do_ioctl;
1057         }
1058
1059         /* new hsm agent to new mds */
1060         if (lk->lk_data_count > 0) {
1061                 new_size = offsetof(struct lustre_kernelcomm,
1062                                     lk_data[lk->lk_data_count]);
1063                 OBD_ALLOC(tmp, new_size);
1064                 if (tmp == NULL)
1065                         GOTO(out_lk, rc = -ENOMEM);
1066
1067                 OBD_FREE(lk, size);
1068                 lk = tmp;
1069                 size = new_size;
1070
1071                 if (copy_from_user(lk, data, size))
1072                         GOTO(out_lk, rc = -EFAULT);
1073         }
1074
1075         /* new hsm agent to old MDS */
1076         if (!exp_connect_archive_id_array(exp)) {
1077                 __u32 archives = 0;
1078
1079                 if (lk->lk_data_count > LL_HSM_ORIGIN_MAX_ARCHIVE)
1080                         GOTO(out_lk, rc = -EINVAL);
1081
1082                 for (i = 0; i < lk->lk_data_count; i++) {
1083                         if (lk->lk_data[i] > LL_HSM_ORIGIN_MAX_ARCHIVE) {
1084                                 rc = -EINVAL;
1085                                 CERROR("%s: archive id %d requested but only "
1086                                        "[0 - %zu] supported: rc = %d\n",
1087                                        exp->exp_obd->obd_name, lk->lk_data[i],
1088                                        LL_HSM_ORIGIN_MAX_ARCHIVE, rc);
1089                                 GOTO(out_lk, rc);
1090                         }
1091
1092                         if (lk->lk_data[i] == 0) {
1093                                 archives = 0;
1094                                 break;
1095                         }
1096
1097                         archives |= (1 << (lk->lk_data[i] - 1));
1098                 }
1099                 lk->lk_flags &= ~LK_FLG_DATANR;
1100                 lk->lk_data_count = archives;
1101         }
1102 do_ioctl:
1103         rc = obd_iocontrol(cmd, exp, size, lk, NULL);
1104 out_lk:
1105         OBD_FREE(lk, size);
1106         return rc;
1107 }
1108
1109 static int check_owner(int type, int id)
1110 {
1111         switch (type) {
1112         case USRQUOTA:
1113                 if (!uid_eq(current_euid(), make_kuid(&init_user_ns, id)))
1114                         return -EPERM;
1115                 break;
1116         case GRPQUOTA:
1117                 if (!in_egroup_p(make_kgid(&init_user_ns, id)))
1118                         return -EPERM;
1119                 break;
1120         case PRJQUOTA:
1121                 break;
1122         }
1123         return 0;
1124 }
1125
1126 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
1127 {
1128         int cmd = qctl->qc_cmd;
1129         int type = qctl->qc_type;
1130         int id = qctl->qc_id;
1131         int valid = qctl->qc_valid;
1132         int rc = 0;
1133         ENTRY;
1134
1135         switch (cmd) {
1136         case Q_SETQUOTA:
1137         case Q_SETINFO:
1138         case LUSTRE_Q_SETDEFAULT:
1139         case LUSTRE_Q_SETQUOTAPOOL:
1140         case LUSTRE_Q_SETINFOPOOL:
1141                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1142                         RETURN(-EPERM);
1143                 break;
1144         case Q_GETQUOTA:
1145         case LUSTRE_Q_GETDEFAULT:
1146         case LUSTRE_Q_GETQUOTAPOOL:
1147                 if (check_owner(type, id) &&
1148                     (!cfs_capable(CFS_CAP_SYS_ADMIN)))
1149                         RETURN(-EPERM);
1150                 break;
1151         case Q_GETINFO:
1152         case LUSTRE_Q_GETINFOPOOL:
1153                 break;
1154         default:
1155                 CERROR("unsupported quotactl op: %#x\n", cmd);
1156                 RETURN(-ENOTSUPP);
1157         }
1158
1159         if (valid != QC_GENERAL) {
1160                 if (cmd == Q_GETINFO)
1161                         qctl->qc_cmd = Q_GETOINFO;
1162                 else if (cmd == Q_GETQUOTA ||
1163                          cmd == LUSTRE_Q_GETQUOTAPOOL)
1164                         qctl->qc_cmd = Q_GETOQUOTA;
1165                 else
1166                         RETURN(-EINVAL);
1167
1168                 switch (valid) {
1169                 case QC_MDTIDX:
1170                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1171                                            sizeof(*qctl), qctl, NULL);
1172                         break;
1173                 case QC_OSTIDX:
1174                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
1175                                            sizeof(*qctl), qctl, NULL);
1176                         break;
1177                 case QC_UUID:
1178                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
1179                                            sizeof(*qctl), qctl, NULL);
1180                         if (rc == -EAGAIN)
1181                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
1182                                                    sbi->ll_dt_exp,
1183                                                    sizeof(*qctl), qctl, NULL);
1184                         break;
1185                 default:
1186                         rc = -EINVAL;
1187                         break;
1188                 }
1189
1190                 if (rc)
1191                         RETURN(rc);
1192
1193                 qctl->qc_cmd = cmd;
1194         } else {
1195                 struct obd_quotactl *oqctl;
1196                 int oqctl_len = sizeof(*oqctl);
1197
1198                 if (LUSTRE_Q_CMD_IS_POOL(cmd))
1199                         oqctl_len += LOV_MAXPOOLNAME + 1;
1200
1201                 OBD_ALLOC(oqctl, oqctl_len);
1202                 if (oqctl == NULL)
1203                         RETURN(-ENOMEM);
1204
1205                 QCTL_COPY(oqctl, qctl);
1206                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
1207                 if (rc) {
1208                         OBD_FREE(oqctl, oqctl_len);
1209                         RETURN(rc);
1210                 }
1211                 /* If QIF_SPACE is not set, client should collect the
1212                  * space usage from OSSs by itself */
1213                 if ((cmd == Q_GETQUOTA || cmd == LUSTRE_Q_GETQUOTAPOOL) &&
1214                     !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
1215                     !oqctl->qc_dqblk.dqb_curspace) {
1216                         struct obd_quotactl *oqctl_tmp;
1217                         int qctl_len = sizeof(*oqctl_tmp) + LOV_MAXPOOLNAME + 1;
1218
1219                         OBD_ALLOC(oqctl_tmp, qctl_len);
1220                         if (oqctl_tmp == NULL)
1221                                 GOTO(out, rc = -ENOMEM);
1222
1223                         if (cmd == LUSTRE_Q_GETQUOTAPOOL) {
1224                                 oqctl_tmp->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
1225                                 memcpy(oqctl_tmp->qc_poolname,
1226                                        qctl->qc_poolname,
1227                                        LOV_MAXPOOLNAME + 1);
1228                         } else {
1229                                 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1230                         }
1231                         oqctl_tmp->qc_id = oqctl->qc_id;
1232                         oqctl_tmp->qc_type = oqctl->qc_type;
1233
1234                         /* collect space usage from OSTs */
1235                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1236                         rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
1237                         if (!rc || rc == -EREMOTEIO) {
1238                                 oqctl->qc_dqblk.dqb_curspace =
1239                                         oqctl_tmp->qc_dqblk.dqb_curspace;
1240                                 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1241                         }
1242
1243                         /* collect space & inode usage from MDTs */
1244                         oqctl_tmp->qc_cmd = Q_GETOQUOTA;
1245                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
1246                         oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
1247                         rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
1248                         if (!rc || rc == -EREMOTEIO) {
1249                                 oqctl->qc_dqblk.dqb_curspace +=
1250                                         oqctl_tmp->qc_dqblk.dqb_curspace;
1251                                 oqctl->qc_dqblk.dqb_curinodes =
1252                                         oqctl_tmp->qc_dqblk.dqb_curinodes;
1253                                 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1254                         } else {
1255                                 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
1256                         }
1257
1258                         OBD_FREE(oqctl_tmp, qctl_len);
1259                 }
1260 out:
1261                 QCTL_COPY(qctl, oqctl);
1262                 OBD_FREE(oqctl, oqctl_len);
1263         }
1264
1265         RETURN(rc);
1266 }
1267
1268 int ll_rmfid(struct file *file, void __user *arg)
1269 {
1270         const struct fid_array __user *ufa = arg;
1271         struct fid_array *lfa = NULL;
1272         size_t size;
1273         unsigned nr;
1274         int i, rc, *rcs = NULL;
1275         ENTRY;
1276
1277         if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
1278             !(ll_i2sbi(file_inode(file))->ll_flags & LL_SBI_USER_FID2PATH))
1279                 RETURN(-EPERM);
1280         /* Only need to get the buflen */
1281         if (get_user(nr, &ufa->fa_nr))
1282                 RETURN(-EFAULT);
1283         /* DoS protection */
1284         if (nr > OBD_MAX_FIDS_IN_ARRAY)
1285                 RETURN(-E2BIG);
1286
1287         size = offsetof(struct fid_array, fa_fids[nr]);
1288         OBD_ALLOC(lfa, size);
1289         if (!lfa)
1290                 RETURN(-ENOMEM);
1291         OBD_ALLOC_PTR_ARRAY(rcs, nr);
1292         if (!rcs)
1293                 GOTO(free_lfa, rc = -ENOMEM);
1294
1295         if (copy_from_user(lfa, arg, size))
1296                 GOTO(free_rcs, rc = -EFAULT);
1297
1298         /* Call mdc_iocontrol */
1299         rc = md_rmfid(ll_i2mdexp(file_inode(file)), lfa, rcs, NULL);
1300         if (!rc) {
1301                 for (i = 0; i < nr; i++)
1302                         if (rcs[i])
1303                                 lfa->fa_fids[i].f_ver = rcs[i];
1304                 if (copy_to_user(arg, lfa, size))
1305                         rc = -EFAULT;
1306         }
1307
1308 free_rcs:
1309         OBD_FREE_PTR_ARRAY(rcs, nr);
1310 free_lfa:
1311         OBD_FREE(lfa, size);
1312
1313         RETURN(rc);
1314 }
1315
1316 /* This function tries to get a single name component,
1317  * to send to the server. No actual path traversal involved,
1318  * so we limit to NAME_MAX */
1319 static char *ll_getname(const char __user *filename)
1320 {
1321         int ret = 0, len;
1322         char *tmp;
1323
1324         OBD_ALLOC(tmp, NAME_MAX + 1);
1325
1326         if (!tmp)
1327                 return ERR_PTR(-ENOMEM);
1328
1329         len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1330         if (len < 0)
1331                 ret = -ENOENT;
1332         else if (len > NAME_MAX)
1333                 ret = -ENAMETOOLONG;
1334
1335         if (ret) {
1336                 OBD_FREE(tmp, NAME_MAX + 1);
1337                 tmp =  ERR_PTR(ret);
1338         }
1339         return tmp;
1340 }
1341
1342 #define ll_putname(filename) OBD_FREE(filename, NAME_MAX + 1);
1343
1344 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1345 {
1346         struct dentry *dentry = file_dentry(file);
1347         struct inode *inode = file_inode(file);
1348         struct ll_sb_info *sbi = ll_i2sbi(inode);
1349         struct obd_ioctl_data *data;
1350         int rc = 0;
1351         ENTRY;
1352
1353         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1354                PFID(ll_inode2fid(inode)), inode, cmd);
1355
1356         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1357         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1358                 return -ENOTTY;
1359
1360         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1361         switch (cmd) {
1362         case FS_IOC_GETFLAGS:
1363         case FS_IOC_SETFLAGS:
1364                 RETURN(ll_iocontrol(inode, file, cmd, arg));
1365         case FSFILT_IOC_GETVERSION:
1366         case FS_IOC_GETVERSION:
1367                 RETURN(put_user(inode->i_generation, (int __user *)arg));
1368         /* We need to special case any other ioctls we want to handle,
1369          * to send them to the MDS/OST as appropriate and to properly
1370          * network encode the arg field. */
1371         case FS_IOC_SETVERSION:
1372                 RETURN(-ENOTSUPP);
1373
1374         case LL_IOC_GET_MDTIDX: {
1375                 int mdtidx;
1376
1377                 mdtidx = ll_get_mdt_idx(inode);
1378                 if (mdtidx < 0)
1379                         RETURN(mdtidx);
1380
1381                 if (put_user((int)mdtidx, (int __user *)arg))
1382                         RETURN(-EFAULT);
1383
1384                 return 0;
1385         }
1386         case IOC_MDC_LOOKUP: {
1387                                      int namelen, len = 0;
1388                 char *buf = NULL;
1389                 char *filename;
1390
1391                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1392                 if (rc != 0)
1393                         RETURN(rc);
1394                 data = (void *)buf;
1395
1396                 filename = data->ioc_inlbuf1;
1397                 namelen = strlen(filename);
1398                 if (namelen < 1) {
1399                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1400                         GOTO(out_free, rc = -EINVAL);
1401                 }
1402
1403                 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1404                 if (rc < 0) {
1405                         CERROR("%s: lookup %.*s failed: rc = %d\n",
1406                                sbi->ll_fsname, namelen, filename, rc);
1407                         GOTO(out_free, rc);
1408                 }
1409 out_free:
1410                 OBD_FREE_LARGE(buf, len);
1411                 return rc;
1412         }
1413         case LL_IOC_LMV_SETSTRIPE: {
1414                 struct lmv_user_md  *lum;
1415                 char            *buf = NULL;
1416                 char            *filename;
1417                 int              namelen = 0;
1418                 int              lumlen = 0;
1419                 umode_t          mode;
1420                 int              len;
1421                 int              rc;
1422
1423                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1424                 if (rc)
1425                         RETURN(rc);
1426
1427                 data = (void *)buf;
1428                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1429                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1430                         GOTO(lmv_out_free, rc = -EINVAL);
1431
1432                 filename = data->ioc_inlbuf1;
1433                 namelen = data->ioc_inllen1;
1434
1435                 if (namelen < 1) {
1436                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1437                         GOTO(lmv_out_free, rc = -EINVAL);
1438                 }
1439                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1440                 lumlen = data->ioc_inllen2;
1441
1442                 if (!lmv_user_magic_supported(lum->lum_magic)) {
1443                         CERROR("%s: wrong lum magic %x : rc = %d\n", filename,
1444                                lum->lum_magic, -EINVAL);
1445                         GOTO(lmv_out_free, rc = -EINVAL);
1446                 }
1447
1448                 if ((lum->lum_magic == LMV_USER_MAGIC ||
1449                      lum->lum_magic == LMV_USER_MAGIC_SPECIFIC) &&
1450                     lumlen < sizeof(*lum)) {
1451                         CERROR("%s: wrong lum size %d for magic %x : rc = %d\n",
1452                                filename, lumlen, lum->lum_magic, -EINVAL);
1453                         GOTO(lmv_out_free, rc = -EINVAL);
1454                 }
1455
1456                 if (lum->lum_magic == LMV_MAGIC_FOREIGN &&
1457                     lumlen < sizeof(struct lmv_foreign_md)) {
1458                         CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1459                                filename, lum->lum_magic, lumlen, -EFAULT);
1460                         GOTO(lmv_out_free, rc = -EINVAL);
1461                 }
1462
1463                 mode = data->ioc_type;
1464                 rc = ll_dir_setdirstripe(dentry, lum, lumlen, filename, mode);
1465 lmv_out_free:
1466                 OBD_FREE_LARGE(buf, len);
1467                 RETURN(rc);
1468
1469         }
1470         case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1471                 struct lmv_user_md        lum;
1472                 struct lmv_user_md __user *ulump =
1473                                         (struct lmv_user_md __user *)arg;
1474                 int                       rc;
1475
1476                 if (copy_from_user(&lum, ulump, sizeof(lum)))
1477                         RETURN(-EFAULT);
1478
1479                 if (lum.lum_magic != LMV_USER_MAGIC)
1480                         RETURN(-EINVAL);
1481
1482                 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1483
1484                 RETURN(rc);
1485         }
1486         case LL_IOC_LOV_SETSTRIPE_NEW:
1487         case LL_IOC_LOV_SETSTRIPE: {
1488                 struct lov_user_md_v3 *lumv3 = NULL;
1489                 struct lov_user_md_v1 lumv1;
1490                 struct lov_user_md_v1 *lumv1_ptr = &lumv1;
1491                 struct lov_user_md_v1 __user *lumv1p =
1492                         (struct lov_user_md_v1 __user *)arg;
1493                 struct lov_user_md_v3 __user *lumv3p =
1494                         (struct lov_user_md_v3 __user *)arg;
1495                 int lum_size = 0;
1496
1497                 int set_default = 0;
1498
1499                 BUILD_BUG_ON(sizeof(struct lov_user_md_v3) <=
1500                              sizeof(struct lov_comp_md_v1));
1501                 BUILD_BUG_ON(sizeof(*lumv3) != sizeof(*lumv3p));
1502                 /* first try with v1 which is smaller than v3 */
1503                 if (copy_from_user(&lumv1, lumv1p, sizeof(lumv1)))
1504                         RETURN(-EFAULT);
1505
1506                 if (inode->i_sb->s_root == file_dentry(file))
1507                         set_default = 1;
1508
1509                 switch (lumv1.lmm_magic) {
1510                 case LOV_USER_MAGIC_V3:
1511                 case LOV_USER_MAGIC_SPECIFIC:
1512                         lum_size = ll_lov_user_md_size(&lumv1);
1513                         if (lum_size < 0)
1514                                 RETURN(lum_size);
1515                         OBD_ALLOC(lumv3, lum_size);
1516                         if (!lumv3)
1517                                 RETURN(-ENOMEM);
1518                         if (copy_from_user(lumv3, lumv3p, lum_size))
1519                                 GOTO(out, rc = -EFAULT);
1520                         lumv1_ptr = (struct lov_user_md_v1 *)lumv3;
1521                         break;
1522                 case LOV_USER_MAGIC_V1:
1523                         break;
1524                 default:
1525                         GOTO(out, rc = -ENOTSUPP);
1526                 }
1527
1528                 /* in v1 and v3 cases lumv1 points to data */
1529                 rc = ll_dir_setstripe(inode, lumv1_ptr, set_default);
1530 out:
1531                 if (lumv3)
1532                         OBD_FREE(lumv3, lum_size);
1533                 RETURN(rc);
1534         }
1535         case LL_IOC_LMV_GETSTRIPE: {
1536                 struct lmv_user_md __user *ulmv =
1537                                         (struct lmv_user_md __user *)arg;
1538                 struct lmv_user_md      lum;
1539                 struct ptlrpc_request   *request = NULL;
1540                 struct ptlrpc_request   *root_request = NULL;
1541                 union lmv_mds_md        *lmm = NULL;
1542                 int                     lmmsize;
1543                 u64                     valid = 0;
1544                 struct lmv_user_md      *tmp = NULL;
1545                 int                     mdt_index;
1546                 int                     lum_size;
1547                 int                     stripe_count;
1548                 int                     max_stripe_count;
1549                 int                     i;
1550                 int                     rc;
1551
1552                 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1553                         RETURN(-EFAULT);
1554
1555                 max_stripe_count = lum.lum_stripe_count;
1556                 /* lum_magic will indicate which stripe the ioctl will like
1557                  * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1558                  * is for default LMV stripe */
1559                 if (lum.lum_magic == LMV_MAGIC_V1)
1560                         valid |= OBD_MD_MEA;
1561                 else if (lum.lum_magic == LMV_USER_MAGIC)
1562                         valid |= OBD_MD_DEFAULT_MEA;
1563                 else
1564                         RETURN(-EINVAL);
1565
1566                 rc = ll_dir_getstripe_default(inode, (void **)&lmm, &lmmsize,
1567                                               &request, &root_request, valid);
1568                 if (rc != 0)
1569                         GOTO(finish_req, rc);
1570
1571                 /* Get default LMV EA */
1572                 if (lum.lum_magic == LMV_USER_MAGIC) {
1573                         if (lmmsize > sizeof(*ulmv))
1574                                 GOTO(finish_req, rc = -EINVAL);
1575
1576                         if (copy_to_user(ulmv, lmm, lmmsize))
1577                                 GOTO(finish_req, rc = -EFAULT);
1578
1579                         GOTO(finish_req, rc);
1580                 }
1581
1582                 /* if foreign LMV case, fake stripes number */
1583                 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1584                         struct lmv_foreign_md *lfm;
1585
1586                         lfm = (struct lmv_foreign_md *)lmm;
1587                         if (lfm->lfm_length < XATTR_SIZE_MAX -
1588                             offsetof(typeof(*lfm), lfm_value)) {
1589                                 __u32 size = lfm->lfm_length +
1590                                              offsetof(typeof(*lfm), lfm_value);
1591
1592                                 stripe_count = lmv_foreign_to_md_stripes(size);
1593                         } else {
1594                                 CERROR("invalid %d foreign size returned\n",
1595                                             lfm->lfm_length);
1596                                 return -EINVAL;
1597                         }
1598                 } else {
1599                         stripe_count = lmv_mds_md_stripe_count_get(lmm);
1600                 }
1601                 if (max_stripe_count < stripe_count) {
1602                         lum.lum_stripe_count = stripe_count;
1603                         if (copy_to_user(ulmv, &lum, sizeof(lum)))
1604                                 GOTO(finish_req, rc = -EFAULT);
1605                         GOTO(finish_req, rc = -E2BIG);
1606                 }
1607
1608                 /* enough room on user side and foreign case */
1609                 if (lmm->lmv_magic == LMV_MAGIC_FOREIGN) {
1610                         struct lmv_foreign_md *lfm;
1611                         __u32 size;
1612
1613                         lfm = (struct lmv_foreign_md *)lmm;
1614                         size = lfm->lfm_length +
1615                                offsetof(struct lmv_foreign_md, lfm_value);
1616                         if (copy_to_user(ulmv, lfm, size))
1617                                 GOTO(finish_req, rc = -EFAULT);
1618                         GOTO(finish_req, rc);
1619                 }
1620
1621                 lum_size = lmv_user_md_size(stripe_count,
1622                                             LMV_USER_MAGIC_SPECIFIC);
1623                 OBD_ALLOC(tmp, lum_size);
1624                 if (tmp == NULL)
1625                         GOTO(finish_req, rc = -ENOMEM);
1626
1627                 mdt_index = ll_get_mdt_idx(inode);
1628                 if (mdt_index < 0)
1629                         GOTO(out_tmp, rc = -ENOMEM);
1630
1631                 tmp->lum_magic = LMV_MAGIC_V1;
1632                 tmp->lum_stripe_count = 0;
1633                 tmp->lum_stripe_offset = mdt_index;
1634                 tmp->lum_hash_type = lmv_mds_md_hash_type_get(lmm);
1635                 for (i = 0; i < stripe_count; i++) {
1636                         struct lu_fid   fid;
1637
1638                         fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1639                         if (fid_is_sane(&fid)) {
1640                                 mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1641                                 if (mdt_index < 0)
1642                                         GOTO(out_tmp, rc = mdt_index);
1643
1644                                 tmp->lum_objects[i].lum_mds = mdt_index;
1645                                 tmp->lum_objects[i].lum_fid = fid;
1646                         }
1647
1648                         tmp->lum_stripe_count++;
1649                 }
1650
1651                 if (copy_to_user(ulmv, tmp, lum_size))
1652                         GOTO(out_tmp, rc = -EFAULT);
1653 out_tmp:
1654                 OBD_FREE(tmp, lum_size);
1655 finish_req:
1656                 ptlrpc_req_finished(request);
1657                 ptlrpc_req_finished(root_request);
1658                 return rc;
1659         }
1660
1661         case LL_IOC_REMOVE_ENTRY: {
1662                 char            *filename = NULL;
1663                 int              namelen = 0;
1664                 int              rc;
1665
1666                 /* Here is a little hack to avoid sending REINT_RMENTRY to
1667                  * unsupported server, which might crash the server(LU-2730),
1668                  * Because both LVB_TYPE and REINT_RMENTRY will be supported
1669                  * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1670                  * server will support REINT_RMENTRY XXX*/
1671                 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1672                         RETURN(-ENOTSUPP);
1673
1674                 filename = ll_getname((const char __user *)arg);
1675                 if (IS_ERR(filename))
1676                         RETURN(PTR_ERR(filename));
1677
1678                 namelen = strlen(filename);
1679                 if (namelen < 1)
1680                         GOTO(out_rmdir, rc = -EINVAL);
1681
1682                 rc = ll_rmdir_entry(inode, filename, namelen);
1683 out_rmdir:
1684                 if (filename)
1685                         ll_putname(filename);
1686                 RETURN(rc);
1687         }
1688         case LL_IOC_RMFID:
1689                 RETURN(ll_rmfid(file, (void __user *)arg));
1690         case LL_IOC_LOV_SWAP_LAYOUTS:
1691                 RETURN(-EPERM);
1692         case IOC_OBD_STATFS:
1693                 RETURN(ll_obd_statfs(inode, (void __user *)arg));
1694         case LL_IOC_LOV_GETSTRIPE:
1695         case LL_IOC_LOV_GETSTRIPE_NEW:
1696         case LL_IOC_MDC_GETINFO:
1697         case LL_IOC_MDC_GETINFO_OLD:
1698         case IOC_MDC_GETFILEINFO:
1699         case IOC_MDC_GETFILEINFO_OLD:
1700         case IOC_MDC_GETFILESTRIPE: {
1701                 struct ptlrpc_request *request = NULL;
1702                 struct ptlrpc_request *root_request = NULL;
1703                 struct lov_user_md __user *lump;
1704                 struct lov_mds_md *lmm = NULL;
1705                 struct mdt_body *body;
1706                 char *filename = NULL;
1707                 lstat_t __user *statp = NULL;
1708                 lstatx_t __user *stxp = NULL;
1709                 __u64 __user *flagsp = NULL;
1710                 __u32 __user *lmmsizep = NULL;
1711                 struct lu_fid __user *fidp = NULL;
1712                 int lmmsize;
1713
1714                 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1715                     cmd == IOC_MDC_GETFILEINFO ||
1716                     cmd == IOC_MDC_GETFILESTRIPE) {
1717                         filename = ll_getname((const char __user *)arg);
1718                         if (IS_ERR(filename))
1719                                 RETURN(PTR_ERR(filename));
1720
1721                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1722                                                       &lmmsize, &request);
1723                 } else {
1724                         rc = ll_dir_getstripe_default(inode, (void **)&lmm,
1725                                                       &lmmsize, &request,
1726                                                       &root_request, 0);
1727                 }
1728
1729                 if (request) {
1730                         body = req_capsule_server_get(&request->rq_pill,
1731                                                       &RMF_MDT_BODY);
1732                         LASSERT(body != NULL);
1733                 } else {
1734                         GOTO(out_req, rc);
1735                 }
1736
1737                 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1738                                        cmd == LL_IOC_MDC_GETINFO ||
1739                                        cmd == IOC_MDC_GETFILEINFO_OLD ||
1740                                        cmd == LL_IOC_MDC_GETINFO_OLD)) {
1741                         lmmsize = 0;
1742                         rc = 0;
1743                 }
1744
1745                 if (rc < 0)
1746                         GOTO(out_req, rc);
1747
1748                 if (cmd == IOC_MDC_GETFILESTRIPE ||
1749                     cmd == LL_IOC_LOV_GETSTRIPE ||
1750                     cmd == LL_IOC_LOV_GETSTRIPE_NEW) {
1751                         lump = (struct lov_user_md __user *)arg;
1752                 } else if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1753                            cmd == LL_IOC_MDC_GETINFO_OLD){
1754                         struct lov_user_mds_data_v1 __user *lmdp;
1755
1756                         lmdp = (struct lov_user_mds_data_v1 __user *)arg;
1757                         statp = &lmdp->lmd_st;
1758                         lump = &lmdp->lmd_lmm;
1759                 } else {
1760                         struct lov_user_mds_data __user *lmdp;
1761
1762                         lmdp = (struct lov_user_mds_data __user *)arg;
1763                         fidp = &lmdp->lmd_fid;
1764                         stxp = &lmdp->lmd_stx;
1765                         flagsp = &lmdp->lmd_flags;
1766                         lmmsizep = &lmdp->lmd_lmmsize;
1767                         lump = &lmdp->lmd_lmm;
1768                 }
1769
1770                 if (lmmsize == 0) {
1771                         /* If the file has no striping then zero out *lump so
1772                          * that the caller isn't confused by garbage. */
1773                         if (clear_user(lump, sizeof(*lump)))
1774                                 GOTO(out_req, rc = -EFAULT);
1775                 } else if (copy_to_user(lump, lmm, lmmsize)) {
1776                         if (copy_to_user(lump, lmm, sizeof(*lump)))
1777                                 GOTO(out_req, rc = -EFAULT);
1778                         rc = -EOVERFLOW;
1779                 }
1780
1781                 if (cmd == IOC_MDC_GETFILEINFO_OLD ||
1782                     cmd == LL_IOC_MDC_GETINFO_OLD) {
1783                         lstat_t st = { 0 };
1784
1785                         st.st_dev       = inode->i_sb->s_dev;
1786                         st.st_mode      = body->mbo_mode;
1787                         st.st_nlink     = body->mbo_nlink;
1788                         st.st_uid       = body->mbo_uid;
1789                         st.st_gid       = body->mbo_gid;
1790                         st.st_rdev      = body->mbo_rdev;
1791                         st.st_size      = body->mbo_size;
1792                         st.st_blksize   = PAGE_SIZE;
1793                         st.st_blocks    = body->mbo_blocks;
1794                         st.st_atime     = body->mbo_atime;
1795                         st.st_mtime     = body->mbo_mtime;
1796                         st.st_ctime     = body->mbo_ctime;
1797                         st.st_ino       = cl_fid_build_ino(&body->mbo_fid1,
1798                                                 sbi->ll_flags &
1799                                                 LL_SBI_32BIT_API);
1800
1801                         if (copy_to_user(statp, &st, sizeof(st)))
1802                                 GOTO(out_req, rc = -EFAULT);
1803                 } else if (cmd == IOC_MDC_GETFILEINFO ||
1804                            cmd == LL_IOC_MDC_GETINFO) {
1805                         lstatx_t stx = { 0 };
1806                         __u64 valid = body->mbo_valid;
1807
1808                         stx.stx_blksize = PAGE_SIZE;
1809                         stx.stx_nlink = body->mbo_nlink;
1810                         stx.stx_uid = body->mbo_uid;
1811                         stx.stx_gid = body->mbo_gid;
1812                         stx.stx_mode = body->mbo_mode;
1813                         stx.stx_ino = cl_fid_build_ino(&body->mbo_fid1,
1814                                                        sbi->ll_flags &
1815                                                        LL_SBI_32BIT_API);
1816                         stx.stx_size = body->mbo_size;
1817                         stx.stx_blocks = body->mbo_blocks;
1818                         stx.stx_atime.tv_sec = body->mbo_atime;
1819                         stx.stx_ctime.tv_sec = body->mbo_ctime;
1820                         stx.stx_mtime.tv_sec = body->mbo_mtime;
1821                         stx.stx_btime.tv_sec = body->mbo_btime;
1822                         stx.stx_rdev_major = MAJOR(body->mbo_rdev);
1823                         stx.stx_rdev_minor = MINOR(body->mbo_rdev);
1824                         stx.stx_dev_major = MAJOR(inode->i_sb->s_dev);
1825                         stx.stx_dev_minor = MINOR(inode->i_sb->s_dev);
1826                         stx.stx_mask |= STATX_BASIC_STATS | STATX_BTIME;
1827
1828                         /*
1829                          * For a striped directory, the size and blocks returned
1830                          * from MDT is not correct.
1831                          * The size and blocks are aggregated by client across
1832                          * all stripes.
1833                          * Thus for a striped directory, do not return the valid
1834                          * FLSIZE and FLBLOCKS flags to the caller.
1835                          * However, this whould be better decided by the MDS
1836                          * instead of the client.
1837                          */
1838                         if (cmd == LL_IOC_MDC_GETINFO &&
1839                             ll_i2info(inode)->lli_lsm_md != NULL)
1840                                 valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
1841
1842                         if (flagsp && copy_to_user(flagsp, &valid,
1843                                                    sizeof(*flagsp)))
1844                                 GOTO(out_req, rc = -EFAULT);
1845
1846                         if (fidp && copy_to_user(fidp, &body->mbo_fid1,
1847                                                  sizeof(*fidp)))
1848                                 GOTO(out_req, rc = -EFAULT);
1849
1850                         if (!(valid & OBD_MD_FLSIZE))
1851                                 stx.stx_mask &= ~STATX_SIZE;
1852                         if (!(valid & OBD_MD_FLBLOCKS))
1853                                 stx.stx_mask &= ~STATX_BLOCKS;
1854
1855                         if (stxp && copy_to_user(stxp, &stx, sizeof(stx)))
1856                                 GOTO(out_req, rc = -EFAULT);
1857
1858                         if (lmmsizep && copy_to_user(lmmsizep, &lmmsize,
1859                                                      sizeof(*lmmsizep)))
1860                                 GOTO(out_req, rc = -EFAULT);
1861                 }
1862
1863                 EXIT;
1864 out_req:
1865                 ptlrpc_req_finished(request);
1866                 ptlrpc_req_finished(root_request);
1867                 if (filename)
1868                         ll_putname(filename);
1869                 return rc;
1870         }
1871         case OBD_IOC_QUOTACTL: {
1872                 struct if_quotactl *qctl;
1873                 int qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
1874
1875                 OBD_ALLOC(qctl, qctl_len);
1876                 if (!qctl)
1877                         RETURN(-ENOMEM);
1878
1879                 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl)))
1880                         GOTO(out_quotactl, rc = -EFAULT);
1881
1882                 if (LUSTRE_Q_CMD_IS_POOL(qctl->qc_cmd)) {
1883                         char __user *from = (char __user *)arg +
1884                                         offsetof(typeof(*qctl), qc_poolname);
1885                         if (copy_from_user(qctl->qc_poolname, from,
1886                                            LOV_MAXPOOLNAME + 1))
1887                                 GOTO(out_quotactl, rc = -EFAULT);
1888                 }
1889
1890                 rc = quotactl_ioctl(sbi, qctl);
1891                 if (rc == 0 &&
1892                     copy_to_user((void __user *)arg, qctl, sizeof(*qctl)))
1893                         rc = -EFAULT;
1894
1895 out_quotactl:
1896                 OBD_FREE(qctl, qctl_len);
1897                 RETURN(rc);
1898         }
1899         case OBD_IOC_GETDTNAME:
1900         case OBD_IOC_GETMDNAME:
1901                 RETURN(ll_get_obd_name(inode, cmd, arg));
1902         case LL_IOC_FLUSHCTX:
1903                 RETURN(ll_flush_ctx(inode));
1904         case LL_IOC_GETOBDCOUNT: {
1905                 u32 count, vallen;
1906                 struct obd_export *exp;
1907
1908                 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1909                         RETURN(-EFAULT);
1910
1911                 /* get ost count when count is zero, get mdt count otherwise */
1912                 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1913                 vallen = sizeof(count);
1914                 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1915                                   KEY_TGT_COUNT, &vallen, &count);
1916                 if (rc) {
1917                         CERROR("get target count failed: %d\n", rc);
1918                         RETURN(rc);
1919                 }
1920
1921                 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1922                         RETURN(-EFAULT);
1923
1924                 RETURN(0);
1925         }
1926         case LL_IOC_PATH2FID:
1927                 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1928                                      sizeof(struct lu_fid)))
1929                         RETURN(-EFAULT);
1930                 RETURN(0);
1931         case LL_IOC_GET_CONNECT_FLAGS: {
1932                 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1933                                      (void __user *)arg));
1934         }
1935         case OBD_IOC_FID2PATH:
1936                 RETURN(ll_fid2path(inode, (void __user *)arg));
1937         case LL_IOC_GETPARENT:
1938                 RETURN(ll_getparent(file, (void __user *)arg));
1939         case LL_IOC_FID2MDTIDX: {
1940                 struct obd_export *exp = ll_i2mdexp(inode);
1941                 struct lu_fid     fid;
1942                 __u32             index;
1943
1944                 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1945                                    sizeof(fid)))
1946                         RETURN(-EFAULT);
1947
1948                 /* Call mdc_iocontrol */
1949                 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1950                                    (__u32 __user *)&index);
1951                 if (rc != 0)
1952                         RETURN(rc);
1953
1954                 RETURN(index);
1955         }
1956         case LL_IOC_HSM_REQUEST: {
1957                 struct hsm_user_request *hur;
1958                 ssize_t                  totalsize;
1959
1960                 OBD_ALLOC_PTR(hur);
1961                 if (hur == NULL)
1962                         RETURN(-ENOMEM);
1963
1964                 /* We don't know the true size yet; copy the fixed-size part */
1965                 if (copy_from_user(hur, (void __user *)arg, sizeof(*hur))) {
1966                         OBD_FREE_PTR(hur);
1967                         RETURN(-EFAULT);
1968                 }
1969
1970                 /* Compute the whole struct size */
1971                 totalsize = hur_len(hur);
1972                 OBD_FREE_PTR(hur);
1973                 if (totalsize < 0)
1974                         RETURN(-E2BIG);
1975
1976                 /* Final size will be more than double totalsize */
1977                 if (totalsize >= MDS_MAXREQSIZE / 3)
1978                         RETURN(-E2BIG);
1979
1980                 OBD_ALLOC_LARGE(hur, totalsize);
1981                 if (hur == NULL)
1982                         RETURN(-ENOMEM);
1983
1984                 /* Copy the whole struct */
1985                 if (copy_from_user(hur, (void __user *)arg, totalsize))
1986                         GOTO(out_hur, rc = -EFAULT);
1987
1988                 if (hur->hur_request.hr_action == HUA_RELEASE) {
1989                         const struct lu_fid *fid;
1990                         struct inode *f;
1991                         int i;
1992
1993                         for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1994                                 fid = &hur->hur_user_item[i].hui_fid;
1995                                 f = search_inode_for_lustre(inode->i_sb, fid);
1996                                 if (IS_ERR(f)) {
1997                                         rc = PTR_ERR(f);
1998                                         break;
1999                                 }
2000
2001                                 rc = ll_hsm_release(f);
2002                                 iput(f);
2003                                 if (rc != 0)
2004                                         break;
2005                         }
2006                 } else {
2007                         rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
2008                                            hur, NULL);
2009                 }
2010
2011 out_hur:
2012                 OBD_FREE_LARGE(hur, totalsize);
2013
2014                 RETURN(rc);
2015         }
2016         case LL_IOC_HSM_PROGRESS: {
2017                 struct hsm_progress_kernel      hpk;
2018                 struct hsm_progress             hp;
2019
2020                 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
2021                         RETURN(-EFAULT);
2022
2023                 hpk.hpk_fid = hp.hp_fid;
2024                 hpk.hpk_cookie = hp.hp_cookie;
2025                 hpk.hpk_extent = hp.hp_extent;
2026                 hpk.hpk_flags = hp.hp_flags;
2027                 hpk.hpk_errval = hp.hp_errval;
2028                 hpk.hpk_data_version = 0;
2029
2030                 /* File may not exist in Lustre; all progress
2031                  * reported to Lustre root */
2032                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
2033                                    NULL);
2034                 RETURN(rc);
2035         }
2036         case LL_IOC_HSM_CT_START:
2037                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2038                         RETURN(-EPERM);
2039
2040                 rc = copy_and_ct_start(cmd, sbi->ll_md_exp,
2041                                        (struct lustre_kernelcomm __user *)arg);
2042                 RETURN(rc);
2043
2044         case LL_IOC_HSM_COPY_START: {
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_start(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_HSM_COPY_END: {
2064                 struct hsm_copy *copy;
2065                 int              rc;
2066
2067                 OBD_ALLOC_PTR(copy);
2068                 if (copy == NULL)
2069                         RETURN(-ENOMEM);
2070                 if (copy_from_user(copy, (char __user *)arg, sizeof(*copy))) {
2071                         OBD_FREE_PTR(copy);
2072                         RETURN(-EFAULT);
2073                 }
2074
2075                 rc = ll_ioc_copy_end(inode->i_sb, copy);
2076                 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
2077                         rc = -EFAULT;
2078
2079                 OBD_FREE_PTR(copy);
2080                 RETURN(rc);
2081         }
2082         case LL_IOC_MIGRATE: {
2083                 struct lmv_user_md *lum;
2084                 char *buf = NULL;
2085                 int len;
2086                 char *filename;
2087                 int namelen = 0;
2088                 int rc;
2089
2090                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
2091                 if (rc)
2092                         RETURN(rc);
2093
2094                 data = (struct obd_ioctl_data *)buf;
2095                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
2096                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
2097                         GOTO(migrate_free, rc = -EINVAL);
2098
2099                 filename = data->ioc_inlbuf1;
2100                 namelen = data->ioc_inllen1;
2101
2102                 if (namelen < 1 || namelen != strlen(filename) + 1) {
2103                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
2104                         GOTO(migrate_free, rc = -EINVAL);
2105                 }
2106
2107                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
2108                 if (lum->lum_magic != LMV_USER_MAGIC &&
2109                     lum->lum_magic != LMV_USER_MAGIC_SPECIFIC) {
2110                         rc = -EINVAL;
2111                         CERROR("%s: wrong lum magic %x: rc = %d\n",
2112                                filename, lum->lum_magic, rc);
2113                         GOTO(migrate_free, rc);
2114                 }
2115
2116                 rc = ll_migrate(inode, file, lum, filename);
2117 migrate_free:
2118                 OBD_FREE_LARGE(buf, len);
2119
2120                 RETURN(rc);
2121         }
2122         case FS_IOC_FSGETXATTR:
2123                 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
2124         case FS_IOC_FSSETXATTR:
2125                 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
2126         case LL_IOC_PCC_DETACH_BY_FID: {
2127                 struct lu_pcc_detach_fid *detach;
2128                 struct lu_fid *fid;
2129                 struct inode *inode2;
2130                 unsigned long ino;
2131
2132                 OBD_ALLOC_PTR(detach);
2133                 if (detach == NULL)
2134                         RETURN(-ENOMEM);
2135
2136                 if (copy_from_user(detach,
2137                                    (const struct lu_pcc_detach_fid __user *)arg,
2138                                    sizeof(*detach)))
2139                         GOTO(out_detach, rc = -EFAULT);
2140
2141                 fid = &detach->pccd_fid;
2142                 ino = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
2143                 inode2 = ilookup5(inode->i_sb, ino, ll_test_inode_by_fid, fid);
2144                 if (inode2 == NULL)
2145                         /* Target inode is not in inode cache, and PCC file
2146                          * has aleady released, return immdiately.
2147                          */
2148                         GOTO(out_detach, rc = 0);
2149
2150                 if (!S_ISREG(inode2->i_mode))
2151                         GOTO(out_iput, rc = -EINVAL);
2152
2153                 if (!inode_owner_or_capable(inode2))
2154                         GOTO(out_iput, rc = -EPERM);
2155
2156                 rc = pcc_ioctl_detach(inode2, detach->pccd_opt);
2157 out_iput:
2158                 iput(inode2);
2159 out_detach:
2160                 OBD_FREE_PTR(detach);
2161                 RETURN(rc);
2162         }
2163 #ifdef HAVE_LUSTRE_CRYPTO
2164         case LL_IOC_SET_ENCRYPTION_POLICY:
2165                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2166                         return -EOPNOTSUPP;
2167                 return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
2168         case LL_IOC_GET_ENCRYPTION_POLICY_EX:
2169                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2170                         return -EOPNOTSUPP;
2171                 return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
2172         case LL_IOC_ADD_ENCRYPTION_KEY:
2173                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2174                         return -EOPNOTSUPP;
2175                 return llcrypt_ioctl_add_key(file, (void __user *)arg);
2176         case LL_IOC_REMOVE_ENCRYPTION_KEY:
2177                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2178                         return -EOPNOTSUPP;
2179                 return llcrypt_ioctl_remove_key(file, (void __user *)arg);
2180         case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
2181                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2182                         return -EOPNOTSUPP;
2183                 return llcrypt_ioctl_remove_key_all_users(file,
2184                                                           (void __user *)arg);
2185         case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
2186                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
2187                         return -EOPNOTSUPP;
2188                 return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
2189 #endif
2190         default:
2191                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
2192                                      (void __user *)arg));
2193         }
2194 }
2195
2196 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
2197 {
2198         struct inode *inode = file->f_mapping->host;
2199         struct ll_file_data *fd = file->private_data;
2200         struct ll_sb_info *sbi = ll_i2sbi(inode);
2201         int api32 = ll_need_32bit_api(sbi);
2202         loff_t ret = -EINVAL;
2203         ENTRY;
2204
2205         inode_lock(inode);
2206         switch (origin) {
2207         case SEEK_SET:
2208                 break;
2209         case SEEK_CUR:
2210                 offset += file->f_pos;
2211                 break;
2212         case SEEK_END:
2213                 if (offset > 0)
2214                         GOTO(out, ret);
2215                 if (api32)
2216                         offset += LL_DIR_END_OFF_32BIT;
2217                 else
2218                         offset += LL_DIR_END_OFF;
2219                 break;
2220         default:
2221                 GOTO(out, ret);
2222         }
2223
2224         if (offset >= 0 &&
2225             ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
2226              (!api32 && offset <= LL_DIR_END_OFF))) {
2227                 if (offset != file->f_pos) {
2228                         if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
2229                             (!api32 && offset == LL_DIR_END_OFF))
2230                                 fd->lfd_pos = MDS_DIR_END_OFF;
2231                         else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
2232                                 fd->lfd_pos = offset << 32;
2233                         else
2234                                 fd->lfd_pos = offset;
2235                         file->f_pos = offset;
2236                         file->f_version = 0;
2237                 }
2238                 ret = offset;
2239         }
2240         GOTO(out, ret);
2241
2242 out:
2243         inode_unlock(inode);
2244         return ret;
2245 }
2246
2247 static int ll_dir_open(struct inode *inode, struct file *file)
2248 {
2249         ENTRY;
2250         RETURN(ll_file_open(inode, file));
2251 }
2252
2253 static int ll_dir_release(struct inode *inode, struct file *file)
2254 {
2255         ENTRY;
2256         RETURN(ll_file_release(inode, file));
2257 }
2258
2259 const struct file_operations ll_dir_operations = {
2260         .llseek         = ll_dir_seek,
2261         .open           = ll_dir_open,
2262         .release        = ll_dir_release,
2263         .read           = generic_read_dir,
2264 #ifdef HAVE_DIR_CONTEXT
2265         .iterate_shared = ll_iterate,
2266 #else
2267         .readdir        = ll_readdir,
2268 #endif
2269         .unlocked_ioctl = ll_dir_ioctl,
2270         .fsync          = ll_fsync,
2271 };