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