GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / staging / lustre / 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, 2015, 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/uaccess.h>
41 #include <linux/buffer_head.h>   /* for wait_on_buffer */
42 #include <linux/pagevec.h>
43 #include <linux/prefetch.h>
44
45 #define DEBUG_SUBSYSTEM S_LLITE
46
47 #include <obd_support.h>
48 #include <obd_class.h>
49 #include <uapi/linux/lustre/lustre_ioctl.h>
50 #include <lustre_lib.h>
51 #include <lustre_dlm.h>
52 #include <lustre_fid.h>
53 #include <lustre_kernelcomm.h>
54 #include <lustre_swab.h>
55
56 #include "llite_internal.h"
57
58 /*
59  * (new) readdir implementation overview.
60  *
61  * Original lustre readdir implementation cached exact copy of raw directory
62  * pages on the client. These pages were indexed in client page cache by
63  * logical offset in the directory file. This design, while very simple and
64  * intuitive had some inherent problems:
65  *
66  *     . it implies that byte offset to the directory entry serves as a
67  *     telldir(3)/seekdir(3) cookie, but that offset is not stable: in
68  *     ext3/htree directory entries may move due to splits, and more
69  *     importantly,
70  *
71  *     . it is incompatible with the design of split directories for cmd3,
72  *     that assumes that names are distributed across nodes based on their
73  *     hash, and so readdir should be done in hash order.
74  *
75  * New readdir implementation does readdir in hash order, and uses hash of a
76  * file name as a telldir/seekdir cookie. This led to number of complications:
77  *
78  *     . hash is not unique, so it cannot be used to index cached directory
79  *     pages on the client (note, that it requires a whole pageful of hash
80  *     collided entries to cause two pages to have identical hashes);
81  *
82  *     . hash is not unique, so it cannot, strictly speaking, be used as an
83  *     entry cookie. ext3/htree has the same problem and lustre implementation
84  *     mimics their solution: seekdir(hash) positions directory at the first
85  *     entry with the given hash.
86  *
87  * Client side.
88  *
89  * 0. caching
90  *
91  * Client caches directory pages using hash of the first entry as an index. As
92  * noted above hash is not unique, so this solution doesn't work as is:
93  * special processing is needed for "page hash chains" (i.e., sequences of
94  * pages filled with entries all having the same hash value).
95  *
96  * First, such chains have to be detected. To this end, server returns to the
97  * client the hash of the first entry on the page next to one returned. When
98  * client detects that this hash is the same as hash of the first entry on the
99  * returned page, page hash collision has to be handled. Pages in the
100  * hash chain, except first one, are termed "overflow pages".
101  *
102  * Solution to index uniqueness problem is to not cache overflow
103  * pages. Instead, when page hash collision is detected, all overflow pages
104  * from emerging chain are immediately requested from the server and placed in
105  * a special data structure (struct ll_dir_chain). This data structure is used
106  * by ll_readdir() to process entries from overflow pages. When readdir
107  * invocation finishes, overflow pages are discarded. If page hash collision
108  * chain weren't completely processed, next call to readdir will again detect
109  * page hash collision, again read overflow pages in, process next portion of
110  * entries and again discard the pages. This is not as wasteful as it looks,
111  * because, given reasonable hash, page hash collisions are extremely rare.
112  *
113  * 1. directory positioning
114  *
115  * When seekdir(hash) is called, original
116  *
117  *
118  *
119  *
120  *
121  *
122  *
123  *
124  * Server.
125  *
126  * identification of and access to overflow pages
127  *
128  * page format
129  *
130  * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
131  * a header lu_dirpage which describes the start/end hash, and whether this
132  * page is empty (contains no dir entry) or hash collide with next page.
133  * After client receives reply, several pages will be integrated into dir page
134  * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the lu_dirpage
135  * for this integrated page will be adjusted. See lmv_adjust_dirpages().
136  *
137  */
138 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
139                              __u64 offset)
140 {
141         struct md_callback cb_op;
142         struct page *page;
143         int rc;
144
145         cb_op.md_blocking_ast = ll_md_blocking_ast;
146         rc = md_read_page(ll_i2mdexp(dir), op_data, &cb_op, offset, &page);
147         if (rc)
148                 return ERR_PTR(rc);
149
150         return page;
151 }
152
153 void ll_release_page(struct inode *inode, struct page *page, bool remove)
154 {
155         kunmap(page);
156
157         /*
158          * Always remove the page for striped dir, because the page is
159          * built from temporarily in LMV layer
160          */
161         if (inode && S_ISDIR(inode->i_mode) &&
162             ll_i2info(inode)->lli_lsm_md) {
163                 __free_page(page);
164                 return;
165         }
166
167         if (remove) {
168                 lock_page(page);
169                 if (likely(page->mapping))
170                         truncate_complete_page(page->mapping, page);
171                 unlock_page(page);
172         }
173         put_page(page);
174 }
175
176 /**
177  * return IF_* type for given lu_dirent entry.
178  * IF_* flag shld be converted to particular OS file type in
179  * platform llite module.
180  */
181 static __u16 ll_dirent_type_get(struct lu_dirent *ent)
182 {
183         __u16 type = 0;
184         struct luda_type *lt;
185         int len = 0;
186
187         if (le32_to_cpu(ent->lde_attrs) & LUDA_TYPE) {
188                 const unsigned int align = sizeof(struct luda_type) - 1;
189
190                 len = le16_to_cpu(ent->lde_namelen);
191                 len = (len + align) & ~align;
192                 lt = (void *)ent->lde_name + len;
193                 type = IFTODT(le16_to_cpu(lt->lt_type));
194         }
195         return type;
196 }
197
198 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
199                 struct dir_context *ctx)
200 {
201         struct ll_sb_info    *sbi       = ll_i2sbi(inode);
202         __u64              pos          = *ppos;
203         int                is_api32 = ll_need_32bit_api(sbi);
204         int                is_hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
205         struct page       *page;
206         bool               done = false;
207         int                rc = 0;
208
209         page = ll_get_dir_page(inode, op_data, pos);
210
211         while (rc == 0 && !done) {
212                 struct lu_dirpage *dp;
213                 struct lu_dirent  *ent;
214                 __u64 hash;
215                 __u64 next;
216
217                 if (IS_ERR(page)) {
218                         rc = PTR_ERR(page);
219                         break;
220                 }
221
222                 hash = MDS_DIR_END_OFF;
223                 dp = page_address(page);
224                 for (ent = lu_dirent_start(dp); ent && !done;
225                      ent = lu_dirent_next(ent)) {
226                         __u16     type;
227                         int         namelen;
228                         struct lu_fid  fid;
229                         __u64     lhash;
230                         __u64     ino;
231
232                         hash = le64_to_cpu(ent->lde_hash);
233                         if (hash < pos)
234                                 /*
235                                  * Skip until we find target hash
236                                  * value.
237                                  */
238                                 continue;
239
240                         namelen = le16_to_cpu(ent->lde_namelen);
241                         if (namelen == 0)
242                                 /*
243                                  * Skip dummy record.
244                                  */
245                                 continue;
246
247                         if (is_api32 && is_hash64)
248                                 lhash = hash >> 32;
249                         else
250                                 lhash = hash;
251                         fid_le_to_cpu(&fid, &ent->lde_fid);
252                         ino = cl_fid_build_ino(&fid, is_api32);
253                         type = ll_dirent_type_get(ent);
254                         ctx->pos = lhash;
255                         /* For 'll_nfs_get_name_filldir()', it will try
256                          * to access the 'ent' through its 'lde_name',
257                          * so the parameter 'name' for 'ctx->actor()'
258                          * must be part of the 'ent'.
259                          */
260                         done = !dir_emit(ctx, ent->lde_name,
261                                          namelen, ino, type);
262                 }
263
264                 if (done) {
265                         pos = hash;
266                         ll_release_page(inode, page, false);
267                         break;
268                 }
269
270                 next = le64_to_cpu(dp->ldp_hash_end);
271                 pos = next;
272                 if (pos == MDS_DIR_END_OFF) {
273                         /*
274                          * End of directory reached.
275                          */
276                         done = 1;
277                         ll_release_page(inode, page, false);
278                 } else {
279                         /*
280                          * Normal case: continue to the next
281                          * page.
282                          */
283                         ll_release_page(inode, page,
284                                         le32_to_cpu(dp->ldp_flags) &
285                                         LDF_COLLIDE);
286                         next = pos;
287                         page = ll_get_dir_page(inode, op_data, pos);
288                 }
289         }
290
291         ctx->pos = pos;
292         return rc;
293 }
294
295 static int ll_readdir(struct file *filp, struct dir_context *ctx)
296 {
297         struct inode            *inode  = file_inode(filp);
298         struct ll_file_data     *lfd    = LUSTRE_FPRIVATE(filp);
299         struct ll_sb_info       *sbi    = ll_i2sbi(inode);
300         __u64 pos = lfd ? lfd->lfd_pos : 0;
301         int                     hash64  = sbi->ll_flags & LL_SBI_64BIT_HASH;
302         int                     api32   = ll_need_32bit_api(sbi);
303         struct md_op_data *op_data;
304         int                     rc;
305
306         CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p) pos/size %lu/%llu 32bit_api %d\n",
307                PFID(ll_inode2fid(inode)), inode, (unsigned long)pos,
308                i_size_read(inode), api32);
309
310         if (pos == MDS_DIR_END_OFF) {
311                 /*
312                  * end-of-file.
313                  */
314                 rc = 0;
315                 goto out;
316         }
317
318         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
319                                      LUSTRE_OPC_ANY, inode);
320         if (IS_ERR(op_data)) {
321                 rc = PTR_ERR(op_data);
322                 goto out;
323         }
324
325         if (unlikely(op_data->op_mea1)) {
326                 /*
327                  * This is only needed for striped dir to fill ..,
328                  * see lmv_read_page
329                  */
330                 if (file_dentry(filp)->d_parent &&
331                     file_dentry(filp)->d_parent->d_inode) {
332                         __u64 ibits = MDS_INODELOCK_UPDATE;
333                         struct inode *parent;
334
335                         parent = file_dentry(filp)->d_parent->d_inode;
336                         if (ll_have_md_lock(parent, &ibits, LCK_MINMODE))
337                                 op_data->op_fid3 = *ll_inode2fid(parent);
338                 }
339
340                 /*
341                  * If it can not find in cache, do lookup .. on the master
342                  * object
343                  */
344                 if (fid_is_zero(&op_data->op_fid3)) {
345                         rc = ll_dir_get_parent_fid(inode, &op_data->op_fid3);
346                         if (rc) {
347                                 ll_finish_md_op_data(op_data);
348                                 return rc;
349                         }
350                 }
351         }
352         op_data->op_max_pages = sbi->ll_md_brw_pages;
353         ctx->pos = pos;
354         rc = ll_dir_read(inode, &pos, op_data, ctx);
355         pos = ctx->pos;
356         if (lfd)
357                 lfd->lfd_pos = pos;
358
359         if (pos == MDS_DIR_END_OFF) {
360                 if (api32)
361                         pos = LL_DIR_END_OFF_32BIT;
362                 else
363                         pos = LL_DIR_END_OFF;
364         } else {
365                 if (api32 && hash64)
366                         pos >>= 32;
367         }
368         ctx->pos = pos;
369         ll_finish_md_op_data(op_data);
370         filp->f_version = inode->i_version;
371
372 out:
373         if (!rc)
374                 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
375
376         return rc;
377 }
378
379 static int ll_send_mgc_param(struct obd_export *mgc, char *string)
380 {
381         struct mgs_send_param *msp;
382         int rc = 0;
383
384         msp = kzalloc(sizeof(*msp), GFP_NOFS);
385         if (!msp)
386                 return -ENOMEM;
387
388         strlcpy(msp->mgs_param, string, sizeof(msp->mgs_param));
389         rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
390                                 sizeof(struct mgs_send_param), msp, NULL);
391         if (rc)
392                 CERROR("Failed to set parameter: %d\n", rc);
393         kfree(msp);
394
395         return rc;
396 }
397
398 /**
399  * Create striped directory with specified stripe(@lump)
400  *
401  * param[in] parent     the parent of the directory.
402  * param[in] lump       the specified stripes.
403  * param[in] dirname    the name of the directory.
404  * param[in] mode       the specified mode of the directory.
405  *
406  * retval               =0 if striped directory is being created successfully.
407  *                      <0 if the creation is failed.
408  */
409 static int ll_dir_setdirstripe(struct inode *parent, struct lmv_user_md *lump,
410                                const char *dirname, umode_t mode)
411 {
412         struct ptlrpc_request *request = NULL;
413         struct md_op_data *op_data;
414         struct ll_sb_info *sbi = ll_i2sbi(parent);
415         struct inode *inode = NULL;
416         struct dentry dentry;
417         int err;
418
419         if (unlikely(lump->lum_magic != LMV_USER_MAGIC))
420                 return -EINVAL;
421
422         CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p) name %s stripe_offset %d, stripe_count: %u\n",
423                PFID(ll_inode2fid(parent)), parent, dirname,
424                (int)lump->lum_stripe_offset, lump->lum_stripe_count);
425
426         if (lump->lum_stripe_count > 1 &&
427             !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
428                 return -EINVAL;
429
430         if (lump->lum_magic != cpu_to_le32(LMV_USER_MAGIC))
431                 lustre_swab_lmv_user_md(lump);
432
433         if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
434                 mode &= ~current_umask();
435         mode = (mode & (0777 | S_ISVTX)) | S_IFDIR;
436         op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
437                                      strlen(dirname), mode, LUSTRE_OPC_MKDIR,
438                                      lump);
439         if (IS_ERR(op_data)) {
440                 err = PTR_ERR(op_data);
441                 goto err_exit;
442         }
443
444         op_data->op_cli_flags |= CLI_SET_MEA;
445         err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
446                         from_kuid(&init_user_ns, current_fsuid()),
447                         from_kgid(&init_user_ns, current_fsgid()),
448                         cfs_curproc_cap_pack(), 0, &request);
449         ll_finish_md_op_data(op_data);
450
451         err = ll_prep_inode(&inode, request, parent->i_sb, NULL);
452         if (err)
453                 goto err_exit;
454
455         memset(&dentry, 0, sizeof(dentry));
456         dentry.d_inode = inode;
457
458         err = ll_init_security(&dentry, inode, parent);
459         iput(inode);
460
461 err_exit:
462         ptlrpc_req_finished(request);
463         return err;
464 }
465
466 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
467                      int set_default)
468 {
469         struct ll_sb_info *sbi = ll_i2sbi(inode);
470         struct md_op_data *op_data;
471         struct ptlrpc_request *req = NULL;
472         int rc = 0;
473         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
474         struct obd_device *mgc = lsi->lsi_mgc;
475         int lum_size;
476
477         if (lump) {
478                 /*
479                  * This is coming from userspace, so should be in
480                  * local endian.  But the MDS would like it in little
481                  * endian, so we swab it before we send it.
482                  */
483                 switch (lump->lmm_magic) {
484                 case LOV_USER_MAGIC_V1: {
485                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
486                                 lustre_swab_lov_user_md_v1(lump);
487                         lum_size = sizeof(struct lov_user_md_v1);
488                         break;
489                 }
490                 case LOV_USER_MAGIC_V3: {
491                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
492                                 lustre_swab_lov_user_md_v3(
493                                         (struct lov_user_md_v3 *)lump);
494                         lum_size = sizeof(struct lov_user_md_v3);
495                         break;
496                 }
497                 case LMV_USER_MAGIC: {
498                         if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
499                                 lustre_swab_lmv_user_md(
500                                         (struct lmv_user_md *)lump);
501                         lum_size = sizeof(struct lmv_user_md);
502                         break;
503                 }
504                 default: {
505                         CDEBUG(D_IOCTL, "bad userland LOV MAGIC: %#08x != %#08x nor %#08x\n",
506                                lump->lmm_magic, LOV_USER_MAGIC_V1,
507                                LOV_USER_MAGIC_V3);
508                         return -EINVAL;
509                 }
510                 }
511         } else {
512                 lum_size = sizeof(struct lov_user_md_v1);
513         }
514
515         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
516                                      LUSTRE_OPC_ANY, NULL);
517         if (IS_ERR(op_data))
518                 return PTR_ERR(op_data);
519
520         /* swabbing is done in lov_setstripe() on server side */
521         rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
522         ll_finish_md_op_data(op_data);
523         ptlrpc_req_finished(req);
524         if (rc)
525                 return rc;
526
527 #if OBD_OCD_VERSION(2, 13, 53, 0) > LUSTRE_VERSION_CODE
528         /*
529          * 2.9 server has stored filesystem default stripe in ROOT xattr,
530          * and it's stored into system config for backward compatibility.
531          *
532          * In the following we use the fact that LOV_USER_MAGIC_V1 and
533          * LOV_USER_MAGIC_V3 have the same initial fields so we do not
534          * need to make the distinction between the 2 versions
535          */
536         if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
537                 char *param = NULL;
538                 char *buf;
539
540                 param = kzalloc(MGS_PARAM_MAXLEN, GFP_NOFS);
541                 if (!param)
542                         return -ENOMEM;
543
544                 buf = param;
545                 /* Get fsname and assume devname to be -MDT0000. */
546                 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
547                 strcat(buf, "-MDT0000.lov");
548                 buf += strlen(buf);
549
550                 /* Set root stripesize */
551                 sprintf(buf, ".stripesize=%u",
552                         lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
553                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
554                 if (rc)
555                         goto end;
556
557                 /* Set root stripecount */
558                 sprintf(buf, ".stripecount=%hd",
559                         lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
560                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
561                 if (rc)
562                         goto end;
563
564                 /* Set root stripeoffset */
565                 sprintf(buf, ".stripeoffset=%hd",
566                         lump ? le16_to_cpu(lump->lmm_stripe_offset) :
567                         (typeof(lump->lmm_stripe_offset))(-1));
568                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
569
570 end:
571                 kfree(param);
572         }
573 #endif
574         return rc;
575 }
576
577 /**
578  * This function will be used to get default LOV/LMV/Default LMV
579  * @valid will be used to indicate which stripe it will retrieve
580  *      OBD_MD_MEA              LMV stripe EA
581  *      OBD_MD_DEFAULT_MEA      Default LMV stripe EA
582  *      otherwise               Default LOV EA.
583  * Each time, it can only retrieve 1 stripe EA
584  **/
585 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
586                      struct ptlrpc_request **request, u64 valid)
587 {
588         struct ll_sb_info *sbi = ll_i2sbi(inode);
589         struct mdt_body   *body;
590         struct lov_mds_md *lmm = NULL;
591         struct ptlrpc_request *req = NULL;
592         int rc, lmmsize;
593         struct md_op_data *op_data;
594
595         rc = ll_get_max_mdsize(sbi, &lmmsize);
596         if (rc)
597                 return rc;
598
599         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
600                                      0, lmmsize, LUSTRE_OPC_ANY,
601                                      NULL);
602         if (IS_ERR(op_data))
603                 return PTR_ERR(op_data);
604
605         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
606         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
607         ll_finish_md_op_data(op_data);
608         if (rc < 0) {
609                 CDEBUG(D_INFO, "md_getattr failed on inode " DFID ": rc %d\n",
610                        PFID(ll_inode2fid(inode)), rc);
611                 goto out;
612         }
613
614         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
615
616         lmmsize = body->mbo_eadatasize;
617
618         if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
619             lmmsize == 0) {
620                 rc = -ENODATA;
621                 goto out;
622         }
623
624         lmm = req_capsule_server_sized_get(&req->rq_pill,
625                                            &RMF_MDT_MD, lmmsize);
626         LASSERT(lmm);
627
628         /*
629          * This is coming from the MDS, so is probably in
630          * little endian.  We convert it to host endian before
631          * passing it to userspace.
632          */
633         /* We don't swab objects for directories */
634         switch (le32_to_cpu(lmm->lmm_magic)) {
635         case LOV_MAGIC_V1:
636                 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC)
637                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
638                 break;
639         case LOV_MAGIC_V3:
640                 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC)
641                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
642                 break;
643         case LMV_MAGIC_V1:
644                 if (cpu_to_le32(LMV_MAGIC) != LMV_MAGIC)
645                         lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
646                 break;
647         case LMV_USER_MAGIC:
648                 if (cpu_to_le32(LMV_USER_MAGIC) != LMV_USER_MAGIC)
649                         lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
650                 break;
651         default:
652                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
653                 rc = -EPROTO;
654         }
655 out:
656         *plmm = lmm;
657         *plmm_size = lmmsize;
658         *request = req;
659         return rc;
660 }
661
662 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
663 {
664         struct md_op_data *op_data;
665         int mdt_index, rc;
666
667         op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
668         if (!op_data)
669                 return -ENOMEM;
670
671         op_data->op_flags |= MF_GET_MDT_IDX;
672         op_data->op_fid1 = *fid;
673         rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
674         mdt_index = op_data->op_mds;
675         kvfree(op_data);
676         if (rc < 0)
677                 return rc;
678
679         return mdt_index;
680 }
681
682 /*
683  *  Get MDT index for the inode.
684  */
685 int ll_get_mdt_idx(struct inode *inode)
686 {
687         return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
688 }
689
690 /**
691  * Generic handler to do any pre-copy work.
692  *
693  * It sends a first hsm_progress (with extent length == 0) to coordinator as a
694  * first information for it that real work has started.
695  *
696  * Moreover, for a ARCHIVE request, it will sample the file data version and
697  * store it in \a copy.
698  *
699  * \return 0 on success.
700  */
701 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
702 {
703         struct ll_sb_info               *sbi = ll_s2sbi(sb);
704         struct hsm_progress_kernel       hpk;
705         int rc2, rc = 0;
706
707         /* Forge a hsm_progress based on data from copy. */
708         hpk.hpk_fid = copy->hc_hai.hai_fid;
709         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
710         hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
711         hpk.hpk_extent.length = 0;
712         hpk.hpk_flags = 0;
713         hpk.hpk_errval = 0;
714         hpk.hpk_data_version = 0;
715
716         /* For archive request, we need to read the current file version. */
717         if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
718                 struct inode    *inode;
719                 __u64            data_version = 0;
720
721                 /* Get inode for this fid */
722                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
723                 if (IS_ERR(inode)) {
724                         hpk.hpk_flags |= HP_FLAG_RETRY;
725                         /* hpk_errval is >= 0 */
726                         hpk.hpk_errval = -PTR_ERR(inode);
727                         rc = PTR_ERR(inode);
728                         goto progress;
729                 }
730
731                 /* Read current file data version */
732                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
733                 iput(inode);
734                 if (rc != 0) {
735                         CDEBUG(D_HSM, "Could not read file data version of "
736                                       DFID " (rc = %d). Archive request (%#llx) could not be done.\n",
737                                       PFID(&copy->hc_hai.hai_fid), rc,
738                                       copy->hc_hai.hai_cookie);
739                         hpk.hpk_flags |= HP_FLAG_RETRY;
740                         /* hpk_errval must be >= 0 */
741                         hpk.hpk_errval = -rc;
742                         goto progress;
743                 }
744
745                 /* Store in the hsm_copy for later copytool use.
746                  * Always modified even if no lsm.
747                  */
748                 copy->hc_data_version = data_version;
749         }
750
751 progress:
752         /* On error, the request should be considered as completed */
753         if (hpk.hpk_errval > 0)
754                 hpk.hpk_flags |= HP_FLAG_COMPLETED;
755         rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
756                             &hpk, NULL);
757
758         return rc ? rc : rc2;
759 }
760
761 /**
762  * Generic handler to do any post-copy work.
763  *
764  * It will send the last hsm_progress update to coordinator to inform it
765  * that copy is finished and whether it was successful or not.
766  *
767  * Moreover,
768  * - for ARCHIVE request, it will sample the file data version and compare it
769  *   with the version saved in ll_ioc_copy_start(). If they do not match, copy
770  *   will be considered as failed.
771  * - for RESTORE request, it will sample the file data version and send it to
772  *   coordinator which is useful if the file was imported as 'released'.
773  *
774  * \return 0 on success.
775  */
776 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
777 {
778         struct ll_sb_info               *sbi = ll_s2sbi(sb);
779         struct hsm_progress_kernel       hpk;
780         int rc2, rc = 0;
781
782         /* If you modify the logic here, also check llapi_hsm_copy_end(). */
783         /* Take care: copy->hc_hai.hai_action, len, gid and data are not
784          * initialized if copy_end was called with copy == NULL.
785          */
786
787         /* Forge a hsm_progress based on data from copy. */
788         hpk.hpk_fid = copy->hc_hai.hai_fid;
789         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
790         hpk.hpk_extent = copy->hc_hai.hai_extent;
791         hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
792         hpk.hpk_errval = copy->hc_errval;
793         hpk.hpk_data_version = 0;
794
795         /* For archive request, we need to check the file data was not changed.
796          *
797          * For restore request, we need to send the file data version, this is
798          * useful when the file was created using hsm_import.
799          */
800         if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
801              (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
802             (copy->hc_errval == 0)) {
803                 struct inode    *inode;
804                 __u64            data_version = 0;
805
806                 /* Get lsm for this fid */
807                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
808                 if (IS_ERR(inode)) {
809                         hpk.hpk_flags |= HP_FLAG_RETRY;
810                         /* hpk_errval must be >= 0 */
811                         hpk.hpk_errval = -PTR_ERR(inode);
812                         rc = PTR_ERR(inode);
813                         goto progress;
814                 }
815
816                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
817                 iput(inode);
818                 if (rc) {
819                         CDEBUG(D_HSM, "Could not read file data version. Request could not be confirmed.\n");
820                         if (hpk.hpk_errval == 0)
821                                 hpk.hpk_errval = -rc;
822                         goto progress;
823                 }
824
825                 /* Store in the hsm_copy for later copytool use.
826                  * Always modified even if no lsm.
827                  */
828                 hpk.hpk_data_version = data_version;
829
830                 /* File could have been stripped during archiving, so we need
831                  * to check anyway.
832                  */
833                 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
834                     (copy->hc_data_version != data_version)) {
835                         CDEBUG(D_HSM, "File data version mismatched. File content was changed during archiving. "
836                                DFID ", start:%#llx current:%#llx\n",
837                                PFID(&copy->hc_hai.hai_fid),
838                                copy->hc_data_version, data_version);
839                         /* File was changed, send error to cdt. Do not ask for
840                          * retry because if a file is modified frequently,
841                          * the cdt will loop on retried archive requests.
842                          * The policy engine will ask for a new archive later
843                          * when the file will not be modified for some tunable
844                          * time
845                          */
846                         hpk.hpk_flags &= ~HP_FLAG_RETRY;
847                         rc = -EBUSY;
848                         /* hpk_errval must be >= 0 */
849                         hpk.hpk_errval = -rc;
850                 }
851         }
852
853 progress:
854         rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
855                             &hpk, NULL);
856
857         return rc ? rc : rc2;
858 }
859
860 static int copy_and_ioctl(int cmd, struct obd_export *exp,
861                           const void __user *data, size_t size)
862 {
863         void *copy;
864         int rc;
865
866         copy = memdup_user(data, size);
867         if (IS_ERR(copy))
868                 return PTR_ERR(copy);
869
870         rc = obd_iocontrol(cmd, exp, size, copy, NULL);
871         kfree(copy);
872
873         return rc;
874 }
875
876 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
877 {
878         int cmd = qctl->qc_cmd;
879         int type = qctl->qc_type;
880         int id = qctl->qc_id;
881         int valid = qctl->qc_valid;
882         int rc = 0;
883
884         switch (cmd) {
885         case Q_SETQUOTA:
886         case Q_SETINFO:
887                 if (!capable(CFS_CAP_SYS_ADMIN))
888                         return -EPERM;
889                 break;
890         case Q_GETQUOTA:
891                 if (((type == USRQUOTA &&
892                       !uid_eq(current_euid(), make_kuid(&init_user_ns, id))) ||
893                      (type == GRPQUOTA &&
894                       !in_egroup_p(make_kgid(&init_user_ns, id)))) &&
895                       !capable(CFS_CAP_SYS_ADMIN))
896                         return -EPERM;
897                 break;
898         case Q_GETINFO:
899                 break;
900         default:
901                 CERROR("unsupported quotactl op: %#x\n", cmd);
902                 return -ENOTTY;
903         }
904
905         if (valid != QC_GENERAL) {
906                 if (cmd == Q_GETINFO)
907                         qctl->qc_cmd = Q_GETOINFO;
908                 else if (cmd == Q_GETQUOTA)
909                         qctl->qc_cmd = Q_GETOQUOTA;
910                 else
911                         return -EINVAL;
912
913                 switch (valid) {
914                 case QC_MDTIDX:
915                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
916                                            sizeof(*qctl), qctl, NULL);
917                         break;
918                 case QC_OSTIDX:
919                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
920                                            sizeof(*qctl), qctl, NULL);
921                         break;
922                 case QC_UUID:
923                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
924                                            sizeof(*qctl), qctl, NULL);
925                         if (rc == -EAGAIN)
926                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
927                                                    sbi->ll_dt_exp,
928                                                    sizeof(*qctl), qctl, NULL);
929                         break;
930                 default:
931                         rc = -EINVAL;
932                         break;
933                 }
934
935                 if (rc)
936                         return rc;
937
938                 qctl->qc_cmd = cmd;
939         } else {
940                 struct obd_quotactl *oqctl;
941
942                 oqctl = kzalloc(sizeof(*oqctl), GFP_NOFS);
943                 if (!oqctl)
944                         return -ENOMEM;
945
946                 QCTL_COPY(oqctl, qctl);
947                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
948                 if (rc) {
949                         kfree(oqctl);
950                         return rc;
951                 }
952                 /* If QIF_SPACE is not set, client should collect the
953                  * space usage from OSSs by itself
954                  */
955                 if (cmd == Q_GETQUOTA &&
956                     !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
957                     !oqctl->qc_dqblk.dqb_curspace) {
958                         struct obd_quotactl *oqctl_tmp;
959
960                         oqctl_tmp = kzalloc(sizeof(*oqctl_tmp), GFP_NOFS);
961                         if (!oqctl_tmp) {
962                                 rc = -ENOMEM;
963                                 goto out;
964                         }
965
966                         oqctl_tmp->qc_cmd = Q_GETOQUOTA;
967                         oqctl_tmp->qc_id = oqctl->qc_id;
968                         oqctl_tmp->qc_type = oqctl->qc_type;
969
970                         /* collect space usage from OSTs */
971                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
972                         rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
973                         if (!rc || rc == -EREMOTEIO) {
974                                 oqctl->qc_dqblk.dqb_curspace =
975                                         oqctl_tmp->qc_dqblk.dqb_curspace;
976                                 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
977                         }
978
979                         /* collect space & inode usage from MDTs */
980                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
981                         oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
982                         rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
983                         if (!rc || rc == -EREMOTEIO) {
984                                 oqctl->qc_dqblk.dqb_curspace +=
985                                         oqctl_tmp->qc_dqblk.dqb_curspace;
986                                 oqctl->qc_dqblk.dqb_curinodes =
987                                         oqctl_tmp->qc_dqblk.dqb_curinodes;
988                                 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
989                         } else {
990                                 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
991                         }
992
993                         kfree(oqctl_tmp);
994                 }
995 out:
996                 QCTL_COPY(qctl, oqctl);
997                 kfree(oqctl);
998         }
999
1000         return rc;
1001 }
1002
1003 /* This function tries to get a single name component,
1004  * to send to the server. No actual path traversal involved,
1005  * so we limit to NAME_MAX
1006  */
1007 static char *ll_getname(const char __user *filename)
1008 {
1009         int ret = 0, len;
1010         char *tmp;
1011
1012         tmp = kzalloc(NAME_MAX + 1, GFP_KERNEL);
1013         if (!tmp)
1014                 return ERR_PTR(-ENOMEM);
1015
1016         len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1017         if (len < 0)
1018                 ret = len;
1019         else if (len == 0)
1020                 ret = -ENOENT;
1021         else if (len > NAME_MAX && tmp[NAME_MAX] != 0)
1022                 ret = -ENAMETOOLONG;
1023
1024         if (ret) {
1025                 kfree(tmp);
1026                 tmp =  ERR_PTR(ret);
1027         }
1028         return tmp;
1029 }
1030
1031 #define ll_putname(filename) kfree(filename)
1032
1033 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1034 {
1035         struct inode *inode = file_inode(file);
1036         struct ll_sb_info *sbi = ll_i2sbi(inode);
1037         struct obd_ioctl_data *data;
1038         int rc = 0;
1039
1040         CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), cmd=%#x\n",
1041                PFID(ll_inode2fid(inode)), inode, cmd);
1042
1043         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1044         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1045                 return -ENOTTY;
1046
1047         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1048         switch (cmd) {
1049         case FSFILT_IOC_GETFLAGS:
1050         case FSFILT_IOC_SETFLAGS:
1051                 return ll_iocontrol(inode, file, cmd, arg);
1052         case FSFILT_IOC_GETVERSION_OLD:
1053         case FSFILT_IOC_GETVERSION:
1054                 return put_user(inode->i_generation, (int __user *)arg);
1055         /* We need to special case any other ioctls we want to handle,
1056          * to send them to the MDS/OST as appropriate and to properly
1057          * network encode the arg field.
1058         case FSFILT_IOC_SETVERSION_OLD:
1059         case FSFILT_IOC_SETVERSION:
1060         */
1061         case LL_IOC_GET_MDTIDX: {
1062                 int mdtidx;
1063
1064                 mdtidx = ll_get_mdt_idx(inode);
1065                 if (mdtidx < 0)
1066                         return mdtidx;
1067
1068                 if (put_user((int)mdtidx, (int __user *)arg))
1069                         return -EFAULT;
1070
1071                 return 0;
1072         }
1073         case IOC_MDC_LOOKUP: {
1074                 int namelen, len = 0;
1075                 char *buf = NULL;
1076                 char *filename;
1077
1078                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1079                 if (rc)
1080                         return rc;
1081                 data = (void *)buf;
1082
1083                 filename = data->ioc_inlbuf1;
1084                 namelen = strlen(filename);
1085
1086                 if (namelen < 1) {
1087                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1088                         rc = -EINVAL;
1089                         goto out_free;
1090                 }
1091
1092                 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1093                 if (rc < 0) {
1094                         CERROR("%s: lookup %.*s failed: rc = %d\n",
1095                                ll_get_fsname(inode->i_sb, NULL, 0), namelen,
1096                                filename, rc);
1097                         goto out_free;
1098                 }
1099 out_free:
1100                 kvfree(buf);
1101                 return rc;
1102         }
1103         case LL_IOC_LMV_SETSTRIPE: {
1104                 struct lmv_user_md  *lum;
1105                 char            *buf = NULL;
1106                 char            *filename;
1107                 int              namelen = 0;
1108                 int              lumlen = 0;
1109                 umode_t mode;
1110                 int              len;
1111                 int              rc;
1112
1113                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1114                 if (rc)
1115                         return rc;
1116
1117                 data = (void *)buf;
1118                 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
1119                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0) {
1120                         rc = -EINVAL;
1121                         goto lmv_out_free;
1122                 }
1123
1124                 filename = data->ioc_inlbuf1;
1125                 namelen = data->ioc_inllen1;
1126
1127                 if (namelen < 1) {
1128                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1129                         rc = -EINVAL;
1130                         goto lmv_out_free;
1131                 }
1132                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1133                 lumlen = data->ioc_inllen2;
1134
1135                 if (lum->lum_magic != LMV_USER_MAGIC ||
1136                     lumlen != sizeof(*lum)) {
1137                         CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1138                                filename, lum->lum_magic, lumlen, -EFAULT);
1139                         rc = -EINVAL;
1140                         goto lmv_out_free;
1141                 }
1142
1143 #if OBD_OCD_VERSION(2, 9, 50, 0) > LUSTRE_VERSION_CODE
1144                 mode = data->ioc_type != 0 ? data->ioc_type : 0777;
1145 #else
1146                 mode = data->ioc_type;
1147 #endif
1148                 rc = ll_dir_setdirstripe(inode, lum, filename, mode);
1149 lmv_out_free:
1150                 kvfree(buf);
1151                 return rc;
1152         }
1153         case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1154                 struct lmv_user_md __user *ulump;
1155                 struct lmv_user_md lum;
1156                 int rc;
1157
1158                 ulump = (struct lmv_user_md __user *)arg;
1159                 if (copy_from_user(&lum, ulump, sizeof(lum)))
1160                         return -EFAULT;
1161
1162                 if (lum.lum_magic != LMV_USER_MAGIC)
1163                         return -EINVAL;
1164
1165                 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1166
1167                 return rc;
1168         }
1169         case LL_IOC_LOV_SETSTRIPE: {
1170                 struct lov_user_md_v3 lumv3;
1171                 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1172                 struct lov_user_md_v1 __user *lumv1p = (void __user *)arg;
1173                 struct lov_user_md_v3 __user *lumv3p = (void __user *)arg;
1174
1175                 int set_default = 0;
1176
1177                 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1178                 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1179                         sizeof(lumv3p->lmm_objects[0]));
1180                 /* first try with v1 which is smaller than v3 */
1181                 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1182                         return -EFAULT;
1183
1184                 if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
1185                         if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1186                                 return -EFAULT;
1187                 }
1188
1189                 if (is_root_inode(inode))
1190                         set_default = 1;
1191
1192                 /* in v1 and v3 cases lumv1 points to data */
1193                 rc = ll_dir_setstripe(inode, lumv1, set_default);
1194
1195                 return rc;
1196         }
1197         case LL_IOC_LMV_GETSTRIPE: {
1198                 struct lmv_user_md __user *ulmv;
1199                 struct lmv_user_md lum;
1200                 struct ptlrpc_request *request = NULL;
1201                 struct lmv_user_md *tmp = NULL;
1202                 union lmv_mds_md *lmm = NULL;
1203                 u64 valid = 0;
1204                 int max_stripe_count;
1205                 int stripe_count;
1206                 int mdt_index;
1207                 int lum_size;
1208                 int lmmsize;
1209                 int rc;
1210                 int i;
1211
1212                 ulmv = (struct lmv_user_md __user *)arg;
1213                 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1214                         return -EFAULT;
1215
1216                 max_stripe_count = lum.lum_stripe_count;
1217                 /*
1218                  * lum_magic will indicate which stripe the ioctl will like
1219                  * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1220                  * is for default LMV stripe
1221                  */
1222                 if (lum.lum_magic == LMV_MAGIC_V1)
1223                         valid |= OBD_MD_MEA;
1224                 else if (lum.lum_magic == LMV_USER_MAGIC)
1225                         valid |= OBD_MD_DEFAULT_MEA;
1226                 else
1227                         return -EINVAL;
1228
1229                 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize, &request,
1230                                       valid);
1231                 if (rc)
1232                         goto finish_req;
1233
1234                 /* Get default LMV EA */
1235                 if (lum.lum_magic == LMV_USER_MAGIC) {
1236                         if (lmmsize > sizeof(*ulmv)) {
1237                                 rc = -EINVAL;
1238                                 goto finish_req;
1239                         }
1240
1241                         if (copy_to_user(ulmv, lmm, lmmsize))
1242                                 rc = -EFAULT;
1243
1244                         goto finish_req;
1245                 }
1246
1247                 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1248                 if (max_stripe_count < stripe_count) {
1249                         lum.lum_stripe_count = stripe_count;
1250                         if (copy_to_user(ulmv, &lum, sizeof(lum))) {
1251                                 rc = -EFAULT;
1252                                 goto finish_req;
1253                         }
1254                         rc = -E2BIG;
1255                         goto finish_req;
1256                 }
1257
1258                 lum_size = lmv_user_md_size(stripe_count, LMV_MAGIC_V1);
1259                 tmp = kzalloc(lum_size, GFP_NOFS);
1260                 if (!tmp) {
1261                         rc = -ENOMEM;
1262                         goto finish_req;
1263                 }
1264
1265                 mdt_index = ll_get_mdt_idx(inode);
1266                 if (mdt_index < 0) {
1267                         rc = -ENOMEM;
1268                         goto out_tmp;
1269                 }
1270                 tmp->lum_magic = LMV_MAGIC_V1;
1271                 tmp->lum_stripe_count = 0;
1272                 tmp->lum_stripe_offset = mdt_index;
1273                 for (i = 0; i < stripe_count; i++) {
1274                         struct lu_fid fid;
1275
1276                         fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1277                         mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1278                         if (mdt_index < 0) {
1279                                 rc = mdt_index;
1280                                 goto out_tmp;
1281                         }
1282                         tmp->lum_objects[i].lum_mds = mdt_index;
1283                         tmp->lum_objects[i].lum_fid = fid;
1284                         tmp->lum_stripe_count++;
1285                 }
1286
1287                 if (copy_to_user(ulmv, tmp, lum_size)) {
1288                         rc = -EFAULT;
1289                         goto out_tmp;
1290                 }
1291 out_tmp:
1292                 kfree(tmp);
1293 finish_req:
1294                 ptlrpc_req_finished(request);
1295                 return rc;
1296         }
1297
1298         case LL_IOC_LOV_SWAP_LAYOUTS:
1299                 return -EPERM;
1300         case IOC_OBD_STATFS:
1301                 return ll_obd_statfs(inode, (void __user *)arg);
1302         case LL_IOC_LOV_GETSTRIPE:
1303         case LL_IOC_MDC_GETINFO:
1304         case IOC_MDC_GETFILEINFO:
1305         case IOC_MDC_GETFILESTRIPE: {
1306                 struct ptlrpc_request *request = NULL;
1307                 struct lov_user_md __user *lump;
1308                 struct lov_mds_md *lmm = NULL;
1309                 struct mdt_body *body;
1310                 char *filename = NULL;
1311                 int lmmsize;
1312
1313                 if (cmd == IOC_MDC_GETFILEINFO ||
1314                     cmd == IOC_MDC_GETFILESTRIPE) {
1315                         filename = ll_getname((const char __user *)arg);
1316                         if (IS_ERR(filename))
1317                                 return PTR_ERR(filename);
1318
1319                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1320                                                       &lmmsize, &request);
1321                 } else {
1322                         rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize,
1323                                               &request, 0);
1324                 }
1325
1326                 if (request) {
1327                         body = req_capsule_server_get(&request->rq_pill,
1328                                                       &RMF_MDT_BODY);
1329                         LASSERT(body);
1330                 } else {
1331                         goto out_req;
1332                 }
1333
1334                 if (rc < 0) {
1335                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1336                                                cmd == LL_IOC_MDC_GETINFO)) {
1337                                 rc = 0;
1338                                 goto skip_lmm;
1339                         } else {
1340                                 goto out_req;
1341                         }
1342                 }
1343
1344                 if (cmd == IOC_MDC_GETFILESTRIPE ||
1345                     cmd == LL_IOC_LOV_GETSTRIPE) {
1346                         lump = (struct lov_user_md __user *)arg;
1347                 } else {
1348                         struct lov_user_mds_data __user *lmdp;
1349
1350                         lmdp = (struct lov_user_mds_data __user *)arg;
1351                         lump = &lmdp->lmd_lmm;
1352                 }
1353                 if (copy_to_user(lump, lmm, lmmsize)) {
1354                         if (copy_to_user(lump, lmm, sizeof(*lump))) {
1355                                 rc = -EFAULT;
1356                                 goto out_req;
1357                         }
1358                         rc = -EOVERFLOW;
1359                 }
1360 skip_lmm:
1361                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1362                         struct lov_user_mds_data __user *lmdp;
1363                         lstat_t st = { 0 };
1364
1365                         st.st_dev     = inode->i_sb->s_dev;
1366                         st.st_mode    = body->mbo_mode;
1367                         st.st_nlink   = body->mbo_nlink;
1368                         st.st_uid     = body->mbo_uid;
1369                         st.st_gid     = body->mbo_gid;
1370                         st.st_rdev    = body->mbo_rdev;
1371                         st.st_size    = body->mbo_size;
1372                         st.st_blksize = PAGE_SIZE;
1373                         st.st_blocks  = body->mbo_blocks;
1374                         st.st_atime   = body->mbo_atime;
1375                         st.st_mtime   = body->mbo_mtime;
1376                         st.st_ctime   = body->mbo_ctime;
1377                         st.st_ino     = cl_fid_build_ino(&body->mbo_fid1,
1378                                                          sbi->ll_flags &
1379                                                          LL_SBI_32BIT_API);
1380
1381                         lmdp = (struct lov_user_mds_data __user *)arg;
1382                         if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st))) {
1383                                 rc = -EFAULT;
1384                                 goto out_req;
1385                         }
1386                 }
1387
1388 out_req:
1389                 ptlrpc_req_finished(request);
1390                 if (filename)
1391                         ll_putname(filename);
1392                 return rc;
1393         }
1394         case OBD_IOC_QUOTACTL: {
1395                 struct if_quotactl *qctl;
1396
1397                 qctl = kzalloc(sizeof(*qctl), GFP_NOFS);
1398                 if (!qctl)
1399                         return -ENOMEM;
1400
1401                 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl))) {
1402                         rc = -EFAULT;
1403                         goto out_quotactl;
1404                 }
1405
1406                 rc = quotactl_ioctl(sbi, qctl);
1407
1408                 if (rc == 0 && copy_to_user((void __user *)arg, qctl,
1409                                             sizeof(*qctl)))
1410                         rc = -EFAULT;
1411
1412 out_quotactl:
1413                 kfree(qctl);
1414                 return rc;
1415         }
1416         case OBD_IOC_GETDTNAME:
1417         case OBD_IOC_GETMDNAME:
1418                 return ll_get_obd_name(inode, cmd, arg);
1419         case LL_IOC_FLUSHCTX:
1420                 return ll_flush_ctx(inode);
1421         case LL_IOC_GETOBDCOUNT: {
1422                 int count, vallen;
1423                 struct obd_export *exp;
1424
1425                 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1426                         return -EFAULT;
1427
1428                 /* get ost count when count is zero, get mdt count otherwise */
1429                 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1430                 vallen = sizeof(count);
1431                 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1432                                   KEY_TGT_COUNT, &vallen, &count);
1433                 if (rc) {
1434                         CERROR("get target count failed: %d\n", rc);
1435                         return rc;
1436                 }
1437
1438                 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1439                         return -EFAULT;
1440
1441                 return 0;
1442         }
1443         case LL_IOC_PATH2FID:
1444                 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1445                                  sizeof(struct lu_fid)))
1446                         return -EFAULT;
1447                 return 0;
1448         case LL_IOC_GET_CONNECT_FLAGS: {
1449                 return obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1450                                      (void __user *)arg);
1451         }
1452         case OBD_IOC_CHANGELOG_SEND:
1453         case OBD_IOC_CHANGELOG_CLEAR:
1454                 if (!capable(CFS_CAP_SYS_ADMIN))
1455                         return -EPERM;
1456
1457                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void __user *)arg,
1458                                     sizeof(struct ioc_changelog));
1459                 return rc;
1460         case OBD_IOC_FID2PATH:
1461                 return ll_fid2path(inode, (void __user *)arg);
1462         case LL_IOC_GETPARENT:
1463                 return ll_getparent(file, (void __user *)arg);
1464         case LL_IOC_FID2MDTIDX: {
1465                 struct obd_export *exp = ll_i2mdexp(inode);
1466                 struct lu_fid fid;
1467                 __u32 index;
1468
1469                 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1470                                    sizeof(fid)))
1471                         return -EFAULT;
1472
1473                 /* Call mdc_iocontrol */
1474                 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1475                                    &index);
1476                 if (rc)
1477                         return rc;
1478
1479                 return index;
1480         }
1481         case LL_IOC_HSM_REQUEST: {
1482                 struct hsm_user_request *hur;
1483                 ssize_t                  totalsize;
1484
1485                 hur = memdup_user((void __user *)arg, sizeof(*hur));
1486                 if (IS_ERR(hur))
1487                         return PTR_ERR(hur);
1488
1489                 /* Compute the whole struct size */
1490                 totalsize = hur_len(hur);
1491                 kfree(hur);
1492                 if (totalsize < 0)
1493                         return -E2BIG;
1494
1495                 /* Final size will be more than double totalsize */
1496                 if (totalsize >= MDS_MAXREQSIZE / 3)
1497                         return -E2BIG;
1498
1499                 hur = libcfs_kvzalloc(totalsize, GFP_NOFS);
1500                 if (!hur)
1501                         return -ENOMEM;
1502
1503                 /* Copy the whole struct */
1504                 if (copy_from_user(hur, (void __user *)arg, totalsize)) {
1505                         kvfree(hur);
1506                         return -EFAULT;
1507                 }
1508
1509                 if (hur->hur_request.hr_action == HUA_RELEASE) {
1510                         const struct lu_fid *fid;
1511                         struct inode *f;
1512                         int i;
1513
1514                         for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1515                                 fid = &hur->hur_user_item[i].hui_fid;
1516                                 f = search_inode_for_lustre(inode->i_sb, fid);
1517                                 if (IS_ERR(f)) {
1518                                         rc = PTR_ERR(f);
1519                                         break;
1520                                 }
1521
1522                                 rc = ll_hsm_release(f);
1523                                 iput(f);
1524                                 if (rc != 0)
1525                                         break;
1526                         }
1527                 } else {
1528                         rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1529                                            hur, NULL);
1530                 }
1531
1532                 kvfree(hur);
1533
1534                 return rc;
1535         }
1536         case LL_IOC_HSM_PROGRESS: {
1537                 struct hsm_progress_kernel      hpk;
1538                 struct hsm_progress             hp;
1539
1540                 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
1541                         return -EFAULT;
1542
1543                 hpk.hpk_fid = hp.hp_fid;
1544                 hpk.hpk_cookie = hp.hp_cookie;
1545                 hpk.hpk_extent = hp.hp_extent;
1546                 hpk.hpk_flags = hp.hp_flags;
1547                 hpk.hpk_errval = hp.hp_errval;
1548                 hpk.hpk_data_version = 0;
1549
1550                 /* File may not exist in Lustre; all progress
1551                  * reported to Lustre root
1552                  */
1553                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1554                                    NULL);
1555                 return rc;
1556         }
1557         case LL_IOC_HSM_CT_START:
1558                 if (!capable(CFS_CAP_SYS_ADMIN))
1559                         return -EPERM;
1560
1561                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void __user *)arg,
1562                                     sizeof(struct lustre_kernelcomm));
1563                 return rc;
1564
1565         case LL_IOC_HSM_COPY_START: {
1566                 struct hsm_copy *copy;
1567                 int              rc;
1568
1569                 copy = memdup_user((char __user *)arg, sizeof(*copy));
1570                 if (IS_ERR(copy))
1571                         return PTR_ERR(copy);
1572
1573                 rc = ll_ioc_copy_start(inode->i_sb, copy);
1574                 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
1575                         rc = -EFAULT;
1576
1577                 kfree(copy);
1578                 return rc;
1579         }
1580         case LL_IOC_HSM_COPY_END: {
1581                 struct hsm_copy *copy;
1582                 int              rc;
1583
1584                 copy = memdup_user((char __user *)arg, sizeof(*copy));
1585                 if (IS_ERR(copy))
1586                         return PTR_ERR(copy);
1587
1588                 rc = ll_ioc_copy_end(inode->i_sb, copy);
1589                 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
1590                         rc = -EFAULT;
1591
1592                 kfree(copy);
1593                 return rc;
1594         }
1595         case LL_IOC_MIGRATE: {
1596                 char *buf = NULL;
1597                 const char *filename;
1598                 int namelen = 0;
1599                 int len;
1600                 int rc;
1601                 int mdtidx;
1602
1603                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1604                 if (rc < 0)
1605                         return rc;
1606
1607                 data = (struct obd_ioctl_data *)buf;
1608                 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
1609                     !data->ioc_inllen1 || !data->ioc_inllen2) {
1610                         rc = -EINVAL;
1611                         goto migrate_free;
1612                 }
1613
1614                 filename = data->ioc_inlbuf1;
1615                 namelen = data->ioc_inllen1;
1616                 if (namelen < 1 || namelen != strlen(filename) + 1) {
1617                         rc = -EINVAL;
1618                         goto migrate_free;
1619                 }
1620
1621                 if (data->ioc_inllen2 != sizeof(mdtidx)) {
1622                         rc = -EINVAL;
1623                         goto migrate_free;
1624                 }
1625                 mdtidx = *(int *)data->ioc_inlbuf2;
1626
1627                 rc = ll_migrate(inode, file, mdtidx, filename, namelen - 1);
1628 migrate_free:
1629                 kvfree(buf);
1630
1631                 return rc;
1632         }
1633
1634         default:
1635                 return obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1636                                      (void __user *)arg);
1637         }
1638 }
1639
1640 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1641 {
1642         struct inode *inode = file->f_mapping->host;
1643         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1644         struct ll_sb_info *sbi = ll_i2sbi(inode);
1645         int api32 = ll_need_32bit_api(sbi);
1646         loff_t ret = -EINVAL;
1647
1648         switch (origin) {
1649         case SEEK_SET:
1650                 break;
1651         case SEEK_CUR:
1652                 offset += file->f_pos;
1653                 break;
1654         case SEEK_END:
1655                 if (offset > 0)
1656                         goto out;
1657                 if (api32)
1658                         offset += LL_DIR_END_OFF_32BIT;
1659                 else
1660                         offset += LL_DIR_END_OFF;
1661                 break;
1662         default:
1663                 goto out;
1664         }
1665
1666         if (offset >= 0 &&
1667             ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1668              (!api32 && offset <= LL_DIR_END_OFF))) {
1669                 if (offset != file->f_pos) {
1670                         if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1671                             (!api32 && offset == LL_DIR_END_OFF))
1672                                 fd->lfd_pos = MDS_DIR_END_OFF;
1673                         else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1674                                 fd->lfd_pos = offset << 32;
1675                         else
1676                                 fd->lfd_pos = offset;
1677                         file->f_pos = offset;
1678                         file->f_version = 0;
1679                 }
1680                 ret = offset;
1681         }
1682         goto out;
1683
1684 out:
1685         return ret;
1686 }
1687
1688 static int ll_dir_open(struct inode *inode, struct file *file)
1689 {
1690         return ll_file_open(inode, file);
1691 }
1692
1693 static int ll_dir_release(struct inode *inode, struct file *file)
1694 {
1695         return ll_file_release(inode, file);
1696 }
1697
1698 const struct file_operations ll_dir_operations = {
1699         .llseek   = ll_dir_seek,
1700         .open     = ll_dir_open,
1701         .release  = ll_dir_release,
1702         .read     = generic_read_dir,
1703         .iterate_shared  = ll_readdir,
1704         .unlocked_ioctl   = ll_dir_ioctl,
1705         .fsync    = ll_fsync,
1706 };