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