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