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