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