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