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