]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/staging/lustre/lustre/llite/dir.c
5b2e47c246f3dd1970b48038d9b9c7fbab5c3e69
[linux.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         filp->f_version = inode->i_version;
373
374 out:
375         if (!rc)
376                 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
377
378         return rc;
379 }
380
381 static int ll_send_mgc_param(struct obd_export *mgc, char *string)
382 {
383         struct mgs_send_param *msp;
384         int rc = 0;
385
386         msp = kzalloc(sizeof(*msp), GFP_NOFS);
387         if (!msp)
388                 return -ENOMEM;
389
390         strlcpy(msp->mgs_param, string, sizeof(msp->mgs_param));
391         rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
392                                 sizeof(struct mgs_send_param), msp, NULL);
393         if (rc)
394                 CERROR("Failed to set parameter: %d\n", rc);
395         kfree(msp);
396
397         return rc;
398 }
399
400 /**
401  * Create striped directory with specified stripe(@lump)
402  *
403  * param[in] parent     the parent of the directory.
404  * param[in] lump       the specified stripes.
405  * param[in] dirname    the name of the directory.
406  * param[in] mode       the specified mode of the directory.
407  *
408  * retval               =0 if striped directory is being created successfully.
409  *                      <0 if the creation is failed.
410  */
411 static int ll_dir_setdirstripe(struct inode *parent, struct lmv_user_md *lump,
412                                const char *dirname, umode_t mode)
413 {
414         struct ptlrpc_request *request = NULL;
415         struct md_op_data *op_data;
416         struct ll_sb_info *sbi = ll_i2sbi(parent);
417         struct inode *inode = NULL;
418         struct dentry dentry;
419         int err;
420
421         if (unlikely(lump->lum_magic != LMV_USER_MAGIC))
422                 return -EINVAL;
423
424         CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p) name %s stripe_offset %d, stripe_count: %u\n",
425                PFID(ll_inode2fid(parent)), parent, dirname,
426                (int)lump->lum_stripe_offset, lump->lum_stripe_count);
427
428         if (lump->lum_stripe_count > 1 &&
429             !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_DIR_STRIPE))
430                 return -EINVAL;
431
432         if (lump->lum_magic != cpu_to_le32(LMV_USER_MAGIC))
433                 lustre_swab_lmv_user_md(lump);
434
435         if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
436                 mode &= ~current_umask();
437         mode = (mode & (0777 | S_ISVTX)) | S_IFDIR;
438         op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
439                                      strlen(dirname), mode, LUSTRE_OPC_MKDIR,
440                                      lump);
441         if (IS_ERR(op_data)) {
442                 err = PTR_ERR(op_data);
443                 goto err_exit;
444         }
445
446         op_data->op_cli_flags |= CLI_SET_MEA;
447         err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
448                         from_kuid(&init_user_ns, current_fsuid()),
449                         from_kgid(&init_user_ns, current_fsgid()),
450                         cfs_curproc_cap_pack(), 0, &request);
451         ll_finish_md_op_data(op_data);
452
453         err = ll_prep_inode(&inode, request, parent->i_sb, NULL);
454         if (err)
455                 goto err_exit;
456
457         memset(&dentry, 0, sizeof(dentry));
458         dentry.d_inode = inode;
459
460         err = ll_init_security(&dentry, inode, parent);
461         iput(inode);
462
463 err_exit:
464         ptlrpc_req_finished(request);
465         return err;
466 }
467
468 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
469                      int set_default)
470 {
471         struct ll_sb_info *sbi = ll_i2sbi(inode);
472         struct md_op_data *op_data;
473         struct ptlrpc_request *req = NULL;
474         int rc = 0;
475         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
476         struct obd_device *mgc = lsi->lsi_mgc;
477         int lum_size;
478
479         if (lump) {
480                 /*
481                  * This is coming from userspace, so should be in
482                  * local endian.  But the MDS would like it in little
483                  * endian, so we swab it before we send it.
484                  */
485                 switch (lump->lmm_magic) {
486                 case LOV_USER_MAGIC_V1: {
487                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
488                                 lustre_swab_lov_user_md_v1(lump);
489                         lum_size = sizeof(struct lov_user_md_v1);
490                         break;
491                 }
492                 case LOV_USER_MAGIC_V3: {
493                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
494                                 lustre_swab_lov_user_md_v3(
495                                         (struct lov_user_md_v3 *)lump);
496                         lum_size = sizeof(struct lov_user_md_v3);
497                         break;
498                 }
499                 case LMV_USER_MAGIC: {
500                         if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
501                                 lustre_swab_lmv_user_md(
502                                         (struct lmv_user_md *)lump);
503                         lum_size = sizeof(struct lmv_user_md);
504                         break;
505                 }
506                 default: {
507                         CDEBUG(D_IOCTL,
508                                "bad userland LOV MAGIC: %#08x != %#08x nor %#08x\n",
509                                lump->lmm_magic, LOV_USER_MAGIC_V1,
510                                LOV_USER_MAGIC_V3);
511                         return -EINVAL;
512                 }
513                 }
514         } else {
515                 lum_size = sizeof(struct lov_user_md_v1);
516         }
517
518         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
519                                      LUSTRE_OPC_ANY, NULL);
520         if (IS_ERR(op_data))
521                 return PTR_ERR(op_data);
522
523         /* swabbing is done in lov_setstripe() on server side */
524         rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size, &req);
525         ll_finish_md_op_data(op_data);
526         ptlrpc_req_finished(req);
527         if (rc)
528                 return rc;
529
530 #if OBD_OCD_VERSION(2, 13, 53, 0) > LUSTRE_VERSION_CODE
531         /*
532          * 2.9 server has stored filesystem default stripe in ROOT xattr,
533          * and it's stored into system config for backward compatibility.
534          *
535          * In the following we use the fact that LOV_USER_MAGIC_V1 and
536          * LOV_USER_MAGIC_V3 have the same initial fields so we do not
537          * need to make the distinction between the 2 versions
538          */
539         if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
540                 char *param = NULL;
541                 char *buf;
542
543                 param = kzalloc(MGS_PARAM_MAXLEN, GFP_NOFS);
544                 if (!param)
545                         return -ENOMEM;
546
547                 buf = param;
548                 /* Get fsname and assume devname to be -MDT0000. */
549                 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
550                 strcat(buf, "-MDT0000.lov");
551                 buf += strlen(buf);
552
553                 /* Set root stripesize */
554                 sprintf(buf, ".stripesize=%u",
555                         lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
556                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
557                 if (rc)
558                         goto end;
559
560                 /* Set root stripecount */
561                 sprintf(buf, ".stripecount=%hd",
562                         lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
563                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
564                 if (rc)
565                         goto end;
566
567                 /* Set root stripeoffset */
568                 sprintf(buf, ".stripeoffset=%hd",
569                         lump ? le16_to_cpu(lump->lmm_stripe_offset) :
570                         (typeof(lump->lmm_stripe_offset))(-1));
571                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
572
573 end:
574                 kfree(param);
575         }
576 #endif
577         return rc;
578 }
579
580 /**
581  * This function will be used to get default LOV/LMV/Default LMV
582  * @valid will be used to indicate which stripe it will retrieve
583  *      OBD_MD_MEA              LMV stripe EA
584  *      OBD_MD_DEFAULT_MEA      Default LMV stripe EA
585  *      otherwise               Default LOV EA.
586  * Each time, it can only retrieve 1 stripe EA
587  **/
588 int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
589                      struct ptlrpc_request **request, u64 valid)
590 {
591         struct ll_sb_info *sbi = ll_i2sbi(inode);
592         struct mdt_body   *body;
593         struct lov_mds_md *lmm = NULL;
594         struct ptlrpc_request *req = NULL;
595         int rc, lmmsize;
596         struct md_op_data *op_data;
597
598         rc = ll_get_max_mdsize(sbi, &lmmsize);
599         if (rc)
600                 return rc;
601
602         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
603                                      0, lmmsize, LUSTRE_OPC_ANY,
604                                      NULL);
605         if (IS_ERR(op_data))
606                 return PTR_ERR(op_data);
607
608         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
609         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
610         ll_finish_md_op_data(op_data);
611         if (rc < 0) {
612                 CDEBUG(D_INFO, "md_getattr failed on inode " DFID ": rc %d\n",
613                        PFID(ll_inode2fid(inode)), rc);
614                 goto out;
615         }
616
617         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
618
619         lmmsize = body->mbo_eadatasize;
620
621         if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
622             lmmsize == 0) {
623                 rc = -ENODATA;
624                 goto out;
625         }
626
627         lmm = req_capsule_server_sized_get(&req->rq_pill,
628                                            &RMF_MDT_MD, lmmsize);
629         LASSERT(lmm);
630
631         /*
632          * This is coming from the MDS, so is probably in
633          * little endian.  We convert it to host endian before
634          * passing it to userspace.
635          */
636         /* We don't swab objects for directories */
637         switch (le32_to_cpu(lmm->lmm_magic)) {
638         case LOV_MAGIC_V1:
639                 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC)
640                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
641                 break;
642         case LOV_MAGIC_V3:
643                 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC)
644                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
645                 break;
646         case LMV_MAGIC_V1:
647                 if (cpu_to_le32(LMV_MAGIC) != LMV_MAGIC)
648                         lustre_swab_lmv_mds_md((union lmv_mds_md *)lmm);
649                 break;
650         case LMV_USER_MAGIC:
651                 if (cpu_to_le32(LMV_USER_MAGIC) != LMV_USER_MAGIC)
652                         lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
653                 break;
654         default:
655                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
656                 rc = -EPROTO;
657         }
658 out:
659         *plmm = lmm;
660         *plmm_size = lmmsize;
661         *request = req;
662         return rc;
663 }
664
665 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
666 {
667         struct md_op_data *op_data;
668         int mdt_index, rc;
669
670         op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
671         if (!op_data)
672                 return -ENOMEM;
673
674         op_data->op_flags |= MF_GET_MDT_IDX;
675         op_data->op_fid1 = *fid;
676         rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
677         mdt_index = op_data->op_mds;
678         kvfree(op_data);
679         if (rc < 0)
680                 return rc;
681
682         return mdt_index;
683 }
684
685 /*
686  *  Get MDT index for the inode.
687  */
688 int ll_get_mdt_idx(struct inode *inode)
689 {
690         return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
691 }
692
693 /**
694  * Generic handler to do any pre-copy work.
695  *
696  * It sends a first hsm_progress (with extent length == 0) to coordinator as a
697  * first information for it that real work has started.
698  *
699  * Moreover, for a ARCHIVE request, it will sample the file data version and
700  * store it in \a copy.
701  *
702  * \return 0 on success.
703  */
704 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
705 {
706         struct ll_sb_info               *sbi = ll_s2sbi(sb);
707         struct hsm_progress_kernel       hpk;
708         int rc2, rc = 0;
709
710         /* Forge a hsm_progress based on data from copy. */
711         hpk.hpk_fid = copy->hc_hai.hai_fid;
712         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
713         hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
714         hpk.hpk_extent.length = 0;
715         hpk.hpk_flags = 0;
716         hpk.hpk_errval = 0;
717         hpk.hpk_data_version = 0;
718
719         /* For archive request, we need to read the current file version. */
720         if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
721                 struct inode    *inode;
722                 __u64            data_version = 0;
723
724                 /* Get inode for this fid */
725                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
726                 if (IS_ERR(inode)) {
727                         hpk.hpk_flags |= HP_FLAG_RETRY;
728                         /* hpk_errval is >= 0 */
729                         hpk.hpk_errval = -PTR_ERR(inode);
730                         rc = PTR_ERR(inode);
731                         goto progress;
732                 }
733
734                 /* Read current file data version */
735                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
736                 iput(inode);
737                 if (rc != 0) {
738                         CDEBUG(D_HSM,
739                                "Could not read file data version of " DFID " (rc = %d). Archive request (%#llx) could not be done.\n",
740                                PFID(&copy->hc_hai.hai_fid), rc,
741                                copy->hc_hai.hai_cookie);
742                         hpk.hpk_flags |= HP_FLAG_RETRY;
743                         /* hpk_errval must be >= 0 */
744                         hpk.hpk_errval = -rc;
745                         goto progress;
746                 }
747
748                 /* Store in the hsm_copy for later copytool use.
749                  * Always modified even if no lsm.
750                  */
751                 copy->hc_data_version = data_version;
752         }
753
754 progress:
755         /* On error, the request should be considered as completed */
756         if (hpk.hpk_errval > 0)
757                 hpk.hpk_flags |= HP_FLAG_COMPLETED;
758         rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
759                             &hpk, NULL);
760
761         return rc ? rc : rc2;
762 }
763
764 /**
765  * Generic handler to do any post-copy work.
766  *
767  * It will send the last hsm_progress update to coordinator to inform it
768  * that copy is finished and whether it was successful or not.
769  *
770  * Moreover,
771  * - for ARCHIVE request, it will sample the file data version and compare it
772  *   with the version saved in ll_ioc_copy_start(). If they do not match, copy
773  *   will be considered as failed.
774  * - for RESTORE request, it will sample the file data version and send it to
775  *   coordinator which is useful if the file was imported as 'released'.
776  *
777  * \return 0 on success.
778  */
779 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
780 {
781         struct ll_sb_info               *sbi = ll_s2sbi(sb);
782         struct hsm_progress_kernel       hpk;
783         int rc2, rc = 0;
784
785         /* If you modify the logic here, also check llapi_hsm_copy_end(). */
786         /* Take care: copy->hc_hai.hai_action, len, gid and data are not
787          * initialized if copy_end was called with copy == NULL.
788          */
789
790         /* Forge a hsm_progress based on data from copy. */
791         hpk.hpk_fid = copy->hc_hai.hai_fid;
792         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
793         hpk.hpk_extent = copy->hc_hai.hai_extent;
794         hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
795         hpk.hpk_errval = copy->hc_errval;
796         hpk.hpk_data_version = 0;
797
798         /* For archive request, we need to check the file data was not changed.
799          *
800          * For restore request, we need to send the file data version, this is
801          * useful when the file was created using hsm_import.
802          */
803         if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
804              (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
805             (copy->hc_errval == 0)) {
806                 struct inode    *inode;
807                 __u64            data_version = 0;
808
809                 /* Get lsm for this fid */
810                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
811                 if (IS_ERR(inode)) {
812                         hpk.hpk_flags |= HP_FLAG_RETRY;
813                         /* hpk_errval must be >= 0 */
814                         hpk.hpk_errval = -PTR_ERR(inode);
815                         rc = PTR_ERR(inode);
816                         goto progress;
817                 }
818
819                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
820                 iput(inode);
821                 if (rc) {
822                         CDEBUG(D_HSM,
823                                "Could not read file data version. Request could not be confirmed.\n");
824                         if (hpk.hpk_errval == 0)
825                                 hpk.hpk_errval = -rc;
826                         goto progress;
827                 }
828
829                 /* Store in the hsm_copy for later copytool use.
830                  * Always modified even if no lsm.
831                  */
832                 hpk.hpk_data_version = data_version;
833
834                 /* File could have been stripped during archiving, so we need
835                  * to check anyway.
836                  */
837                 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
838                     (copy->hc_data_version != data_version)) {
839                         CDEBUG(D_HSM, "File data version mismatched. File content was changed during archiving. " DFID ", start:%#llx current:%#llx\n",
840                                PFID(&copy->hc_hai.hai_fid),
841                                copy->hc_data_version, data_version);
842                         /* File was changed, send error to cdt. Do not ask for
843                          * retry because if a file is modified frequently,
844                          * the cdt will loop on retried archive requests.
845                          * The policy engine will ask for a new archive later
846                          * when the file will not be modified for some tunable
847                          * time
848                          */
849                         hpk.hpk_flags &= ~HP_FLAG_RETRY;
850                         rc = -EBUSY;
851                         /* hpk_errval must be >= 0 */
852                         hpk.hpk_errval = -rc;
853                 }
854         }
855
856 progress:
857         rc2 = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
858                             &hpk, NULL);
859
860         return rc ? rc : rc2;
861 }
862
863 static int copy_and_ioctl(int cmd, struct obd_export *exp,
864                           const void __user *data, size_t size)
865 {
866         void *copy;
867         int rc;
868
869         copy = memdup_user(data, size);
870         if (IS_ERR(copy))
871                 return PTR_ERR(copy);
872
873         rc = obd_iocontrol(cmd, exp, size, copy, NULL);
874         kfree(copy);
875
876         return rc;
877 }
878
879 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
880 {
881         int cmd = qctl->qc_cmd;
882         int type = qctl->qc_type;
883         int id = qctl->qc_id;
884         int valid = qctl->qc_valid;
885         int rc = 0;
886
887         switch (cmd) {
888         case Q_SETQUOTA:
889         case Q_SETINFO:
890                 if (!capable(CFS_CAP_SYS_ADMIN))
891                         return -EPERM;
892                 break;
893         case Q_GETQUOTA:
894                 if (((type == USRQUOTA &&
895                       !uid_eq(current_euid(), make_kuid(&init_user_ns, id))) ||
896                      (type == GRPQUOTA &&
897                       !in_egroup_p(make_kgid(&init_user_ns, id)))) &&
898                       !capable(CFS_CAP_SYS_ADMIN))
899                         return -EPERM;
900                 break;
901         case Q_GETINFO:
902                 break;
903         default:
904                 CERROR("unsupported quotactl op: %#x\n", cmd);
905                 return -ENOTTY;
906         }
907
908         if (valid != QC_GENERAL) {
909                 if (cmd == Q_GETINFO)
910                         qctl->qc_cmd = Q_GETOINFO;
911                 else if (cmd == Q_GETQUOTA)
912                         qctl->qc_cmd = Q_GETOQUOTA;
913                 else
914                         return -EINVAL;
915
916                 switch (valid) {
917                 case QC_MDTIDX:
918                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
919                                            sizeof(*qctl), qctl, NULL);
920                         break;
921                 case QC_OSTIDX:
922                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
923                                            sizeof(*qctl), qctl, NULL);
924                         break;
925                 case QC_UUID:
926                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
927                                            sizeof(*qctl), qctl, NULL);
928                         if (rc == -EAGAIN)
929                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
930                                                    sbi->ll_dt_exp,
931                                                    sizeof(*qctl), qctl, NULL);
932                         break;
933                 default:
934                         rc = -EINVAL;
935                         break;
936                 }
937
938                 if (rc)
939                         return rc;
940
941                 qctl->qc_cmd = cmd;
942         } else {
943                 struct obd_quotactl *oqctl;
944
945                 oqctl = kzalloc(sizeof(*oqctl), GFP_NOFS);
946                 if (!oqctl)
947                         return -ENOMEM;
948
949                 QCTL_COPY(oqctl, qctl);
950                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
951                 if (rc) {
952                         kfree(oqctl);
953                         return rc;
954                 }
955                 /* If QIF_SPACE is not set, client should collect the
956                  * space usage from OSSs by itself
957                  */
958                 if (cmd == Q_GETQUOTA &&
959                     !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
960                     !oqctl->qc_dqblk.dqb_curspace) {
961                         struct obd_quotactl *oqctl_tmp;
962
963                         oqctl_tmp = kzalloc(sizeof(*oqctl_tmp), GFP_NOFS);
964                         if (!oqctl_tmp) {
965                                 rc = -ENOMEM;
966                                 goto out;
967                         }
968
969                         oqctl_tmp->qc_cmd = Q_GETOQUOTA;
970                         oqctl_tmp->qc_id = oqctl->qc_id;
971                         oqctl_tmp->qc_type = oqctl->qc_type;
972
973                         /* collect space usage from OSTs */
974                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
975                         rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
976                         if (!rc || rc == -EREMOTEIO) {
977                                 oqctl->qc_dqblk.dqb_curspace =
978                                         oqctl_tmp->qc_dqblk.dqb_curspace;
979                                 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
980                         }
981
982                         /* collect space & inode usage from MDTs */
983                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
984                         oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
985                         rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
986                         if (!rc || rc == -EREMOTEIO) {
987                                 oqctl->qc_dqblk.dqb_curspace +=
988                                         oqctl_tmp->qc_dqblk.dqb_curspace;
989                                 oqctl->qc_dqblk.dqb_curinodes =
990                                         oqctl_tmp->qc_dqblk.dqb_curinodes;
991                                 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
992                         } else {
993                                 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
994                         }
995
996                         kfree(oqctl_tmp);
997                 }
998 out:
999                 QCTL_COPY(qctl, oqctl);
1000                 kfree(oqctl);
1001         }
1002
1003         return rc;
1004 }
1005
1006 /* This function tries to get a single name component,
1007  * to send to the server. No actual path traversal involved,
1008  * so we limit to NAME_MAX
1009  */
1010 static char *ll_getname(const char __user *filename)
1011 {
1012         int ret = 0, len;
1013         char *tmp;
1014
1015         tmp = kzalloc(NAME_MAX + 1, GFP_KERNEL);
1016         if (!tmp)
1017                 return ERR_PTR(-ENOMEM);
1018
1019         len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1020         if (len < 0)
1021                 ret = len;
1022         else if (len == 0)
1023                 ret = -ENOENT;
1024         else if (len > NAME_MAX && tmp[NAME_MAX] != 0)
1025                 ret = -ENAMETOOLONG;
1026
1027         if (ret) {
1028                 kfree(tmp);
1029                 tmp =  ERR_PTR(ret);
1030         }
1031         return tmp;
1032 }
1033
1034 #define ll_putname(filename) kfree(filename)
1035
1036 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1037 {
1038         struct inode *inode = file_inode(file);
1039         struct ll_sb_info *sbi = ll_i2sbi(inode);
1040         struct obd_ioctl_data *data;
1041         int rc = 0;
1042
1043         CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), cmd=%#x\n",
1044                PFID(ll_inode2fid(inode)), inode, cmd);
1045
1046         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1047         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1048                 return -ENOTTY;
1049
1050         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1051         switch (cmd) {
1052         case FSFILT_IOC_GETFLAGS:
1053         case FSFILT_IOC_SETFLAGS:
1054                 return ll_iocontrol(inode, file, cmd, arg);
1055         case FSFILT_IOC_GETVERSION_OLD:
1056         case FSFILT_IOC_GETVERSION:
1057                 return put_user(inode->i_generation, (int __user *)arg);
1058         /* We need to special case any other ioctls we want to handle,
1059          * to send them to the MDS/OST as appropriate and to properly
1060          * network encode the arg field.
1061         case FSFILT_IOC_SETVERSION_OLD:
1062         case FSFILT_IOC_SETVERSION:
1063         */
1064         case LL_IOC_GET_MDTIDX: {
1065                 int mdtidx;
1066
1067                 mdtidx = ll_get_mdt_idx(inode);
1068                 if (mdtidx < 0)
1069                         return mdtidx;
1070
1071                 if (put_user((int)mdtidx, (int __user *)arg))
1072                         return -EFAULT;
1073
1074                 return 0;
1075         }
1076         case IOC_MDC_LOOKUP: {
1077                 int namelen, len = 0;
1078                 char *buf = NULL;
1079                 char *filename;
1080
1081                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1082                 if (rc)
1083                         return rc;
1084                 data = (void *)buf;
1085
1086                 filename = data->ioc_inlbuf1;
1087                 namelen = strlen(filename);
1088
1089                 if (namelen < 1) {
1090                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1091                         rc = -EINVAL;
1092                         goto out_free;
1093                 }
1094
1095                 rc = ll_get_fid_by_name(inode, filename, namelen, NULL, NULL);
1096                 if (rc < 0) {
1097                         CERROR("%s: lookup %.*s failed: rc = %d\n",
1098                                ll_get_fsname(inode->i_sb, NULL, 0), namelen,
1099                                filename, rc);
1100                         goto out_free;
1101                 }
1102 out_free:
1103                 kvfree(buf);
1104                 return rc;
1105         }
1106         case LL_IOC_LMV_SETSTRIPE: {
1107                 struct lmv_user_md  *lum;
1108                 char            *buf = NULL;
1109                 char            *filename;
1110                 int              namelen = 0;
1111                 int              lumlen = 0;
1112                 umode_t mode;
1113                 int              len;
1114                 int              rc;
1115
1116                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1117                 if (rc)
1118                         return rc;
1119
1120                 data = (void *)buf;
1121                 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
1122                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0) {
1123                         rc = -EINVAL;
1124                         goto lmv_out_free;
1125                 }
1126
1127                 filename = data->ioc_inlbuf1;
1128                 namelen = data->ioc_inllen1;
1129
1130                 if (namelen < 1) {
1131                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1132                         rc = -EINVAL;
1133                         goto lmv_out_free;
1134                 }
1135                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1136                 lumlen = data->ioc_inllen2;
1137
1138                 if (lum->lum_magic != LMV_USER_MAGIC ||
1139                     lumlen != sizeof(*lum)) {
1140                         CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1141                                filename, lum->lum_magic, lumlen, -EFAULT);
1142                         rc = -EINVAL;
1143                         goto lmv_out_free;
1144                 }
1145
1146 #if OBD_OCD_VERSION(2, 9, 50, 0) > LUSTRE_VERSION_CODE
1147                 mode = data->ioc_type != 0 ? data->ioc_type : 0777;
1148 #else
1149                 mode = data->ioc_type;
1150 #endif
1151                 rc = ll_dir_setdirstripe(inode, lum, filename, mode);
1152 lmv_out_free:
1153                 kvfree(buf);
1154                 return rc;
1155         }
1156         case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1157                 struct lmv_user_md __user *ulump;
1158                 struct lmv_user_md lum;
1159                 int rc;
1160
1161                 ulump = (struct lmv_user_md __user *)arg;
1162                 if (copy_from_user(&lum, ulump, sizeof(lum)))
1163                         return -EFAULT;
1164
1165                 if (lum.lum_magic != LMV_USER_MAGIC)
1166                         return -EINVAL;
1167
1168                 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1169
1170                 return rc;
1171         }
1172         case LL_IOC_LOV_SETSTRIPE: {
1173                 struct lov_user_md_v3 lumv3;
1174                 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1175                 struct lov_user_md_v1 __user *lumv1p = (void __user *)arg;
1176                 struct lov_user_md_v3 __user *lumv3p = (void __user *)arg;
1177
1178                 int set_default = 0;
1179
1180                 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1181                 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1182                         sizeof(lumv3p->lmm_objects[0]));
1183                 /* first try with v1 which is smaller than v3 */
1184                 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1185                         return -EFAULT;
1186
1187                 if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
1188                         if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1189                                 return -EFAULT;
1190                 }
1191
1192                 if (is_root_inode(inode))
1193                         set_default = 1;
1194
1195                 /* in v1 and v3 cases lumv1 points to data */
1196                 rc = ll_dir_setstripe(inode, lumv1, set_default);
1197
1198                 return rc;
1199         }
1200         case LL_IOC_LMV_GETSTRIPE: {
1201                 struct lmv_user_md __user *ulmv;
1202                 struct lmv_user_md lum;
1203                 struct ptlrpc_request *request = NULL;
1204                 struct lmv_user_md *tmp = NULL;
1205                 union lmv_mds_md *lmm = NULL;
1206                 u64 valid = 0;
1207                 int max_stripe_count;
1208                 int stripe_count;
1209                 int mdt_index;
1210                 int lum_size;
1211                 int lmmsize;
1212                 int rc;
1213                 int i;
1214
1215                 ulmv = (struct lmv_user_md __user *)arg;
1216                 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
1217                         return -EFAULT;
1218
1219                 max_stripe_count = lum.lum_stripe_count;
1220                 /*
1221                  * lum_magic will indicate which stripe the ioctl will like
1222                  * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1223                  * is for default LMV stripe
1224                  */
1225                 if (lum.lum_magic == LMV_MAGIC_V1)
1226                         valid |= OBD_MD_MEA;
1227                 else if (lum.lum_magic == LMV_USER_MAGIC)
1228                         valid |= OBD_MD_DEFAULT_MEA;
1229                 else
1230                         return -EINVAL;
1231
1232                 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize, &request,
1233                                       valid);
1234                 if (rc)
1235                         goto finish_req;
1236
1237                 /* Get default LMV EA */
1238                 if (lum.lum_magic == LMV_USER_MAGIC) {
1239                         if (lmmsize > sizeof(*ulmv)) {
1240                                 rc = -EINVAL;
1241                                 goto finish_req;
1242                         }
1243
1244                         if (copy_to_user(ulmv, lmm, lmmsize))
1245                                 rc = -EFAULT;
1246
1247                         goto finish_req;
1248                 }
1249
1250                 stripe_count = lmv_mds_md_stripe_count_get(lmm);
1251                 if (max_stripe_count < stripe_count) {
1252                         lum.lum_stripe_count = stripe_count;
1253                         if (copy_to_user(ulmv, &lum, sizeof(lum))) {
1254                                 rc = -EFAULT;
1255                                 goto finish_req;
1256                         }
1257                         rc = -E2BIG;
1258                         goto finish_req;
1259                 }
1260
1261                 lum_size = lmv_user_md_size(stripe_count, LMV_MAGIC_V1);
1262                 tmp = kzalloc(lum_size, GFP_NOFS);
1263                 if (!tmp) {
1264                         rc = -ENOMEM;
1265                         goto finish_req;
1266                 }
1267
1268                 mdt_index = ll_get_mdt_idx(inode);
1269                 if (mdt_index < 0) {
1270                         rc = -ENOMEM;
1271                         goto out_tmp;
1272                 }
1273                 tmp->lum_magic = LMV_MAGIC_V1;
1274                 tmp->lum_stripe_count = 0;
1275                 tmp->lum_stripe_offset = mdt_index;
1276                 for (i = 0; i < stripe_count; i++) {
1277                         struct lu_fid fid;
1278
1279                         fid_le_to_cpu(&fid, &lmm->lmv_md_v1.lmv_stripe_fids[i]);
1280                         mdt_index = ll_get_mdt_idx_by_fid(sbi, &fid);
1281                         if (mdt_index < 0) {
1282                                 rc = mdt_index;
1283                                 goto out_tmp;
1284                         }
1285                         tmp->lum_objects[i].lum_mds = mdt_index;
1286                         tmp->lum_objects[i].lum_fid = fid;
1287                         tmp->lum_stripe_count++;
1288                 }
1289
1290                 if (copy_to_user(ulmv, tmp, lum_size)) {
1291                         rc = -EFAULT;
1292                         goto out_tmp;
1293                 }
1294 out_tmp:
1295                 kfree(tmp);
1296 finish_req:
1297                 ptlrpc_req_finished(request);
1298                 return rc;
1299         }
1300
1301         case LL_IOC_LOV_SWAP_LAYOUTS:
1302                 return -EPERM;
1303         case IOC_OBD_STATFS:
1304                 return ll_obd_statfs(inode, (void __user *)arg);
1305         case LL_IOC_LOV_GETSTRIPE:
1306         case LL_IOC_MDC_GETINFO:
1307         case IOC_MDC_GETFILEINFO:
1308         case IOC_MDC_GETFILESTRIPE: {
1309                 struct ptlrpc_request *request = NULL;
1310                 struct lov_user_md __user *lump;
1311                 struct lov_mds_md *lmm = NULL;
1312                 struct mdt_body *body;
1313                 char *filename = NULL;
1314                 int lmmsize;
1315
1316                 if (cmd == IOC_MDC_GETFILEINFO ||
1317                     cmd == IOC_MDC_GETFILESTRIPE) {
1318                         filename = ll_getname((const char __user *)arg);
1319                         if (IS_ERR(filename))
1320                                 return PTR_ERR(filename);
1321
1322                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1323                                                       &lmmsize, &request);
1324                 } else {
1325                         rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize,
1326                                               &request, 0);
1327                 }
1328
1329                 if (request) {
1330                         body = req_capsule_server_get(&request->rq_pill,
1331                                                       &RMF_MDT_BODY);
1332                         LASSERT(body);
1333                 } else {
1334                         goto out_req;
1335                 }
1336
1337                 if (rc < 0) {
1338                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1339                                                cmd == LL_IOC_MDC_GETINFO)) {
1340                                 rc = 0;
1341                                 goto skip_lmm;
1342                         } else {
1343                                 goto out_req;
1344                         }
1345                 }
1346
1347                 if (cmd == IOC_MDC_GETFILESTRIPE ||
1348                     cmd == LL_IOC_LOV_GETSTRIPE) {
1349                         lump = (struct lov_user_md __user *)arg;
1350                 } else {
1351                         struct lov_user_mds_data __user *lmdp;
1352
1353                         lmdp = (struct lov_user_mds_data __user *)arg;
1354                         lump = &lmdp->lmd_lmm;
1355                 }
1356                 if (copy_to_user(lump, lmm, lmmsize)) {
1357                         if (copy_to_user(lump, lmm, sizeof(*lump))) {
1358                                 rc = -EFAULT;
1359                                 goto out_req;
1360                         }
1361                         rc = -EOVERFLOW;
1362                 }
1363 skip_lmm:
1364                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1365                         struct lov_user_mds_data __user *lmdp;
1366                         lstat_t st = { 0 };
1367
1368                         st.st_dev     = inode->i_sb->s_dev;
1369                         st.st_mode    = body->mbo_mode;
1370                         st.st_nlink   = body->mbo_nlink;
1371                         st.st_uid     = body->mbo_uid;
1372                         st.st_gid     = body->mbo_gid;
1373                         st.st_rdev    = body->mbo_rdev;
1374                         st.st_size    = body->mbo_size;
1375                         st.st_blksize = PAGE_SIZE;
1376                         st.st_blocks  = body->mbo_blocks;
1377                         st.st_atime   = body->mbo_atime;
1378                         st.st_mtime   = body->mbo_mtime;
1379                         st.st_ctime   = body->mbo_ctime;
1380                         st.st_ino     = cl_fid_build_ino(&body->mbo_fid1,
1381                                                          sbi->ll_flags &
1382                                                          LL_SBI_32BIT_API);
1383
1384                         lmdp = (struct lov_user_mds_data __user *)arg;
1385                         if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st))) {
1386                                 rc = -EFAULT;
1387                                 goto out_req;
1388                         }
1389                 }
1390
1391 out_req:
1392                 ptlrpc_req_finished(request);
1393                 if (filename)
1394                         ll_putname(filename);
1395                 return rc;
1396         }
1397         case OBD_IOC_QUOTACTL: {
1398                 struct if_quotactl *qctl;
1399
1400                 qctl = kzalloc(sizeof(*qctl), GFP_NOFS);
1401                 if (!qctl)
1402                         return -ENOMEM;
1403
1404                 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl))) {
1405                         rc = -EFAULT;
1406                         goto out_quotactl;
1407                 }
1408
1409                 rc = quotactl_ioctl(sbi, qctl);
1410
1411                 if (rc == 0 && copy_to_user((void __user *)arg, qctl,
1412                                             sizeof(*qctl)))
1413                         rc = -EFAULT;
1414
1415 out_quotactl:
1416                 kfree(qctl);
1417                 return rc;
1418         }
1419         case OBD_IOC_GETDTNAME:
1420         case OBD_IOC_GETMDNAME:
1421                 return ll_get_obd_name(inode, cmd, arg);
1422         case LL_IOC_FLUSHCTX:
1423                 return ll_flush_ctx(inode);
1424         case LL_IOC_GETOBDCOUNT: {
1425                 int count, vallen;
1426                 struct obd_export *exp;
1427
1428                 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
1429                         return -EFAULT;
1430
1431                 /* get ost count when count is zero, get mdt count otherwise */
1432                 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1433                 vallen = sizeof(count);
1434                 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1435                                   KEY_TGT_COUNT, &vallen, &count);
1436                 if (rc) {
1437                         CERROR("get target count failed: %d\n", rc);
1438                         return rc;
1439                 }
1440
1441                 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
1442                         return -EFAULT;
1443
1444                 return 0;
1445         }
1446         case LL_IOC_PATH2FID:
1447                 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1448                                  sizeof(struct lu_fid)))
1449                         return -EFAULT;
1450                 return 0;
1451         case LL_IOC_GET_CONNECT_FLAGS: {
1452                 return obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1453                                      (void __user *)arg);
1454         }
1455         case OBD_IOC_CHANGELOG_SEND:
1456         case OBD_IOC_CHANGELOG_CLEAR:
1457                 if (!capable(CFS_CAP_SYS_ADMIN))
1458                         return -EPERM;
1459
1460                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void __user *)arg,
1461                                     sizeof(struct ioc_changelog));
1462                 return rc;
1463         case OBD_IOC_FID2PATH:
1464                 return ll_fid2path(inode, (void __user *)arg);
1465         case LL_IOC_GETPARENT:
1466                 return ll_getparent(file, (void __user *)arg);
1467         case LL_IOC_FID2MDTIDX: {
1468                 struct obd_export *exp = ll_i2mdexp(inode);
1469                 struct lu_fid fid;
1470                 __u32 index;
1471
1472                 if (copy_from_user(&fid, (const struct lu_fid __user *)arg,
1473                                    sizeof(fid)))
1474                         return -EFAULT;
1475
1476                 /* Call mdc_iocontrol */
1477                 rc = obd_iocontrol(LL_IOC_FID2MDTIDX, exp, sizeof(fid), &fid,
1478                                    &index);
1479                 if (rc)
1480                         return rc;
1481
1482                 return index;
1483         }
1484         case LL_IOC_HSM_REQUEST: {
1485                 struct hsm_user_request *hur;
1486                 ssize_t                  totalsize;
1487
1488                 hur = memdup_user((void __user *)arg, sizeof(*hur));
1489                 if (IS_ERR(hur))
1490                         return PTR_ERR(hur);
1491
1492                 /* Compute the whole struct size */
1493                 totalsize = hur_len(hur);
1494                 kfree(hur);
1495                 if (totalsize < 0)
1496                         return -E2BIG;
1497
1498                 /* Final size will be more than double totalsize */
1499                 if (totalsize >= MDS_MAXREQSIZE / 3)
1500                         return -E2BIG;
1501
1502                 hur = libcfs_kvzalloc(totalsize, GFP_NOFS);
1503                 if (!hur)
1504                         return -ENOMEM;
1505
1506                 /* Copy the whole struct */
1507                 if (copy_from_user(hur, (void __user *)arg, totalsize)) {
1508                         kvfree(hur);
1509                         return -EFAULT;
1510                 }
1511
1512                 if (hur->hur_request.hr_action == HUA_RELEASE) {
1513                         const struct lu_fid *fid;
1514                         struct inode *f;
1515                         int i;
1516
1517                         for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1518                                 fid = &hur->hur_user_item[i].hui_fid;
1519                                 f = search_inode_for_lustre(inode->i_sb, fid);
1520                                 if (IS_ERR(f)) {
1521                                         rc = PTR_ERR(f);
1522                                         break;
1523                                 }
1524
1525                                 rc = ll_hsm_release(f);
1526                                 iput(f);
1527                                 if (rc != 0)
1528                                         break;
1529                         }
1530                 } else {
1531                         rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1532                                            hur, NULL);
1533                 }
1534
1535                 kvfree(hur);
1536
1537                 return rc;
1538         }
1539         case LL_IOC_HSM_PROGRESS: {
1540                 struct hsm_progress_kernel      hpk;
1541                 struct hsm_progress             hp;
1542
1543                 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
1544                         return -EFAULT;
1545
1546                 hpk.hpk_fid = hp.hp_fid;
1547                 hpk.hpk_cookie = hp.hp_cookie;
1548                 hpk.hpk_extent = hp.hp_extent;
1549                 hpk.hpk_flags = hp.hp_flags;
1550                 hpk.hpk_errval = hp.hp_errval;
1551                 hpk.hpk_data_version = 0;
1552
1553                 /* File may not exist in Lustre; all progress
1554                  * reported to Lustre root
1555                  */
1556                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1557                                    NULL);
1558                 return rc;
1559         }
1560         case LL_IOC_HSM_CT_START:
1561                 if (!capable(CFS_CAP_SYS_ADMIN))
1562                         return -EPERM;
1563
1564                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void __user *)arg,
1565                                     sizeof(struct lustre_kernelcomm));
1566                 return rc;
1567
1568         case LL_IOC_HSM_COPY_START: {
1569                 struct hsm_copy *copy;
1570                 int              rc;
1571
1572                 copy = memdup_user((char __user *)arg, sizeof(*copy));
1573                 if (IS_ERR(copy))
1574                         return PTR_ERR(copy);
1575
1576                 rc = ll_ioc_copy_start(inode->i_sb, copy);
1577                 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
1578                         rc = -EFAULT;
1579
1580                 kfree(copy);
1581                 return rc;
1582         }
1583         case LL_IOC_HSM_COPY_END: {
1584                 struct hsm_copy *copy;
1585                 int              rc;
1586
1587                 copy = memdup_user((char __user *)arg, sizeof(*copy));
1588                 if (IS_ERR(copy))
1589                         return PTR_ERR(copy);
1590
1591                 rc = ll_ioc_copy_end(inode->i_sb, copy);
1592                 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
1593                         rc = -EFAULT;
1594
1595                 kfree(copy);
1596                 return rc;
1597         }
1598         case LL_IOC_MIGRATE: {
1599                 char *buf = NULL;
1600                 const char *filename;
1601                 int namelen = 0;
1602                 int len;
1603                 int rc;
1604                 int mdtidx;
1605
1606                 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1607                 if (rc < 0)
1608                         return rc;
1609
1610                 data = (struct obd_ioctl_data *)buf;
1611                 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
1612                     !data->ioc_inllen1 || !data->ioc_inllen2) {
1613                         rc = -EINVAL;
1614                         goto migrate_free;
1615                 }
1616
1617                 filename = data->ioc_inlbuf1;
1618                 namelen = data->ioc_inllen1;
1619                 if (namelen < 1 || namelen != strlen(filename) + 1) {
1620                         rc = -EINVAL;
1621                         goto migrate_free;
1622                 }
1623
1624                 if (data->ioc_inllen2 != sizeof(mdtidx)) {
1625                         rc = -EINVAL;
1626                         goto migrate_free;
1627                 }
1628                 mdtidx = *(int *)data->ioc_inlbuf2;
1629
1630                 rc = ll_migrate(inode, file, mdtidx, filename, namelen - 1);
1631 migrate_free:
1632                 kvfree(buf);
1633
1634                 return rc;
1635         }
1636
1637         default:
1638                 return obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1639                                      (void __user *)arg);
1640         }
1641 }
1642
1643 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1644 {
1645         struct inode *inode = file->f_mapping->host;
1646         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1647         struct ll_sb_info *sbi = ll_i2sbi(inode);
1648         int api32 = ll_need_32bit_api(sbi);
1649         loff_t ret = -EINVAL;
1650
1651         switch (origin) {
1652         case SEEK_SET:
1653                 break;
1654         case SEEK_CUR:
1655                 offset += file->f_pos;
1656                 break;
1657         case SEEK_END:
1658                 if (offset > 0)
1659                         goto out;
1660                 if (api32)
1661                         offset += LL_DIR_END_OFF_32BIT;
1662                 else
1663                         offset += LL_DIR_END_OFF;
1664                 break;
1665         default:
1666                 goto out;
1667         }
1668
1669         if (offset >= 0 &&
1670             ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1671              (!api32 && offset <= LL_DIR_END_OFF))) {
1672                 if (offset != file->f_pos) {
1673                         if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1674                             (!api32 && offset == LL_DIR_END_OFF))
1675                                 fd->lfd_pos = MDS_DIR_END_OFF;
1676                         else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1677                                 fd->lfd_pos = offset << 32;
1678                         else
1679                                 fd->lfd_pos = offset;
1680                         file->f_pos = offset;
1681                         file->f_version = 0;
1682                 }
1683                 ret = offset;
1684         }
1685         goto out;
1686
1687 out:
1688         return ret;
1689 }
1690
1691 static int ll_dir_open(struct inode *inode, struct file *file)
1692 {
1693         return ll_file_open(inode, file);
1694 }
1695
1696 static int ll_dir_release(struct inode *inode, struct file *file)
1697 {
1698         return ll_file_release(inode, file);
1699 }
1700
1701 const struct file_operations ll_dir_operations = {
1702         .llseek   = ll_dir_seek,
1703         .open     = ll_dir_open,
1704         .release  = ll_dir_release,
1705         .read     = generic_read_dir,
1706         .iterate_shared  = ll_readdir,
1707         .unlocked_ioctl   = ll_dir_ioctl,
1708         .fsync    = ll_fsync,
1709 };