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