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