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